From c29df10f0ab47db63509634fb1661ff55b8b47b3 Mon Sep 17 00:00:00 2001 From: Roger Planas Date: Tue, 4 Jul 2017 12:03:55 +0100 Subject: Sluift: Add support for Lua 5.3 This patch adds support for Lua 5.3, while keeping support for Lua 5.1 (a build with the bundled Lua succeeds) Test-information: Builds cleanly on Lua 5.1, 5.2 and Lua 5.3. Tests that make use of Sluift work with both Lua 5.2 and Lua 5.3 Change-Id: I4cbe2eb09ec8c753d8624047f5456be1de72c679 diff --git a/Sluift/ElementConvertors/FormConvertor.cpp b/Sluift/ElementConvertors/FormConvertor.cpp index 90fd9fe..245e0f9 100644 --- a/Sluift/ElementConvertors/FormConvertor.cpp +++ b/Sluift/ElementConvertors/FormConvertor.cpp @@ -29,7 +29,7 @@ namespace { lua_pushnil(L); for (lua_pushnil(L); lua_next(L, index) != 0; ) { lua_getfield(L, -1, "name"); - if (lua_equal(L, -1, 2)) { + if (lua_compare(L, -1, 2, LUA_OPEQ)) { lua_pop(L, 1); return 1; } @@ -44,7 +44,7 @@ namespace { if (lua_type(L, -1) == LUA_TTABLE) { for (lua_pushnil(L); lua_next(L, -2) != 0; ) { lua_getfield(L, -1, "name"); - if (lua_equal(L, -1, 2)) { + if (lua_compare(L, -1, 2, LUA_OPEQ)) { lua_pushvalue(L, 3); lua_setfield(L, -3, "value"); foundField = true; diff --git a/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp b/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp index c6ba09e..8f8a660 100644 --- a/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp @@ -33,7 +33,7 @@ std::shared_ptr PubSubAffiliationsConvertor::doConvertFromLu lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp b/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp index ac86024..19ab09a 100644 --- a/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp @@ -39,7 +39,7 @@ std::shared_ptr PubSubEventItemConvertor::doConvertFromLua(lua_ lua_getfield(L, -1, "data"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp b/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp index 7a3cde1..66f798e 100644 --- a/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp @@ -34,7 +34,7 @@ std::shared_ptr PubSubEventItemsConvertor::doConvertFromLua(lu lua_getfield(L, -1, "items"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { @@ -51,7 +51,7 @@ std::shared_ptr PubSubEventItemsConvertor::doConvertFromLua(lu lua_getfield(L, -1, "retracts"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubItemConvertor.cpp b/Sluift/ElementConvertors/PubSubItemConvertor.cpp index 27fd4a3..b46419a 100644 --- a/Sluift/ElementConvertors/PubSubItemConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubItemConvertor.cpp @@ -29,7 +29,7 @@ std::shared_ptr PubSubItemConvertor::doConvertFromLua(lua_State* L) lua_getfield(L, -1, "data"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubItemsConvertor.cpp b/Sluift/ElementConvertors/PubSubItemsConvertor.cpp index 5fa1bd3..0bb8eb9 100644 --- a/Sluift/ElementConvertors/PubSubItemsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubItemsConvertor.cpp @@ -33,7 +33,7 @@ std::shared_ptr PubSubItemsConvertor::doConvertFromLua(lua_State* L lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp b/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp index ee8a8cb..9ca76fc 100644 --- a/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp @@ -33,7 +33,7 @@ std::shared_ptr PubSubOwnerAffiliationsConvertor::doCon lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp b/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp index 88085b5..3530efe 100644 --- a/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp @@ -33,7 +33,7 @@ std::shared_ptr PubSubOwnerSubscriptionsConvertor::doC lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubPublishConvertor.cpp b/Sluift/ElementConvertors/PubSubPublishConvertor.cpp index 63c97bc..d8a2742 100644 --- a/Sluift/ElementConvertors/PubSubPublishConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubPublishConvertor.cpp @@ -34,7 +34,7 @@ std::shared_ptr PubSubPublishConvertor::doConvertFromLua(lua_Stat lua_getfield(L, -1, "items"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubRetractConvertor.cpp b/Sluift/ElementConvertors/PubSubRetractConvertor.cpp index c070ad6..c27ad7d 100644 --- a/Sluift/ElementConvertors/PubSubRetractConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubRetractConvertor.cpp @@ -34,7 +34,7 @@ std::shared_ptr PubSubRetractConvertor::doConvertFromLua(lua_Stat lua_getfield(L, -1, "items"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp b/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp index 3712192..2325181 100644 --- a/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp @@ -33,7 +33,7 @@ std::shared_ptr PubSubSubscriptionsConvertor::doConvertFrom lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/ElementConvertors/SecurityLabelConvertor.cpp b/Sluift/ElementConvertors/SecurityLabelConvertor.cpp index 21d9a8f..8bc323a 100644 --- a/Sluift/ElementConvertors/SecurityLabelConvertor.cpp +++ b/Sluift/ElementConvertors/SecurityLabelConvertor.cpp @@ -26,7 +26,7 @@ std::shared_ptr SecurityLabelConvertor::doConvertFromLua(lua_Stat lua_getfield(L, -1, "equivalent_labels"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::string > items; - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (lua_isstring(L, -1)) { diff --git a/Sluift/ElementConvertors/StanzaConvertor.h b/Sluift/ElementConvertors/StanzaConvertor.h index bdaaad3..a7b4e4e 100644 --- a/Sluift/ElementConvertors/StanzaConvertor.h +++ b/Sluift/ElementConvertors/StanzaConvertor.h @@ -46,7 +46,7 @@ namespace Swift { lua_pop(L, 1); lua_getfield(L, -1, "payloads"); if (lua_type(L, -1) == LUA_TTABLE) { - for(size_t i = 0; i < lua_objlen(L, -1); ++i) { + for(size_t i = 0; i < lua_rawlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { diff --git a/Sluift/Lua/LuaUtils.h b/Sluift/Lua/LuaUtils.h index b87eb38..b9f5bff 100644 --- a/Sluift/Lua/LuaUtils.h +++ b/Sluift/Lua/LuaUtils.h @@ -12,7 +12,22 @@ #include #if LUA_VERSION_NUM < 502 -#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#define lua_compare(L,idx1,idx2,LUA_OPEQ) lua_equal(L,(idx1),(idx2)) +#define lua_rawlen(L, i) lua_objlen(L,(i)) +#endif +#if LUA_VERSION_NUM >= 503 +#undef luaL_register +#define luaL_register(L, n, l) \ + lua_getglobal( L, n ); \ + if( lua_isnil( L, -1 ) ) \ + { \ + lua_pop( L, 1 ); \ + lua_newtable( L ); \ + } \ + luaL_setfuncs( L, (l), 0 ); \ + lua_pushvalue( L, -1 ); \ + lua_setglobal( L, n ); #endif namespace Swift { diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 75f675d..ae2f610 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -701,7 +701,7 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( lua_getfield(L, 2, "groups"); if (!lua_isnil(L, -1)) { if (lua_type(L, -1) == LUA_TTABLE) { - for (size_t i = 1; i <= lua_objlen(L, -1); ++i) { + for (size_t i = 1; i <= lua_rawlen(L, -1); ++i) { lua_rawgeti(L, -1, boost::numeric_cast(i)); const char* rawGroup = lua_tostring(L, -1); if (rawGroup) { diff --git a/Sluift/core.lua b/Sluift/core.lua index daa8636..f35a603 100644 --- a/Sluift/core.lua +++ b/Sluift/core.lua @@ -6,7 +6,8 @@ local sluift = select(1, ...) local _G = _G -local pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, unpack, io = pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, unpack, io +local pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, io = pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, io +local unpack = table.unpack or unpack local setmetatable, getmetatable = setmetatable, getmetatable local string = require "string" local table = require "table" -- cgit v0.10.2-6-g49f6