From ac959fed0082cb253d45c7a04866e8654e962442 Mon Sep 17 00:00:00 2001 From: Anders F Bjorklund Date: Wed, 12 Sep 2012 23:26:38 +0200 Subject: Add lua 5.2 support. Add compatibility support for both lua-5.1 and lua-5.2, assuming that the LUA_COMPAT might have been disabled. Signed-off-by: Panu Matilainen --- lib/rpmliblua.c | 14 ++++++++++++-- luaext/lposix.c | 10 +++++++--- luaext/lrexlib.c | 4 ++-- rpmio/rpmlua.c | 40 +++++++++++++++++++++++++++++++++------- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/rpmliblua.c b/lib/rpmliblua.c index 046ed3180..c43679a4b 100644 --- a/lib/rpmliblua.c +++ b/lib/rpmliblua.c @@ -23,16 +23,26 @@ static int rpm_vercmp(lua_State *L) return rc; } -static const luaL_reg luarpmlib_f[] = { +static const luaL_Reg luarpmlib_f[] = { {"vercmp", rpm_vercmp}, {NULL, NULL} }; +#ifndef lua_pushglobaltable +#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#endif + void rpmLuaInit(void) { rpmlua lua = rpmluaGetGlobalState(); - lua_pushvalue(lua->L, LUA_GLOBALSINDEX); + lua_pushglobaltable(lua->L); +#if (LUA_VERSION_NUM < 502) || defined(LUA_COMPAT_MODULE) luaL_register(lua->L, "rpm", luarpmlib_f); +#else + luaL_pushmodule(lua->L, "rpm", 1); + lua_insert(lua->L, -1); + luaL_setfuncs(lua->L, luarpmlib_f, 0); +#endif return; } diff --git a/luaext/lposix.c b/luaext/lposix.c index 3b251571d..f3c787eab 100644 --- a/luaext/lposix.c +++ b/luaext/lposix.c @@ -810,7 +810,7 @@ static int Pmkstemp(lua_State *L) return 2; } -static const luaL_reg R[] = +static const luaL_Reg R[] = { {"access", Paccess}, {"chdir", Pchdir}, @@ -874,15 +874,19 @@ static int exit_override(lua_State *L) exit(luaL_optint(L, 1, EXIT_SUCCESS)); } -static const luaL_reg os_overrides[] = +static const luaL_Reg os_overrides[] = { {"exit", exit_override}, {NULL, NULL} }; +#ifndef lua_pushglobaltable +#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#endif + int luaopen_rpm_os(lua_State *L) { - lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_pushglobaltable(L); luaL_openlib(L, "os", os_overrides, 0); return 0; } diff --git a/luaext/lrexlib.c b/luaext/lrexlib.c index 81931c0a6..9da5c8254 100644 --- a/luaext/lrexlib.c +++ b/luaext/lrexlib.c @@ -169,7 +169,7 @@ static int rex_gc (lua_State *L) return 0; } -static const luaL_reg rexmeta[] = { +static const luaL_Reg rexmeta[] = { {"match", rex_match}, {"gmatch", rex_gmatch}, {"__gc", rex_gc}, @@ -305,7 +305,7 @@ static const luaL_reg pcremeta[] = { /* Open the library */ -static const luaL_reg rexlib[] = { +static const luaL_Reg rexlib[] = { #ifdef WITH_POSIX {"newPOSIX", rex_comp}, #endif diff --git a/rpmio/rpmlua.c b/rpmio/rpmlua.c index 319c0d020..86d0408ed 100644 --- a/rpmio/rpmlua.c +++ b/rpmio/rpmlua.c @@ -7,6 +7,18 @@ #include #include +#ifndef lua_open +#define lua_open() luaL_newstate() +#endif + +#ifndef lua_strlen +#define lua_strlen(L,i) lua_rawlen(L, (i)) +#endif + +#ifndef lua_pushglobaltable +#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#endif + #include #include @@ -53,10 +65,10 @@ rpmlua rpmluaNew() { rpmlua lua = (rpmlua) xcalloc(1, sizeof(*lua)); struct stat st; - const luaL_reg *lib; + const luaL_Reg *lib; char *initlua = rpmGenPath(rpmConfigDir(), "init.lua", NULL); - static const luaL_reg extlibs[] = { + static const luaL_Reg extlibs[] = { {"posix", luaopen_posix}, {"rex", luaopen_rex}, {"rpm", luaopen_rpm}, @@ -74,12 +86,26 @@ rpmlua rpmluaNew() lua_call(L, 1, 0); lua_settop(L, 0); } +#ifndef LUA_GLOBALSINDEX + lua_pushglobaltable(L); +#endif lua_pushliteral(L, "LUA_PATH"); lua_pushfstring(L, "%s/%s", rpmConfigDir(), "/lua/?.lua"); +#ifdef LUA_GLOBALSINDEX lua_rawset(L, LUA_GLOBALSINDEX); +#else + lua_settable(L, -3); +#endif lua_pushliteral(L, "print"); lua_pushcfunction(L, rpm_print); +#ifdef LUA_GLOBALSINDEX lua_rawset(L, LUA_GLOBALSINDEX); +#else + lua_settable(L, -3); +#endif +#ifndef LUA_GLOBALSINDEX + lua_pop(L, 1); +#endif rpmluaSetData(lua, "lua", lua); if (stat(initlua, &st) != -1) (void)rpmluaRunScriptFile(lua, initlua); @@ -191,7 +217,7 @@ void rpmluaSetVar(rpmlua _lua, rpmluav var) } if (!var->listmode || lua->pushsize > 0) { if (lua->pushsize == 0) - lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_pushglobaltable(L); if (pushvar(L, var->keyType, &var->key) != -1) { if (pushvar(L, var->valueType, &var->value) != -1) lua_rawset(L, -3); @@ -228,7 +254,7 @@ void rpmluaGetVar(rpmlua _lua, rpmluav var) lua_State *L = lua->L; if (!var->listmode) { if (lua->pushsize == 0) - lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_pushglobaltable(L); if (pushvar(L, var->keyType, &var->key) != -1) { lua_rawget(L, -2); popvar(L, &var->valueType, &var->value); @@ -261,7 +287,7 @@ static int findkey(lua_State *L, int oper, const char *key, va_list va) vsnprintf(buf, blen + 1, key, va); s = e = buf; - lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_pushglobaltable(L); for (;;) { if (*e == '\0' || *e == '.') { if (e != s) { @@ -822,7 +848,7 @@ static int rpm_print (lua_State *L) return 0; } -static const luaL_reg rpmlib[] = { +static const luaL_Reg rpmlib[] = { {"b64encode", rpm_b64encode}, {"b64decode", rpm_b64decode}, {"expand", rpm_expand}, @@ -836,7 +862,7 @@ static const luaL_reg rpmlib[] = { static int luaopen_rpm(lua_State *L) { - lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_pushglobaltable(L); luaL_openlib(L, "rpm", rpmlib, 0); return 0; } -- cgit v1.2.3