blueloveTH пре 2 година
родитељ
комит
ac334e83a1
5 измењених фајлова са 19 додато и 23 уклоњено
  1. 12 0
      include/pocketpy/bindings.h
  2. 0 12
      include/pocketpy/cffi.h
  3. 7 4
      include/typings/c.pyi
  4. 0 6
      src/cffi.cpp
  5. 0 1
      tests/99_cffi_2.py

+ 12 - 0
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);                                              \
             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<i64>(self._());                                 \
+        });
+
 }   // namespace pkpy
 }   // namespace pkpy

+ 0 - 12
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; }
     bool operator>=(const VoidP& other) const { return ptr >= other.ptr; }
     bool operator>=(const VoidP& other) const { return ptr >= other.ptr; }
 
 
-
     Str hex() const{
     Str hex() const{
         std::stringstream ss;
         std::stringstream ss;
         ss << std::hex << reinterpret_cast<intptr_t>(ptr);
         ss << std::hex << reinterpret_cast<intptr_t>(ptr);
@@ -88,17 +87,6 @@ struct C99Struct{
     static void _register(VM* vm, PyObject* mod, PyObject* type);
     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{
 struct ReflType{
     std::string_view name;
     std::string_view name;
     size_t size;
     size_t size;

+ 7 - 4
include/typings/c.pyi

@@ -12,6 +12,7 @@ class void_p:
     def __sub__(self, i: int) -> 'void_p': ...
     def __sub__(self, i: int) -> 'void_p': ...
     def __eq__(self, other: 'void_p') -> bool: ...
     def __eq__(self, other: 'void_p') -> bool: ...
     def __ne__(self, other: 'void_p') -> bool: ...
     def __ne__(self, other: 'void_p') -> bool: ...
+    def __hash__(self) -> int: ...
 
 
     def hex(self) -> str: ...
     def hex(self) -> str: ...
 
 
@@ -57,7 +58,7 @@ class struct:
 
 
     def addr(self) -> 'void_p': ...
     def addr(self) -> 'void_p': ...
     def copy(self) -> 'struct': ...
     def copy(self) -> 'struct': ...
-    def size(self) -> int: ...
+    def sizeof(self) -> int: ...
     def __eq__(self, other: 'struct') -> bool: ...
     def __eq__(self, other: 'struct') -> bool: ...
     def __ne__(self, other: 'struct') -> bool: ...
     def __ne__(self, other: 'struct') -> bool: ...
 
 
@@ -137,11 +138,13 @@ class _StructLike(Generic[T]):
     @classmethod
     @classmethod
     def from_struct(cls, s: struct) -> T: ...
     def from_struct(cls, s: struct) -> T: ...
 
 
-    def addr(self) -> '_Pointer[T]': ...
+    def addr(self) -> '_PointerLike[T]': ...
     def sizeof(self) -> int: ...
     def sizeof(self) -> int: ...
     def copy(self) -> T: ...
     def copy(self) -> T: ...
     def __eq__(self, other: T) -> bool: ...
     def __eq__(self, other: T) -> bool: ...
     def __ne__(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: ...

+ 0 - 6
src/cffi.cpp

@@ -119,12 +119,6 @@ namespace pkpy{
             return VAR(ss.str());
             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<std::string_view>()(view);
-        });
-
         vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){
         vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){
             C99Struct& self = _CAST(C99Struct&, args[0]);
             C99Struct& self = _CAST(C99Struct&, args[0]);
             return VAR_T(VoidP, self.p);
             return VAR_T(VoidP, self.p);

+ 0 - 1
tests/99_cffi_2.py

@@ -51,6 +51,5 @@ a = array(10, item_size=4)
 assert a.item_count == 10
 assert a.item_count == 10
 assert a.item_size == 4
 assert a.item_size == 4
 
 
-_ = hash(a)
 a[4] = int_(123)
 a[4] = int_(123)
 assert a[4] == int_(123)
 assert a[4] == int_(123)