blueloveTH 3 лет назад
Родитель
Сommit
10b8afdeb2
7 измененных файлов с 144 добавлено и 131 удалено
  1. 8 4
      src/codeobject.h
  2. 1 1
      src/compiler.h
  3. 2 1
      src/obj.h
  4. 2 1
      src/opcodes.h
  5. 65 65
      src/pocketpy.h
  6. 1 0
      src/str.h
  7. 65 59
      src/vm.h

+ 8 - 4
src/codeobject.h

@@ -104,13 +104,17 @@ private:
     std::vector<PyVar> s_data;
     int ip = 0;
 public:
-    PyVarDict* f_globals;
-    PyVarDict f_locals;
+    PyVar _module;
+    PyVarDict& f_locals;
+
+    inline PyVarDict& f_globals(){
+        return _module->attribs;
+    }
 
     const CodeObject* code;
 
-    Frame(const CodeObject* code, PyVarDict locals, PyVarDict* globals)
-        : code(code), f_locals(locals), f_globals(globals) {}
+    Frame(const CodeObject* code, PyVar _module, PyVarDict& locals)
+        : code(code), _module(_module), f_locals(locals) {}
 
     inline const ByteCode& readCode() {
         return code->co_code[ip++];

+ 1 - 1
src/compiler.h

@@ -328,7 +328,7 @@ public:
         EXPR_TUPLE();
         emitCode(OP_RETURN_VALUE);
         this->codes.pop();
-        emitCode(OP_LOAD_CONST, getCode()->addConst(vm->PyFunction(func)));
+        emitCode(OP_LOAD_LAMBDA, getCode()->addConst(vm->PyFunction(func)));
     }
 
     void exprAssign() {

+ 2 - 1
src/obj.h

@@ -15,6 +15,7 @@ class PyObject;
 class CodeObject;
 class BasePointer;
 class VM;
+class Frame;
 
 typedef std::shared_ptr<PyObject> PyVar;
 typedef PyVar PyVarOrNull;
@@ -46,7 +47,7 @@ public:
 typedef std::unordered_map<_Str, PyVar> PyVarDict;
 typedef std::shared_ptr<const BasePointer> _Pointer;
 
-typedef PyVar (*_CppFunc)(VM*, PyVarList);
+typedef PyVar (*_CppFunc)(VM*, Frame*, PyVarList);
 typedef std::shared_ptr<CodeObject> _Code;
 
 struct _Func {

+ 2 - 1
src/opcodes.h

@@ -36,7 +36,8 @@ OPCODE(JUMP_IF_FALSE_OR_POP)
 OPCODE(LOAD_NONE)
 OPCODE(LOAD_TRUE)
 OPCODE(LOAD_FALSE)
-OPCODE(LOAD_EVAL_FN)       // load eval() callable into stack
+OPCODE(LOAD_EVAL_FN)        // load eval() callable into stack
+OPCODE(LOAD_LAMBDA)         // LOAD_CONST + set __module__ attr
 
 OPCODE(ASSERT)
 OPCODE(RAISE_ERROR)

+ 65 - 65
src/pocketpy.h

@@ -6,7 +6,7 @@
 #define PK_VERSION "0.2.0"
 
 #define BIND_NUM_ARITH_OPT(name, op)                                                                    \
-    _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, PyVarList args){               \
+    _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, Frame* frame, PyVarList args){               \
         if(!vm->isIntOrFloat(args[0], args[1]))                                                         \
             vm->typeError("unsupported operand type(s) for " #op );                        \
         if(args[0]->isType(vm->_tp_int) && args[1]->isType(vm->_tp_int)){                               \
@@ -17,7 +17,7 @@
     });
 
 #define BIND_NUM_LOGICAL_OPT(name, op, fallback)                                                        \
-    _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, PyVarList args){               \
+    _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, Frame* frame, PyVarList args){               \
         if(!vm->isIntOrFloat(args[0], args[1])){                                                        \
             if constexpr(fallback) return vm->PyBool(args[0] op args[1]);                               \
             vm->typeError("unsupported operand type(s) for " #op );                        \
@@ -40,7 +40,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
 #undef BIND_NUM_ARITH_OPT
 #undef BIND_NUM_LOGICAL_OPT
 
-    _vm->bindBuiltinFunc("print", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("print", [](VM* vm, Frame* frame, PyVarList args) {
         for (auto& arg : args){
             _Str s = vm->PyStr_AS_C(vm->asStr(arg)) + " ";
             vm->_stdout(s.c_str());
@@ -49,62 +49,62 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->None;
     });
 
-    _vm->bindBuiltinFunc("eval", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("eval", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         const _Str& expr = vm->PyStr_AS_C(args[0]);
         _Code code = compile(vm, expr.c_str(), "<eval>", EVAL_MODE);
         if(code == nullptr) return vm->None;
-        return vm->_exec(code);      // not working in function
+        return vm->_exec(code, frame->_module, frame->f_locals);
     });
 
-    _vm->bindBuiltinFunc("isinstance", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("isinstance", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 2);
         return vm->PyBool(vm->isInstance(args[0], args[1]));
     });
 
-    _vm->bindBuiltinFunc("repr", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("repr", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         return vm->asRepr(args[0]);
     });
 
-    _vm->bindBuiltinFunc("hash", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("hash", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         return vm->PyInt(vm->hash(args[0]));
     });
 
-    _vm->bindBuiltinFunc("chr", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("chr", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         _Int i = vm->PyInt_AS_C(args[0]);
         if (i < 0 || i > 128) vm->valueError("chr() arg not in range(128)");
         return vm->PyStr(_Str(1, (char)i));
     });
 
-    _vm->bindBuiltinFunc("ord", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("ord", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         _Str s = vm->PyStr_AS_C(args[0]);
         if (s.size() != 1) vm->typeError("ord() expected an ASCII character");
         return vm->PyInt((_Int)s[0]);
     });
 
-    _vm->bindBuiltinFunc("dir", [](VM* vm, PyVarList args) {
+    _vm->bindBuiltinFunc("dir", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         PyVarList ret;
         for (auto& [k, _] : args[0]->attribs) ret.push_back(vm->PyStr(k));
         return vm->PyList(ret);
     });
 
-    _vm->bindMethod("object", "__repr__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("object", "__repr__", [](VM* vm, Frame* frame, PyVarList args) {
         PyVar _self = args[0];
         _Str s = "<" + _self->getTypeName() + " object at " + std::to_string((uintptr_t)_self.get()) + ">";
         return vm->PyStr(s);
     });
 
-    _vm->bindMethod("type", "__new__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("type", "__new__", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         return args[0]->attribs[__class__];
     });
 
-    _vm->bindMethod("range", "__new__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("range", "__new__", [](VM* vm, Frame* frame, PyVarList args) {
         _Range r;
         switch (args.size()) {
             case 1: r.stop = vm->PyInt_AS_C(args[0]); break;
@@ -115,17 +115,17 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyRange(r);
     });
 
-    _vm->bindMethod("range", "__iter__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("range", "__iter__", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkType(args[0], vm->_tp_range);
         auto iter = std::make_shared<RangeIterator>(vm, args[0]);
         return vm->PyIter(iter);
     });
 
-    _vm->bindMethod("NoneType", "__repr__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("NoneType", "__repr__", [](VM* vm, Frame* frame, PyVarList args) {
         return vm->PyStr("None");
     });
 
-    _vm->bindMethodMulti({"int", "float"}, "__truediv__", [](VM* vm, PyVarList args) {
+    _vm->bindMethodMulti({"int", "float"}, "__truediv__", [](VM* vm, Frame* frame, PyVarList args) {
         if(!vm->isIntOrFloat(args[0], args[1]))
             vm->typeError("unsupported operand type(s) for " "/" );
         _Float rhs = vm->numToFloat(args[1]);
@@ -133,7 +133,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyFloat(vm->numToFloat(args[0]) / rhs);
     });
 
-    _vm->bindMethodMulti({"int", "float"}, "__pow__", [](VM* vm, PyVarList args) {
+    _vm->bindMethodMulti({"int", "float"}, "__pow__", [](VM* vm, Frame* frame, PyVarList args) {
         if(!vm->isIntOrFloat(args[0], args[1]))
             vm->typeError("unsupported operand type(s) for " "**" );
         if(args[0]->isType(vm->_tp_int) && args[1]->isType(vm->_tp_int)){
@@ -144,7 +144,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 
     /************ PyInt ************/
-    _vm->bindMethod("int", "__new__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("int", "__new__", [](VM* vm, Frame* frame, PyVarList args) {
         if(args.size() == 0) return vm->PyInt(0);
         vm->__checkArgSize(args, 1);
         if (args[0]->isType(vm->_tp_int)) return args[0];
@@ -163,7 +163,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->None;
     });
 
-    _vm->bindMethod("int", "__floordiv__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("int", "__floordiv__", [](VM* vm, Frame* frame, PyVarList args) {
         if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int))
             vm->typeError("unsupported operand type(s) for " "//" );
         _Int rhs = vm->PyInt_AS_C(args[1]);
@@ -171,24 +171,24 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyInt(vm->PyInt_AS_C(args[0]) / rhs);
     });
 
-    _vm->bindMethod("int", "__mod__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("int", "__mod__", [](VM* vm, Frame* frame, PyVarList args) {
         if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int))
             vm->typeError("unsupported operand type(s) for " "%" );
         return vm->PyInt(vm->PyInt_AS_C(args[0]) % vm->PyInt_AS_C(args[1]));
     });
 
-    _vm->bindMethod("int", "__neg__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("int", "__neg__", [](VM* vm, Frame* frame, PyVarList args) {
         if(!args[0]->isType(vm->_tp_int))
             vm->typeError("unsupported operand type(s) for " "-" );
         return vm->PyInt(-1 * vm->PyInt_AS_C(args[0]));
     });
 
-    _vm->bindMethod("int", "__repr__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("int", "__repr__", [](VM* vm, Frame* frame, PyVarList args) {
         return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0])));
     });
 
     /************ PyFloat ************/
