Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LUA_MULTRET not working as expected

Tags:

lua

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;
}

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...

I'm using Lua 5.1.5...

like image 884
evadeflow Avatar asked Dec 29 '13 16:12

evadeflow


1 Answers

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);

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.

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);

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

luaL_loadstring(L, "return foo()");
lua_pcall(L, 0, LUA_MULTRET, 0);
like image 170
Oliver Avatar answered Nov 06 '22 08:11

Oliver