add bindstaticmethod

This commit is contained in:
blueloveTH 2024-11-14 18:54:25 +08:00
parent 35f973059c
commit 212a705a4d
5 changed files with 23 additions and 15 deletions

View File

@ -376,6 +376,11 @@ PK_API void py_bind(py_Ref obj, const char* sig, py_CFunction f);
/// @param name name of the method.
/// @param f function to bind.
PK_API void py_bindmethod(py_Type type, const char* name, py_CFunction f);
/// Bind a static method to type via "argc-based" style.
/// @param type the target type.
/// @param name name of the method.
/// @param f function to bind.
PK_API void py_bindstaticmethod(py_Type type, const char* name, py_CFunction f);
/// Bind a function to the object via "argc-based" style.
/// @param obj the target object.
/// @param name name of the function.

View File

@ -860,8 +860,8 @@ void pk__add_module_linalg() {
py_newvec2(_const(vec2, "DOWN"), (c11_vec2){{0, 1}});
// clang-format on
py_bindmethod(vec2, "angle", vec2_angle_STATIC);
py_bindmethod(vec2, "smooth_damp", vec2_smoothdamp_STATIC);
py_bindstaticmethod(vec2, "angle", vec2_angle_STATIC);
py_bindstaticmethod(vec2, "smooth_damp", vec2_smoothdamp_STATIC);
py_bindproperty(vec2, "x", vec2__x, NULL);
py_bindproperty(vec2, "y", vec2__y, NULL);
@ -884,9 +884,9 @@ void pk__add_module_linalg() {
py_bindmethod(mat3x3, "inverse", mat3x3_inverse);
py_bindmethod(mat3x3, "copy_", mat3x3_copy_);
py_bindmethod(mat3x3, "inverse_", mat3x3_inverse_);
py_bindmethod(mat3x3, "zeros", mat3x3_zeros_STATIC);
py_bindmethod(mat3x3, "identity", mat3x3_identity_STATIC);
py_bindmethod(mat3x3, "trs", mat3x3_trs_STATIC);
py_bindstaticmethod(mat3x3, "zeros", mat3x3_zeros_STATIC);
py_bindstaticmethod(mat3x3, "identity", mat3x3_identity_STATIC);
py_bindstaticmethod(mat3x3, "trs", mat3x3_trs_STATIC);
py_bindmethod(mat3x3, "copy_trs_", mat3x3_copy_trs_);
py_bindmethod(mat3x3, "t", mat3x3_t);
py_bindmethod(mat3x3, "r", mat3x3_r);

View File

@ -117,7 +117,6 @@ static bool type__annotations__(int argc, py_Ref argv) {
}
void pk_object__register() {
// TODO: use staticmethod
py_bindmagic(tp_object, __new__, pk__object_new);
py_bindmagic(tp_object, __hash__, object__hash__);

View File

@ -54,6 +54,17 @@ void py_bindmethod(py_Type type, const char* name, py_CFunction f) {
py_setdict(py_tpobject(type), py_name(name), &tmp);
}
void py_bindstaticmethod(py_Type type, const char* name, py_CFunction f) {
py_TValue tmp;
py_newnativefunc(&tmp, f);
bool ok = py_tpcall(tp_staticmethod, 1, &tmp);
if(!ok) {
py_printexc();
c11__abort("py_bindstaticmethod(): failed to create staticmethod");
}
py_setdict(py_tpobject(type), py_name(name), py_retval());
}
void py_bindfunc(py_Ref obj, const char* name, py_CFunction f) {
py_TValue tmp;
py_newnativefunc(&tmp, f);

View File

@ -315,16 +315,9 @@ val = vec2.angle(vec2(-1, 0), vec2(0, -1))
assert 1.57 < val < 1.58
# test about staticmethod
# class mymat3x3(mat3x3):
# def f(self):
# _0 = self.zeros()
# _1 = super().zeros()
# _2 = mat3x3.zeros()
# return _0 == _1 == _2
# assert mymat3x3().f()
d = mat3x3.identity()
d1 = mat3x3(1, 2, 3, 4, 5, 6, 7, 8, 9).identity()
assert d == d1
assert d.copy_(mat3x3.zeros()) is None
assert d == mat3x3.zeros()