/// <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 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 LuaPushValue(LuaState L, lua_Integer arg) { DynValue v = L.At(arg); 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 LuaPushNil(LuaState L) { L.Push(DynValue.Nil); }
protected static void LuaPushLString(LuaState L, CharPtr s, uint len) { string ss = s.ToString((int)len); L.Push(DynValue.NewString(ss)); }
protected static void LuaPushLiteral(LuaState L, string literalString) { L.Push(DynValue.NewString(literalString)); }
protected static void LuaPushInteger(LuaState L, lua_Integer val) { L.Push(DynValue.NewNumber(val)); }
protected static void LuaPushValue(LuaState L, int arg) { var v = L.At(arg); L.Push(v); }
public static int str_gmatch(LuaState L) { var C = new CallbackFunction(gmatch_aux_2, "gmatch"); var s = ArgAsType(L, 1, DataType.String, false).String; var p = PatchPattern(ArgAsType(L, 2, DataType.String, false).String); C.AdditionalData = new GMatchAuxData { S = new CharPtr(s), P = new CharPtr(p), LS = (uint) s.Length, POS = 0 }; L.Push(DynValue.NewCallback(C)); return 1; }