Lua/Porting notes

From Gentoo Wiki
< Lua
Jump to: navigation, search

This page shows some migration notes between major versions of Lua.

Functions and structures declared as deprecated can be used with USE-flag "deprecated" on specific Lua major release (5.1, 5.2 etc), but next release usually completely remove deprecated code. Avoid using the "deprecated" flag and try to migrate to the new API.

Lua 5.1

luaL_getn / luaL_setn

Compatibility define in luaconf.h: LUA_COMPAT_GETN

Replace luaL_getn with lua_objlen:

- luaL_getn(lua_state, i);
+ lua_objlen(lua_state, i);

luaL_setn completely dropped, is safe to remove it from code.


Replace lua_open with luaL_newstate:

- lua_open();
+ luaL_newstate();


Compatibility define in luaconf.h: LUA_COMPAT_OPENLIB

Lua manual says that this function should be replaced with luaL_register, which is deprecated in Lua 5.2 too. For universal approach it's better "backport" luaL_setfuncs and use it as substitution for luaL_openlib and luaL_register:

// Place it somewhere in code
#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
** Adapted from Lua 5.2.0
static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
  luaL_checkstack(L, nup+1, "too many upvalues");
  for (; l->name != NULL; l++) {  /* fill the table with given functions */
    int i;
    lua_pushstring(L, l->name);
    for (i = 0; i < nup; i++)  /* copy upvalues to the top */
      lua_pushvalue(L, -(nup+1));
    lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
    lua_settable(L, -(nup + 3));
  lua_pop(L, nup);  /* remove upvalues */

Calls to luaL_openlib and luaL_register should be changed according to its second argument. If second argument is NULL, migration is simple:

- luaL_openlib(L, NULL, lreg, x);
+ luaL_setfuncs(L, lreg, x);

- luaL_register(L, NULL, lreg);
+ luaL_setfuncs(L, lreg, 0);

Calls such as luaL_openlib(L, name, lreg, x) and luaL_register(L, name, lreg) should be carefully rewritten because a global table with the given name will be searched and possibly created. When possible, it should be rewritten to luaL_setfuncs(L, lreg, 0).

Lua 5.2

lua_strlen / lua_objlen

Replace them with lua_rawlen:

- lua_strlen(L, i);
+ lua_rawlen(L, i);

- lua_objlen(L, i);
+ lua_rawlen(L, i);

lua_equal / lua_lessthan

Replace them with appropriate lua_compare calling:

- lua_equal(L, idx1, idx2);
+ lua_compare(L, idx1, idx2, LUA_OPEQ);

- lua_lessthan(L, idx1, idx2);
+ lua_compare(L, idx1, idx2, LUA_OPLT);


Compatibility define in luaconf.h: LUA_COMPAT_MODULE

See #luaL_openlib

External resources