[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: setglobals and Lua 5.0
- From: "Wim Couwenberg" <w.couwenberg@...>
- Date: Wed, 23 Apr 2003 12:42:33 +0200
The second half of my previous reply (about the __index method) is plain
silly, sorry about that... I found your earlier mail and will give it
another try...
Define a hook by:
static int zeus_func(lua_State *L); /* defined below */
static int zeus_hook(lua_State *L)
/* in: table, index
out: closed zeus_func */
{
lua_pushvalue(L, 2); /* copy global name */
lua_pushcclosure(L, zeus_func, 1); /* bind it to zeus_func as upvalue
*/
return 1;
}
And set it as __index method of the globals table:
lua_newtable(L);
lua_pushstring(L, "__index");
lua_pushcfunction(L, zeus_hook);
lua_settable(L, -3);
lua_setmetatable(L, LUA_GLOBALSINDEX);
Then if a global name "SomeName" is not defined, our hook will be called and
it returns zeus_func closed with "SomeName" as its first upvaue. Now
zeus_func can look something like:
static int zeus_func(lua_State *L)
{
/* push macro name (first upvalue)... */
lua_pushvalue(L, lua_upvalueindex(1));
/* check the macro name and do your thing... */
}
Bye,
Wim