update bindings

This commit is contained in:
blueloveTH 2023-09-29 12:24:34 +08:00
parent d484868655
commit 895b3a8d1d
3 changed files with 43 additions and 40 deletions

View File

@ -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<T>::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>(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<decltype(std::declval<wT>()._())>; \
static_assert(std::is_trivially_copyable<vT>::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>(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

View File

@ -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);
};

View File

@ -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<PyStructTime>(type);