-    _vm->bindMethod("float", "__new__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("float", "__new__", [](VM* vm, Frame* frame, PyVarList args) {
         if(args.size() == 0) return vm->PyFloat(0.0);
         vm->__checkArgSize(args, 1);
         if (args[0]->isType(vm->_tp_int)) return vm->PyFloat((_Float)vm->PyInt_AS_C(args[0]));
@@ -209,11 +209,11 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->None;
     });
 
-    _vm->bindMethod("float", "__neg__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("float", "__neg__", [](VM* vm, Frame* frame, PyVarList args) {
         return vm->PyFloat(-1.0 * vm->PyFloat_AS_C(args[0]));
     });
 
-    _vm->bindMethod("float", "__repr__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("float", "__repr__", [](VM* vm, Frame* frame, PyVarList args) {
         _Float val = vm->PyFloat_AS_C(args[0]);
         if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val));
         _StrStream ss;
@@ -224,12 +224,12 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 
     /************ PyString ************/
-    _vm->bindMethod("str", "__new__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__new__", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         return vm->asStr(args[0]);
     });
 
-    _vm->bindMethod("str", "__add__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__add__", [](VM* vm, Frame* frame, PyVarList args) {
         if(!args[0]->isType(vm->_tp_str) || !args[1]->isType(vm->_tp_str))
             vm->typeError("unsupported operand type(s) for " "+" );
         const _Str& lhs = vm->PyStr_AS_C(args[0]);
@@ -237,39 +237,39 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyStr(lhs + rhs);
     });
 
