]> git.ipfire.org Git - people/ms/libloc.git/blame - src/lua/compat.h
lua: Add compatibility function to compile with Lua >= 5.1
[people/ms/libloc.git] / src / lua / compat.h
CommitLineData
031bae06
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15*/
16
17#ifndef LUA_LOCATION_COMPAT_H
18#define LUA_LOCATION_COMPAT_H
19
20#include <lua.h>
21#include <lauxlib.h>
22
23#if LUA_VERSION_RELEASE_NUM < 502
24
25static inline void luaL_setmetatable(lua_State* L, const char* name) {
26 luaL_checkstack(L, 1, "not enough stack slots");
27 luaL_getmetatable(L, name);
28 lua_setmetatable(L, -2);
29}
30
31static inline void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
32 int i;
33
34 luaL_checkstack(L, nup+1, "too many upvalues");
35
36 for (; l->name != NULL; l++) {
37 lua_pushstring(L, l->name);
38
39 for (i = 0; i < nup; i++)
40 lua_pushvalue(L, -(nup + 1));
41
42 lua_pushcclosure(L, l->func, nup);
43 lua_settable(L, -(nup + 3));
44 }
45
46 lua_pop(L, nup);
47}
48
49static inline void luaL_newlib(lua_State* L, const luaL_Reg* l) {
50 lua_newtable(L);
51 luaL_setfuncs(L, l, 0);
52}
53
54#endif /* Lua < 5.2 */
55
56#endif /* LUA_LOCATION_COMPAT_H */