我怎么能从C知道Lua函数的返回值计数? [英] How can I know return value count of a Lua function from C?

查看:179
本文介绍了我怎么能从C知道Lua函数的返回值计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Lua可以返回多个值.但是目前,我必须对返回值的计数进行硬编码.我可以动态地在运行时知道计数吗?

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?

推荐答案

是.

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;

使用LUA_MULTRET,然后使用lua_gettop找出调用前后的堆栈顶部.

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

这篇关于我怎么能从C知道Lua函数的返回值计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