diff --git a/include/pocketpy/bindings.h b/include/pocketpy/bindings.h index 6740bd9d..eba1b6b5 100644 --- a/include/pocketpy/bindings.h +++ b/include/pocketpy/bindings.h @@ -145,4 +145,16 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){ return VAR(self == other); \ }); \ +#define PY_POINTER_LIKE(wT) \ + vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ + wT& self = _CAST(wT&, _0); \ + if(!vm->isinstance(_1, wT::_type(vm))) return vm->NotImplemented; \ + wT& other = _CAST(wT&, _1); \ + return VAR(self._() == other._()); \ + }); \ + vm->bind__hash__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ \ + wT& self = _CAST(wT&, obj); \ + return reinterpret_cast(self._()); \ + }); + } // namespace pkpy \ No newline at end of file diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index 8b6a582f..532c9a64 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -49,7 +49,6 @@ struct VoidP{ bool operator>(const VoidP& other) const { return ptr > other.ptr; } bool operator>=(const VoidP& other) const { return ptr >= other.ptr; } - Str hex() const{ std::stringstream ss; ss << std::hex << reinterpret_cast(ptr); @@ -88,17 +87,6 @@ struct C99Struct{ static void _register(VM* vm, PyObject* mod, PyObject* type); }; -struct ReflField{ - std::string_view name; - int offset; - bool operator<(const ReflField& other) const{ return name < other.name; } - bool operator==(const ReflField& other) const{ return name == other.name; } - bool operator!=(const ReflField& other) const{ return name != other.name; } - bool operator<(std::string_view other) const{ return name < other; } - bool operator==(std::string_view other) const{ return name == other; } - bool operator!=(std::string_view other) const{ return name != other; } -}; - struct ReflType{ std::string_view name; size_t size; diff --git a/include/typings/c.pyi b/include/typings/c.pyi index 2f928e70..4b63c883 100644 --- a/include/typings/c.pyi +++ b/include/typings/c.pyi @@ -12,6 +12,7 @@ class void_p: def __sub__(self, i: int) -> 'void_p': ... def __eq__(self, other: 'void_p') -> bool: ... def __ne__(self, other: 'void_p') -> bool: ... + def __hash__(self) -> int: ... def hex(self) -> str: ... @@ -57,7 +58,7 @@ class struct: def addr(self) -> 'void_p': ... def copy(self) -> 'struct': ... - def size(self) -> int: ... + def sizeof(self) -> int: ... def __eq__(self, other: 'struct') -> bool: ... def __ne__(self, other: 'struct') -> bool: ... @@ -137,11 +138,13 @@ class _StructLike(Generic[T]): @classmethod def from_struct(cls, s: struct) -> T: ... - def addr(self) -> '_Pointer[T]': ... + def addr(self) -> '_PointerLike[T]': ... def sizeof(self) -> int: ... def copy(self) -> T: ... def __eq__(self, other: T) -> bool: ... def __ne__(self, other: T) -> bool: ... -class _Pointer(Generic[T], void_p): - pass +class _PointerLike(Generic[T]): + def __eq__(self, other) -> bool: ... + def __ne__(self, other) -> bool: ... + def __hash__(self) -> int: ... diff --git a/src/cffi.cpp b/src/cffi.cpp index 837b94f3..bb678f6e 100644 --- a/src/cffi.cpp +++ b/src/cffi.cpp @@ -119,12 +119,6 @@ namespace pkpy{ return VAR(ss.str()); }); - vm->bind__hash__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ - C99Struct& self = _CAST(C99Struct&, obj); - std::string_view view((char*)self.p, self.size); - return (i64)std::hash()(view); - }); - vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ C99Struct& self = _CAST(C99Struct&, args[0]); return VAR_T(VoidP, self.p); diff --git a/tests/99_cffi_2.py b/tests/99_cffi_2.py index 8737d33d..06a5406d 100644 --- a/tests/99_cffi_2.py +++ b/tests/99_cffi_2.py @@ -51,6 +51,5 @@ a = array(10, item_size=4) assert a.item_count == 10 assert a.item_size == 4 -_ = hash(a) a[4] = int_(123) assert a[4] == int_(123) \ No newline at end of file