This is almost的副本这个问题 https://stackoverflow.com/questions/12528820/lual-dostring-puts-nothing-on-the-stack/;但是,答案表明并不能解决我的问题,而且我没有使用luaL_dostring()
直接宏(虽然我am使用它扩展成的同一对调用)。鉴于这个程序:
#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在堆栈上留下任何结果吗?运行上述程序的输出是:
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...