How can I know return value count of a Lua function from C?

给你一囗甜甜゛ 提交于 2019-12-03 11:48:53

问题


luaL_loadstring(L, "return 3, 4, 5");
int R       =   lua_pcall(L, 0, 3, 0);

Lua can return multiple values. But currently I have to hardcode the count of the return values. Can I know the count at runtime dynamically?


回答1:


Yes.

int top = lua_gettop(L);
luaL_loadstring(L, "return 3, 4, 5");
int R = lua_pcall(L, 0, LUA_MULTRET, 0);
int nresults = lua_gettop(L) - top;

You use LUA_MULTRET, and then use lua_gettop to figure out the top of the stack before and after the call.



来源:https://stackoverflow.com/questions/6434610/how-can-i-know-return-value-count-of-a-lua-function-from-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!