如何知道 C 中 Lua 函数的返回值计数? [英] How can I know return value count of a Lua function from C?

查看:22
本文介绍了如何知道 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天全站免登陆