diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index ea1f586b..9591bd56 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -234,12 +234,20 @@ bool py_checktype(py_Ref self, py_Type type) PY_RAISE; #define py_checkstr(self) py_checktype(self, tp_str) /************* References *************/ + /// Get the i-th register. /// All registers are located in a contiguous memory. py_GlobalRef py_getreg(int i); /// Set the i-th register. void py_setreg(int i, py_Ref val); +/// Get variable in the `__main__` module. +py_GlobalRef py_getglobal(const char* name); +/// Set variable in the `__main__` module. +void py_setglobal(const char* name, py_Ref val); +/// Get variable in the `builtins` module. +py_GlobalRef py_getbuiltin(const char* name); + /// Equivalent to `*dst = *src`. void py_assign(py_Ref dst, py_Ref src); /// Get the last return value. diff --git a/src/public/modules.c b/src/public/modules.c index 559ee09e..75d8ed57 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -13,6 +13,18 @@ py_Ref py_getmodule(const char* path) { return NameDict__try_get(&vm->modules, py_name(path)); } +py_Ref py_getbuiltin(const char* name){ + return py_getdict(&pk_current_vm->builtins, py_name(name)); +} + +py_Ref py_getglobal(const char* name){ + return py_getdict(&pk_current_vm->main, py_name(name)); +} + +void py_setglobal(const char* name, py_Ref val){ + py_setdict(&pk_current_vm->main, py_name(name), val); +} + py_Ref py_newmodule(const char* path) { ManagedHeap* heap = &pk_current_vm->heap; PyObject* obj = ManagedHeap__new(heap, tp_module, -1, 0);