From 56097f6927ebf0b76abd2825bd8e41302844ea92 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 5 Aug 2024 23:24:07 +0800 Subject: [PATCH] ... --- include/pocketpy/common/utils.h | 6 ----- include/pocketpy/interpreter/frame.h | 34 +++++++++------------------- include/pocketpy/pocketpy.h | 6 +++++ src/interpreter/frame.c | 26 +++++++++++++++++---- src/interpreter/vm.c | 1 - src/public/modules.c | 3 +-- src/public/stack_ops.c | 6 +++++ 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/include/pocketpy/common/utils.h b/include/pocketpy/common/utils.h index 301a3cbe..f959699f 100644 --- a/include/pocketpy/common/utils.h +++ b/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) \ diff --git a/include/pocketpy/interpreter/frame.h b/include/pocketpy/interpreter/frame.h index 26640227..ab5de56d 100644 --- a/include/pocketpy/interpreter/frame.h +++ b/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*); diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index e8024b19..590e851b 100644 --- a/include/pocketpy/pocketpy.h +++ b/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. diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index 5ef8ae86..fbf44f97 100644 --- a/src/interpreter/frame.c +++ b/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); } \ No newline at end of file diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index cfb0c4d3..1eeceb93 100644 --- a/src/interpreter/vm.c +++ b/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); diff --git a/src/public/modules.c b/src/public/modules.c index ef9fc3e4..e4c0338e 100644 --- a/src/public/modules.c +++ b/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]; } diff --git a/src/public/stack_ops.c b/src/public/stack_ops.c index 741aae3d..73bab5ed 100644 --- a/src/public/stack_ops.c +++ b/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 */