This commit is contained in:
blueloveTH 2022-11-10 15:18:21 +08:00
parent 47c6639627
commit c9b58e2270
2 changed files with 17 additions and 13 deletions

View File

@ -56,7 +56,8 @@ void __initializeBuiltinFunctions(VM* _vm) {
if (!args[0]->isType(vm->_tp_str)) vm->typeError("eval() argument must be a string");
const _Str& expr = vm->PyStr_AS_C(args[0]);
_Code code = compile(vm, expr, "<eval>", EVAL_MODE);
return vm->exec(code); // not working in function
if(code == nullptr) return vm->None;
return vm->_exec(code); // not working in function
});
_vm->bindBuiltinFunc("repr", [](VM* vm, PyVarList args) {
@ -413,7 +414,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
void __runCodeBuiltins(VM* vm, const char* src){
_Code code = compile(vm, src, "builtins.py");
vm->exec(code, {}, vm->builtins);
if(code != nullptr) vm->_exec(code, {}, vm->builtins);
}
#include "builtins.h"

View File

@ -390,7 +390,7 @@ public:
}
if(i < args.size()) typeError("too many arguments");
return exec(fn.code, locals);
return _exec(fn.code, locals);
}
typeError("'" + callable->getTypeName() + "' object is not callable");
return None;
@ -401,16 +401,8 @@ public:
}
PyVar exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){
if(code == nullptr) UNREACHABLE();
if(_module == nullptr) _module = _main;
auto frame = std::make_shared<Frame>(
code.get(),
locals,
&_module->attribs
);
try {
return runFrame(frame);
return _exec(code, locals, _module);
} catch (const std::exception& e) {
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
_stderr(e.what());
@ -423,6 +415,17 @@ public:
}
}
PyVar _exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){
if(code == nullptr) UNREACHABLE();
if(_module == nullptr) _module = _main;
auto frame = std::make_shared<Frame>(
code.get(),
locals,
&_module->attribs
);
return runFrame(frame);
}
PyVar newUserClassType(_Str name, PyVar base){
PyVar obj = newClassType(name, base);
setAttr(obj, "__name__", PyStr(name));