diff --git a/include/pocketpy/bindings.h b/include/pocketpy/bindings.h index 45099910..13a30545 100644 --- a/include/pocketpy/bindings.h +++ b/include/pocketpy/bindings.h @@ -148,15 +148,15 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){ }, {}, BindType::STATICMETHOD); \ vm->bind_method<0>(type, "to_struct", [](VM* vm, ArgsView args){ \ wT& self = _CAST(wT&, args[0]); \ - return VAR_T(C99Struct, self._(), sizeof(vT)); \ + return vm->new_user_object(self._(), sizeof(vT)); \ }); \ vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ \ wT& self = _CAST(wT&, args[0]); \ - return VAR_T(VoidP, self._()); \ + return vm->new_user_object(self._()); \ }); \ vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){ \ wT& self = _CAST(wT&, args[0]); \ - return VAR_T(wT, *self._()); \ + return vm->new_user_object(*self._()); \ }); \ vm->bind_method<0>(type, "sizeof", [](VM* vm, ArgsView args){ \ return VAR(sizeof(vT)); \ diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index 7ebab26d..9dfecec0 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -11,8 +11,6 @@ namespace pkpy { return vm->register_user_class(mod, #name, base); \ } -#define VAR_T(T, ...) vm->heap.gcnew(vm->_tp_user(), __VA_ARGS__) - struct VoidP{ void* ptr; VoidP(const void* ptr): ptr(const_cast(ptr)){} diff --git a/src/array2d.cpp b/src/array2d.cpp index 1fc61d6b..63521b2a 100644 --- a/src/array2d.cpp +++ b/src/array2d.cpp @@ -377,7 +377,7 @@ void add_module_array2d(VM* vm){ vm->register_user_class(mod, "_array2d_iter"); vm->bind__iter__(vm->_tp_user(), [](VM* vm, PyObject* _0){ - return VAR_T(Array2dIter, _0); + return vm->new_user_object(_0); }); } diff --git a/src/cffi.cpp b/src/cffi.cpp index e869ae44..897acda2 100644 --- a/src/cffi.cpp +++ b/src/cffi.cpp @@ -69,7 +69,7 @@ namespace pkpy{ else vm->ValueError(_S("invalid hex char: '", s[i+1], "'")); buffer.p[i/2] = c; } - return VAR_T(C99Struct, std::move(buffer)); + return vm->new_user_object(std::move(buffer)); }, {}, BindType::STATICMETHOD); vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ @@ -81,7 +81,7 @@ namespace pkpy{ vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ C99Struct& self = _CAST(C99Struct&, args[0]); - return VAR_T(VoidP, self.p); + return vm->new_user_object(self.p); }); vm->bind_method<0>(type, "sizeof", [](VM* vm, ArgsView args){ @@ -164,7 +164,7 @@ void add_module_c(VM* vm){ vm->register_user_class(mod, "void_p", true); vm->register_user_class(mod, "struct", true); - mod->attr().set("NULL", VAR_T(VoidP, nullptr)); + mod->attr().set("NULL", vm->new_user_object(nullptr)); vm->bind(mod, "p_cast(ptr: 'void_p', cls: type[T]) -> T", [](VM* vm, ArgsView args){ VoidP& ptr = CAST(VoidP&, args[0]); @@ -193,7 +193,7 @@ void add_module_c(VM* vm){ #define BIND_PRIMITIVE(T, CNAME) \ vm->bind_func<1>(mod, CNAME "_", [](VM* vm, ArgsView args){ \ T val = CAST(T, args[0]); \ - return VAR_T(C99Struct, &val, sizeof(T)); \ + return vm->new_user_object(&val, sizeof(T)); \ }); \ type = vm->new_type_object(mod, CNAME "_p", vm->_tp_user()); \ mod->attr().set(CNAME "_p", type); \ @@ -273,7 +273,7 @@ void add_module_c(VM* vm){ } PyObject* from_void_p(VM* vm, void* p){ - return VAR_T(VoidP, p); + return vm->new_user_object(p); } } // namespace pkpy \ No newline at end of file diff --git a/src/iter.cpp b/src/iter.cpp index f76da05c..647dc009 100644 --- a/src/iter.cpp +++ b/src/iter.cpp @@ -110,7 +110,7 @@ namespace pkpy{ } PyObject* VM::_py_generator(Frame&& frame, ArgsView buffer){ - return VAR_T(Generator, std::move(frame), buffer); + return vm->new_user_object(std::move(frame), buffer); } } // namespace pkpy \ No newline at end of file diff --git a/src/linalg.cpp b/src/linalg.cpp index 81192c1d..c9cb2eca 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -157,7 +157,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind_method<1>(type, "rotate", [](VM* vm, ArgsView args){ Vec2 self = _CAST(Vec2&, args[0]); float radian = CAST(f64, args[1]); - return VAR_T(Vec2, self.rotate(radian)); + return vm->new_user_object(self.rotate(radian)); }); vm->bind_method<1>(type, "rotate_", [](VM* vm, ArgsView args){ @@ -340,42 +340,42 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Mat3x3& self = _CAST(Mat3x3&, _0); Mat3x3& other = CAST(Mat3x3&, _1); - return VAR_T(Mat3x3, self + other); + return vm->new_user_object(self + other); }); vm->bind__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Mat3x3& self = _CAST(Mat3x3&, _0); Mat3x3& other = CAST(Mat3x3&, _1); - return VAR_T(Mat3x3, self - other); + return vm->new_user_object(self - other); }); vm->bind__mul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Mat3x3& self = _CAST(Mat3x3&, _0); f64 other = CAST_F(_1); - return VAR_T(Mat3x3, self * other); + return vm->new_user_object(self * other); }); vm->bind_method<1>(type, "__rmul__", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); f64 other = CAST_F(args[1]); - return VAR_T(Mat3x3, self * other); + return vm->new_user_object(self * other); }); vm->bind__truediv__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Mat3x3& self = _CAST(Mat3x3&, _0); f64 other = CAST_F(_1); - return VAR_T(Mat3x3, self / other); + return vm->new_user_object(self / other); }); vm->bind__matmul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Mat3x3& self = _CAST(Mat3x3&, _0); if(vm->is_user_type(_1)){ const Mat3x3& other = _CAST(Mat3x3&, _1); - return VAR_T(Mat3x3, self.matmul(other)); + return vm->new_user_object(self.matmul(other)); } if(vm->is_user_type(_1)){ const Vec3& other = _CAST(Vec3&, _1); - return VAR_T(Vec3, self.matmul(other)); + return vm->new_user_object(self.matmul(other)); } return vm->NotImplemented; }); @@ -384,7 +384,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s const Mat3x3& self = _CAST(Mat3x3&, args[0]); const Mat3x3& other = CAST(Mat3x3&, args[1]); if(args[2] == vm->None){ - return VAR_T(Mat3x3, self.matmul(other)); + return vm->new_user_object(self.matmul(other)); }else{ Mat3x3& out = CAST(Mat3x3&, args[2]); out = self.matmul(other); @@ -399,21 +399,21 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind_method<0>(type, "transpose", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); - return VAR_T(Mat3x3, self.transpose()); + return vm->new_user_object(self.transpose()); }); vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ Mat3x3& self = _CAST(Mat3x3&, obj); Mat3x3 ret; if(!self.inverse(ret)) vm->ValueError("matrix is not invertible"); - return VAR_T(Mat3x3, ret); + return vm->new_user_object(ret); }); vm->bind_method<0>(type, "inverse", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); Mat3x3 ret; if(!self.inverse(ret)) vm->ValueError("matrix is not invertible"); - return VAR_T(Mat3x3, ret); + return vm->new_user_object(ret); }); vm->bind_method<0>(type, "inverse_", [](VM* vm, ArgsView args){ @@ -432,17 +432,17 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s // @staticmethod vm->bind(type, "zeros()", [](VM* vm, ArgsView args){ - return VAR_T(Mat3x3, Mat3x3::zeros()); + return vm->new_user_object(Mat3x3::zeros()); }, {}, BindType::STATICMETHOD); // @staticmethod vm->bind(type, "ones()", [](VM* vm, ArgsView args){ - return VAR_T(Mat3x3, Mat3x3::ones()); + return vm->new_user_object(Mat3x3::ones()); }, {}, BindType::STATICMETHOD); // @staticmethod vm->bind(type, "identity()", [](VM* vm, ArgsView args){ - return VAR_T(Mat3x3, Mat3x3::identity()); + return vm->new_user_object(Mat3x3::identity()); }, {}, BindType::STATICMETHOD); /*************** affine transformations ***************/ @@ -451,7 +451,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s Vec2 t = CAST(Vec2, args[0]); f64 r = CAST_F(args[1]); Vec2 s = CAST(Vec2, args[2]); - return VAR_T(Mat3x3, Mat3x3::trs(t, r, s)); + return vm->new_user_object(Mat3x3::trs(t, r, s)); }, {}, BindType::STATICMETHOD); vm->bind(type, "copy_trs_(self, t: vec2, r: float, s: vec2)", [](VM* vm, ArgsView args){ @@ -491,7 +491,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind_method<0>(type, "_t", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); - return VAR_T(Vec2, self._t()); + return vm->new_user_object(self._t()); }); vm->bind_method<0>(type, "_r", [](VM* vm, ArgsView args){ @@ -501,14 +501,14 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind_method<0>(type, "_s", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); - return VAR_T(Vec2, self._s()); + return vm->new_user_object(self._s()); }); vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){ const Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 v = CAST(Vec2, args[1]); Vec2 res(self._11 * v.x + self._12 * v.y + self._13, self._21 * v.x + self._22 * v.y + self._23); - return VAR_T(Vec2, res); + return vm->new_user_object(res); }); vm->bind_method<1>(type, "inverse_transform_point", [](VM* vm, ArgsView args){ @@ -517,14 +517,14 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s Mat3x3 inv; if(!self.inverse(inv)) vm->ValueError("matrix is not invertible"); Vec2 res(inv._11 * v.x + inv._12 * v.y + inv._13, inv._21 * v.x + inv._22 * v.y + inv._23); - return VAR_T(Vec2, res); + return vm->new_user_object(res); }); vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){ const Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 v = CAST(Vec2, args[1]); Vec2 res(self._11 * v.x + self._12 * v.y, self._21 * v.x + self._22 * v.y); - return VAR_T(Vec2, res); + return vm->new_user_object(res); }); vm->bind_method<1>(type, "inverse_transform_vector", [](VM* vm, ArgsView args){ @@ -533,7 +533,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s Mat3x3 inv; if(!self.inverse(inv)) vm->ValueError("matrix is not invertible"); Vec2 res(inv._11 * v.x + inv._12 * v.y, inv._21 * v.x + inv._22 * v.y); - return VAR_T(Vec2, res); + return vm->new_user_object(res); }); } diff --git a/src/modules.cpp b/src/modules.cpp index 78f59d77..8200b979 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -75,7 +75,7 @@ void add_module_time(VM* vm){ vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) { auto now = std::chrono::system_clock::now(); std::time_t t = std::chrono::system_clock::to_time_t(now); - return VAR_T(PyStructTime, t); + return vm->new_user_object(t); }); } diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index b3b4e9dd..3732567d 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -371,7 +371,7 @@ void init_builtins(VM* _vm) { return VAR(r); }); - _vm->bind__iter__(VM::tp_range, [](VM* vm, PyObject* obj) { return VAR_T(RangeIter, PK_OBJ_GET(Range, obj)); }); + _vm->bind__iter__(VM::tp_range, [](VM* vm, PyObject* obj) { return vm->new_user_object(PK_OBJ_GET(Range, obj)); }); // tp_nonetype _vm->bind__repr__(_vm->_tp(_vm->None), [](VM* vm, PyObject* _0) { @@ -567,7 +567,7 @@ void init_builtins(VM* _vm) { return VAR(self.index(CAST(Str&, _1)) != -1); }); _vm->bind__str__(VM::tp_str, [](VM* vm, PyObject* _0) { return _0; }); - _vm->bind__iter__(VM::tp_str, [](VM* vm, PyObject* _0) { return VAR_T(StringIter, _0); }); + _vm->bind__iter__(VM::tp_str, [](VM* vm, PyObject* _0) { return vm->new_user_object(_0); }); _vm->bind__repr__(VM::tp_str, [](VM* vm, PyObject* _0) { const Str& self = _CAST(Str&, _0); return VAR(self.escape()); @@ -993,7 +993,7 @@ void init_builtins(VM* _vm) { }); _vm->bind__iter__(VM::tp_list, [](VM* vm, PyObject* _0) { List& self = _CAST(List&, _0); - return VAR_T(ArrayIter, _0, self.begin(), self.end()); + return vm->new_user_object(_0, self.begin(), self.end()); }); _vm->bind__getitem__(VM::tp_list, PyArrayGetItem); _vm->bind__setitem__(VM::tp_list, [](VM* vm, PyObject* _0, PyObject* _1, PyObject* _2){ @@ -1055,7 +1055,7 @@ void init_builtins(VM* _vm) { _vm->bind__iter__(VM::tp_tuple, [](VM* vm, PyObject* _0) { Tuple& self = _CAST(Tuple&, _0); - return VAR_T(ArrayIter, _0, self.begin(), self.end()); + return vm->new_user_object(_0, self.begin(), self.end()); }); _vm->bind__getitem__(VM::tp_tuple, PyArrayGetItem); _vm->bind__len__(VM::tp_tuple, [](VM* vm, PyObject* obj) {