从C调用Lua 5.2函数
发布时间:2020-12-16 05:03:35 所属栏目:百科 来源:网络整理
导读:我对Lua很新. 我一直在看一些如何从C调用Lua函数的示例代码,但示例代码使用5.1,我试图让它与5.2一起工作. 以下是我的评论中的示例代码: lua_State *luaState = luaL_newstate();luaopen_io(luaState);luaL_loadfile(luaState,"myLuaScript.lua");lua_pcall(
我对Lua很新.
我一直在看一些如何从C调用Lua函数的示例代码,但示例代码使用5.1,我试图让它与5.2一起工作. 以下是我的评论中的示例代码: lua_State *luaState = luaL_newstate(); luaopen_io(luaState); luaL_loadfile(luaState,"myLuaScript.lua"); lua_pcall(luaState,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); 我已经在5.2参考manuel(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); 解决方法
下面的代码应该适用于5.1和5.2.
lua_getglobal(luaState,"myLuaFunction"); lua_pcall(luaState,0); 但请确保测试luaL_loadfile和lua_pcall的返回码.使用luaL_dofile可能会更好. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |