LUA_MULTRET无法正常工作 [英] LUA_MULTRET not working as expected

查看:233
本文介绍了LUA_MULTRET无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎是

This is almost a duplicate of this question; however, the answer suggested there doesn't fix my problem, and I'm not using the luaL_dostring() macro directly (though I am using the same pair of calls that it expands to). Given this program:

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <lua.hpp>

static int _foo(lua_State* L)
{
    lua_pushinteger(L, 1);
    lua_pushinteger(L, 2);
    lua_pushinteger(L, 3);
    printf("In foo(): pushed %d elements...\n", lua_gettop(L));
    return 3;
}

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    lua_pushcfunction(L, _foo);
    lua_setglobal(L, "foo");

    // This leaves three results on the stack...
    lua_pushcfunction(L, _foo);
    lua_pcall(L, 0, LUA_MULTRET, 0);
    int nresults = lua_gettop(L);
    printf("After foo(): %d results left on the stack...\n", nresults);
    lua_settop(L, 0);

    // ... and so does this.
    luaL_loadstring(L, "foo()");
    lua_pcall(L, 0, 3, 0);
    nresults = lua_gettop(L);
    printf("After foo(): %d results left on the stack...\n", nresults);
    lua_settop(L, 0);

    // But this does NOT. Why?
    luaL_loadstring(L, "foo()");
    lua_pcall(L, 0, LUA_MULTRET, 0);
    nresults = lua_gettop(L);
    printf("After foo(): %d results left on the stack...\n", nresults);
    return 0;
}

为什么最后一次调用lua_pcall(L, 0, LUA_MULTRET, 0) not 不会在堆栈上留下任何结果?运行上述程序的输出为:

Why does the last call to lua_pcall(L, 0, LUA_MULTRET, 0) not leave any results on the stack? The output from running the above program is:

In foo(): pushed 3 elements...
After foo(): 3 results left on the stack...
In foo(): pushed 3 elements...
After foo(): 3 results left on the stack...
In foo(): pushed 3 elements...
After foo(): 0 results left on the stack...

我正在使用Lua 5.1.5 ...

I'm using Lua 5.1.5...

推荐答案

在第一次调用中,您将C函数foo推入了堆栈,然后对其进行了调用.但是luaL_loadstring创建了一个块,因此在第2次和第3次调用中,您先推送一个块然后调用它,但是该块不返回任何内容,该块仅调用foo().因此

In the first call you are pushing the C function foo onto the stack then calling it. But luaL_loadstring creates a chunk so in the 2nd and 3rd calls you are pushing a chunk then calling it, but the chunk does not return anything, the chunk just calls foo(). Hence

lua_pcall(L, 0, 3, 0);

在Lua堆栈上创建3个nil,因为Lua确保您要求的3个值在那里,即使块未返回任何值.还有

creates 3 nils on the Lua stack because Lua makes sure that the 3 values you asked are there, even if chunk returned none. Also

lua_pcall(L, 0, LUA_MULTRET, 0);

不返回任何内容,因为该块不返回任何内容.

returns nothing because the chunk returned nothing.

如果要从Lua执行foo,请将foo全局变量放在堆栈中:

If you want to execute foo from Lua, put the foo global variable on the stack:

lua_getglobal(L, "foo");
lua_pcall(L, 0, LUA_MULTRET, 0);

或者,使块返回foo()返回的值:

Alternately, make the chunk return what foo() returns:

luaL_loadstring(L, "return foo()");
lua_pcall(L, 0, LUA_MULTRET, 0);

这篇关于LUA_MULTRET无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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