This commit is contained in:
blueloveTH 2023-06-13 20:18:43 +08:00
parent c5f39948f0
commit 925679a040
2 changed files with 12 additions and 12 deletions

View File

@ -21,12 +21,9 @@ typedef int (*LuaStyleFuncC)(VM*);
struct NativeFunc { struct NativeFunc {
NativeFuncC f; NativeFuncC f;
int argc; // DONOT include self int argc;
bool method;
// this is designed for lua style C bindings // 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; LuaStyleFuncC _lua_f;
using UserData = char[32]; using UserData = char[32];
@ -52,7 +49,14 @@ struct NativeFunc {
return reinterpret_cast<const T&>(_userdata); 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; PyObject* operator()(VM* vm, ArgsView args) const;
}; };

View File

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