19

I have a program that embeds Lua and implements a form of lazy function lookup.

The way it worked in Lua 5.1, whenever a symbol was undefined the interpreter would call a global function hook that would then resolve the symbol.

This is a small portion of C code that implemented this lazy function lookup:

int function_hook(lua_State *pLuaState)
{
  // do the function lookup here
  ....
  return 1;
}

......

//-- create table containing the hook details
lua_newtable(pLuaState);
lua_pushstring(pLuaState, "__index");
lua_pushcfunction(pLuaState, function_hook);
lua_settable(pLuaState, -3);

//-- set global index callback hook
lua_setmetatable(pLuaState, LUA_GLOBALSINDEX);

I'm now trying to move this code to Lua 5.2 and have run into a problem.

In Lua 5.2 the LUA_GLOBALSINDEX value is no longer defined so this line of code no longer compiles.

//-- set global call back hook
lua_setmetatable(pLuaState, LUA_GLOBALSINDEX);

There is a reference to this change to LUA_GLOBALSINDEX but unfortunately it has not helped.

What would be the best way to re-write this one line of code to have the interpreter call the function_hook whenever it finds an unresolved symbol?

greatwolf
  • 20,287
  • 13
  • 71
  • 105
jussij
  • 10,370
  • 1
  • 33
  • 49

2 Answers2

18

The global environment is now stored at a special index in the registry. Try:

//-- get global environment table from registry
lua_rawgeti(pLuaState, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);

//-- create table containing the hook details
lua_newtable(pLuaState);
lua_pushstring(pLuaState, "__index");
lua_pushcfunction(pLuaState, function_hook);
lua_settable(pLuaState, -3);

//-- set global index callback hook
lua_setmetatable(pLuaState, -2);

//-- remove the global environment table from the stack
lua_pop(pLuaState, 1);
MattJ
  • 7,924
  • 1
  • 28
  • 33
  • 25
    You can also use `lua_pushglobaltable(pLuaState)` instead of `lua_rawgeti(pLuaState, LUA_RIDX_GLOBALS, LUA_REGISTRYINDEX)` to shield your code from future changes. You can also define `lua_pushglobaltable` for Lua 5.1 as `lua_pushvalue(L, LUA_GLOBALSINDEX)`. – lhf Apr 10 '12 at 11:40
0

here is patch: http://lua-users.org/lists/lua-l/2013-01/msg00352.html

lua_pushvalue(L,LUA_GLOBALSINDEX);
=>
lua_pushglobaltable(L);

len = luaL_getn(L, -1);
=>
len = lua_rawlen(L, -1);

lua_getfenv(L, lo);
=>
lua_getuservalue(L, lo);

lua_setfenv(L, lo);
=>
lua_setuservalue(L, lo);
andrewchan2022
  • 4,953
  • 45
  • 48