简体   繁体   English

luaL_dostring什么都不放在堆栈上?

[英]luaL_dostring puts nothing on the stack?

I'm trying to learn the basics of interfacing Lua with C++, but I've run into a problem. 我正在尝试学习Lua与C ++接口的基础知识,但我遇到了一个问题。 I want to call a function that returns a string, and then work with the string on the C++ side, but luaL_dostring seems to put nothing on the Lua stack. 我想调用一个返回字符串的函数,然后使用C ++端的字符串,但是luaL_dostring似乎没有在Lua堆栈上放任何东西。

Even a simple test doesn't seem to work properly: 即使是简单的测试似乎也不能正常工作:

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;

Output: 输出:

stack top is 0
stack top is 1

Any ideas? 有任何想法吗?

Aha, found the problem. 啊哈,发现了问题。 According to this page , in Lua 5.1 luaL_dostring ignores returns. 根据这个页面 ,在Lua 5.1中luaL_dostring忽略了返回。 The code I had would probably work in Lua 5.2. 我的代码可能适用于Lua 5.2。

To alter the functionality, you should use: 要更改功能,您应该使用:

#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