From: Michael Tremer Date: Sun, 31 Mar 2024 14:16:14 +0000 (+0000) Subject: lua: Add compatibility function to compile with Lua >= 5.1 X-Git-Tag: 0.9.18~71 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=031bae06e70aef5449b0f0fb9650a4e97eb0e949;p=location%2Flibloc.git lua: Add compatibility function to compile with Lua >= 5.1 Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 65b3a76..9afae8d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -239,6 +239,7 @@ luadir = $(LUA_INSTALL_CMOD) src_lua_location_la_SOURCES = \ src/lua/as.c \ src/lua/as.h \ + src/lua/compat.h \ src/lua/country.c \ src/lua/country.h \ src/lua/database.c \ diff --git a/src/lua/as.c b/src/lua/as.c index d36f2d8..558fcbf 100644 --- a/src/lua/as.c +++ b/src/lua/as.c @@ -25,6 +25,7 @@ #include "location.h" #include "as.h" +#include "compat.h" typedef struct as { struct loc_as* as; diff --git a/src/lua/compat.h b/src/lua/compat.h new file mode 100644 index 0000000..f0172b8 --- /dev/null +++ b/src/lua/compat.h @@ -0,0 +1,56 @@ +/* + libloc - A library to determine the location of someone on the Internet + + Copyright (C) 2024 IPFire Development Team + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*/ + +#ifndef LUA_LOCATION_COMPAT_H +#define LUA_LOCATION_COMPAT_H + +#include +#include + +#if LUA_VERSION_RELEASE_NUM < 502 + +static inline void luaL_setmetatable(lua_State* L, const char* name) { + luaL_checkstack(L, 1, "not enough stack slots"); + luaL_getmetatable(L, name); + lua_setmetatable(L, -2); +} + +static inline void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) { + int i; + + luaL_checkstack(L, nup+1, "too many upvalues"); + + for (; l->name != NULL; l++) { + lua_pushstring(L, l->name); + + for (i = 0; i < nup; i++) + lua_pushvalue(L, -(nup + 1)); + + lua_pushcclosure(L, l->func, nup); + lua_settable(L, -(nup + 3)); + } + + lua_pop(L, nup); +} + +static inline void luaL_newlib(lua_State* L, const luaL_Reg* l) { + lua_newtable(L); + luaL_setfuncs(L, l, 0); +} + +#endif /* Lua < 5.2 */ + +#endif /* LUA_LOCATION_COMPAT_H */ diff --git a/src/lua/country.c b/src/lua/country.c index 7d2a78a..816bd2f 100644 --- a/src/lua/country.c +++ b/src/lua/country.c @@ -24,6 +24,7 @@ #include #include "location.h" +#include "compat.h" #include "country.h" typedef struct country { diff --git a/src/lua/database.c b/src/lua/database.c index 9f5d177..841041d 100644 --- a/src/lua/database.c +++ b/src/lua/database.c @@ -24,6 +24,7 @@ #include "location.h" #include "as.h" +#include "compat.h" #include "country.h" #include "database.h" #include "network.h" diff --git a/src/lua/location.c b/src/lua/location.c index 6f2157c..2d22a81 100644 --- a/src/lua/location.c +++ b/src/lua/location.c @@ -26,6 +26,7 @@ #include "location.h" #include "as.h" +#include "compat.h" #include "country.h" #include "database.h" #include "network.h" diff --git a/src/lua/location.h b/src/lua/location.h index cc261e6..0708988 100644 --- a/src/lua/location.h +++ b/src/lua/location.h @@ -21,6 +21,8 @@ #include +#include "compat.h" + extern struct loc_ctx* ctx; int luaopen_location(lua_State* L); diff --git a/src/lua/network.c b/src/lua/network.c index 1d5e0af..a25166e 100644 --- a/src/lua/network.c +++ b/src/lua/network.c @@ -24,6 +24,7 @@ #include #include "location.h" +#include "compat.h" #include "network.h" typedef struct network {