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 {
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;
};

View File

@ -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, ')'
));
}