]> git.ipfire.org Git - location/libloc.git/commitdiff
lua: Add compatibility function to compile with Lua >= 5.1
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 31 Mar 2024 14:16:14 +0000 (14:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 31 Mar 2024 14:16:14 +0000 (14:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/lua/as.c
src/lua/compat.h [new file with mode: 0644]
src/lua/country.c
src/lua/database.c
src/lua/location.c
src/lua/location.h
src/lua/network.c

index 65b3a76d8db67917b123f419aac3c254fd4e7551..9afae8d6fb680897e6b46c82a2cfb89173e1433e 100644 (file)
@@ -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 \
index d36f2d88885b25811afcdcf0556a3631487772ec..558fcbf474e71242ff6c1aca950941fad9aa35fa 100644 (file)
@@ -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 (file)
index 0000000..f0172b8
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
+
+       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 <lua.h>
+#include <lauxlib.h>
+
+#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 */
index 7d2a78a96382ee9ec6449e3d16bd81d26a815b86..816bd2f21c8a15ee6670405858284a07e48df746 100644 (file)
@@ -24,6 +24,7 @@
 #include <libloc/country.h>
 
 #include "location.h"
+#include "compat.h"
 #include "country.h"
 
 typedef struct country {
index 9f5d177076a5487c1a29db1467e86beb917c43a3..841041d89df6472363b584f0b8a6f38f9bb93f60 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "location.h"
 #include "as.h"
+#include "compat.h"
 #include "country.h"
 #include "database.h"
 #include "network.h"
index 6f2157cfc0bd9ef32f8d8fc89ad528501775ef8e..2d22a816c3f7b1a019afe241e541a438dd9fff2d 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "location.h"
 #include "as.h"
+#include "compat.h"
 #include "country.h"
 #include "database.h"
 #include "network.h"
index cc261e61e53c924eadb9b808c979d85b174b9327..07089881a635d8bfc205a45aeab38a986911fce7 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <libloc/libloc.h>
 
+#include "compat.h"
+
 extern struct loc_ctx* ctx;
 
 int luaopen_location(lua_State* L);
index 1d5e0af4d5c1faa83034827d31b47b067edf70bb..a25166e23b8946f895c15f0c77be10adb0925c07 100644 (file)
@@ -24,6 +24,7 @@
 #include <libloc/network.h>
 
 #include "location.h"
+#include "compat.h"
 #include "network.h"
 
 typedef struct network {