[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: how do i add a metatable to a table in C?
- From: "Paulo Lopes" <pmlopes@...>
- Date: Wed, 21 Nov 2007 20:17:08 +0100
static int access_intvar (lua_State *L)
{
int *var = (int *) lua_topointer(L, lua_upvalueindex(1));
if(lua_gettop(L) == 0) {
lua_pushnumber(L, *var);
return 1;
} else {
*var = luaL_checkint(L, 1);
return 0;
}
}
int main()
{
lua_State *L = lua_open();
luaL_openlibs(L);
int foo = 100;
lua_newtable(L);
lua_pushstring(L, "foo");
lua_pushlightuserdata(L, &foo);
lua_pushcclosure(L, access_intvar, 1);
lua_settable(L, -3);
lua_setglobal(L, "global");
// main execution
if(luaL_dofile(L, "test.lua") != 0)
{
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
I'd like to add some metatable to the main "global" table in order to
call the cclosure just by:
global.foo = value and x = global.foo
right now it works if i call like: global.foo(value) and x = global.foo()
i want to map __index and __newindex functions to the closure but i'm
a bit confused on how to make it.