-    _vm->bindMethod("str", "__len__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__len__", [](VM* vm, Frame* frame, PyVarList args) {
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         return vm->PyInt(_self.u8_length());
     });
 
-    _vm->bindMethod("str", "__contains__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__contains__", [](VM* vm, Frame* frame, PyVarList args) {
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         const _Str& _other = vm->PyStr_AS_C(args[1]);
         return vm->PyBool(_self.str().find(_other.str()) != _Str::npos);
     });
 
-    _vm->bindMethod("str", "__str__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__str__", [](VM* vm, Frame* frame, PyVarList args) {
         return args[0]; // str is immutable
     });
 
-    _vm->bindMethod("str", "__iter__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__iter__", [](VM* vm, Frame* frame, PyVarList args) {
         auto it = std::make_shared<StringIterator>(vm, args[0]);
         return vm->PyIter(it);
     });
 
-    _vm->bindMethod("str", "__repr__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__repr__", [](VM* vm, Frame* frame, PyVarList args) {
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         // we just do a simple repr here, no escaping
         return vm->PyStr("'" + _self.str() + "'");
     });
 
-    _vm->bindMethod("str", "__eq__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__eq__", [](VM* vm, Frame* frame, PyVarList args) {
         if(args[0]->isType(vm->_tp_str) && args[1]->isType(vm->_tp_str))
             return vm->PyBool(vm->PyStr_AS_C(args[0]) == vm->PyStr_AS_C(args[1]));
         return vm->PyBool(args[0] == args[1]);      // fallback
     });
 
-    _vm->bindMethod("str", "__getitem__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__getitem__", [](VM* vm, Frame* frame, PyVarList args) {
         const _Str& _self (vm->PyStr_AS_C(args[0]));
 
         if(args[1]->isType(vm->_tp_slice)){
@@ -283,19 +283,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyStr(_self.u8_getitem(_index));
     });
 
-    _vm->bindMethod("str", "__gt__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__gt__", [](VM* vm, Frame* frame, PyVarList args) {
         const _Str& _self (vm->PyStr_AS_C(args[0]));
         const _Str& _obj (vm->PyStr_AS_C(args[1]));
         return vm->PyBool(_self > _obj);
     });
 
-    _vm->bindMethod("str", "__lt__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "__lt__", [](VM* vm, Frame* frame, PyVarList args) {
         const _Str& _self (vm->PyStr_AS_C(args[0]));
         const _Str& _obj (vm->PyStr_AS_C(args[1]));
         return vm->PyBool(_self < _obj);
     });
 
