Wil*_*ice 4 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;
Run Code Online (Sandbox Code Playgroud)
输出:
stack top is 0
stack top is 1
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Wil*_*ice 12
啊哈,发现了问题.根据这个页面,在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))
Run Code Online (Sandbox Code Playgroud)