繁体   English   中英

luaL_dostring什么都不放在堆栈上?

[英]luaL_dostring puts nothing on the stack?

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

有任何想法吗?

啊哈,发现了问题。 根据这个页面 ,在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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM