// Export native function to lua. // - mouguangyi 2016.07.28 public void RegLuaBridgeFunction(LuaBridgeFunction function) { var executer = new LuaExecuter(this, function); LuaLib.LuaPushNumber(luaState, executer.Index); LuaLib.LuaPushCClosure(luaState, LuaExecuter._Execute, 1); LuaLib.LuaSetGlobal(luaState, executer.Name); }
private object _GetObject(string[] remainingPath) { object returnValue = null; for (int i = 0; i < remainingPath.Length; i++) { LuaLib.LuaPushString(luaState, remainingPath[i]); LuaLib.LuaGetTable(luaState, -2); returnValue = LuaExecuter._ParseLuaValue(luaState, -1); if (returnValue == null) { break; } } return(returnValue); }
public Dictionary <object, object> ToMap() { var map = new Dictionary <object, object>(); int oldTop = LuaLib.LuaGetTop(luaState); try { LuaLib.LuaGetRef(this.luaState, this.Reference); LuaLib.LuaPushNil(this.luaState); while (0 != LuaLib.LuaNext(this.luaState, -2)) { map[LuaExecuter._ParseLuaValue(this.luaState, -2)] = LuaExecuter._ParseLuaValue(this.luaState, -1); LuaLib.LuaSetTop(this.luaState, -2); } } finally { LuaLib.LuaSetTop(this.luaState, oldTop); } return(map); }
public object[] ToArray() { var list = new List <object>(); int oldTop = LuaLib.LuaGetTop(luaState); try { LuaLib.LuaGetRef(this.luaState, this.Reference); LuaLib.LuaPushNil(this.luaState); while (0 != LuaLib.LuaNext(this.luaState, -2)) { list.Add(LuaExecuter._ParseLuaValue(this.luaState, -1)); LuaLib.LuaSetTop(this.luaState, -2); } } finally { LuaLib.LuaSetTop(this.luaState, oldTop); } return(list.ToArray()); }
private object[] _PopValues(LuaState luaState, int oldTop) { int newTop = LuaLib.LuaGetTop(luaState); if (oldTop == newTop) { return(null); } else { var returnValues = new List <object>(); for (int i = oldTop + 1; i <= newTop; i++) { returnValues.Add(LuaExecuter._ParseLuaValue(luaState, i)); } LuaLib.LuaSetTop(luaState, oldTop); return(returnValues.ToArray()); } }
private static void _ThrowExceptionFromError(LuaState luaState, int oldTop) { object err = LuaExecuter._ParseLuaValue(luaState, -1); LuaLib.LuaSetTop(luaState, oldTop); // A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved) var luaEx = err as Exception; if (null != luaEx) { throw luaEx; } // A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it if (null == err) { err = "Unknown Lua Error"; } throw new Exception(err.ToString()); }
public object[] Call(object function, params object[] args) { int oldTop = LuaLib.LuaGetTop(this.luaState); if (function is string) { var fullPath = function as string; var path = fullPath.Split('.'); LuaLib.LuaGetGlobal(this.luaState, path[0]); function = LuaExecuter._ParseLuaValue(luaState, -1); if (path.Length > 1) { var remainingPath = new string[path.Length - 1]; Array.Copy(path, 1, remainingPath, 0, path.Length - 1); function = _GetObject(remainingPath); } } LuaExecuter._PushObject(this.luaState, function); if (null != args) { for (int i = 0; i < args.Length; i++) { LuaExecuter._PushObject(this.luaState, args[i]); } } try { int error = LuaLib.LuaPCall(this.luaState, args.Length, -1, 0); if (error != 0) { _ThrowExceptionFromError(this.luaState, oldTop); } } catch (Exception e) { Logger <ILuaRuntime> .X(e); } return(_PopValues(this.luaState, oldTop)); }