Porting to Lua 5.2, LUA_GLOBALSINDEX problem

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

There is a 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 ().


I understand correctly that:

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

... is the right replacement for:

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

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

+6
source share
1 answer

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

+4
source

Source: https://habr.com/ru/post/907185/


All Articles