public void PushNewValueObject(IntPtr luaState, object o, int index) { LuaDLL.luanet_newudata(luaState, index); LuaDLL.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName); if (LuaDLL.lua_isnil(luaState, -1)) { Debuger.LogWarning("Create not wrap ulua type:" + o.GetType().AssemblyQualifiedName); LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaDLL.lua_rawset(luaState, -3); } LuaDLL.lua_setmetatable(luaState, -2); }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { if (metatable == "luaNet_metatable") { // Gets or creates the metatable for the object's type LuaDLL.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName); if (LuaDLL.lua_isnil(luaState, -1)) { LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaDLL.lua_rawset(luaState, -3); } } else { LuaDLL.luaL_getmetatable(luaState, metatable); } // Stores the object index in the Lua list and pushes the // index into the Lua stack //if (!o.GetType().IsValueType) //{ LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.luanet_newudata(luaState, index); LuaDLL.lua_pushvalue(luaState, -3); LuaDLL.lua_remove(luaState, -4); LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); //} //else //{ // LuaDLL.luanet_newudata(luaState, index); // LuaDLL.lua_pushvalue(luaState, -2); // LuaDLL.lua_remove(luaState, -3); // LuaDLL.lua_setmetatable(luaState, -2); //} }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { //LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.lua_getref(luaState, weakTableRef); LuaDLL.luanet_newudata(luaState, index); if (metatable == "luaNet_metatable") { // Gets or creates the metatable for the object's type //string meta = t.AssemblyQualifiedName //LuaDLL.luaL_getmetatable(luaState, meta); Type t = o.GetType(); PushMetaTable(luaState, o.GetType()); if (LuaDLL.lua_isnil(luaState, -1)) { string meta = t.AssemblyQualifiedName; #if UNITY_EDITOR //UnityEngine.Debug.LogWarning("Create not wrap ulua type:" + meta); #endif LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, meta); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaDLL.lua_rawset(luaState, -3); } } else { LuaDLL.luaL_getmetatable(luaState, metatable); } LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); }
private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { LuaDLL.lua_getref(luaState, this.weakTableRef); LuaDLL.luanet_newudata(luaState, index); if (metatable == "luaNet_metatable") { Type type = o.GetType(); ObjectTranslator.PushMetaTable(luaState, o.GetType()); if (LuaDLL.lua_isnil(luaState, -1)) { string assemblyQualifiedName = type.AssemblyQualifiedName; Debugger.Log("<color=green>Create not wrap ulua type:" + assemblyQualifiedName + "</color>", new object[0]); LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, assemblyQualifiedName); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1.0); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, this.metaFunctions.gcFunction, 0); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, this.metaFunctions.toStringFunction, 0); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, this.metaFunctions.newindexFunction, 0); LuaDLL.lua_rawset(luaState, -3); } } else { LuaDLL.luaL_getmetatable(luaState, metatable); } LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); }
private static void PushMetaTable(IntPtr L, Type t) { int num = -1; if (!ObjectTranslator.typeMetaMap.TryGetValue(t, out num)) { LuaDLL.luaL_getmetatable(L, t.AssemblyQualifiedName); if (!LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pushvalue(L, -1); num = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); ObjectTranslator.typeMetaMap.Add(t, num); } } else { LuaDLL.lua_getref(L, num); } }
public void PushNewValueObject(IntPtr luaState, object o, int index, string metatable) { if (metatable == "luaNet_metatable") { // Gets or creates the metatable for the object's type LuaDLL.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName); if (LuaDLL.lua_isnil(luaState, -1)) { LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaDLL.lua_rawset(luaState, -3); } } else { LuaDLL.luaL_getmetatable(luaState, metatable); } LuaDLL.luanet_newudata(luaState, index); LuaDLL.lua_pushvalue(luaState, -2); LuaDLL.lua_remove(luaState, -3); LuaDLL.lua_setmetatable(luaState, -2); }
/* * Pushes a CLR object into the Lua stack as an userdata * with the provided metatable */ public void pushObject(IntPtr luaState, object o, string metatable) { int index = -1; // Pushes nil if (o == null) { LuaDLL.lua_pushnil(luaState); return; } // Object already in the list of Lua objects? Push the stored reference. bool found = (!o.GetType().IsValueType) && objectsBackMap.TryGetValue(o, out index); if (found) { LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.lua_rawgeti(luaState, -1, index); // Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep // occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call // this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect // object here // did we find a non nil object in our table? if not, we need to call collect object LuaTypes type = LuaDLL.lua_type(luaState, -1); if (type != LuaTypes.LUA_TNIL) { LuaDLL.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack return; } // MetaFunctions.dumpStack(this, luaState); LuaDLL.lua_remove(luaState, -1); // remove the nil object value LuaDLL.lua_remove(luaState, -1); // remove the metatable collectObject(o, index); // Remove from both our tables and fall out to get a new ID } index = addObject(o); pushNewObject(luaState, o, index, metatable); }
static void PushMetaTable(IntPtr L, Type t) { int reference = -1; if (!typeMetaMap.TryGetValue(t, out reference)) { LuaDLL.luaL_getmetatable(L, t.AssemblyQualifiedName); if (!LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pushvalue(L, -1); reference = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); typeMetaMap.Add(t, reference); } } else { LuaDLL.lua_getref(L, reference); } }
public void PushCachedObject(IntPtr luaState, int index) { LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaTypes type = LuaDLL.lua_type(luaState, -1); LuaDLL.lua_rawgeti(luaState, -1, index); type = LuaDLL.lua_type(luaState, -1); if (type != LuaTypes.LUA_TNIL) { LuaDLL.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack } else { LuaDLL.lua_remove(luaState, -1); // remove the nil object value LuaDLL.lua_remove(luaState, -1); // remove the metatable UnityEngine.Debug.LogError("cache object is lost"); } }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { CreateMetaTable(luaState, o, metatable); // Stores the object index in the Lua list and pushes the index into the Lua stack //if (!o.GetType().IsValueType) //{ LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.luanet_newudata(luaState, index); LuaDLL.lua_pushvalue(luaState, -3); LuaDLL.lua_remove(luaState, -4); LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); //} //else //{ // LuaDLL.luanet_newudata(luaState, index); // LuaDLL.lua_pushvalue(luaState, -2); // LuaDLL.lua_remove(luaState, -3); // LuaDLL.lua_setmetatable(luaState, -2); //} }
public void LuaGetMetaTable(string meta) { LuaDLL.luaL_getmetatable(L, meta); }