-    _vm->bindMethod("str", "upper", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "upper", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1, true);
         const _Str& _self (vm->PyStr_AS_C(args[0]));
         _StrStream ss;
@@ -303,7 +303,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyStr(ss);
     });
 
-    _vm->bindMethod("str", "lower", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "lower", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1, true);
         const _Str& _self (vm->PyStr_AS_C(args[0]));
         _StrStream ss;
@@ -311,7 +311,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyStr(ss);
     });
 
-    _vm->bindMethod("str", "replace", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "replace", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 3, true);
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         const _Str& _old = vm->PyStr_AS_C(args[1]);
@@ -326,21 +326,21 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyStr(_copy);
     });
 
-    _vm->bindMethod("str", "startswith", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "startswith", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 2, true);
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         const _Str& _prefix = vm->PyStr_AS_C(args[1]);
         return vm->PyBool(_self.str().find(_prefix.str()) == 0);
     });
 
-    _vm->bindMethod("str", "endswith", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "endswith", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 2, true);
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         const _Str& _suffix = vm->PyStr_AS_C(args[1]);
         return vm->PyBool(_self.str().rfind(_suffix.str()) == _self.str().length() - _suffix.str().length());
     });
 
-    _vm->bindMethod("str", "join", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("str", "join", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 2, true);
         const _Str& _self = vm->PyStr_AS_C(args[0]);
         const PyVarList& _list = vm->PyList_AS_C(args[1]);
@@ -353,20 +353,20 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 
     /************ PyList ************/
-    _vm->bindMethod("list", "__iter__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "__iter__", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkType(args[0], vm->_tp_list);
         auto iter = std::make_shared<VectorIterator>(vm, args[0]);
         return vm->PyIter(iter);
     });
 
-    _vm->bindMethod("list", "append", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "append", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 2, true);
         PyVarList& _self = vm->PyList_AS_C(args[0]);
         _self.push_back(args[1]);
         return vm->None;
     });
 
-    _vm->bindMethod("list", "insert", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "insert", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 3, true);
         PyVarList& _self = vm->PyList_AS_C(args[0]);
         int _index = vm->PyInt_AS_C(args[1]);
@@ -375,18 +375,18 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->None;
     });
 
-    _vm->bindMethod("list", "clear", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "clear", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1, true);
         vm->PyList_AS_C(args[0]).clear();
         return vm->None;
     });
 
-    _vm->bindMethod("list", "copy", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "copy", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1, true);
         return vm->PyList(vm->PyList_AS_C(args[0]));
     });
 
-    _vm->bindMethod("list", "pop", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "pop", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1, true);
         PyVarList& _self = vm->PyList_AS_C(args[0]);
         if(_self.empty()) vm->indexError("pop from empty list");
@@ -395,7 +395,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return ret;
     });      
 
-    _vm->bindMethod("list", "__add__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "__add__", [](VM* vm, Frame* frame, PyVarList args) {
         const PyVarList& _self = vm->PyList_AS_C(args[0]);
         const PyVarList& _obj = vm->PyList_AS_C(args[1]);
         PyVarList _new_list = _self;
@@ -403,12 +403,12 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyList(_new_list);
     });
 
-    _vm->bindMethod("list", "__len__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "__len__", [](VM* vm, Frame* frame, PyVarList args) {
         const PyVarList& _self = vm->PyList_AS_C(args[0]);
         return vm->PyInt(_self.size());
     });
 
-    _vm->bindMethod("list", "__getitem__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "__getitem__", [](VM* vm, Frame* frame, PyVarList args) {
         const PyVarList& _self = vm->PyList_AS_C(args[0]);
 
         if(args[1]->isType(vm->_tp_slice)){
@@ -425,7 +425,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return _self[_index];
     });
 
-    _vm->bindMethod("list", "__setitem__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "__setitem__", [](VM* vm, Frame* frame, PyVarList args) {
         PyVarList& _self = vm->PyList_AS_C(args[0]);
         int _index = vm->PyInt_AS_C(args[1]);
         _index = vm->normalizedIndex(_index, _self.size());
@@ -433,7 +433,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->None;
     });
 
