Skip to content

Commit

Permalink
added __address__ in VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Oct 17, 2022
1 parent 09b4ffa commit 1def7cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/lang/poesie-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
typedef struct lua_vm {
lua_State *L;
ABT_mutex mutex;
margo_instance_id mid;
hg_addr_t address;
}* lua_vm_t;

static int poesie_lua_execute(void* impl, const char* code, char** output);
Expand All @@ -42,12 +44,18 @@ int poesie_lua_vm_init(poesie_vm_t vm, margo_instance_id mid, const char* name)
}
luaL_openlibs(lvm->L);

lvm->mid = mid;
margo_addr_self(mid, &lvm->address);

lua_pushstring(lvm->L, name ? name : "<anonymous>");
lua_setglobal(lvm->L, "__name__");

lua_pushlightuserdata(lvm->L, mid);
lua_setglobal(lvm->L, "__mid__");

lua_pushlightuserdata(lvm->L, lvm->address);
lua_setglobal(lvm->L, "__address__");

vm->lang = POESIE_LANG_LUA;
vm->impl = (void*)lvm;
vm->execute = &poesie_lua_execute;
Expand Down Expand Up @@ -79,6 +87,7 @@ static int poesie_lua_finalize(void* impl)
if(!lvm) return POESIE_ERR_INVALID_ARG;
lua_close(lvm->L);
ABT_mutex_free(&(lvm->mutex));
margo_addr_free(lvm->mid, lvm->address);
free(lvm);
return 0;
}
Expand Down
9 changes: 9 additions & 0 deletions src/lang/poesie-python.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ typedef struct python_vm {
PyObject* main;
PyObject* globalDictionary;
PyObject* localDictionary;
margo_instance_id mid;
hg_addr_t address;
ABT_mutex mutex;
}* python_vm_t;

Expand Down Expand Up @@ -69,9 +71,11 @@ int poesie_py_vm_init(poesie_vm_t vm, margo_instance_id mid, const char* name)
PyThreadState* old_ts = PyThreadState_Swap(new_ts);
#endif
/* initialize the context */
pvm->mid = mid;
pvm->main = PyImport_AddModule("__main__");
pvm->globalDictionary = PyModule_GetDict(pvm->main);
pvm->localDictionary = PyDict_New();
margo_addr_self(mid, &pvm->address);

if(name) {
PyObject* namePyObj = PyUnicode_FromString(name);
Expand All @@ -83,6 +87,10 @@ int poesie_py_vm_init(poesie_vm_t vm, margo_instance_id mid, const char* name)
PyDict_SetItemString(pvm->localDictionary, "__mid__", pyMid);
Py_DECREF(pyMid);

PyObject* pyAddr = PyCapsule_New((void*)pvm->address, "hg_addr_t", NULL);
PyDict_SetItemString(pvm->localDictionary, "__address__", pyAddr);
Py_DECREF(pyAddr);

#ifdef ALLOW_SUBINT
/* swap to back to old thread state */
PyThreadState_Swap(old_ts);
Expand Down Expand Up @@ -207,6 +215,7 @@ static int poesie_py_finalize(void* impl)
PyThreadState_Swap(old_ts);
#endif
ABT_mutex_free(&pvm->mutex);
margo_addr_free(pvm->mid, pvm->address);
free(pvm);
if(atomic_fetch_sub(&num_open_vms, 1) == 1) {
finalize_python();
Expand Down

0 comments on commit 1def7cb

Please sign in to comment.