blueloveTH 1 vuosi sitten
vanhempi
commit
6a7da5a1d5

+ 1 - 3
include/pocketpy/interpreter/frame.h

@@ -42,7 +42,6 @@ typedef struct Frame {
     py_StackRef function;  // a function object or NULL (global scope)
     py_StackRef p0;        // unwinding base
     py_StackRef locals;    // locals base
-    const CodeObject* locals_co;
     UnwindTarget* uw_list;
 } Frame;
 
@@ -50,8 +49,7 @@ Frame* Frame__new(const CodeObject* co,
                   py_TValue* module,
                   py_StackRef function,
                   py_StackRef p0,
-                  py_StackRef locals,
-                  const CodeObject* locals_co);
+                  py_StackRef locals);
 void Frame__delete(Frame* self);
 
 int Frame__ip(const Frame* self);

+ 1 - 1
src/interpreter/ceval.c

@@ -136,7 +136,7 @@ FrameResult VM__run_top_frame(VM* self) {
                 Function* ud = py_newobject(SP(), tp_function, 0, sizeof(Function));
                 Function__ctor(ud, decl, &frame->module);
                 if(decl->nested) {
-                    ud->closure = FastLocals__to_namedict(frame->locals, frame->locals_co);
+                    ud->closure = FastLocals__to_namedict(frame->locals, frame->co);
                     py_Name name = py_name(decl->code.name->data);
                     // capture itself to allow recursion
                     NameDict__set(ud->closure, name, *SP());

+ 2 - 4
src/interpreter/frame.c

@@ -38,8 +38,7 @@ Frame* Frame__new(const CodeObject* co,
                   py_TValue* module,
                   py_StackRef function,
                   py_StackRef p0,
-                  py_StackRef locals,
-                  const CodeObject* locals_co) {
+                  py_StackRef locals) {
     static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
     Frame* self = PoolFrame_alloc();
     self->f_back = NULL;
@@ -49,7 +48,6 @@ Frame* Frame__new(const CodeObject* co,
     self->function = function;
     self->p0 = p0;
     self->locals = locals;
-    self->locals_co = locals_co;
     self->uw_list = NULL;
     return self;
 }
@@ -151,5 +149,5 @@ int Frame__iblock(const Frame* self) {
 }
 
 py_TValue* Frame__f_locals_try_get(Frame* self, py_Name name) {
-    return FastLocals__try_get_by_name(self->locals, self->locals_co, name);
+    return FastLocals__try_get_by_name(self->locals, self->co, name);
 }

+ 2 - 2
src/interpreter/vm.c

@@ -409,7 +409,7 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
                 memcpy(argv, self->__vectorcall_buffer, co->nlocals * sizeof(py_TValue));
                 // submit the call
                 if(!fn->cfunc) {
-                    VM__push_frame(self, Frame__new(co, &fn->module, p0, p0, argv, co));
+                    VM__push_frame(self, Frame__new(co, &fn->module, p0, p0, argv));
                     return opcall ? RES_CALL : VM__run_top_frame(self);
                 } else {
                     bool ok = fn->cfunc(co->nlocals, argv);
@@ -433,7 +433,7 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
                 // initialize local variables to py_NIL
                 memset(p1, 0, (char*)self->stack.sp - (char*)p1);
                 // submit the call
-                VM__push_frame(self, Frame__new(co, &fn->module, p0, p0, argv, co));
+                VM__push_frame(self, Frame__new(co, &fn->module, p0, p0, argv));
                 return opcall ? RES_CALL : VM__run_top_frame(self);
             case FuncType_GENERATOR:
                 assert(false);

+ 1 - 1
src/public/internal.c

@@ -93,7 +93,7 @@ bool py_exec(const char* source, const char* filename, enum py_CompileMode mode,
 
     if(!module) module = &vm->main;
 
-    Frame* frame = Frame__new(&co, module, NULL, vm->stack.sp, vm->stack.sp, &co);
+    Frame* frame = Frame__new(&co, module, NULL, vm->stack.sp, vm->stack.sp);
     VM__push_frame(vm, frame);
     FrameResult res = VM__run_top_frame(vm);
     CodeObject__dtor(&co);

+ 5 - 3
src/public/modules.c

@@ -317,13 +317,15 @@ static bool NoneType__repr__(int argc, py_Ref argv) {
 static bool builtins_exec(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     PY_CHECK_ARG_TYPE(0, tp_str);
-    return py_exec(py_tostr(argv), "<exec>", EXEC_MODE, NULL);
+    Frame* frame = pk_current_vm->top_frame;
+    return py_exec(py_tostr(argv), "<exec>", EXEC_MODE, &frame->module);
 }
 
 static bool builtins_eval(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     PY_CHECK_ARG_TYPE(0, tp_str);
-    return py_exec(py_tostr(argv), "<eval>", EVAL_MODE, NULL);
+    Frame* frame = pk_current_vm->top_frame;
+    return py_exec(py_tostr(argv), "<eval>", EVAL_MODE, &frame->module);
 }
 
 static bool builtins_isinstance(int argc, py_Ref argv) {
@@ -493,7 +495,7 @@ static bool super__new__(int argc, py_Ref argv) {
         if(frame->function) {
             Function* func = py_touserdata(frame->function);
             *class_arg = *(py_Type*)PyObject__userdata(func->clazz);
-            if(frame->locals_co->nlocals > 0) self_arg = &frame->locals[0];
+            if(frame->co->nlocals > 0) self_arg = &frame->locals[0];
         }
         if(class_arg == 0 || self_arg == NULL) return RuntimeError("super(): no arguments");
     } else if(argc == 3) {