Porting to Lua 5.2, LUA_GLOBALSINDEX trouble

大城市里の小女人 提交于 2019-12-08 20:01:14

问题


In the code example: http://lua-users.org/wiki/SimplerCppBinding

There is the code:

lua_pushstring(L, T::className);
lua_pushvalue(L, methods);
lua_settable(L, LUA_GLOBALSINDEX);  //<--- LUA_GLOBALSINDEX removed in Lua 5.2

lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methods);
lua_settable(L, metatable); 

In Lua 5.2, LUA_GLOBALSINDEX no longer exists. Instead, it has lua_setglobal() and lua_getglobal().


Am I correct in thinking that:

lua_pushvalue(L, methods);
lua_setglobal(L, T::className);

...is the correct replacement for:

lua_pushstring(L, T::className);
lua_pushvalue(L, methods);
lua_settable(L, LUA_GLOBALSINDEX);

I'm too new to Lua to be sure, I haven't used it for 8 months. Looking at the documentation, I'm thinking this is correct, but would like verification.


回答1:


Instead of lua_settable(L,LUA_GLOBALSINDEX); use lua_setglobal(L,T::className);. That works in both Lua 5.1 and 5.2.



来源:https://stackoverflow.com/questions/9057943/porting-to-lua-5-2-lua-globalsindex-trouble

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!