From 2d5e515ae74e5cbcaccb6ee2879322c26df8b1fc Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 18 Aug 2024 12:54:37 +0800 Subject: [PATCH] fix `py_inspect_currentfunction` --- include/pocketpy/interpreter/vm.h | 3 ++- include/pocketpy/pocketpy.h | 3 ++- src/interpreter/vm.c | 3 +++ src/public/py_exception.c | 1 + src/public/stack_ops.c | 4 +--- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 0979226f..a9bb2b8c 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -51,7 +51,8 @@ typedef struct VM { py_TValue reg[8]; // users' registers - py_TValue* __curr_class; + py_StackRef __curr_class; + py_StackRef __curr_function; py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES]; ManagedHeap heap; diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index f6b04d99..dbcd6871 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -310,8 +310,9 @@ PK_EXPORT void py_setslot(py_Ref self, int i, py_Ref val); /************* Inspection *************/ -/// Get the current `function` object from the stack. +/// Get the current `function` object on the stack. /// Return `NULL` if not available. +/// NOTE: This function should be placed at the beginning of your decl-based bindings. PK_EXPORT py_StackRef py_inspect_currentfunction(); /// Get the current `module` object where the code is executed. /// Return `NULL` if not available. diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index b8631172..06e59f89 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -70,6 +70,7 @@ void VM__ctor(VM* self) { self->is_curr_exc_handled = false; self->__curr_class = NULL; + self->__curr_function = NULL; ManagedHeap__ctor(&self->heap, self); ValueStack__ctor(&self->stack); @@ -445,8 +446,10 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall return opcall ? RES_CALL : VM__run_top_frame(self); } else { // decl-based binding + self->__curr_function = p0; bool ok = py_callcfunc(fn->cfunc, co->nlocals, argv); self->stack.sp = p0; + self->__curr_function = NULL; return ok ? RES_RETURN : RES_ERROR; } } diff --git a/src/public/py_exception.c b/src/public/py_exception.c index 70c3a8ca..50b82c7e 100644 --- a/src/public/py_exception.c +++ b/src/public/py_exception.c @@ -153,6 +153,7 @@ void py_clearexc(py_StackRef p0) { vm->curr_exception = *py_NIL; vm->is_curr_exc_handled = false; vm->__curr_class = NULL; + vm->__curr_function = NULL; if(p0) vm->stack.sp = p0; } diff --git a/src/public/stack_ops.c b/src/public/stack_ops.c index c7f79a4b..cd656758 100644 --- a/src/public/stack_ops.c +++ b/src/public/stack_ops.c @@ -70,9 +70,7 @@ void py_setslot(py_Ref self, int i, py_Ref val) { } py_StackRef py_inspect_currentfunction(){ - Frame* frame = pk_current_vm->top_frame; - if(!frame || !frame->has_function) return NULL; - return frame->p0; + return pk_current_vm->__curr_function; } py_GlobalRef py_inspect_currentmodule(){