Bladeren bron

add `py_getvmctx`

blueloveTH 1 jaar geleden
bovenliggende
commit
5ad606859f
4 gewijzigde bestanden met toevoegingen van 14 en 0 verwijderingen
  1. 1 0
      include/pocketpy/interpreter/vm.h
  2. 4 0
      include/pocketpy/pocketpy.h
  3. 1 0
      src/interpreter/vm.c
  4. 8 0
      src/public/internal.c

+ 1 - 0
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
     bool is_curr_exc_handled;  // handled by try-except block but not cleared yet
 
 
     py_TValue reg[8];  // users' registers
     py_TValue reg[8];  // users' registers
+    void* ctx;         // user-defined context
 
 
     py_StackRef __curr_class;
     py_StackRef __curr_class;
     py_StackRef __curr_function;
     py_StackRef __curr_function;

+ 4 - 0
include/pocketpy/pocketpy.h

@@ -92,6 +92,10 @@ PK_EXPORT int py_currentvm();
 PK_EXPORT void py_switchvm(int index);
 PK_EXPORT void py_switchvm(int index);
 /// Reset the current VM.
 /// Reset the current VM.
 PK_EXPORT void py_resetvm();
 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.
 /// Set `sys.argv`. Used for storing command-line arguments.
 PK_EXPORT void py_sys_setargv(int argc, char** argv);
 PK_EXPORT void py_sys_setargv(int argc, char** argv);
 /// Setup the callbacks for the current VM.
 /// Setup the callbacks for the current VM.

+ 1 - 0
src/interpreter/vm.c

@@ -71,6 +71,7 @@ void VM__ctor(VM* self) {
     self->curr_exception = *py_NIL;
     self->curr_exception = *py_NIL;
     self->is_curr_exc_handled = false;
     self->is_curr_exc_handled = false;
 
 
+    self->ctx = NULL;
     self->__curr_class = NULL;
     self->__curr_class = NULL;
     self->__curr_function = NULL;
     self->__curr_function = NULL;
 
 

+ 8 - 0
src/public/internal.c

@@ -75,6 +75,14 @@ int py_currentvm() {
     return -1;
     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) {
 void py_sys_setargv(int argc, char** argv) {
     py_GlobalRef sys = py_getmodule("sys");
     py_GlobalRef sys = py_getmodule("sys");
     py_Ref argv_list = py_getdict(sys, py_name("argv"));
     py_Ref argv_list = py_getdict(sys, py_name("argv"));