This commit is contained in:
blueloveTH 2023-04-18 20:31:53 +08:00
parent b1eb38c009
commit 08a8a2f252
2 changed files with 6 additions and 8 deletions

View File

@ -13,16 +13,17 @@ class VM;
typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
typedef std::function<PyObject*(VM*, ArgsView)> NativeFuncCpp;
using NativeFuncRaw = std::variant<NativeFuncC, NativeFuncCpp>;
typedef shared_ptr<CodeObject> CodeObject_;
struct NativeFunc {
NativeFuncRaw f;
NativeFuncC f;
NativeFuncCpp f_cpp;
int argc; // DONOT include self
bool method;
NativeFunc(NativeFuncRaw f, int argc, bool method) : f(f), argc(argc), method(method) {}
NativeFunc(NativeFuncC f, int argc, bool method) : f(f), argc(argc), method(method) {}
NativeFunc(NativeFuncCpp f, int argc, bool method) : f(nullptr), f_cpp(f), argc(argc), method(method) {}
PyObject* operator()(VM* vm, ArgsView args) const;
};

View File

@ -395,11 +395,8 @@ inline PyObject* NativeFunc::operator()(VM* vm, ArgsView args) const{
if(argc != -1 && args_size != argc) {
vm->TypeError(fmt("expected ", argc, " arguments, but got ", args_size));
}
if(std::holds_alternative<NativeFuncC>(f)){
return std::get<NativeFuncC>(f)(vm, args);
}else{
return std::get<NativeFuncCpp>(f)(vm, args);
}
if(f != nullptr) return f(vm, args);
return f_cpp(vm, args);
}
inline void CodeObject::optimize(VM* vm){