Parcourir la source

move `PY_FIELD` to `PY_FIELD_EX`

blueloveTH il y a 1 an
Parent
commit
a0f2c1a378
5 fichiers modifiés avec 82 ajouts et 42 suppressions
  1. 2 2
      docs/bindings.md
  2. 48 8
      include/pocketpy/bindings.h
  3. 5 5
      src/array2d.cpp
  4. 18 18
      src/linalg.cpp
  5. 9 9
      src/modules.cpp

+ 2 - 2
docs/bindings.md

@@ -136,9 +136,9 @@ struct wrapped__Point{
 
     static void _register(VM* vm, PyObject* mod, PyObject* type){
         // wrap field x
-        PY_FIELD(wrapped__Point, "x", _, x)
+        PY_FIELD(wrapped__Point, "x", x)
         // wrap field y
-        PY_FIELD(wrapped__Point, "y", _, y)
+        PY_FIELD(wrapped__Point, "y", y)
 
         // __init__ method
         vm->bind(type, "__init__(self, x, y)", [](VM* vm, ArgsView args){

+ 48 - 8
include/pocketpy/bindings.h

@@ -74,7 +74,7 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){
     vm->bind(obj, sig, proxy_wrapper, proxy);
 }
 /*****************************************************************/
-#define PY_FIELD(T, NAME, REF, EXPR)       \
+#define PY_FIELD_EX(T, NAME, REF, EXPR)       \
         vm->bind_property(type, NAME,               \
             [](VM* vm, ArgsView args){              \
                 T& self = PK_OBJ_GET(T, args[0]);   \
@@ -86,14 +86,14 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){
                 return vm->None;                                                    \
             });
 
-#define PY_READONLY_FIELD(T, NAME, REF, EXPR)          \
-        vm->bind_property(type, NAME,                  \
-            [](VM* vm, ArgsView args){              \
-                T& self = PK_OBJ_GET(T, args[0]);   \
-                return VAR(self.REF()->EXPR);       \
+#define PY_READONLY_FIELD_EX(T, NAME, REF, EXPR)            \
+        vm->bind_property(type, NAME,                       \
+            [](VM* vm, ArgsView args){                      \
+                T& self = PK_OBJ_GET(T, args[0]);           \
+                return VAR(self.REF()->EXPR);               \
             });
 
-#define PY_PROPERTY(T, NAME, REF, FGET, FSET)  \
+#define PY_PROPERTY_EX(T, NAME, REF, FGET, FSET)  \
         vm->bind_property(type, NAME,                   \
             [](VM* vm, ArgsView args){                  \
                 T& self = PK_OBJ_GET(T, args[0]);       \
@@ -106,12 +106,52 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){
                 return vm->None;                            \
             });
 
-#define PY_READONLY_PROPERTY(T, NAME, REF, FGET)  \
+#define PY_READONLY_PROPERTY_EX(T, NAME, REF, FGET)  \
         vm->bind_property(type, NAME,                   \
             [](VM* vm, ArgsView args){                  \
                 T& self = PK_OBJ_GET(T, args[0]);       \
                 return VAR(self.REF()->FGET());         \
             });
+/*****************************************************************/
+#define PY_FIELD(T, NAME, EXPR)       \
+        vm->bind_property(type, NAME,               \
+            [](VM* vm, ArgsView args){              \
+                T& self = PK_OBJ_GET(T, args[0]);   \
+                return VAR(self.EXPR);       \
+            },                                      \
+            [](VM* vm, ArgsView args){              \
+                T& self = PK_OBJ_GET(T, args[0]);   \
+                self.EXPR = CAST(decltype(self.EXPR), args[1]);       \
+                return vm->None;                                                    \
+            });
+
+#define PY_READONLY_FIELD(T, NAME, EXPR)            \
+        vm->bind_property(type, NAME,                       \
+            [](VM* vm, ArgsView args){                      \
+                T& self = PK_OBJ_GET(T, args[0]);           \
+                return VAR(self.EXPR);               \
+            });
+
+#define PY_PROPERTY(T, NAME, FGET, FSET)  \
+        vm->bind_property(type, NAME,                   \
+            [](VM* vm, ArgsView args){                  \
+                T& self = PK_OBJ_GET(T, args[0]);       \
+                return VAR(self.FGET());         \
+            },                                          \
+            [](VM* vm, ArgsView args){                  \
+                T& self = _CAST(T&, args[0]);           \
+                using __NT = decltype(self.FGET());   \
+                self.FSET(CAST(__NT, args[1]));       \
+                return vm->None;                            \
+            });
+
+#define PY_READONLY_PROPERTY(T, NAME, FGET)  \
+        vm->bind_property(type, NAME,                   \
+            [](VM* vm, ArgsView args){                  \
+                T& self = PK_OBJ_GET(T, args[0]);       \
+                return VAR(self.FGET());                \
+            });
+/*****************************************************************/
 
 #define PY_STRUCT_LIKE(wT)   \
         using vT = std::remove_pointer_t<decltype(std::declval<wT>()._())>;         \

+ 5 - 5
src/array2d.cpp

@@ -60,11 +60,11 @@ struct Array2d{
             return vm->None;
         });
 
-        PY_READONLY_FIELD(Array2d, "n_cols", _, n_cols);
-        PY_READONLY_FIELD(Array2d, "n_rows", _, n_rows);
-        PY_READONLY_FIELD(Array2d, "width", _, n_cols);
-        PY_READONLY_FIELD(Array2d, "height", _, n_rows);
-        PY_READONLY_FIELD(Array2d, "numel", _, numel);
+        PY_READONLY_FIELD(Array2d, "n_cols", n_cols);
+        PY_READONLY_FIELD(Array2d, "n_rows", n_rows);
+        PY_READONLY_FIELD(Array2d, "width", n_cols);
+        PY_READONLY_FIELD(Array2d, "height", n_rows);
+        PY_READONLY_FIELD(Array2d, "numel", numel);
 
         vm->bind(type, "is_valid(self, col: int, row: int)", [](VM* vm, ArgsView args){
             Array2d& self = PK_OBJ_GET(Array2d, args[0]);

+ 18 - 18
src/linalg.cpp

@@ -167,8 +167,8 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
             return vm->None;
         });
 
-        PY_FIELD(Vec2, "x", _, x)
-        PY_FIELD(Vec2, "y", _, y)
+        PY_FIELD(Vec2, "x", x)
+        PY_FIELD(Vec2, "y", y)
 
         BIND_VEC_VEC_OP(2, __add__, +)
         BIND_VEC_VEC_OP(2, __sub__, -)
@@ -202,9 +202,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
             return VAR(ss.str());
         });
 
-        PY_FIELD(Vec3, "x", _, x)
-        PY_FIELD(Vec3, "y", _, y)
-        PY_FIELD(Vec3, "z", _, z)
+        PY_FIELD(Vec3, "x", x)
+        PY_FIELD(Vec3, "y", y)
+        PY_FIELD(Vec3, "z", z)
 
         BIND_VEC_VEC_OP(3, __add__, +)
         BIND_VEC_VEC_OP(3, __sub__, -)
@@ -238,10 +238,10 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
             return VAR(ss.str());
         });
 
