From 895b3a8d1d2f5fa1b181b0a1fd9dbffc14ad2c62 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 29 Sep 2023 12:24:34 +0800 Subject: [PATCH] update bindings --- include/pocketpy/bindings.h | 77 ++++++++++++++++++------------------- include/pocketpy/linalg.h | 4 ++ src/pocketpy.cpp | 2 +- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/include/pocketpy/bindings.h b/include/pocketpy/bindings.h index 50022550..f4d3bbc8 100644 --- a/include/pocketpy/bindings.h +++ b/include/pocketpy/bindings.h @@ -78,11 +78,11 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){ vm->bind_property(type, NAME, \ [](VM* vm, ArgsView args){ \ T& self = _CAST(T&, args[0]); \ - return VAR(self.REF().EXPR); \ + return VAR(self.REF()->EXPR); \ }, \ [](VM* vm, ArgsView args){ \ T& self = _CAST(T&, args[0]); \ - self.REF().EXPR = CAST(decltype(self.REF().EXPR), args[1]); \ + self.REF()->EXPR = CAST(decltype(self.REF()->EXPR), args[1]); \ return vm->None; \ }); @@ -90,19 +90,19 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){ vm->bind_property(type, NAME, \ [](VM* vm, ArgsView args){ \ T& self = _CAST(T&, args[0]); \ - return VAR(self.REF().EXPR); \ + return VAR(self.REF()->EXPR); \ }); #define PY_PROPERTY(T, NAME, REF, FGET, FSET) \ vm->bind_property(type, NAME, \ [](VM* vm, ArgsView args){ \ T& self = _CAST(T&, args[0]); \ - return VAR(self.REF().FGET()); \ + return VAR(self.REF()->FGET()); \ }, \ [](VM* vm, ArgsView args){ \ T& self = _CAST(T&, args[0]); \ - using __NT = decltype(self.REF().FGET()); \ - self.REF().FSET(CAST(__NT, args[1])); \ + using __NT = decltype(self.REF()->FGET()); \ + self.REF()->FSET(CAST(__NT, args[1])); \ return vm->None; \ }); @@ -110,40 +110,39 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){ vm->bind_property(type, NAME, \ [](VM* vm, ArgsView args){ \ T& self = _CAST(T&, args[0]); \ - return VAR(self.REF().FGET()); \ + return VAR(self.REF()->FGET()); \ }); -#define PY_STRUCT_LIKE(T) \ - static_assert(std::is_trivially_copyable::value); \ - vm->bind_func<1>(type, "from_struct", [](VM* vm, ArgsView args){ \ - C99Struct& s = CAST(C99Struct&, args[0]); \ - if(s.size != sizeof(T)) vm->ValueError("size mismatch"); \ - PyObject* obj = vm->heap.gcnew(T::_type(vm)); \ - memcpy(&_CAST(T&, obj), s.p, sizeof(T)); \ - return obj; \ - }); \ - vm->bind_method<0>(type, "to_struct", [](VM* vm, ArgsView args){ \ - T& self = _CAST(T&, args[0]); \ - return VAR_T(C99Struct, &self, sizeof(T)); \ - }); \ - vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ \ - T& self = _CAST(T&, args[0]); \ - return VAR_T(VoidP, &self); \ - }); \ - vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){ \ - T& self = _CAST(T&, args[0]); \ - return VAR_T(T, self); \ - }); \ - vm->bind_method<0>(type, "sizeof", [](VM* vm, ArgsView args){ \ - return VAR(sizeof(T)); \ - }); \ - vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ - T& self = _CAST(T&, _0); \ - if(!vm->isinstance(_1, T::_type(vm))){ \ - return vm->NotImplemented; \ - } \ - T& other = _CAST(T&, _1); \ - return VAR(self == other); \ - }); \ +#define PY_STRUCT_LIKE(wT) \ + using vT = std::remove_pointer_t()._())>; \ + static_assert(std::is_trivially_copyable::value); \ + vm->bind_func<1>(type, "from_struct", [](VM* vm, ArgsView args){ \ + C99Struct& s = CAST(C99Struct&, args[0]); \ + if(s.size != sizeof(vT)) vm->ValueError("size mismatch"); \ + PyObject* obj = vm->heap.gcnew(wT::_type(vm)); \ + memcpy(_CAST(wT&, obj)._(), s.p, sizeof(vT)); \ + return obj; \ + }); \ + vm->bind_method<0>(type, "to_struct", [](VM* vm, ArgsView args){ \ + wT& self = _CAST(wT&, args[0]); \ + return VAR_T(C99Struct, self._(), sizeof(vT)); \ + }); \ + vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ \ + wT& self = _CAST(wT&, args[0]); \ + return VAR_T(VoidP, self._()); \ + }); \ + vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){ \ + wT& self = _CAST(wT&, args[0]); \ + return VAR_T(wT, *self._()); \ + }); \ + vm->bind_method<0>(type, "sizeof", [](VM* vm, ArgsView args){ \ + return VAR(sizeof(vT)); \ + }); \ + 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._()); \ + }); \ } // namespace pkpy \ No newline at end of file diff --git a/include/pocketpy/linalg.h b/include/pocketpy/linalg.h index a7c94318..496a5e6b 100644 --- a/include/pocketpy/linalg.h +++ b/include/pocketpy/linalg.h @@ -276,6 +276,7 @@ struct PyVec2: Vec2 { PyVec2() : Vec2() {} PyVec2(const Vec2& v) : Vec2(v) {} PyVec2(const PyVec2& v) = default; + Vec2* _() { return this; } static void _register(VM* vm, PyObject* mod, PyObject* type); }; @@ -286,6 +287,7 @@ struct PyVec3: Vec3 { PyVec3() : Vec3() {} PyVec3(const Vec3& v) : Vec3(v) {} PyVec3(const PyVec3& v) = default; + Vec3* _() { return this; } static void _register(VM* vm, PyObject* mod, PyObject* type); }; @@ -296,6 +298,7 @@ struct PyVec4: Vec4{ PyVec4(): Vec4(){} PyVec4(const Vec4& v): Vec4(v){} PyVec4(const PyVec4& v) = default; + Vec4* _(){ return this; } static void _register(VM* vm, PyObject* mod, PyObject* type); }; @@ -306,6 +309,7 @@ struct PyMat3x3: Mat3x3{ PyMat3x3(): Mat3x3(){} PyMat3x3(const Mat3x3& other): Mat3x3(other){} PyMat3x3(const PyMat3x3& other) = default; + Mat3x3* _(){ return this; } static void _register(VM* vm, PyObject* mod, PyObject* type); }; diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 762b887a..ff1b1332 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1453,7 +1453,7 @@ struct PyStructTime{ tm_isdst = tm->tm_isdst; } - PyStructTime& _() { return *this; } + PyStructTime* _() { return this; } static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_notimplemented_constructor(type);