blueloveTH 1 рік тому
батько
коміт
f27548c6df
2 змінених файлів з 20 додано та 0 видалено
  1. 8 0
      include/pocketpy/pocketpy.h
  2. 12 0
      src/public/modules.c

+ 8 - 0
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.

+ 12 - 0
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);