public static int registerTable(IntPtr luaState)
{
#if __NOGEN__
throwError(luaState, "Tables as Objects not implemnented");
#else
ObjectTranslator translator = ObjectTranslator.FromState(luaState);
if (LuaDLL.lua_type(luaState, 1) == LuaTypes.LUA_TTABLE)
{
LuaTable luaTable = translator.getTable(luaState, 1);
string superclassName = LuaDLL.lua_tostring(luaState, 2);
if (superclassName != null)
{
Type klass = translator.FindType(superclassName);
if (klass != null)
{
// Creates and pushes the object in the stack, setting
// it as the metatable of the first argument
object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
translator.pushObject(luaState, obj, "luaNet_metatable");
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_pushstring(luaState, "__index");
LuaDLL.lua_pushvalue(luaState, -3);
LuaDLL.lua_settable(luaState, -3);
LuaDLL.lua_pushstring(luaState, "__newindex");
LuaDLL.lua_pushvalue(luaState, -3);
LuaDLL.lua_settable(luaState, -3);
LuaDLL.lua_setmetatable(luaState, 1);
// Pushes the object again, this time as the base field
// of the table and with the luaNet_searchbase metatable
LuaDLL.lua_pushstring(luaState, "base");
int index = translator.addObject(obj);
translator.pushNewObject(luaState, obj, index, "luaNet_searchbase");
LuaDLL.lua_rawset(luaState, 1);
}
else
{
translator.throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
}
}
else
{
translator.throwError(luaState, "register_table: superclass name can not be null");
}
}
else
{
translator.throwError(luaState, "register_table: first arg is not a table");
}
#endif
return(0);
}