protected void GetVarAndCleanG(string varName) { BBLua.lua_getglobal(ls.L, varName); //fetch value BBLua.lua_pushnil(ls.L); BBLua.lua_setglobal(ls.L, varName); //remove from _G }
static LuaResult FakePcall(UIntPtr L, int nargs, int nresults, int errfunc) { if (!GlobalState.CatchErrors) { return(BBLua.lua_pcall(L, nargs, nresults, errfunc)); } BBLua.lua_pushcclosure(L, ErrorHook.errorHandlerPtr, 0); BBLua.lua_insert(L, -nargs - 2); LuaResult res; if (nresults >= 0) { res = BBLua.lua_pcall(L, nargs, nresults, -nargs - 2); } else //LUA_MULTRET { int stackBefore = BBLua.lua_gettop(L); res = BBLua.lua_pcall(L, nargs, nresults, -nargs - 2); int stackNow = BBLua.lua_gettop(L); if (res == LuaResult.OK) { nresults = stackNow - stackBefore + nargs + 1; } else { nresults = 0; } } if (res != LuaResult.OK) { BBLua.lua_settop(L, -2); //remove errormsg for (int i = 0; i < nresults; i++) //push dummy returns, hopefully settlers accepts this { BBLua.lua_pushnil(L); } } BBLua.lua_remove(L, -nresults - 1); //remove error handler return(LuaResult.OK); }
public string TosToString(bool popStack, bool noExpand, Dictionary <IntPtr, bool> printedTables) { LuaType type = BBLua.lua_type(this.L, -1); string result; switch (type) { case LuaType.Nil: result = "nil"; break; case LuaType.Boolean: result = BBLua.lua_toboolean(this.L, -1).ToString(); break; case LuaType.Number: result = BBLua.lua_tonumber(this.L, -1).ToString(); break; case LuaType.String: result = "\"" + BBLua.lua_tostring(this.L, -1) + "\""; break; case LuaType.Function: result = GetTosFunctionInfo(); break; case LuaType.Table: if (noExpand) { goto default; } IntPtr tblPtr = BBLua.lua_topointer(this.L, -1); if (printedTables.ContainsKey(tblPtr)) { result = "<Table, recursion>"; break; } result = "{"; BBLua.lua_pushnil(this.L); while (BBLua.lua_next(this.L, -2) != 0) { printedTables.Add(tblPtr, true); string val = IndentMultiLine(TosToString(true, false, printedTables)); string key = TosToString(false, true, printedTables); printedTables.Remove(tblPtr); if (key[0] == '\"') { string rawString = key.Substring(1, key.Length - 2); if (alphaNumeric.IsMatch(rawString)) { result += "\n " + rawString + " = " + val + ","; } else { result += "\n [" + key + "] = " + val + ","; } } else { result += "\n [" + key + "] = " + val + ","; } } result += "\n}"; break; case LuaType.UserData: case LuaType.LightUserData: IntPtr ptr = BBLua.lua_touserdata(this.L, -1); result = "<" + type.ToString() + ", at 0x" + ptr.ToInt32().ToString("X") + ">"; break; default: result = "<" + type.ToString() + ">"; break; } if (popStack) { BBLua.lua_settop(this.L, -2); } return(result); }