[英]How to get multiple return values from function in Lua C API?
I would like to know how to get multiple return values from function in Lua C API. 我想知道如何从Lua C API中的函数中获取多个返回值。
Lua Code: Lua代码:
function test(a, b)
return a, b -- I would like to get these values in C++
end
C++ Code: (part where it calls the function) C ++代码:(调用函数的部分)
/* push functions and arguments */
lua_getglobal(L, "test"); /* function to be called */
lua_pushnumber(L, 3); /* push 1st argument */
lua_pushnumber(L, 4); /* push 2nd argument */
/* call the function in Lua (2 arguments, 2 return) */
if (lua_pcall(L, 2, 2, 0) != 0)
{
printf(L, "error: %s\n", lua_tostring(L, -1));
return;
}
int ret1 = lua_tonumber(L, -1);
int ret2 = lua_tonumber(L, -1);
printf(L, "returned: %d %d\n", ret1, ret2);
The result I get: 结果我得到:
returned: 4 4
返回:4 4
The result I expect: 结果我期待:
returned: 3 4
返回:3 4
lua_tonumber
does not alter the lua_State
's stack. lua_tonumber
不会改变lua_State
的堆栈。 You need to read it at two different index 1 : 您需要在两个不同的索引1处读取它:
int ret1 = lua_tonumber(L, -2);
int ret2 = lua_tonumber(L, -1);
printf(L, "returned: %d %d\n", ret1, ret2);
Before you call test
, your stack looks like this: 在调用
test
之前,您的堆栈如下所示:
lua_getglobal(L, "test"); /* function to be called */
lua_pushnumber(L, 3); /* push 1st argument */
lua_pushnumber(L, 4); /* push 2nd argument */
| 4 | <--- 2
+-----------+
| 3 | <--- 1
+-----------+
| test | <--- 0
+===========+
After its call 2 : 在致电2之后 :
lua_pcall(L, 2, 2, 0)
+-----------+
| 3 | <--- -1
+-----------+
| 4 | <--- -2
+===========+
An alternative would be to manually pop the results after you've read it: 另一种方法是在阅读后手动弹出结果:
int ret1 = lua_tonumber(L, -1);
lua_pop(L, 1);
int ret2 = lua_tonumber(L, -1);
lua_pop(L, 1);
printf(L, "returned: %d %d\n", ret1, ret2);
1) " If a function returns multiple results, the first result is pushed first; so, if there are n
results, the first one will be at index -n
and the last at index -1
." 1) “ 如果函数返回多个结果,则首先推送第一个结果;因此,如果有
n
结果,则第一个结果将在索引-n
,而最后一个结果在索引-1
。” Programming in Lua : 25.2 Lua编程:25.2
2) " Before pushing the results, lua_pcall
removes from the stack the function and its arguments ." 2) “ 在推送结果之前,
lua_pcall
从lua_pcall
删除函数及其参数 。” Programming in Lua : 25.2 Lua编程:25.2
You are taking the same index twice: 您使用相同的索引两次:
int ret1 = lua_tonumber(L, -1);
int ret2 = lua_tonumber(L, -1);
Stack is filled like this: 堆栈填充如下:
-- Lua
return a, b
+---+
| b | <-- top ("relative" index -1)
+---+
| a | <-- -2
+---+
So your C++ code should be: 所以你的C ++代码应该是:
// I don't know what ret1 or ret2 suppose to be.
// 1 = first on stack, or first return value?
// renamed to a and b for consistency with lua return a,b
int b = lua_tonumber(L, -1);
int a = lua_tonumber(L, -2);
// lua_pop(L, 2); // don't forget to pop the values
From 24.2.3 – Other Stack Operations : 从24.2.3开始 - 其他堆栈操作 :
[...]The lua_gettop function returns the number of elements in the stack, which is also the index of the top element.
[...] lua_gettop函数返回堆栈中元素的数量,这也是顶部元素的索引。 Notice that a negative index -x is equivalent to the positive index gettop - x + 1. [...]
请注意,负索引-x等于正索引gettop - x + 1. [...]
This negative indeces positioning is valid for all Lua functions regarding the stack access, including lua_tonumber. 这个负的indeces定位对于有关堆栈访问的所有Lua函数都有效,包括lua_tonumber。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.