blueloveTH 1 год назад
Родитель
Сommit
56097f6927

+ 0 - 6
include/pocketpy/common/utils.h

@@ -7,12 +7,6 @@
 extern "C" {
 #endif
 
-#ifdef __cplusplus
-#define PK_INLINE inline
-#else
-#define PK_INLINE static inline
-#endif
-
 #define PK_REGION(name) 1
 
 #define PK_SLICE_LOOP(i, start, stop, step)                                                        \

+ 11 - 23
include/pocketpy/interpreter/frame.h

@@ -38,38 +38,26 @@ typedef struct Frame {
     struct Frame* f_back;
     const Bytecode* ip;
     const CodeObject* co;
-    py_TValue module;    // weak ref
-    PyObject* function;  // a function object or NULL (global scope)
-    py_TValue* p0;       // unwinding base
-    py_TValue* locals;   // locals base
+    py_TValue module;      // weak ref
+    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;
 
 Frame* Frame__new(const CodeObject* co,
                   py_TValue* module,
-                  const py_TValue* function,
-                  py_TValue* p0,
-                  py_TValue* locals,
+                  py_StackRef function,
+                  py_StackRef p0,
+                  py_StackRef locals,
                   const CodeObject* locals_co);
 void Frame__delete(Frame* self);
 
-PK_INLINE int Frame__ip(const Frame* self) { return self->ip - (Bytecode*)self->co->codes.data; }
-
-PK_INLINE int Frame__lineno(const Frame* self) {
-    int ip = Frame__ip(self);
-    return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).lineno;
-}
-
-PK_INLINE int Frame__iblock(const Frame* self) {
-    int ip = Frame__ip(self);
-    return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).iblock;
-}
-
-PK_INLINE py_TValue* Frame__f_locals_try_get(Frame* self, py_Name name) {
-    return FastLocals__try_get_by_name(self->locals, self->locals_co, name);
-}
-
+int Frame__ip(const Frame* self);
+int Frame__lineno(const Frame* self);
+int Frame__iblock(const Frame* self);
+py_TValue* Frame__f_locals_try_get(Frame* self, py_Name name);
 py_TValue* Frame__f_closure_try_get(Frame* self, py_Name name);
 
 int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*);

+ 6 - 0
include/pocketpy/pocketpy.h

@@ -246,6 +246,12 @@ py_ObjectRef py_getslot(py_Ref self, int i);
 /// Set the i-th slot of the object.
 void py_setslot(py_Ref self, int i, py_Ref val);
 
+/************* Inspection *************/
+
+/// Get the current `function` object from the stack.
+/// Return `NULL` if not available.
+py_StackRef py_inspect_currentfunction();
+
 /************* Bindings *************/
 
 /// Bind a function to the object via "decl-based" style.

+ 21 - 5
src/interpreter/frame.c

@@ -36,9 +36,9 @@ void UnwindTarget__delete(UnwindTarget* self) { free(self); }
 
 Frame* Frame__new(const CodeObject* co,
                   py_TValue* module,
-                  const py_TValue* function,
-                  py_TValue* p0,
-                  py_TValue* locals,
+                  py_StackRef function,
+                  py_StackRef p0,
+                  py_StackRef locals,
                   const CodeObject* locals_co) {
     static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
     Frame* self = PoolFrame_alloc();
@@ -46,7 +46,7 @@ Frame* Frame__new(const CodeObject* co,
     self->ip = (Bytecode*)co->codes.data - 1;
     self->co = co;
     self->module = *module;
-    self->function = function ? function->_obj : NULL;
+    self->function = function;
     self->p0 = p0;
     self->locals = locals;
     self->locals_co = locals_co;
@@ -133,7 +133,23 @@ void Frame__set_unwind_target(Frame* self, py_TValue* sp) {
 
 py_TValue* Frame__f_closure_try_get(Frame* self, py_Name name) {
     if(self->function == NULL) return NULL;
-    Function* ud = PyObject__userdata(self->function);
+    Function* ud = py_touserdata(self->function);
     if(ud->closure == NULL) return NULL;
     return NameDict__try_get(ud->closure, name);
+}
+
+int Frame__ip(const Frame* self) { return self->ip - (Bytecode*)self->co->codes.data; }
+
+int Frame__lineno(const Frame* self) {
+    int ip = Frame__ip(self);
+    return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).lineno;
+}
+
+int Frame__iblock(const Frame* self) {
+    int ip = Frame__ip(self);
+    return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).iblock;
+}
+
+py_TValue* Frame__f_locals_try_get(Frame* self, py_Name name) {
+    return FastLocals__try_get_by_name(self->locals, self->locals_co, name);
 }

+ 0 - 1
src/interpreter/vm.c

@@ -559,7 +559,6 @@ void ManagedHeap__mark(ManagedHeap* self) {
     // mark frame
     for(Frame* frame = vm->top_frame; frame; frame = frame->f_back) {
         mark_value(&frame->module);
-        if(frame->function) mark_object(frame->function);
     }
     // mark vm's registers
     mark_value(&vm->last_retval);

+ 1 - 2
src/public/modules.c

@@ -491,8 +491,7 @@ static bool super__new__(int argc, py_Ref argv) {
     if(argc == 1) {
         // super()
         if(frame->function) {
-            // class_arg = PK_OBJ_GET(Function, frame->_callable)._class;
-            Function* func = PyObject__userdata(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];
         }

+ 6 - 0
src/public/stack_ops.c

@@ -58,6 +58,12 @@ void py_setslot(py_Ref self, int i, py_Ref val) {
     PyObject__slots(self->_obj)[i] = *val;
 }
 
+py_StackRef py_inspect_currentfunction(){
+    Frame* frame = pk_current_vm->top_frame;
+    if(!frame) return NULL;
+    return frame->function;
+}
+
 void py_assign(py_Ref dst, py_Ref src) { *dst = *src; }
 
 /* Stack References */