我正在尝试学习将 Lua 与 C++ 交互的基础知识,但我遇到了一个问题。 我想调用一个返回字符串的函数,然后在 C++ 端处理该字符串,但 luaL_dostring 似乎没有在 Lua 堆栈上放任何东西。
即使是简单的测试似乎也无法正常工作:
lua_State* lua = lua_open();
luaL_openlibs(lua);
//Test dostring.
luaL_dostring(lua, "return 'derp'");
int top = lua_gettop(lua);
cout << "stack top is " <<top << endl;
//Next, test pushstring.
lua_pushstring(lua, "derp");
top = lua_gettop(lua);
cout << "stack top is " << top << endl;
输出:
stack top is 0
stack top is 1
有什么想法吗?
最佳答案
啊哈,发现问题了。根据this page , 在 Lua 5.1 中 luaL_dostring 忽略返回值。我的代码可能适用于 Lua 5.2。
要改变功能,您应该使用:
#undef luaL_dostring
#define luaL_dostring(L,s) \
(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
关于c++ - luaL_dostring 什么都不放在堆栈上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12528820/