lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am trying to do a lua_replace on the globals table, then process a
user script, then restore the previous global table.  But it doesn't
seem to work.  I must be missing something with respect to the
globals_index table.  I can successfully replace the table, but when I
restore it, funny stuff happens.  For instance, my overriden print
function has been replaced with the one from the baselib and when that
gets called, the lookup of tostring in luaB_print returns nil.  Does
anyone have any pointers?

Thanks,
Mike

Sample:

//open generic librarys
luaL_openlibs(L);
//override print
lua_pushcfunction(L, _print);
lua_setglobal(L, "print");

//replace globals table
lua_pushvalue(L, LUA_GLOBALSINDEX);
old_globals = luaL_ref(L, LUA_REGISTRYINDEX);

//set the new table
lua_newtable(L);
lua_replace(L, LUA_GLOBALSINDEX);

//open generic librarys for the new globals table
luaL_openlibs(L);
//override print
lua_pushcfunction(L, _print);
lua_setglobal(L, "print");

//restore the globals table
lua_rawgeti(L, LUA_REGISTRYINDEX, old_globals);
lua_replace(L, LUA_GLOBALSINDEX);

//this should be the old global's table print
lua_getglobal(L, "print");
lua_pushstring(L, "test");
lua_call(L, 1, 0);