diff --git a/src/c.pyi b/src/c.pyi index 24069d2e..8f8e452a 100644 --- a/src/c.pyi +++ b/src/c.pyi @@ -8,7 +8,7 @@ def memset(ptr: 'void_p', value: int, size: int) -> None: ... def memcpy(dst: 'void_p', src: 'void_p', size: int) -> None: ... class _refl: - pass + def __call__(self) -> 'struct': ... class void_p: def __add__(self, i: int) -> 'void_p': ... @@ -57,6 +57,6 @@ class void_p: def set_base_offset(self, offset: str) -> None: ... class struct: - def address(self) -> 'void_p': ... + def addr(self) -> 'void_p': ... def copy(self) -> 'struct': ... def size(self) -> int: ... diff --git a/src/cffi.h b/src/cffi.h index 999e86cd..80eb582f 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -173,7 +173,7 @@ struct C99Struct{ C99Struct() { p = _inlined; } C99Struct(void* p, int size){ _init(size); - memcpy(this->p, p, size); + if(p!=nullptr) memcpy(this->p, p, size); } ~C99Struct(){ if(p!=_inlined) free(p); } @@ -185,7 +185,7 @@ struct C99Struct{ static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_default_constructor(type); - vm->bind_method<0>(type, "address", [](VM* vm, ArgsView args){ + vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ C99Struct& self = _CAST(C99Struct&, args[0]); return VAR_T(VoidP, self.p); }); @@ -264,6 +264,12 @@ struct C99ReflType{ PY_CLASS(C99ReflType, c, "_refl") static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_constructor<-1>(type, CPP_NOT_IMPLEMENTED()); + + vm->bind_method<0>(type, "__call__", [](VM* vm, ArgsView args){ + PyObject* self = args[0]; + i64 size = CAST(i64, self->attr("__size__")); + return VAR_T(C99Struct, nullptr, size); + }); } };