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

后端 未结 1 1686
迷失自我
迷失自我 2021-02-05 16:03
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 cou

1条回答
  •  醉话见心
    2021-02-05 16:44

    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.

    0 讨论(0)
提交回复
热议问题