public static object[] CallLunaFunction(this Lua lua, string fileName, string function, params object[] args) { var luaState = lua.LuaState; var translator = lua.Translator; var oldTop = LuaLib.LuaGetTop(luaState); LuaLib.LuaGetGlobal(luaState, "CallImportFuncion"); translator.Push(luaState, fileName); translator.Push(luaState, function); for (var i = 0; i < args.Length; i++) { translator.Push(luaState, args[i]); } var error = LuaLib.LuaPCall(luaState, 2 + args.Length, -1, 0); if (error != 0) { var curTop = LuaLib.LuaGetTop(luaState); if (curTop > oldTop) { object err = translator.GetObject(luaState, -1); UnityEngine.Debug.LogError(err); } LuaLib.LuaSetTop(luaState, oldTop); return null; } var ret = translator.PopValues(luaState, oldTop); return ret; }
/// <summary> /// Executes a Lua chunk and returns all the chunk's return values in an array. /// </summary> /// <param name = "chunk">Chunk to execute</param> /// <param name = "chunkName">Name to associate with the chunk. Defaults to "chunk".</param> /// <returns></returns> public object[] DoString(string chunk, string chunkName = "chunk") { int oldTop = LuaLib.LuaGetTop(luaState); executing = true; if (LuaLib.LuaLLoadBuffer(luaState, chunk, chunkName) == 0) { try { if (LuaLib.LuaPCall(luaState, 0, -1, 0) == 0) { return(translator.PopValues(luaState, oldTop)); } else { ThrowExceptionFromError(oldTop); } } finally { executing = false; } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }
/* * Excutes a Lua file and returns all the chunk's return * values in an array */ public object[] DoFile(string fileName) { int oldTop = LuaLib.LuaGetTop(luaState); if (LuaLib.LuaLLoadFile(luaState, fileName) == 0) { executing = true; try { if (LuaLib.LuaPCall(luaState, 0, -1, 0) == 0) { return(translator.PopValues(luaState, oldTop)); } else { ThrowExceptionFromError(oldTop); } } finally { executing = false; } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }
static int PrintWarning(LuaState luaState) { // For each argument we'll 'tostring' it int n = LuaLib.LuaGetTop(luaState); var sb = new StringBuilder("Lua:"); LuaLib.LuaGetGlobal(luaState, "tostring"); for( int i = 1; i <= n; i++ ) { LuaLib.LuaPushValue(luaState, -1); /* function to be called */ LuaLib.LuaPushValue(luaState, i); /* value to print */ LuaLib.LuaPCall(luaState, 1, 1, 0); var s = LuaLib.LuaToString(luaState, -1); sb.Append(s); if(i > 1) { sb.Append("\t"); } LuaLib.LuaPop(luaState, 1); /* pop result */ } UnityEngine.Debug.LogWarning(sb.ToString()); return 0; }
public static void LoadImportExpand(this Lua lua) { int result = LuaLib.LuaLLoadBuffer(lua.LuaState, LunaPackage, "Import.lua(in Import.cs)"); if (result != 0) throw new NotSupportedException("Import.LoadExpand failed"); LuaLib.LuaPCall (lua.LuaState, 0, -1, 0); }
/// <summary> /// <para>Return a debug.traceback() call result (a multi-line string, containing a full stack trace, including C calls.</para> /// <para>Note: it won't return anything unless the interpreter is in the middle of execution - that is, it only makes sense to call it from a method called from Lua, or during a coroutine yield.</para> /// </summary> public string GetDebugTraceback() { int oldTop = LuaLib.LuaGetTop(luaState); LuaLib.LuaGetGlobal(luaState, "debug"); // stack: debug LuaLib.LuaGetField(luaState, -1, "traceback"); // stack: debug,traceback LuaLib.LuaRemove(luaState, -2); // stack: traceback LuaLib.LuaPCall(luaState, 0, -1, 0); return(translator.PopValues(luaState, oldTop)[0] as string); }
/* * Calls the object as a function with the provided arguments and * casting returned values to the types in returnTypes before returning * them in an array */ internal object[] CallFunction(object function, object[] args, Type[] returnTypes) { int nArgs = 0; int oldTop = LuaLib.LuaGetTop(luaState); if (!LuaLib.LuaCheckStack(luaState, args.Length + 6)) { throw new LuaException("Lua stack overflow"); } LuaLib.LuaPushStdCallCFunction(luaState, DoTraceback); int funcTop = LuaLib.LuaGetTop(luaState); var debug_traceback_index = -1; translator.Push(luaState, function); debug_traceback_index -= 1; if (args != null) { nArgs = args.Length; for (int i = 0; i < args.Length; i++) { translator.Push(luaState, args[i]); debug_traceback_index -= 1; } } executing = true; try { int error = LuaLib.LuaPCall(luaState, nArgs, -1, debug_traceback_index); if (error != 0) { ThrowExceptionFromError(oldTop); } } finally { executing = false; } object[] values = returnTypes != null?translator.PopValues(luaState, funcTop, returnTypes) : translator.PopValues(luaState, funcTop); translator.PopValues(luaState, oldTop); return(values); }
public static void CheckStack(this Lua lua) { var luaState = lua.LuaState; var top = LuaLib.LuaGetTop(luaState); if (top == 0) return; LuaLib.LuaPushString(luaState, "Script stack leak!"); LuaLib.LuaInsert(luaState, 1); LuaLib.LuaPushNumber(luaState, top); LuaLib.LuaInsert(luaState, 1); LuaLib.LuaGetGlobal(luaState, "print"); LuaLib.LuaInsert(luaState, 1); LuaLib.LuaPCall(luaState, top + 2, 0, 0); LuaLib.LuaSetTop(luaState, 0); }
/* * Calls the object as a function with the provided arguments and * casting returned values to the types in returnTypes before returning * them in an array */ internal object[] CallFunction(object function, object[] args, Type[] returnTypes) { int nArgs = 0; int oldTop = LuaLib.LuaGetTop(luaState); if (!LuaLib.LuaCheckStack(luaState, args.Length + 6)) { throw new LuaException("Lua stack overflow"); } translator.Push(luaState, function); if (args != null) { nArgs = args.Length; for (int i = 0; i < args.Length; i++) { translator.Push(luaState, args [i]); } } executing = true; try { int errfunction = 0; if (UseTraceback) { errfunction = PushDebugTraceback(luaState, nArgs); oldTop++; } int error = LuaLib.LuaPCall(luaState, nArgs, -1, errfunction); if (error != 0) { ThrowExceptionFromError(oldTop); } } finally { executing = false; } return(returnTypes != null?translator.PopValues(luaState, oldTop, returnTypes) : translator.PopValues(luaState, oldTop)); }
/// <summary> /// Executes a Lua chunk and returns all the chunk's return values in an array. /// </summary> /// <param name = "chunk">Chunk to execute</param> /// <param name = "chunkName">Name to associate with the chunk. Defaults to "chunk".</param> /// <returns></returns> public object[] DoString(byte[] chunk, string chunkName = "chunk") { int oldTop = LuaLib.LuaGetTop(luaState); executing = true; if (LuaLib.LuaLLoadBuffer(luaState, chunk, chunkName) == 0) { int errfunction = 0; if (UseTraceback) { errfunction = PushDebugTraceback(luaState, 0); oldTop++; } try { if (LuaLib.LuaPCall(luaState, 0, -1, errfunction) == 0) { return(translator.PopValues(luaState, oldTop)); } else { ThrowExceptionFromError(oldTop); } } finally { executing = false; } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }