Przeglądaj źródła

remove `set_userdata`

blueloveTH 1 rok temu
rodzic
commit
4ef2e9d156
3 zmienionych plików z 8 dodań i 24 usunięć
  1. 2 4
      include/pocketpy/bindings.h
  2. 4 16
      include/pocketpy/codeobject.h
  3. 2 4
      src/vm.cpp

+ 2 - 4
include/pocketpy/bindings.h

@@ -97,8 +97,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){
         F T::*field = lambda_get_userdata<F T::*>(args.begin());
         return VAR(self.*field);
     };
-    PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1);
-    PK_OBJ_GET(NativeFunc, _0).set_userdata(field);
+    PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1, field);
     PyObject* _1 = vm->None;
     if constexpr (!ReadOnly){
         auto fset = [](VM* vm, ArgsView args){
@@ -107,8 +106,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){
             self.*field = py_cast<F>(vm, args[1]);
             return vm->None;
         };
-        _1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2);
-        PK_OBJ_GET(NativeFunc, _1).set_userdata(field);
+        _1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2, field);
     }
     PyObject* prop = VAR(Property(_0, _1));
     obj->attr().set(StrName(name_sv), prop);

+ 4 - 16
include/pocketpy/codeobject.h

@@ -123,24 +123,12 @@ struct FuncDecl {
 
 struct NativeFunc {
     NativeFuncC f;
-
-    // old style argc-based call
-    int argc;
-
-    // new style decl-based call
-    FuncDecl_ decl;
-
+    int argc;           // old style argc-based call
+    FuncDecl_ decl;     // new style decl-based call
     any _userdata;
 
-    void set_userdata(any&& data) {
-        if(_userdata){
-            throw std::runtime_error("NativeFunc userdata already set");
-        }
-        _userdata = std::move(data);
-    }
-
-    NativeFunc(NativeFuncC f, int argc): f(f), argc(argc) {}
-    NativeFunc(NativeFuncC f, FuncDecl_ decl): f(f), argc(-1), decl(decl) {}
+    NativeFunc(NativeFuncC f, int argc, any userdata={}): f(f), argc(argc), decl(nullptr), _userdata(std::move(userdata)) {}
+    NativeFunc(NativeFuncC f, FuncDecl_ decl, any userdata={}): f(f), argc(-1), decl(decl), _userdata(std::move(userdata)) {}
 
     void check_size(VM* vm, ArgsView args) const;
     PyObject* call(VM* vm, ArgsView args) const { return f(vm, args); }

+ 2 - 4
src/vm.cpp

@@ -1303,8 +1303,7 @@ void VM::setattr(PyObject* obj, StrName name, PyObject* value){
 }
 
 PyObject* VM::bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata, BindType bt) {
-    PyObject* nf = VAR(NativeFunc(fn, argc));
-    PK_OBJ_GET(NativeFunc, nf).set_userdata(std::move(userdata));
+    PyObject* nf = VAR(NativeFunc(fn, argc, std::move(userdata)));
     switch(bt){
         case BindType::DEFAULT: break;
         case BindType::STATICMETHOD: nf = VAR(StaticMethod(nf)); break;
@@ -1331,8 +1330,7 @@ PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, Native
     }
     FuncDecl_ decl = co->func_decls[0];
     decl->docstring = docstring;
-    PyObject* f_obj = VAR(NativeFunc(fn, decl));
-    PK_OBJ_GET(NativeFunc, f_obj).set_userdata(std::move(userdata));
+    PyObject* f_obj = VAR(NativeFunc(fn, decl, std::move(userdata)));
 
     switch(bt){
         case BindType::STATICMETHOD: