Skip to content

Commit

Permalink
lua: port interface to lua5.2 & lua5.3
Browse files Browse the repository at this point in the history
Close #1066
  • Loading branch information
razvancrainea committed Jan 14, 2020
1 parent 4e5ac0d commit c7028af
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
21 changes: 15 additions & 6 deletions modules/lua/Makefile
Expand Up @@ -7,12 +7,24 @@ auto_gen=
NAME=lua.so

MYSQLCFG=$(shell which mysql_config)
LIB_LUA = $(shell pkg-config --libs lua5.1 2> /dev/null) $(shell pkg-config --libs lua-5.1 2> /dev/null) $(shell pkg-config --libs lua 2>/dev/null)
FLAGS_LUA = $(shell pkg-config --cflags lua5.1 2> /dev/null) $(shell pkg-config --cflags lua-5.1 2> /dev/null) $(shell pkg-config --cflags lua 2>/dev/null)
LIB_LUA_NAME?=$(shell \
if pkg-config --exists lua; then echo lua; \
elif pkg-config --exists lua5.3; then echo lua5.3; \
elif pkg-config --exists lua-5.3; then echo lua-5.3; \
elif pkg-config --exists lua53; then echo lua53; \
elif pkg-config --exists lua5.2; then ehco lua5.2; \
elif pkg-config --exists lua-5.2; then ehco lua-5.2; \
elif pkg-config --exists lua52; then echo lua52; \
elif pkg-config --exists lua5.1; then ehco lua5.1; \
elif pkg-config --exists lua-5.1; then ehco lua-5.1; \
elif pkg-config --exists lua51; then echo lua51; fi)

LIB_LUA = $(shell pkg-config --libs $(LIB_LUA_NAME) 2>/dev/null)
FLAGS_LUA = $(shell pkg-config --cflags $(LIB_LUA_NAME) 2>/dev/null)
ifeq (,$(findstring -l,$(LIB_LUA)))
LIB_LUA = -llua
endif
DEFS += $(shell $(MYSQLCFG) --include | sed 's/\(-I.*\)\/mysql/\1/g' ) -I/usr/include/lua5.1/ $(FLAGS_LUA)
DEFS += $(shell $(MYSQLCFG) --include | sed 's/\(-I.*\)\/mysql/\1/g' ) $(FLAGS_LUA)
LIBS += $(shell $(MYSQLCFG) --libs) -lmemcached $(LIB_LUA)

ifneq ($(CROSS_COMPILE),)
Expand All @@ -21,6 +33,3 @@ LIBS = -L$(LOCALBASE)/lib $(LIBS)
endif

include ../../Makefile.modules

install_module_custom:
echo "installing Lua OpenSIPS packages ..."
43 changes: 43 additions & 0 deletions modules/lua/compat.h
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2020 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/

#ifndef _LUA_COMPAT_H_
#define _LUA_COMPAT_H_

#include <lua.h>

#if LUA_VERSION_NUM > 501
/* LUA 5.2 & LUA 5.3 */
#define luaL_openlib(_state, _libname, _reg, _num) \
luaL_setfuncs(_state, _reg, _num)
#if LUA_VERSION_NUM > 502
/* LUA 5.3 */
#define luaL_checklong(L,n) (long)luaL_checkinteger(L, (n))
#endif
#else
/* LUA 5.1 */
#define luaL_Reg luaL_reg
#define lua_pushglobaltable(_state) \
lua_pushvalue(_state, LUA_GLOBALSINDEX)
#endif

#endif /* _LUA_COMPAT_H_ */
5 changes: 3 additions & 2 deletions modules/lua/sipapi.c
Expand Up @@ -49,6 +49,7 @@
#include "siplua.h"
#include "sipluafunc.h"
#include "sipapi.h"
#include "compat.h"

#if 0
static void siplua_moduleFunc_free(const char *func, cmd_export_t *exp_func_struct,
Expand Down Expand Up @@ -892,7 +893,7 @@ static int l_siplua_moduleFunc(lua_State *L)
return 1;
}

static const struct luaL_reg siplua_api_mylib [] =
static const struct luaL_Reg siplua_api_mylib [] =
{
{"getType", l_siplua_getType},
{"getURI_User", l_siplua_getURI_User},
Expand Down Expand Up @@ -920,7 +921,7 @@ static const struct luaL_reg siplua_api_mylib [] =

void siplua_register_api_cclosures(lua_State *L)
{
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_pushglobaltable(L);
luaL_openlib(L, NULL, siplua_api_mylib, 0);
lua_remove(L, -1);
}
Expand Down
3 changes: 2 additions & 1 deletion modules/lua/sipdatetime.c
Expand Up @@ -25,6 +25,7 @@

#include <lua.h>
#include <lauxlib.h>
#include "compat.h"

struct sipdatetime
{
Expand Down Expand Up @@ -182,7 +183,7 @@ static int l_sipdatetime___index(lua_State *L)
return 1;
}

static const struct luaL_reg siplua_datetime_mylib [] =
static const struct luaL_Reg siplua_datetime_mylib [] =
{
{"duplicate", l_sipdatetime_duplicate},
{"strftime", l_sipdatetime_strftime},
Expand Down
3 changes: 2 additions & 1 deletion modules/lua/sipmemcache.c
Expand Up @@ -29,6 +29,7 @@
#include "../../ut.h"

#include "sipluafunc.h"
#include "compat.h"

#if !defined(LIBMEMCACHED_VERSION_HEX) || LIBMEMCACHED_VERSION_HEX < 0x00037000
typedef memcached_return memcached_return_t;
Expand Down Expand Up @@ -324,7 +325,7 @@ int l_sipmemcache_multi_get(lua_State *L)
return 1;
}

static const struct luaL_reg siplua_memcache_mylib [] =
static const struct luaL_Reg siplua_memcache_mylib [] =
{
{"server_add", l_sipmemcache_server_add},
{"add", l_sipmemcache_add},
Expand Down
5 changes: 3 additions & 2 deletions modules/lua/sipmysql.c
Expand Up @@ -30,6 +30,7 @@
#include "../../mem/mem.h"

#include "sipluafunc.h"
#include "compat.h"

#define SIPMYSQL_FETCH_NUM (1 << 0)
#define SIPMYSQL_FETCH_ASSOC (1 << 1)
Expand Down Expand Up @@ -882,7 +883,7 @@ int l_sipmysql_stmt___index(lua_State *L)
return 1;
}

static const struct luaL_reg siplua_mysql_mylib [] =
static const struct luaL_Reg siplua_mysql_mylib [] =
{
{"close", l_sipmysql_close},
{"query", l_sipmysql_query},
Expand All @@ -899,7 +900,7 @@ static const struct luaL_reg siplua_mysql_mylib [] =
{NULL, NULL} /* sentinel */
};

static const struct luaL_reg siplua_mysql_stmt_mylib [] =
static const struct luaL_Reg siplua_mysql_stmt_mylib [] =
{
{"close", l_sipmysql_stmt_close},
{"bind", l_sipmysql_stmt_bind},
Expand Down
5 changes: 3 additions & 2 deletions modules/lua/sipstate.c
Expand Up @@ -39,6 +39,7 @@
#include "sipwatch.h"
#include "sipdatetime.h"
#include "sipstate.h"
#include "compat.h"

static const luaL_Reg siplua_libs[] = {
{"", luaopen_base},
Expand Down Expand Up @@ -307,7 +308,7 @@ static int l_sipstate_setCoreDebug(lua_State *L)
return 0;
}

static const struct luaL_reg siplua_state_mylib [] =
static const struct luaL_Reg siplua_state_mylib [] =
{
{"xlog", l_sipstate_xlog},
{"xdbg", l_sipstate_xdbg},
Expand All @@ -327,7 +328,7 @@ static const struct luaL_reg siplua_state_mylib [] =

static void siplua_register_state_cclosures(lua_State *L)
{
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_pushglobaltable(L);
luaL_openlib(L, NULL, siplua_state_mylib, 0);
lua_remove(L, -1);
}
Expand Down
5 changes: 3 additions & 2 deletions modules/lua/sipwatch.c
Expand Up @@ -30,6 +30,7 @@
#include "crc32.h"
#include "sipapi.h"
#include "sipwatch.h"
#include "compat.h"

struct siplua_watch *siplua_watch;

Expand Down Expand Up @@ -154,7 +155,7 @@ static int l_sipwatch_getFlagFromExtension(lua_State *L)
return 1;
}

static const struct luaL_reg siplua_watch_mylib [] =
static const struct luaL_Reg siplua_watch_mylib [] =
{
{"watch_getFlag", l_sipwatch_getFlag},
{"watch_getFlagFromExtension", l_sipwatch_getFlagFromExtension},
Expand All @@ -163,7 +164,7 @@ static const struct luaL_reg siplua_watch_mylib [] =

void siplua_register_watch_cclosures(lua_State *L)
{
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_pushglobaltable(L);
luaL_openlib(L, NULL, siplua_watch_mylib, 0);
lua_remove(L, -1);
}

0 comments on commit c7028af

Please sign in to comment.