blueloveTH 2 年之前
父節點
當前提交
925679a040
共有 2 個文件被更改,包括 12 次插入12 次删除
  1. 9 5
      src/obj.h
  2. 3 7
      src/vm.h

+ 9 - 5
src/obj.h

@@ -21,12 +21,9 @@ typedef int (*LuaStyleFuncC)(VM*);
 
 struct NativeFunc {
     NativeFuncC f;
-    int argc;       // DONOT include self
-    bool method;
+    int argc;
 
     // this is designed for lua style C bindings
-    // access it via `_CAST(NativeFunc&, args[-2])._lua_f`
-    // (-2) or (-1) depends on the calling convention
     LuaStyleFuncC _lua_f;
 
     using UserData = char[32];
@@ -52,7 +49,14 @@ struct NativeFunc {
         return reinterpret_cast<const T&>(_userdata);
     }
     
-    NativeFunc(NativeFuncC f, int argc, bool method) : f(f), argc(argc), method(method), _has_userdata(false) {}
+    NativeFunc(NativeFuncC f, int argc, bool method){
+        this->f = f;
+        this->argc = argc;
+        if(argc != -1) this->argc += (int)method;
+        _lua_f = nullptr;
+        _has_userdata = false;
+    }
+
     PyObject* operator()(VM* vm, ArgsView args) const;
 };
 

+ 3 - 7
src/vm.h

@@ -665,9 +665,8 @@ public:
 };
 
 inline PyObject* NativeFunc::operator()(VM* vm, ArgsView args) const{
-    int args_size = args.size() - (int)method;  // remove self
-    if(args_size != argc && argc != -1) {
-        vm->TypeError(fmt("expected ", argc, " arguments, but got ", args_size));
+    if(args.size() != argc && argc != -1) {
+        vm->TypeError(fmt("expected ", argc, " arguments, but got ", args.size()));
     }
 #if DEBUG_EXTRA_CHECK
     if(f == nullptr) FATAL_ERROR();
@@ -1235,10 +1234,7 @@ inline PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
 
         if(args.size() < fn.argc){
             vm->TypeError(fmt(
-                "expected ",
-                fn.argc - (int)method_call,
-                " positional arguments, but got ",
-                args.size() - (int)method_call,
+                "expected ", fn.argc, " positional arguments, but got ", args.size(),
                 " (", fn.decl->code->name, ')'
             ));
         }