public static void Register(Lua.lua_State L)
{
objects.Add(L, new Dictionary<int, LWF.Button>());
objectIdentifiers.Add(L, new Dictionary<LWF.Button, int>());
int methods;
Lua.lua_newtable(L);
methods = Lua.lua_gettop(L);
// use a single table
// sometimes more convenient
int metatable=methods;
Lua.luaL_dostring(L, "if not __luna then __luna={} end");
Lua.lua_pushstring(L, "__luna");
Lua.lua_gettable(L, Lua.LUA_GLOBALSINDEX);
// unlike original luna class, this class uses the same table for methods and metatable
// store methods table in __luna global table so that
// scripts can add functions written in Lua.
Lua.lua_pushstring(L, LunaTraits_LWF_Button.className);
Lua.lua_pushvalue(L, methods);
Lua.lua_settable(L, -3); // __luna[className]=methods
Lua.lua_pushliteral(L, "__index");
Lua.lua_pushvalue(L, methods);
Lua.lua_settable(L, metatable); // metatable.__index=methods
/* Lua.lua_pushliteral(L, "__tostring"); */
/* Lua.lua_pushcfunction(L, tostring_T); */
/* Lua.lua_settable(L, metatable);// metatable.__tostring=tostring_T */
Lua.lua_pushliteral(L, "__gc");
Lua.lua_pushcfunction(L, gc_T);
Lua.lua_settable(L, metatable);
/*if (false)
{
// ctor supports only classname:new
Lua.lua_pushliteral(L, "new");
Lua.lua_pushcfunction(L, new_T);
Lua.lua_settable(L, methods); // add new_T to metatable table
}
else
*/
{
// ctor supports both classname:new(...) and classname(...)
// very slight memory and performance overhead, so
// no reason to support only one
Lua.lua_newtable(L); // mt for method table
{
Lua.lua_pushcfunction(L, new_T);
Lua.lua_pushvalue(L, -1); // dup new_T function
set(L, methods, "new"); // add new_T to method table
}
set(L, -3, "__call"); // mt.__call = new_T
Lua.lua_setmetatable(L, methods);
}
// fill method table with metatable from class T
for (int i = 0;; i++)
{
LunaTraits_LWF_Button.RegType l = LunaTraits_LWF_Button.methods[i];
if (l.name == null) break;
Lua.lua_pushstring(L, l.name);
Lua.lua_pushcclosure(L, l.mfunc, 0);
Lua.lua_settable(L, methods);
}
Lua.lua_pop(L, 2); // drop methods and __luna
}