-    _vm->bindMethod("list", "__delitem__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("list", "__delitem__", [](VM* vm, Frame* frame, PyVarList args) {
         PyVarList& _self = vm->PyList_AS_C(args[0]);
         int _index = vm->PyInt_AS_C(args[1]);
         _index = vm->normalizedIndex(_index, _self.size());
@@ -442,24 +442,24 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 
     /************ PyTuple ************/
-    _vm->bindMethod("tuple", "__new__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("tuple", "__new__", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
-        PyVarList _list = vm->PyList_AS_C(vm->call(vm->builtins->attribs["list"], args));
+        PyVarList _list = vm->PyList_AS_C(vm->call(frame, vm->builtins->attribs["list"], args));
         return vm->PyTuple(_list);
     });
 
-    _vm->bindMethod("tuple", "__iter__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("tuple", "__iter__", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkType(args[0], vm->_tp_tuple);
         auto iter = std::make_shared<VectorIterator>(vm, args[0]);
         return vm->PyIter(iter);
     });
 
-    _vm->bindMethod("tuple", "__len__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("tuple", "__len__", [](VM* vm, Frame* frame, PyVarList args) {
         const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
         return vm->PyInt(_self.size());
     });
 
-    _vm->bindMethod("tuple", "__getitem__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("tuple", "__getitem__", [](VM* vm, Frame* frame, PyVarList args) {
         const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
         int _index = vm->PyInt_AS_C(args[1]);
         _index = vm->normalizedIndex(_index, _self.size());
@@ -467,19 +467,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 
     /************ PyBool ************/
-    _vm->bindMethod("bool", "__repr__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("bool", "__repr__", [](VM* vm, Frame* frame, PyVarList args) {
         bool val = vm->PyBool_AS_C(args[0]);
         return vm->PyStr(val ? "True" : "False");
     });
 
