【发布时间】:2013-02-19 19:26:33
【问题描述】:
我对 Lua 很陌生。 我一直在查看一些如何从 C++ 调用 Lua 函数的示例代码,但示例代码使用的是 5.1,我正在尝试让它与 5.2 一起使用。
这是我的 cmets 有问题的示例代码:
lua_State *luaState = luaL_newstate();
luaopen_io(luaState);
luaL_loadfile(luaState, "myLuaScript.lua");
lua_pcall(luaState, 0, LUA_MULTRET, 0);
//the code below needs to be rewritten i suppose
lua_pushstring(luaState, "myLuaFunction");
//the line of code below does not work in 5.2
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pcall(luaState, 0, 0, 0);
我在 5.2 参考手册 (http://www.lua.org/manual/5.2/manual.html#8.3) 中读到需要从注册表中获取全局环境(而不是上面的 lua_gettable 语句),但我无法确定需要对哪些更改进行更改让这个工作。例如,我已经尝试过:
lua_pushglobaltable(luaState);
lua_pushstring(luaState, "myLuaFunction");
lua_gettable(luaState, -2);
lua_pcall(luaState, 0, 0, 0);
【问题讨论】: