public bool DoBuffer(byte[] bytes, string fn, out object ret) { // ensure no utf-8 bom, LuaJIT can read BOM, but Lua cannot! bytes = CleanUTF8Bom(bytes); ret = null; int errfunc = LuaObject.PushTry(statePointer); if (LuaNativeMethods.luaL_loadbuffer(statePointer, bytes, bytes.Length, fn) == 0) { if (LuaNativeMethods.lua_pcall(statePointer, 0, LuaNativeMethods.LUAMultRet, errfunc) != 0) { LuaNativeMethods.lua_pop(statePointer, 2); return(false); } LuaNativeMethods.lua_remove(statePointer, errfunc); // pop error function ret = TopObjects(errfunc - 1); return(true); } string err = LuaNativeMethods.lua_tostring(statePointer, -1); LuaNativeMethods.lua_pop(statePointer, 2); throw new Exception("File " + fn + ": " + err); }
public static bool CheckType(IntPtr ptr, int p, out Type t) { string tname = null; LuaTypes lt = LuaNativeMethods.lua_type(ptr, p); switch (lt) { case LuaTypes.TYPE_USERDATA: object o = CheckObj(ptr, p); if (o.GetType() != monoType) { throw new Exception(string.Format("{0} expect Type, got {1}", p, o.GetType().Name)); } t = (Type)o; return(true); case LuaTypes.TYPE_TABLE: LuaNativeMethods.lua_pushstring(ptr, "__type"); LuaNativeMethods.lua_rawget(ptr, p); if (!LuaNativeMethods.lua_isnil(ptr, -1)) { t = (Type)CheckObj(ptr, -1); LuaNativeMethods.lua_pop(ptr, 1); return(true); } else { LuaNativeMethods.lua_pushstring(ptr, "__fullname"); LuaNativeMethods.lua_rawget(ptr, p); tname = LuaNativeMethods.lua_tostring(ptr, -1); LuaNativeMethods.lua_pop(ptr, 2); } break; case LuaTypes.TYPE_STRING: CheckType(ptr, p, out tname); break; } if (tname == null) { throw new Exception("expect string or type table"); } t = LuaObject.FindType(tname); if (t != null && lt == LuaTypes.TYPE_TABLE) { LuaNativeMethods.lua_pushstring(ptr, "__type"); PushLightObject(ptr, t); LuaNativeMethods.lua_rawset(ptr, p); } return(t != null); }
public static int LoadFile(IntPtr ptr) { Loader(ptr); if (LuaNativeMethods.lua_isnil(ptr, -1)) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); return(LuaObject.Error(ptr, "Can't find {0}", fileName)); } return(2); }
public static int Import(IntPtr ptr) { try { LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_STRING); string str = LuaNativeMethods.lua_tostring(ptr, 1); string[] ns = str.Split('.'); LuaNativeMethods.lua_pushglobaltable(ptr); for (int n = 0; n < ns.Length; n++) { LuaNativeMethods.lua_getfield(ptr, -1, ns[n]); if (!LuaNativeMethods.lua_istable(ptr, -1)) { return(LuaObject.Error(ptr, "expect {0} is type table", ns)); } LuaNativeMethods.lua_remove(ptr, -2); } LuaNativeMethods.lua_pushnil(ptr); while (LuaNativeMethods.lua_next(ptr, -2) != 0) { string key = LuaNativeMethods.lua_tostring(ptr, -2); LuaNativeMethods.lua_getglobal(ptr, key); if (!LuaNativeMethods.lua_isnil(ptr, -1)) { LuaNativeMethods.lua_pop(ptr, 1); return(LuaObject.Error(ptr, "{0} had existed, import can't overload it.", key)); } LuaNativeMethods.lua_pop(ptr, 1); LuaNativeMethods.lua_setglobal(ptr, key); } LuaNativeMethods.lua_pop(ptr, 1); LuaObject.PushValue(ptr, true); return(1); } catch (Exception e) { return(LuaObject.Error(ptr, e)); } }
public static bool CheckType(IntPtr ptr, int p, out string v) { if (LuaNativeMethods.lua_isuserdata(ptr, p) > 0) { object o = CheckObj(ptr, p); if (o is string) { v = o as string; return(true); } } else if (LuaNativeMethods.lua_isstring(ptr, p)) { v = LuaNativeMethods.lua_tostring(ptr, p); return(true); } v = null; return(false); }
public static int As(IntPtr ptr) { try { if (!LuaObject.IsTypeTable(ptr, 2)) { return(Error(ptr, "No matched type of param 2")); } string meta = LuaNativeMethods.lua_tostring(ptr, -1); LuaNativeMethods.luaL_getmetatable(ptr, meta); LuaNativeMethods.lua_setmetatable(ptr, 1); LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_pushvalue(ptr, 1); return(2); } catch (Exception e) { return(Error(ptr, e)); } }
public static int PrintError(IntPtr ptr) { int n = LuaNativeMethods.lua_gettop(ptr); s.Length = 0; LuaNativeMethods.lua_getglobal(ptr, "tostring"); for (int i = 1; i <= n; i++) { if (i > 1) { s.Append(" "); } LuaNativeMethods.lua_pushvalue(ptr, -1); LuaNativeMethods.lua_pushvalue(ptr, i); LuaNativeMethods.lua_call(ptr, 1, 1); s.Append(LuaNativeMethods.lua_tostring(ptr, -1)); LuaNativeMethods.lua_pop(ptr, 1); } LuaNativeMethods.lua_settop(ptr, n); LuaNativeMethods.lua_getglobal(ptr, "debug"); LuaNativeMethods.lua_getfield(ptr, -1, "traceback"); LuaNativeMethods.lua_call(ptr, 0, 1); s.Append("\n"); s.Append(LuaNativeMethods.lua_tostring(ptr, -1)); LuaNativeMethods.lua_pop(ptr, 1); Logger.LogError(s.ToString(), true); if (ErrorEvent != null) { ErrorEvent(s.ToString()); } return(0); }
public static int DoFile(IntPtr ptr) { int n = LuaNativeMethods.lua_gettop(ptr); Loader(ptr); if (!LuaNativeMethods.lua_toboolean(ptr, -2)) { return(2); } else { if (LuaNativeMethods.lua_isnil(ptr, -1)) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); return(LuaObject.Error(ptr, "Can't find {0}", fileName)); } int k = LuaNativeMethods.lua_gettop(ptr); LuaNativeMethods.lua_call(ptr, 0, LuaNativeMethods.LUAMultRet); k = LuaNativeMethods.lua_gettop(ptr); return(k - n); } }
public static int ErrorReport(IntPtr ptr) { LuaNativeMethods.lua_getglobal(ptr, "debug"); LuaNativeMethods.lua_getfield(ptr, -1, "traceback"); LuaNativeMethods.lua_pushvalue(ptr, 1); LuaNativeMethods.lua_pushnumber(ptr, 2); LuaNativeMethods.lua_call(ptr, 2, 1); LuaNativeMethods.lua_remove(ptr, -2); string error = LuaNativeMethods.lua_tostring(ptr, -1); LuaNativeMethods.lua_pop(ptr, 1); Logger.LogError(error, true); if (ErrorEvent != null) { ErrorEvent(error); } LuaNativeMethods.lua_getglobal(ptr, "dumpstack"); LuaNativeMethods.lua_call(ptr, 0, 0); return(0); }
public bool Resume(out object retVal) { if (this.thread == IntPtr.Zero) { Logger.LogError("thread: already disposed?"); retVal = null; return(false); } int status = LuaNativeMethods.lua_status(this.thread); if (status != 0 && status != (int)LuaThreadStatus.LUA_YIELD) { Logger.LogError("thread: wrong status ?= " + status); retVal = null; return(false); } int result = LuaNativeMethods.lua_resume(this.thread, 0); if (result != (int)LuaThreadStatus.LUA_YIELD) { if (result != 0) { string error = LuaNativeMethods.lua_tostring(this.thread, -1); Logger.LogError(string.Format("wrong result ?= {0} err: {1}", result, error)); } retVal = null; return(false); } int argsFromYield = LuaNativeMethods.lua_gettop(this.thread); retVal = this.TopObjects(argsFromYield); return(true); }
public static int Loader(IntPtr ptr) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); byte[] bytes = LoadFile(fileName); if (bytes != null) { if (LuaNativeMethods.luaL_loadbuffer(ptr, bytes, bytes.Length, "@" + fileName) == 0) { LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_insert(ptr, -2); return(2); } else { string errstr = LuaNativeMethods.lua_tostring(ptr, -1); return(LuaObject.Error(ptr, errstr)); } } LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_pushnil(ptr); return(2); }
public static int PanicCallback(IntPtr ptr) { string reason = string.Format("unprotected error in call to Lua API ({0})", LuaNativeMethods.lua_tostring(ptr, -1)); throw new Exception(reason); }
public static object CheckVar(IntPtr ptr, int p) { LuaTypes type = LuaNativeMethods.lua_type(ptr, p); switch (type) { case LuaTypes.TYPE_NUMBER: return(LuaNativeMethods.lua_tonumber(ptr, p)); case LuaTypes.TYPE_STRING: return(LuaNativeMethods.lua_tostring(ptr, p)); case LuaTypes.TYPE_BOOLEAN: return(LuaNativeMethods.lua_toboolean(ptr, p)); case LuaTypes.TYPE_FUNCTION: { LuaFunction v; CheckType(ptr, p, out v); return(v); } case LuaTypes.TYPE_TABLE: { if (IsLuaValueType(ptr, p)) { if (LuaTypeCheck(ptr, p, "Vector2")) { Vector2 v; CheckType(ptr, p, out v); return(v); } else if (LuaTypeCheck(ptr, p, "Vector3")) { Vector3 v; CheckType(ptr, p, out v); return(v); } else if (LuaTypeCheck(ptr, p, "Vector4")) { Vector4 v; CheckType(ptr, p, out v); return(v); } else if (LuaTypeCheck(ptr, p, "Quaternion")) { Quaternion v; CheckType(ptr, p, out v); return(v); } else if (LuaTypeCheck(ptr, p, "Color")) { Color c; CheckType(ptr, p, out c); return(c); } Logger.LogError("unknown lua value type"); return(null); } else if (IsLuaClass(ptr, p)) { return(CheckObj(ptr, p)); } else { LuaTable v; CheckType(ptr, p, out v); return(v); } } case LuaTypes.TYPE_USERDATA: return(LuaObject.CheckObj(ptr, p)); case LuaTypes.TYPE_THREAD: { LuaThread lt; CheckType(ptr, p, out lt); return(lt); } default: return(null); } }