public static string[] CheckStringArray(IntPtr L, int stackPos) { LuaTypes luatype = LuaDLL.lua_type(L, stackPos); if (luatype == LuaTypes.LUA_TTABLE) { int index = 1; string retVal = null; Type t = typeof(string); List <string> list = new List <string>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, index); luatype = LuaDLL.lua_type(L, -1); if (luatype == LuaTypes.LUA_TNIL) { LuaDLL.lua_pop(L, 1); return(list.ToArray()); } else if (!TypeChecker.CheckType(L, t, -1)) { LuaDLL.lua_pop(L, 1); break; } retVal = ToString(L, -1); list.Add(retVal); LuaDLL.lua_pop(L, 1); ++index; } } else if (luatype == LuaTypes.LUA_TUSERDATA) { return((string[])CheckObject(L, stackPos, typeof(string[]))); } else if (luatype == LuaTypes.LUA_TNIL) { return(null); } LuaDLL.luaL_typerror(L, stackPos, "string[]"); return(null); }
public static T[] CheckObjectArray <T>(IntPtr L, int stackPos) { LuaTypes luatype = LuaDLL.lua_type(L, stackPos); if (luatype == LuaTypes.LUA_TTABLE) { int index = 1; T val = default(T); Type t = typeof(T); List <T> list = new List <T>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, index); luatype = LuaDLL.lua_type(L, -1); if (luatype == LuaTypes.LUA_TNIL) { LuaDLL.lua_pop(L, 1); return(list.ToArray());; } else if (!CheckType(L, t, -1)) { LuaDLL.lua_pop(L, 1); break; } val = (T)ToVarObject(L, -1); list.Add(val); LuaDLL.lua_pop(L, 1); ++index; } } else if (luatype == LuaTypes.LUA_TUSERDATA) { return((T[])CheckObject(L, stackPos, typeof(T[]))); } else if (luatype == LuaTypes.LUA_TNIL) { return(null); } LuaDLL.luaL_typerror(L, stackPos, typeof(T[]).FullName); return(null); }
static void AddLuaLoader2(IntPtr L) { LuaDLL.lua_getglobal(L, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); for (int i = LuaDLL.lua_objlen(L, loaderTable) + 1; i > 1; i--) { LuaDLL.lua_rawgeti(L, loaderTable, i - 1); LuaDLL.lua_rawseti(L, loaderTable, i); } LuaDLL.lua_pushstdcallcfunction(L, Loader); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); }
public static string[] CheckStringArray(IntPtr L, int stackPos) { LuaTypes luaTypes = LuaDLL.lua_type(L, stackPos); if (luaTypes == LuaTypes.LUA_TTABLE) { int num = 1; Type typeFromHandle = typeof(string); List <string> list = new List <string>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, num); if (LuaDLL.lua_type(L, -1) == LuaTypes.LUA_TNIL) { break; } if (!TypeChecker.CheckType(L, typeFromHandle, -1)) { goto Block_3; } string text = ToLua.ToString(L, -1); list.Add(text); LuaDLL.lua_pop(L, 1); num++; } LuaDLL.lua_pop(L, 1); return(list.ToArray()); Block_3: LuaDLL.lua_pop(L, 1); } else { if (luaTypes == LuaTypes.LUA_TUSERDATA) { return((string[])ToLua.CheckObject(L, stackPos, typeof(string[]))); } if (luaTypes == LuaTypes.LUA_TNIL) { return(null); } } LuaDLL.luaL_typerror(L, stackPos, "string[]", null); return(null); }
public static T[] CheckNumberArray <T>(IntPtr L, int stackPos) { LuaTypes luaTypes = LuaDLL.lua_type(L, stackPos); if (luaTypes == LuaTypes.LUA_TTABLE) { int num = 1; T t = default(T); List <T> list = new List <T>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, num); luaTypes = LuaDLL.lua_type(L, -1); if (luaTypes == LuaTypes.LUA_TNIL) { break; } if (luaTypes != LuaTypes.LUA_TNUMBER) { goto Block_3; } T t2 = (T)((object)Convert.ChangeType(LuaDLL.lua_tonumber(L, -1), typeof(T))); list.Add(t2); LuaDLL.lua_pop(L, 1); num++; } LuaDLL.lua_pop(L, 1); return(list.ToArray()); Block_3 :; } else { if (luaTypes == LuaTypes.LUA_TUSERDATA) { return((T[])ToLua.CheckObject(L, stackPos, typeof(T[]))); } if (luaTypes == LuaTypes.LUA_TNIL) { return(null); } } LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeof(T[])), null); return(null); }
public static T[] CheckNumberArray <T>(IntPtr L, int stackPos) { LuaTypes luatype = LuaDLL.lua_type(L, stackPos); if (luatype == LuaTypes.LUA_TTABLE) { int index = 1; T ret = default(T); List <T> list = new List <T>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, index); luatype = LuaDLL.lua_type(L, -1); if (luatype == LuaTypes.LUA_TNIL) { LuaDLL.lua_pop(L, 1); return(list.ToArray()); } else if (luatype != LuaTypes.LUA_TNUMBER) { break; } ret = (T)Convert.ChangeType(LuaDLL.lua_tonumber(L, -1), typeof(T)); list.Add(ret); LuaDLL.lua_pop(L, 1); ++index; } } else if (luatype == LuaTypes.LUA_TUSERDATA) { return((T[])CheckObject(L, stackPos, typeof(T[]))); } else if (luatype == LuaTypes.LUA_TNIL) { return(null); } LuaDLL.luaL_typerror(L, stackPos, LuaMisc.GetTypeName(typeof(T[]))); return(null); }
/* * 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); }
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"); } }
public static void SetSearcher(IntPtr L, LuaCSFunction loader) { int top = LuaDLL.lua_gettop(L); LuaDLL.wlua_getglobal(L, "package"); //package LuaDLL.wlua_getfield(L, -1, "searchers"); //package,searchers LuaDLL.wlua_getglobal(L, "cswrapfunc"); //package,searchers,wrap LuaDLL.wLua_wrapfunction(L, -1, loader); //package,searchers,wrap,loader LuaDLL.wlua_pushcclosure(L, LuaFuncs.searcher, 1); //package,searchers,wrap,searcher LuaDLL.lua_remove(L, -2); //package,searchers,searcher int searchersIndex = LuaDLL.lua_gettop(L) - 1; for (int e = (int)LuaDLL.lua_rawlen(L, searchersIndex) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, searchersIndex, e - 1); //package,searchers,searcher,value LuaDLL.lua_rawseti(L, searchersIndex, e); //package,searchers,searcher } LuaDLL.lua_rawseti(L, searchersIndex, 1); //package,searchers LuaDLL.lua_settop(L, top); }
//必须检测类型 public static object[] CheckObjectArray(IntPtr L, int stackPos) { LuaTypes luatype = LuaDLL.lua_type(L, stackPos); if (luatype == LuaTypes.LUA_TTABLE) { int index = 1; object val = null; List <object> list = new List <object>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, index); luatype = LuaDLL.lua_type(L, -1); if (luatype == LuaTypes.LUA_TNIL) { LuaDLL.lua_pop(L, 1); return(list.ToArray());; } val = ToVarObject(L, -1); list.Add(val); LuaDLL.lua_pop(L, 1); ++index; } } else if (luatype == LuaTypes.LUA_TUSERDATA) { return((object[])CheckObject(L, stackPos, typeof(object[]))); } else if (luatype == LuaTypes.LUA_TNIL) { return(null); } LuaDLL.luaL_typerror(L, stackPos, "object[] or table"); return(null); }
public static void lua_getref(IntPtr luaState, int reference) { LuaDLL.lua_rawgeti(luaState, LuaIndexes.LUA_REGISTRYINDEX, reference); }
public LuaState() { // Create State L = LuaDLL.luaL_newstate(); // Create LuaInterface library LuaDLL.luaL_openlibs(L); LuaDLL.lua_pushstring(L, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(L, true); LuaDLL.lua_settable(L, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "luanet"); LuaDLL.lua_pushvalue(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鍘嬪叆浜哶G琛? LuaDLL.lua_getglobal(L, "luanet"); LuaDLL.lua_pushstring(L, "getmetatable"); LuaDLL.lua_getglobal(L, "getmetatable"); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pushstring(L, "rawget"); LuaDLL.lua_getglobal(L, "rawget"); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pushstring(L, "rawset"); LuaDLL.lua_getglobal(L, "rawset"); LuaDLL.lua_settable(L, -3); // Set luanet as global for object translator LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鐢╨uanet鏇挎崲_G琛? translator = new ObjectTranslator(this, L); LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鎭㈠_G琛? translator.PushTranslator(L); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaCSFunction(LuaStatic.panic); LuaDLL.lua_atpanic(L, panicCallback); printFunction = new LuaCSFunction(LuaStatic.print); LuaDLL.lua_pushstdcallcfunction(L, printFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "lprint"); warnFunction = new LuaCSFunction(LuaStatic.warn); LuaDLL.lua_pushstdcallcfunction(L, warnFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "lwarn"); breakFunction = new LuaCSFunction(LuaStatic.breakFunc); LuaDLL.lua_pushstdcallcfunction(L, breakFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "breakpoint"); loadfileFunction = new LuaCSFunction(LuaStatic.loadfile); LuaDLL.lua_pushstdcallcfunction(L, loadfileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); dofileFunction = new LuaCSFunction(LuaStatic.dofile); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); pcallFunction = new LuaCSFunction(LuaStatic.pcall); LuaDLL.lua_pushstdcallcfunction(L, pcallFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "pcall"); LuaDLL.lua_pushstdcallcfunction(L, LuaStatic.errorFunc_traceback); errorFuncRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); // Insert our loader FIRST loaderFunction = new LuaCSFunction(LuaStatic.loader); LuaDLL.lua_pushstdcallcfunction(L, loaderFunction); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.luaL_getn(L, loaderTable) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); DoString(LuaStatic.init_luanet); tracebackFunction = new LuaCSFunction(LuaStatic.traceback); }
public void LuaRawGetI(int tableIndex, int index) { LuaDLL.lua_rawgeti(L, tableIndex, index); }
public LuaState() { // Create State L = LuaDLL.luaL_newstate(); // Create LuaInterface library LuaDLL.luaL_openlibs(L); LuaDLL.lua_pushstring(L, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(L, true); LuaDLL.lua_settable(L, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "luanet"); LuaDLL.lua_pushvalue(L, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(L, "luanet"); LuaDLL.lua_pushstring(L, "getmetatable"); LuaDLL.lua_getglobal(L, "getmetatable"); LuaDLL.lua_settable(L, -3); // Set luanet as global for object translator LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, L); LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); GCHandle handle = GCHandle.Alloc(translator, GCHandleType.Pinned); IntPtr thisptr = GCHandle.ToIntPtr(handle); LuaDLL.lua_pushlightuserdata(L, thisptr); LuaDLL.lua_setglobal(L, "_translator"); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaCSFunction(LuaStatic.panic); LuaDLL.lua_atpanic(L, panicCallback); printFunction = new LuaCSFunction(LuaStatic.print); LuaDLL.lua_pushstdcallcfunction(L, printFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "print"); loadfileFunction = new LuaCSFunction(LuaStatic.loadfile); LuaDLL.lua_pushstdcallcfunction(L, loadfileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); dofileFunction = new LuaCSFunction(LuaStatic.dofile); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); // Insert our loader FIRST loaderFunction = new LuaCSFunction(LuaStatic.loader); LuaDLL.lua_pushstdcallcfunction(L, loaderFunction); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.luaL_getn(L, loaderTable) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); DoString(LuaStatic.init_luanet); tracebackFunction = new LuaCSFunction(LuaStatic.traceback); }
//L: [namespace Table] public bool AddObject(IntPtr L, System.Object obj, string metatable /* nullable */) { IntPtr userdata = IntPtr.Zero; if (luaObjs.TryGetValue(obj, out userdata)) { LuaDLL.lua_rawgeti(L, LuaDLL.LUA_REGISTRYINDEX, weakRefForUserData); //namespace,reftable LuaDLL.lua_pushlightuserdata(L, userdata); //namespace,reftable,userdataKey LuaDLL.lua_rawget(L, -2); //namespace,reftable,userdata LuaDLL.lua_remove(L, -2); //namespace,userdata if (LuaDLL.lua_isuserdata(L, -1)) { return(true); } else { LuaDLL.lua_pop(L, 1); //namespace } } userdata = LuaDLL.lua_newuserdata(L, 1); //namespace,obj if (metatable == null) { Type type = obj.GetType(); while (type != null) { LuaDLL.wlua_getfield(L, -2, type.Name); //namespace,obj,typet if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); //namespace,obj type = type.BaseType; continue; } if (LuaDLL.lua_istable(L, -1)) { metatable = type.Name; break; } else { LuaDLL.lua_pop(L, 2); //namespace throw new LuaException(L, "metatable must be a table:" + type.Name); } } } else { LuaDLL.wlua_getfield(L, -2, metatable); //namespace,obj,typet if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 2); //namespace throw new LuaException(L, "failed to find metatable:" + metatable); } } //namespace,obj,typet LuaDLL.lua_setmetatable(L, -2); //namespace,obj objs[userdata.ToInt64()] = obj; luaObjs[obj] = userdata; LuaDLL.lua_rawgeti(L, LuaDLL.LUA_REGISTRYINDEX, weakRefForUserData); //namespace,obj,reftable LuaDLL.lua_pushlightuserdata(L, userdata); //namespace,obj,reftable,userdatakey LuaDLL.lua_pushvalue(L, -3); //namespace,obj,reftable,userdatakey,obj LuaDLL.lua_rawset(L, -3); //namespace,obj,reftable LuaDLL.lua_pop(L, 1); //namespace,obj return(true); }