From 7efcbb136ec32d8bf5332118cbe0cc3426e3de88 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 20 Oct 2025 17:52:42 +0800 Subject: [PATCH] fix #400 --- include/pocketpy/interpreter/vm.h | 1 + include/pocketpy/pocketpy.h | 8 ++++++++ src/interpreter/vm.c | 10 ++++++++++ src/public/GlobalSetup.c | 11 +++++++++-- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index b231cfc4..599b5307 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -78,6 +78,7 @@ typedef struct VM { void VM__ctor(VM* self); void VM__dtor(VM* self); +int VM__index(VM* self); void VM__push_frame(VM* self, py_Frame* frame); void VM__pop_frame(VM* self); diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index fab5b4b3..d3969699 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -90,6 +90,12 @@ typedef struct py_Callbacks { void (*gc_mark)(void (*f)(py_Ref val, void* ctx), void* ctx); } py_Callbacks; +/// A struct contains the application-level callbacks. +typedef struct py_AppCallbacks { + void (*on_vm_ctor)(int index); + void (*on_vm_dtor)(int index); +} py_AppCallbacks; + /// Native function signature. /// @param argc number of arguments. /// @param argv array of arguments. Use `py_arg(i)` macro to get the i-th argument. @@ -125,6 +131,8 @@ PK_API void* py_getvmctx(); PK_API void py_setvmctx(void* ctx); /// Setup the callbacks for the current VM. PK_API py_Callbacks* py_callbacks(); +/// Setup the application callbacks +PK_API py_AppCallbacks* py_appcallbacks(); /// Set `sys.argv`. Used for storing command-line arguments. PK_API void py_sys_setargv(int argc, char** argv); diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 0b25a015..069f1b1a 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -278,9 +278,19 @@ void VM__ctor(VM* self) { } while(0); self->main = py_newmodule("__main__"); + + if(py_appcallbacks()->on_vm_ctor) { + int index = VM__index(self); + py_appcallbacks()->on_vm_ctor(index); + } } void VM__dtor(VM* self) { + if(py_appcallbacks()->on_vm_dtor) { + int index = VM__index(self); + py_appcallbacks()->on_vm_dtor(index); + } + // reset traceinfo py_sys_settrace(NULL, true); LineProfiler__dtor(&self->line_profiler); diff --git a/src/public/GlobalSetup.c b/src/public/GlobalSetup.c index ea09569a..704fa354 100644 --- a/src/public/GlobalSetup.c +++ b/src/public/GlobalSetup.c @@ -65,13 +65,15 @@ void py_finalize() { pk_names_finalize(); } -int py_currentvm() { +int VM__index(VM* self) { for(int i = 0; i < 16; i++) { - if(pk_all_vm[i] == pk_current_vm) return i; + if(pk_all_vm[i] == self) return i; } return -1; } +int py_currentvm() { return VM__index(pk_current_vm); } + void py_switchvm(int index) { if(index < 0 || index >= 16) c11__abort("invalid vm index"); if(!pk_all_vm[index]) { @@ -104,6 +106,11 @@ void py_setvmctx(void* ctx) { pk_current_vm->ctx = ctx; } py_Callbacks* py_callbacks() { return &pk_current_vm->callbacks; } +py_AppCallbacks* py_appcallbacks() { + static py_AppCallbacks _callbacks = {0}; + return &_callbacks; +} + ///////////////////////////// void py_sys_setargv(int argc, char** argv) {