【发布时间】:2012-09-13 18:36:30
【问题描述】:
我正在尝试学习 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 环境中,那么你最好将它和它的参数直接推入堆栈并通过 lua_call() 调用它。
标签: c++ lua lua-c++-connection