lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, May 8, 2013 at 11:55 AM, Are Leistad <aleistad@telia.com> wrote:
I shall go with the flow for new projects. Oh, and sorry for the typos, I
was knackered, after many hours of Lua'ing. It was too much fun to sleep...
 
Oh we all know that!  I wouldn't worry that much about module(), it isn't intrinsically evil (but using package.seeall will not gain you friends among those who want to use your code in a sandbox!)

Most of the differences between 5.1 and 5.2 are made easier by the fact that 5.2 is usually compiled with LUA_COMPAT_ALL.  So these things are already defined:

#define lua_strlen(L,i)        lua_rawlen(L, (i))
#define lua_objlen(L,i)        lua_rawlen(L, (i))
#define lua_equal(L,idx1,idx2)        lua_compare(L,(idx1),(idx2),LUA_OPEQ)
#define lua_lessthan(L,idx1,idx2)    lua_compare(L,(idx1),(idx2),LUA_OPLT)

So (unless you make use of function environments) mostly it boils down to the module entry point, and the difference can be easily #ifdefed:

EXPORT int luaopen_mylib (lua_State *L) {
#if LUA_VERSION_NUM > 501
    lua_newtable(L);
    luaL_setfuncs (L,mylib_funs,0);
    lua_pushvalue(L,-1);        // pluck these lines out if they offend you
    lua_setglobal(L,"mylib"); // for they clobber the Holy _G
#else
    luaL_register(L,"mylib",mylib_funs);
#endif
    return 1;
}

Another issue that a porter will find with historical code is that it may contain Lua 5.0-isms that have finally been end-of-lifed.

For instance, luaL_reg is now spelt luaL_Reg, for both 5.1/5.2.  Some of the buffer macros likewise.

steve d.