/// <summary> /// Calls a function. /// To call a function you must use the following protocol: first, the function to be called is pushed onto the stack; then, /// the arguments to the function are pushed in direct order; that is, the first argument is pushed first. Finally you call /// lua_call; nargs is the number of arguments that you pushed onto the stack. All arguments and the function value are /// popped from the stack when the function is called. The function results are pushed onto the stack when the function /// returns. The number of results is adjusted to nresults, unless nresults is LUA_MULTRET. In this case, all results from /// the function are pushed. Lua takes care that the returned values fit into the stack space. The function results are /// pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on /// the top of the stack. /// </summary> /// <param name="L">The LuaState</param> /// <param name="nargs">The number of arguments.</param> /// <param name="nresults">The number of expected results.</param> /// <exception cref="System.NotImplementedException"></exception> protected static void LuaCall(LuaState L, lua_Integer nargs, lua_Integer nresults = LUA_MULTRET) { DynValue[] args = L.GetTopArray(nargs); L.Discard(nargs); DynValue func = L.Pop(); DynValue ret = L.ExecutionContext.Call(func, args); if (nresults != 0) { if (nresults == -1) { nresults = (ret.Type == DataType.Tuple) ? ret.Tuple.Length : 1; } DynValue[] vals = (ret.Type == DataType.Tuple) ? ret.Tuple : new DynValue[1] { ret }; int copied = 0; for (int i = 0; i < vals.Length && copied < nresults; i++, copied++) { L.Push(vals[i]); } while (copied < nresults) { L.Push(DynValue.Nil); } } }
protected static void LuaPop(LuaState L, lua_Integer p) { for (int i = 0; i < p; i++) { L.Pop(); } }
protected static void LuaPop(LuaState L, int p) { for (int i = 0; i < p; i++) { L.Pop(); } }
protected static void LuaGetTable(LuaState L, lua_Integer p) { // DEBT: this should call metamethods, now it performs raw access DynValue key = L.Pop(); DynValue table = L.At(p); if (table.Type != DataType.Table) { throw new NotImplementedException(); } var v = table.Table.Get(key); L.Push(v); }
protected static void LuaGetTable(LuaState L, lua_Integer p) { // DEBT: this should call metamethods, now it performs raw access DynValue key = L.Pop(); DynValue table = L.At(p); if (table.Type != DataType.Table) throw new NotImplementedException(); var v = table.Table.Get(key); L.Push(v); }
protected static void LuaPop(LuaState L, lua_Integer p) { for (int i = 0; i < p; i++) L.Pop(); }
protected static void LuaPop(LuaState L, int p) { for (var i = 0; i < p; i++) L.Pop(); }