public object[] Call(params object[] args) { IntPtr L = _LuaState.L; int oldTop = LuaLib.lua_gettop(L); LuaLib.lua_rawgeti(L, LuaIndices.LUA_REGISTRYINDEX, _RefId); // |f if (args != null) { for (int i = 0; i < args.Length; ++i) { _LuaState.PushObject(args[i]); // |f|a1|a2|... } } if (LuaLib.lua_pcall(L, args.Length, LuaLib.LUA_MULTRET, 0) != 0) // | or |ret1|ret2|... or |err { string error = LuaLib.lua_tostring(L, -1); LuaLib.lua_settop(L, oldTop); // | throw new LuaException(error); } object[] returnObjects = null; int currentTop = LuaLib.lua_gettop(L); if (currentTop != oldTop) { var objects = new List <object>(); for (int i = oldTop + 1; i <= currentTop; ++i) { objects.Add(_LuaState.ToObject(i)); } returnObjects = objects.ToArray(); LuaLib.lua_settop(L, oldTop); // | } return(returnObjects); }
// ------------------------------------------------------------------------------------------------------------- // Public interfaces // ------------------------------------------------------------------------------------------------------------- public object[] DoBuffer(byte[] chunk, string chunkName = "chunk") { int oldTop = LuaLib.lua_gettop(L); if (LuaLib.luaL_loadbuffer(L, chunk, chunk.Length, chunkName) != 0) // |f or |err { string error = LuaLib.lua_tostring(L, -1); // |err LuaLib.lua_settop(L, oldTop); // | throw new LuaException(error); } if (LuaLib.lua_pcall(L, 0, LuaLib.LUA_MULTRET, 0) != 0) // | or |ret1|ret2 or |err { string error = LuaLib.lua_tostring(L, -1); // |err LuaLib.lua_settop(L, oldTop); // | throw new LuaException(error); } int top = LuaLib.lua_gettop(L); if (top == oldTop) { return(null); } var objects = new List <object>(); for (int i = oldTop + 1; i <= top; ++i) { objects.Add(ToObject(i)); } LuaLib.lua_pop(L, top - oldTop); return(objects.ToArray()); }