有没有不删除完整堆栈的 lua-c api 函数?
Is there a lua-c api function which doesn't remove the complete stack?
我的问题是 lua_pcall 清除了堆栈,因为我想在再次调用之前重用堆栈,只需再次更改一次。
有没有一种方法可以复制完整的堆栈并再次粘贴,甚至可以在不清除堆栈的情况下调用 lua 函数?
Lua:
function test(a)
print(a)
end
event.add("test", test)
event.add("test", test)
event.call("test", 42)
C++:
int Func_Event_call(Lua_State* L){
//Stack: String:Eventname, Arguments...
std::string name = luaL_checkstring(L, 1);
... Get array functions from name
for(...){
//load function into stack
lua_rawgeti(L, LUA_REGISTRYINDEX, functions[c]);
lua_replace(L, 1);
//Wanted Stack: Lua_Function, Arguments... <- works for the first function
//call function
lua_pcall(L, nummberArgs, 0, 0);
//stack is now empty because of lua_pcall <- problem
}
return 0;
}
//store stack in Registry
lua_createtable(L, nummberArgs, nummberArgs);
lua_insert(L, 1);
//fill table
for (int c = 0; c < nummberArgs; c++) {
lua_pushinteger(L, c);
lua_insert(L, -2);
lua_settable(L, 1);
}
//store it in Registry and clean up
int reg_key = luaL_ref(L, LUA_REGISTRYINDEX);
lua_remove(L, 1);
...
//restore stack from above
lua_rawgeti(L, LUA_REGISTRYINDEX, reg_key);
//iterate over the table to get all elements, the key is getting removed by lua_next
lua_pushnil(L);
while (lua_next(L, 1) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
/* move 'value'; keeps 'key' for next iteration */
lua_insert(L, -2);
}
//remove table from stack
lua_remove(L, 1);
我通过在注册表中使用 table 解决了这个问题,并将完整的堆栈放入带有数字键的 table 中。调用第二部分后,我可以返回调用第一部分时的堆栈,但是多次。
我的问题是 lua_pcall 清除了堆栈,因为我想在再次调用之前重用堆栈,只需再次更改一次。
有没有一种方法可以复制完整的堆栈并再次粘贴,甚至可以在不清除堆栈的情况下调用 lua 函数?
Lua:
function test(a)
print(a)
end
event.add("test", test)
event.add("test", test)
event.call("test", 42)
C++:
int Func_Event_call(Lua_State* L){
//Stack: String:Eventname, Arguments...
std::string name = luaL_checkstring(L, 1);
... Get array functions from name
for(...){
//load function into stack
lua_rawgeti(L, LUA_REGISTRYINDEX, functions[c]);
lua_replace(L, 1);
//Wanted Stack: Lua_Function, Arguments... <- works for the first function
//call function
lua_pcall(L, nummberArgs, 0, 0);
//stack is now empty because of lua_pcall <- problem
}
return 0;
}
//store stack in Registry
lua_createtable(L, nummberArgs, nummberArgs);
lua_insert(L, 1);
//fill table
for (int c = 0; c < nummberArgs; c++) {
lua_pushinteger(L, c);
lua_insert(L, -2);
lua_settable(L, 1);
}
//store it in Registry and clean up
int reg_key = luaL_ref(L, LUA_REGISTRYINDEX);
lua_remove(L, 1);
...
//restore stack from above
lua_rawgeti(L, LUA_REGISTRYINDEX, reg_key);
//iterate over the table to get all elements, the key is getting removed by lua_next
lua_pushnil(L);
while (lua_next(L, 1) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
/* move 'value'; keeps 'key' for next iteration */
lua_insert(L, -2);
}
//remove table from stack
lua_remove(L, 1);
我通过在注册表中使用 table 解决了这个问题,并将完整的堆栈放入带有数字键的 table 中。调用第二部分后,我可以返回调用第一部分时的堆栈,但是多次。