c++ - luaL_dostring 什么都不放在堆栈上?

标签 c++ lua lua-c++-connection

我正在尝试学习将 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/

相关文章:

c++ - 使用递归的 C++ 中的数字模式函数

c++ - 琐碎的 Spirit Parser 语法的段错误

c++ - 返回字符串 vector 的最简单的 lua 函数

lua - lua_rotate 是做什么的?

c++ - 如何从堆中删除变量?

c++ - 谷歌微基准测试 cpu 缩放警告

lua - 对于循环不完全正确的 Lua

lua - 捕获对 Lua 中不存在函数的调用

c++ - 为什么从 C++ 应用程序调用 luazip 时不提取文件?

c++ - Lua,游戏状态和游戏循环