这几乎是这个问题的重复; 然而,答案建议有没有解决我的问题,我不使用luaL_dostring()直接宏(虽然我正在使用相同的一对调用它扩展到).鉴于此计划:
#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);
// …Run Code Online (Sandbox Code Playgroud) lua ×1