blueloveTH 3 anni fa
parent
commit
c9b58e2270
2 ha cambiato i file con 17 aggiunte e 13 eliminazioni
  1. 3 2
      src/pocketpy.h
  2. 14 11
      src/vm.h

+ 3 - 2
src/pocketpy.h

@@ -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"

+ 14 - 11
src/vm.h

@@ -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;
@@ -399,18 +399,10 @@ public:
     inline PyVar call(const PyVar& obj, const _Str& func, PyVarList args){
         return call(getAttr(obj, func), args);
     }
-    
-    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
-        );
 
+    PyVar exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){
         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());
@@ -422,6 +414,17 @@ public:
             return None;
         }
     }
+    
+    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);