-    _vm->bindMethod("bool", "__eq__", [](VM* vm, PyVarList args) {
+    _vm->bindMethod("bool", "__eq__", [](VM* vm, Frame* frame, PyVarList args) {
         return vm->PyBool(args[0] == args[1]);
     });
 }
 
 void __runCodeBuiltins(VM* vm, const char* src){
     _Code code = compile(vm, src, "builtins.py");
-    if(code != nullptr) vm->_exec(code, {}, vm->builtins);
+    if(code != nullptr) vm->exec(code, vm->builtins);
 }
 
 #include "builtins.h"
@@ -495,7 +495,7 @@ void __runCodeBuiltins(VM* vm, const char* src){
 
 void __addModuleTime(VM* vm){
     PyVar mod = vm->newModule("time");
-    vm->bindFunc(mod, "time", [](VM* vm, PyVarList args) {
+    vm->bindFunc(mod, "time", [](VM* vm, Frame* frame, PyVarList args) {
         auto now = std::chrono::high_resolution_clock::now();
         return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0);
     });
@@ -503,7 +503,7 @@ void __addModuleTime(VM* vm){
 
 void __addModuleSys(VM* vm){
     PyVar mod = vm->newModule("sys");
-    vm->bindFunc(mod, "getrefcount", [](VM* vm, PyVarList args) {
+    vm->bindFunc(mod, "getrefcount", [](VM* vm, Frame* frame, PyVarList args) {
         vm->__checkArgSize(args, 1);
         return vm->PyInt(args[0].use_count());
     });
@@ -539,7 +539,7 @@ extern "C" {
         _Code code = compile(vm, source, name + _Str(".py"));
         if(code != nullptr){
             PyVar _m = vm->newModule(name);
-            vm->exec(code, {}, _m);
+            vm->exec(code, _m);
         }
     }
 }

+ 1 - 0
src/str.h

@@ -153,6 +153,7 @@ const _Str& __iter__ = _Str("__iter__");
 const _Str& __str__ = _Str("__str__");
 const _Str& __repr__ = _Str("__repr__");
 const _Str& __neg__ = _Str("__neg__");
+const _Str& __module__ = _Str("__module__");
 const _Str& __getitem__ = _Str("__getitem__");
 const _Str& __setitem__ = _Str("__setitem__");
 const _Str& __delitem__ = _Str("__delitem__");

+ 65 - 59
src/vm.h

@@ -25,12 +25,11 @@ typedef void(*PrintFn)(const char*);
 
 class VM{
 private:
-    std::stack< std::shared_ptr<Frame> > callstack;
+    std::stack< std::unique_ptr<Frame> > callstack;
     std::vector<PyObject*> numPool;
     PyVarDict _modules;       // 3rd modules
 
-    PyVar runFrame(std::shared_ptr<Frame> frame){
-        callstack.push(frame);
+    PyVar runFrame(Frame* frame){
         while(!frame->isEnd()){
             const ByteCode& byte = frame->readCode();
             //printf("%s (%d) stack_size: %d\n", OP_NAMES[byte.op], byte.arg, frame->stackSize());
@@ -39,12 +38,17 @@ private:
             {
             case OP_NO_OP: break;       // do nothing
             case OP_LOAD_CONST: frame->push(frame->code->co_consts[byte.arg]); break;
+            case OP_LOAD_LAMBDA: {
+                PyVar obj = frame->code->co_consts[byte.arg];
+                setAttr(obj, __module__, frame->_module);
+                frame->push(obj);
+            } break;
             case OP_LOAD_NAME_PTR: {
                 frame->push(PyPointer(frame->code->co_names[byte.arg]));
             } break;
             case OP_STORE_NAME_PTR: {
                 const auto& p = frame->code->co_names[byte.arg];
-                p->set(this, frame.get(), frame->popValue(this));
+                p->set(this, frame, frame->popValue(this));
             } break;
             case OP_BUILD_ATTR_PTR: {
                 const auto& attr = frame->code->co_names[byte.arg];
@@ -59,11 +63,11 @@ private:
             case OP_STORE_PTR: {
                 PyVar obj = frame->popValue(this);
                 _Pointer p = PyPointer_AS_C(frame->__pop());
-                p->set(this, frame.get(), obj);
+                p->set(this, frame, obj);
             } break;
             case OP_DELETE_PTR: {
                 _Pointer p = PyPointer_AS_C(frame->__pop());
-                p->del(this, frame.get());
+                p->del(this, frame);
             } break;
             case OP_BUILD_SMART_TUPLE:
             {
@@ -99,13 +103,14 @@ private:
             case OP_LIST_APPEND: {
                 PyVar obj = frame->popValue(this);
                 PyVar list = frame->topNValue(this, -2);
-                fastCall(list, "append", {list, obj});
+                fastCall(frame, list, "append", {list, obj});
             } break;
             case OP_STORE_FUNCTION:
                 {
                     PyVar obj = frame->popValue(this);
                     const _Func& fn = PyFunction_AS_C(obj);
-                    frame->f_globals->operator[](fn.name) = obj;
+                    setAttr(obj, __module__, frame->_module);
+                    frame->f_globals()[fn.name] = obj;
                 } break;
             case OP_BUILD_CLASS:
                 {
@@ -120,14 +125,9 @@ private:
                         const _Func& f = PyFunction_AS_C(fn);
                         setAttr(cls, f.name, fn);
                     }
-                    frame->f_globals->operator[](clsName) = cls;
-                } break;
-            case OP_RETURN_VALUE:
-                {
-                    PyVar ret = frame->popValue(this);
-                    callstack.pop();
-                    return ret;
+                    frame->f_globals()[clsName] = cls;
                 } break;
+            case OP_RETURN_VALUE: return frame->popValue(this);
             case OP_PRINT_EXPR:
                 {
                     const PyVar& expr = frame->topValue(this);
@@ -140,7 +140,7 @@ private:
                 {
                     PyVar rhs = frame->popValue(this);
                     PyVar lhs = frame->popValue(this);
-                    frame->push(fastCall(lhs, BIN_SPECIAL_METHODS[byte.arg], {lhs,rhs}));
+                    frame->push(fastCall(frame, lhs, BIN_SPECIAL_METHODS[byte.arg], {lhs,rhs}));
                 } break;
             case OP_COMPARE_OP:
                 {
@@ -148,7 +148,7 @@ private:
                     PyVar lhs = frame->popValue(this);
                     // for __ne__ we use the negation of __eq__
                     int op = byte.arg == 3 ? 2 : byte.arg;
-                    PyVar res = fastCall(lhs, CMP_SPECIAL_METHODS[op], {lhs,rhs});
+                    PyVar res = fastCall(frame, lhs, CMP_SPECIAL_METHODS[op], {lhs,rhs});
                     if(op != byte.arg) res = PyBool(!PyBool_AS_C(res));
                     frame->push(res);
                 } break;
@@ -162,14 +162,14 @@ private:
                 {
                     PyVar rhs = frame->popValue(this);
                     PyVar lhs = frame->popValue(this);
-                    bool ret_c = PyBool_AS_C(call(rhs, __contains__, {lhs}));
+                    bool ret_c = PyBool_AS_C(call(frame, rhs, __contains__, {lhs}));
                     if(byte.arg == 1) ret_c = !ret_c;
                     frame->push(PyBool(ret_c));
                 } break;
             case OP_UNARY_NEGATIVE:
                 {
                     PyVar obj = frame->popValue(this);
-                    frame->push(call(obj, __neg__, {}));
+                    frame->push(call(frame, obj, __neg__, {}));
                 } break;
             case OP_UNARY_NOT:
                 {
@@ -202,9 +202,9 @@ private:
             case OP_BUILD_MAP:
                 {
                     PyVarList items = frame->popNValuesReversed(this, byte.arg*2);
-                    PyVar obj = call(builtins->attribs["dict"], {});
+                    PyVar obj = call(frame, builtins->attribs["dict"], {});
                     for(int i=0; i<items.size(); i+=2){
-                        call(obj, __setitem__, {items[i], items[i+1]});
+                        call(frame, obj, __setitem__, {items[i], items[i+1]});
                     }
                     frame->push(obj);
                 } break;
@@ -213,7 +213,7 @@ private:
                 {
                     PyVarList args = frame->popNValuesReversed(this, byte.arg);
                     PyVar callable = frame->popValue(this);
-                    frame->push(call(callable, args));
+                    frame->push(call(frame, callable, args));
                 } break;
             case OP_JUMP_ABSOLUTE: frame->jumpTo(byte.arg); break;
             case OP_GET_ITER:
@@ -221,7 +221,7 @@ private:
                     PyVar obj = frame->popValue(this);
                     PyVarOrNull iter_fn = getAttr(obj, __iter__, false);
                     if(iter_fn != nullptr){
-                        PyVar tmp = call(iter_fn, {obj});
+                        PyVar tmp = call(frame, iter_fn, {obj});
                         PyIter_AS_C(tmp)->var = PyPointer_AS_C(frame->__pop());
                         frame->push(tmp);
                     }else{
@@ -233,7 +233,7 @@ private:
                     const PyVar& iter = frame->topValue(this);
                     auto& it = PyIter_AS_C(iter);
                     if(it->hasNext()){
-                        it->var->set(this, frame.get(), it->next());
+                        it->var->set(this, frame, it->next());
                     }
                     else{
                         frame->popValue(this);
@@ -276,13 +276,10 @@ private:
 
         if(frame->code->src->mode == EVAL_MODE) {
             if(frame->stackSize() != 1) systemError("stack size is not 1 in EVAL_MODE");
-            PyVar ret = frame->popValue(this);
-            callstack.pop();
-            return ret;
+            return frame->popValue(this);
         }
 
         if(frame->stackSize() != 0) systemError("stack not empty in EXEC_MODE");
-        callstack.pop();
         return None;
     }
 
@@ -302,13 +299,13 @@ public:
 
     PyVar asStr(const PyVar& obj){
         PyVarOrNull str_fn = getAttr(obj, __str__, false);
-        if(str_fn != nullptr) return call(str_fn, {});
+        if(str_fn != nullptr) return call(nullptr, str_fn, {});
         return asRepr(obj);
     }
 
     PyVar asRepr(const PyVar& obj){
         if(obj->isType(_tp_type)) return PyStr("<class '" + obj->getName() + "'>");
-        return call(obj, __repr__, {});
+        return call(nullptr, obj, __repr__, {});
     }
 
     PyVar asBool(const PyVar& obj){
@@ -319,18 +316,18 @@ public:
         if(tp == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
         PyVarOrNull len_fn = getAttr(obj, "__len__", false);
         if(len_fn != nullptr){
-            PyVar ret = call(len_fn, {});
+            PyVar ret = call(nullptr, len_fn, {});
             return PyBool(PyInt_AS_C(ret) > 0);
         }
         return True;
     }
 
-    PyVar fastCall(const PyVar& obj, const _Str& name, PyVarList args){
+    PyVar fastCall(Frame* frame, const PyVar& obj, const _Str& name, PyVarList args){
         PyVar cls = obj->attribs[__class__];
         while(cls != None) {
             auto it = cls->attribs.find(name);
             if(it != cls->attribs.end()){
-                return call(it->second, args);
+                return call(frame, it->second, args);
             }
             cls = cls->attribs[__base__];
         }
@@ -338,18 +335,18 @@ public:
         return nullptr;
     }
 
-    PyVar call(PyVar callable, PyVarList args){
+    PyVar call(Frame* frame, PyVar callable, PyVarList args){
         if(callable->isType(_tp_type)){
             auto it = callable->attribs.find(__new__);
             PyVar obj;
             if(it != callable->attribs.end()){
-                obj = call(it->second, args);
+                obj = call(frame, it->second, args);
             }else{
                 obj = newObject(callable, (_Int)-1);
             }
             if(obj->isType(callable)){
                 PyVarOrNull init_fn = getAttr(obj, __init__, false);
-                if (init_fn != nullptr) call(init_fn, args);
+                if (init_fn != nullptr) call(frame, init_fn, args);
             }
             return obj;
         }
@@ -362,7 +359,7 @@ public:
         
         if(callable->isType(_tp_native_function)){
             auto f = std::get<_CppFunc>(callable->_native);
-            return f(this, args);
+            return f(this, frame, args);
         } else if(callable->isType(_tp_function)){
             _Func fn = PyFunction_AS_C(callable);
             PyVarDict locals;
@@ -390,19 +387,27 @@ public:
             }
 
             if(i < args.size()) typeError("too many arguments");
-            return _exec(fn.code, locals);
+
+            auto it_m = callable->attribs.find(__module__);
+            if(it_m != callable->attribs.end()){
+                return _exec(fn.code, it_m->second, locals);
+            }else{
+                return _exec(fn.code, frame->_module, locals);
+            }
         }
         typeError("'" + callable->getTypeName() + "' object is not callable");
         return None;
     }
 
-    inline PyVar call(const PyVar& obj, const _Str& func, PyVarList args){
-        return call(getAttr(obj, func), args);
+    inline PyVar call(Frame* frame, const PyVar& obj, const _Str& func, PyVarList args){
+        return call(frame, getAttr(obj, func), args);
     }
 
-    PyVar exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){
+    PyVar exec(const _Code& code, PyVar _module=nullptr){
+        if(_module == nullptr) _module = _main;
         try {
-            return _exec(code, locals, _module);
+            PyVarDict locals;
+            return _exec(code, _module, locals);
         } catch (const std::exception& e) {
             if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
                 _stderr(e.what());
@@ -414,16 +419,18 @@ public:
             return None;
         }
     }
-    
-    PyVar _exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){
+
+    PyVar _exec(const _Code& code, PyVar _module, PyVarDict& locals){
         if(code == nullptr) UNREACHABLE();
-        if(_module == nullptr) _module = _main;
-        auto frame = std::make_shared<Frame>(
+        Frame* frame = new Frame(
             code.get(),
-            locals,
-            &_module->attribs
+            _module,
+            locals      // pass by reference
         );
-        return runFrame(frame);
+        callstack.push(std::unique_ptr<Frame>(frame));
+        PyVar ret = runFrame(frame);
+        callstack.pop();
+        return ret;
     }
 
     PyVar newUserClassType(_Str name, PyVar base){
@@ -647,8 +654,7 @@ private:
     std::stack<_Str> _cleanErrorAndGetSnapshots(){
         std::stack<_Str> snapshots;
         while (!callstack.empty()){
-            auto frame = callstack.top();
-            snapshots.push(frame->errorSnapshot());
+            snapshots.push(callstack.top()->errorSnapshot());
             callstack.pop();
         }
         return snapshots;
@@ -703,8 +709,8 @@ public:
 PyVar NamePointer::get(VM* vm, Frame* frame) const{
     auto it = frame->f_locals.find(name);
     if(it != frame->f_locals.end()) return it->second;
-    it = frame->f_globals->find(name);
-    if(it != frame->f_globals->end()) return it->second;
+    it = frame->f_globals().find(name);
+    if(it != frame->f_globals().end()) return it->second;
     it = vm->builtins->attribs.find(name);
     if(it != vm->builtins->attribs.end()) return it->second;
     vm->nameError(name);
@@ -719,7 +725,7 @@ void NamePointer::set(VM* vm, Frame* frame, PyVar val) const{
             if(frame->f_locals.count(name) > 0){
                 frame->f_locals[name] = val;
             }else{
-                frame->f_globals->operator[](name) = val;
+                frame->f_globals()[name] = val;
             }
         } break;
         default: UNREACHABLE();
@@ -740,8 +746,8 @@ void NamePointer::del(VM* vm, Frame* frame) const{
             if(frame->f_locals.count(name) > 0){
                 frame->f_locals.erase(name);
             }else{
-                if(frame->f_globals->count(name) > 0){
-                    frame->f_globals->erase(name);
+                if(frame->f_globals().count(name) > 0){
+                    frame->f_globals().erase(name);
                 }else{
                     vm->nameError(name);
                 }
@@ -764,15 +770,15 @@ void AttrPointer::del(VM* vm, Frame* frame) const{
 }
 
 PyVar IndexPointer::get(VM* vm, Frame* frame) const{
-    return vm->call(obj, __getitem__, {index});
+    return vm->call(frame, obj, __getitem__, {index});
 }
 
 void IndexPointer::set(VM* vm, Frame* frame, PyVar val) const{
-    vm->call(obj, __setitem__, {index, val});
+    vm->call(frame, obj, __setitem__, {index, val});
 }
 
 void IndexPointer::del(VM* vm, Frame* frame) const{
-    vm->call(obj, __delitem__, {index});
+    vm->call(frame, obj, __delitem__, {index});
 }
 
 PyVar CompoundPointer::get(VM* vm, Frame* frame) const{