mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add bindstaticmethod
This commit is contained in:
parent
35f973059c
commit
212a705a4d
@ -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 name name of the method.
|
||||||
/// @param f function to bind.
|
/// @param f function to bind.
|
||||||
PK_API void py_bindmethod(py_Type type, const char* name, py_CFunction f);
|
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.
|
/// Bind a function to the object via "argc-based" style.
|
||||||
/// @param obj the target object.
|
/// @param obj the target object.
|
||||||
/// @param name name of the function.
|
/// @param name name of the function.
|
||||||
|
@ -860,8 +860,8 @@ void pk__add_module_linalg() {
|
|||||||
py_newvec2(_const(vec2, "DOWN"), (c11_vec2){{0, 1}});
|
py_newvec2(_const(vec2, "DOWN"), (c11_vec2){{0, 1}});
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
py_bindmethod(vec2, "angle", vec2_angle_STATIC);
|
py_bindstaticmethod(vec2, "angle", vec2_angle_STATIC);
|
||||||
py_bindmethod(vec2, "smooth_damp", vec2_smoothdamp_STATIC);
|
py_bindstaticmethod(vec2, "smooth_damp", vec2_smoothdamp_STATIC);
|
||||||
|
|
||||||
py_bindproperty(vec2, "x", vec2__x, NULL);
|
py_bindproperty(vec2, "x", vec2__x, NULL);
|
||||||
py_bindproperty(vec2, "y", vec2__y, 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, "inverse", mat3x3_inverse);
|
||||||
py_bindmethod(mat3x3, "copy_", mat3x3_copy_);
|
py_bindmethod(mat3x3, "copy_", mat3x3_copy_);
|
||||||
py_bindmethod(mat3x3, "inverse_", mat3x3_inverse_);
|
py_bindmethod(mat3x3, "inverse_", mat3x3_inverse_);
|
||||||
py_bindmethod(mat3x3, "zeros", mat3x3_zeros_STATIC);
|
py_bindstaticmethod(mat3x3, "zeros", mat3x3_zeros_STATIC);
|
||||||
py_bindmethod(mat3x3, "identity", mat3x3_identity_STATIC);
|
py_bindstaticmethod(mat3x3, "identity", mat3x3_identity_STATIC);
|
||||||
py_bindmethod(mat3x3, "trs", mat3x3_trs_STATIC);
|
py_bindstaticmethod(mat3x3, "trs", mat3x3_trs_STATIC);
|
||||||
py_bindmethod(mat3x3, "copy_trs_", mat3x3_copy_trs_);
|
py_bindmethod(mat3x3, "copy_trs_", mat3x3_copy_trs_);
|
||||||
py_bindmethod(mat3x3, "t", mat3x3_t);
|
py_bindmethod(mat3x3, "t", mat3x3_t);
|
||||||
py_bindmethod(mat3x3, "r", mat3x3_r);
|
py_bindmethod(mat3x3, "r", mat3x3_r);
|
||||||
|
@ -117,7 +117,6 @@ static bool type__annotations__(int argc, py_Ref argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pk_object__register() {
|
void pk_object__register() {
|
||||||
// TODO: use staticmethod
|
|
||||||
py_bindmagic(tp_object, __new__, pk__object_new);
|
py_bindmagic(tp_object, __new__, pk__object_new);
|
||||||
|
|
||||||
py_bindmagic(tp_object, __hash__, object__hash__);
|
py_bindmagic(tp_object, __hash__, object__hash__);
|
||||||
|
@ -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);
|
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) {
|
void py_bindfunc(py_Ref obj, const char* name, py_CFunction f) {
|
||||||
py_TValue tmp;
|
py_TValue tmp;
|
||||||
py_newnativefunc(&tmp, f);
|
py_newnativefunc(&tmp, f);
|
||||||
|
@ -315,16 +315,9 @@ val = vec2.angle(vec2(-1, 0), vec2(0, -1))
|
|||||||
assert 1.57 < val < 1.58
|
assert 1.57 < val < 1.58
|
||||||
|
|
||||||
# test about staticmethod
|
# 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()
|
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.copy_(mat3x3.zeros()) is None
|
||||||
assert d == mat3x3.zeros()
|
assert d == mat3x3.zeros()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user