internal void pushObject(IntPtr luaState, object o, string metatable)
{
int index;
int top = LuaDLL.lua_gettop(luaState);
// Pushes nil
if(o==null)
{
LuaDLL.lua_pushnil(luaState);
return;
}
// Object already in the list of Lua objects? Push the stored reference.
if((index=objects.IndexOf(o))!=-1)
{
// during the next call, lua's gc calls the __gc metafunction on "o", so control passes to a
// collectObject() method in another class, which removes it from the objects ArrayList
LuaDLL.luaL_getmetatable(luaState,"luaNet_objects");
LuaDLL.lua_rawgeti(luaState,-1,index);
LuaDLL.lua_remove(luaState,-2);
if(LuaDLL.lua_isnil(luaState,-1))
{
// Our object got GCed mid-way through retrieving it from the cache.
// We should have removed it from the cache, so let's go get it fresh
LuaDLL.lua_settop(luaState, top);
pushObject(luaState, o, metatable); // Gets our fresh variant
return;
}
return;
}
// New object: inserts it in the list
for(int i=0;i<objects.Count;i++)
if(objects[i]==null)
{
objects[i]=o;
index=i;
break;
}
if(index==-1) index=objects.Add(o);
pushNewObject(luaState,o,index,metatable);
}