From da7d30048bf651fc2c7642d8d32528512044a11f Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 29 Sep 2023 14:57:54 +0800 Subject: [PATCH] ... --- include/pocketpy/bindings.h | 4 ++++ include/typings/c.pyi | 6 ++++++ include/typings/linalg.pyi | 7 ++++++- src/cffi.cpp | 5 +++++ tests/99_cffi_2.py | 5 ++++- 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/pocketpy/bindings.h b/include/pocketpy/bindings.h index eba1b6b5..e96fe20a 100644 --- a/include/pocketpy/bindings.h +++ b/include/pocketpy/bindings.h @@ -155,6 +155,10 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){ vm->bind__hash__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ \ wT& self = _CAST(wT&, obj); \ return reinterpret_cast(self._()); \ + }); \ + vm->bind_property(type, "_value: int", [](VM* vm, ArgsView args){ \ + wT& self = _CAST(wT&, args[0]); \ + return VAR(reinterpret_cast(self._())); \ }); } // namespace pkpy \ No newline at end of file diff --git a/include/typings/c.pyi b/include/typings/c.pyi index 4b63c883..b78a1ab8 100644 --- a/include/typings/c.pyi +++ b/include/typings/c.pyi @@ -16,6 +16,9 @@ class void_p: def hex(self) -> str: ... + @property + def _value(self) -> int: ... + def read_char(self) -> int: ... def read_uchar(self) -> int: ... def read_short(self) -> int: ... @@ -148,3 +151,6 @@ class _PointerLike(Generic[T]): def __eq__(self, other) -> bool: ... def __ne__(self, other) -> bool: ... def __hash__(self) -> int: ... + + @property + def _value(self) -> int: ... diff --git a/include/typings/linalg.pyi b/include/typings/linalg.pyi index 6330173d..40bfc210 100644 --- a/include/typings/linalg.pyi +++ b/include/typings/linalg.pyi @@ -1,5 +1,5 @@ from typing import overload -from c import _StructLike +from c import _StructLike, float_p class vec2(_StructLike['vec2']): x: float @@ -107,3 +107,8 @@ class mat3x3(_StructLike['mat3x3']): def transform_point(self, p: vec2) -> vec2: ... def transform_vector(self, v: vec2) -> vec2: ... + +vec2_p = float_p +vec3_p = float_p +vec4_p = float_p +mat3x3_p = float_p diff --git a/src/cffi.cpp b/src/cffi.cpp index bb678f6e..7965f68d 100644 --- a/src/cffi.cpp +++ b/src/cffi.cpp @@ -22,6 +22,11 @@ namespace pkpy{ return VAR(ss.str()); }); + vm->bind_property(type, "_value: int", [](VM* vm, ArgsView args){ + VoidP& self = _CAST(VoidP&, args[0]); + return VAR(reinterpret_cast(self.ptr)); + }); + #define BIND_CMP(name, op) \ vm->bind##name(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){ \ if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return vm->NotImplemented; \ diff --git a/tests/99_cffi_2.py b/tests/99_cffi_2.py index 06a5406d..17d184a2 100644 --- a/tests/99_cffi_2.py +++ b/tests/99_cffi_2.py @@ -46,7 +46,10 @@ my_struct1 = c.struct(16) c_void_1.write_struct(my_struct1) assert c_void_1.read_struct(16) == my_struct1 -from c import array, int_ +from c import array, int_, NULL + +assert NULL._value == 0 + a = array(10, item_size=4) assert a.item_count == 10 assert a.item_size == 4