feat(library/kernel_bindings): add 'set_env/set_environment' commands for updating the global environment

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-06-11 11:03:12 -07:00
parent 8fcc25d55a
commit 431b47377d
2 changed files with 13 additions and 3 deletions

View file

@ -1300,12 +1300,14 @@ void set_global_environment(lua_State * L, environment const & env) {
lua_settable(L, LUA_REGISTRYINDEX); lua_settable(L, LUA_REGISTRYINDEX);
} }
set_environment::set_environment(lua_State * L, environment const & env) { set_environment::set_environment(lua_State * L, environment & env):
m_env(env) {
m_state = L; m_state = L;
set_global_environment(L, env); set_global_environment(L, env);
} }
set_environment::~set_environment() { set_environment::~set_environment() {
m_env = get_global_environment(m_state);
lua_pushlightuserdata(m_state, static_cast<void *>(&g_set_environment_key)); lua_pushlightuserdata(m_state, static_cast<void *>(&g_set_environment_key));
lua_pushnil(m_state); lua_pushnil(m_state);
lua_settable(m_state, LUA_REGISTRYINDEX); lua_settable(m_state, LUA_REGISTRYINDEX);
@ -1325,6 +1327,11 @@ int get_environment(lua_State * L) {
return push_environment(L, get_global_environment(L)); return push_environment(L, get_global_environment(L));
} }
int set_environment(lua_State * L) {
set_global_environment(L, to_environment(L, 1));
return 0;
}
static void environment_migrate(lua_State * src, int i, lua_State * tgt) { static void environment_migrate(lua_State * src, int i, lua_State * tgt) {
push_environment(tgt, to_environment(src, i)); push_environment(tgt, to_environment(src, i));
} }
@ -1342,6 +1349,8 @@ static void open_environment(lua_State * L) {
SET_GLOBAL_FUN(environment_pred, "is_environment"); SET_GLOBAL_FUN(environment_pred, "is_environment");
SET_GLOBAL_FUN(get_environment, "get_environment"); SET_GLOBAL_FUN(get_environment, "get_environment");
SET_GLOBAL_FUN(get_environment, "get_env"); SET_GLOBAL_FUN(get_environment, "get_env");
SET_GLOBAL_FUN(set_environment, "set_environment");
SET_GLOBAL_FUN(set_environment, "set_env");
SET_GLOBAL_FUN(import_modules, "import_modules"); SET_GLOBAL_FUN(import_modules, "import_modules");
SET_GLOBAL_FUN(import_hott_modules, "import_hott_modules"); SET_GLOBAL_FUN(import_hott_modules, "import_hott_modules");
SET_GLOBAL_FUN(export_module, "export_module"); SET_GLOBAL_FUN(export_module, "export_module");

View file

@ -51,9 +51,10 @@ void set_global_environment(lua_State * L, environment const & env);
with an environment object. with an environment object.
*/ */
class set_environment { class set_environment {
environment & m_env;
lua_State * m_state; lua_State * m_state;
public: public:
set_environment(lua_State * L, environment const & env); set_environment(lua_State * L, environment & env);
~set_environment(); ~set_environment();
}; };