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