diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index aa1887b4..e1a3882f 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -32,6 +32,7 @@ typedef struct VM { bool is_curr_exc_handled; // handled by try-except block but not cleared yet py_TValue reg[8]; // users' registers + void* ctx; // user-defined context py_StackRef __curr_class; py_StackRef __curr_function; diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 15fb556a..1f7b0393 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -92,6 +92,10 @@ PK_EXPORT int py_currentvm(); PK_EXPORT void py_switchvm(int index); /// Reset the current VM. PK_EXPORT void py_resetvm(); +/// Get the current VM context. This is used for user-defined data. +PK_EXPORT void* py_getvmctx(); +/// Set the current VM context. This is used for user-defined data. +PK_EXPORT void py_setvmctx(void* ctx); /// Set `sys.argv`. Used for storing command-line arguments. PK_EXPORT void py_sys_setargv(int argc, char** argv); /// Setup the callbacks for the current VM. diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 04624f57..31141e51 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -71,6 +71,7 @@ void VM__ctor(VM* self) { self->curr_exception = *py_NIL; self->is_curr_exc_handled = false; + self->ctx = NULL; self->__curr_class = NULL; self->__curr_function = NULL; diff --git a/src/public/internal.c b/src/public/internal.c index 12eef91f..b5e6037d 100644 --- a/src/public/internal.c +++ b/src/public/internal.c @@ -75,6 +75,14 @@ int py_currentvm() { return -1; } +void* py_getvmctx(){ + return pk_current_vm->ctx; +} + +void py_setvmctx(void* ctx){ + pk_current_vm->ctx = ctx; +} + void py_sys_setargv(int argc, char** argv) { py_GlobalRef sys = py_getmodule("sys"); py_Ref argv_list = py_getdict(sys, py_name("argv"));