Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 617
eonil Avatar asked Jun 22 '11 03:06

eonil


1 Answers

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.

like image 66
John Calsbeek Avatar answered Oct 21 '22 03:10

John Calsbeek