mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
...
This commit is contained in:
parent
ca73a1a248
commit
ac334e83a1
@ -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<i64>(self._()); \
|
||||
});
|
||||
|
||||
} // namespace pkpy
|
@ -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<intptr_t>(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;
|
||||
|
@ -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: ...
|
||||
|
@ -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<std::string_view>()(view);
|
||||
});
|
||||
|
||||
vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){
|
||||
C99Struct& self = _CAST(C99Struct&, args[0]);
|
||||
return VAR_T(VoidP, self.p);
|
||||
|
@ -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)
|
Loading…
x
Reference in New Issue
Block a user