-        PY_FIELD(Vec4, "x", _, x)
-        PY_FIELD(Vec4, "y", _, y)
-        PY_FIELD(Vec4, "z", _, z)
-        PY_FIELD(Vec4, "w", _, w)
+        PY_FIELD(Vec4, "x", x)
+        PY_FIELD(Vec4, "y", y)
+        PY_FIELD(Vec4, "z", z)
+        PY_FIELD(Vec4, "w", w)
 
         BIND_VEC_VEC_OP(4, __add__, +)
         BIND_VEC_VEC_OP(4, __sub__, -)
@@ -327,15 +327,15 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
             self.m[i][j] = CAST_F(value);
         });
 
-        PY_FIELD(Mat3x3, "_11", _, _11)
-        PY_FIELD(Mat3x3, "_12", _, _12)
-        PY_FIELD(Mat3x3, "_13", _, _13)
-        PY_FIELD(Mat3x3, "_21", _, _21)
-        PY_FIELD(Mat3x3, "_22", _, _22)
-        PY_FIELD(Mat3x3, "_23", _, _23)
-        PY_FIELD(Mat3x3, "_31", _, _31)
-        PY_FIELD(Mat3x3, "_32", _, _32)
-        PY_FIELD(Mat3x3, "_33", _, _33)
+        PY_FIELD(Mat3x3, "_11", _11)
+        PY_FIELD(Mat3x3, "_12", _12)
+        PY_FIELD(Mat3x3, "_13", _13)
+        PY_FIELD(Mat3x3, "_21", _21)
+        PY_FIELD(Mat3x3, "_22", _22)
+        PY_FIELD(Mat3x3, "_23", _23)
+        PY_FIELD(Mat3x3, "_31", _31)
+        PY_FIELD(Mat3x3, "_32", _32)
+        PY_FIELD(Mat3x3, "_33", _33)
 
         vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
             Mat3x3& self = _CAST(Mat3x3&, _0);

+ 9 - 9
src/modules.cpp

@@ -40,15 +40,15 @@ struct PyStructTime{
 
     static void _register(VM* vm, PyObject* mod, PyObject* type){
         vm->bind_notimplemented_constructor<PyStructTime>(type);
-        PY_READONLY_FIELD(PyStructTime, "tm_year", _, tm_year);
-        PY_READONLY_FIELD(PyStructTime, "tm_mon", _, tm_mon);
-        PY_READONLY_FIELD(PyStructTime, "tm_mday", _, tm_mday);
-        PY_READONLY_FIELD(PyStructTime, "tm_hour", _, tm_hour);
-        PY_READONLY_FIELD(PyStructTime, "tm_min", _, tm_min);
-        PY_READONLY_FIELD(PyStructTime, "tm_sec", _, tm_sec);
-        PY_READONLY_FIELD(PyStructTime, "tm_wday", _, tm_wday);
-        PY_READONLY_FIELD(PyStructTime, "tm_yday", _, tm_yday);
-        PY_READONLY_FIELD(PyStructTime, "tm_isdst", _, tm_isdst);
+        PY_READONLY_FIELD(PyStructTime, "tm_year", tm_year);
+        PY_READONLY_FIELD(PyStructTime, "tm_mon", tm_mon);
+        PY_READONLY_FIELD(PyStructTime, "tm_mday", tm_mday);
+        PY_READONLY_FIELD(PyStructTime, "tm_hour", tm_hour);
+        PY_READONLY_FIELD(PyStructTime, "tm_min", tm_min);
+        PY_READONLY_FIELD(PyStructTime, "tm_sec", tm_sec);
+        PY_READONLY_FIELD(PyStructTime, "tm_wday", tm_wday);
+        PY_READONLY_FIELD(PyStructTime, "tm_yday", tm_yday);
+        PY_READONLY_FIELD(PyStructTime, "tm_isdst", tm_isdst);
     }
 };