From 537f4a9dacc066893dad17a8a96c450de48abbdb Mon Sep 17 00:00:00 2001
From: Kinsey Moore <kmoore@digium.com>
Date: Wed, 8 Jan 2014 16:34:24 +0000
Subject: [PATCH] pbx_lua: Add support for Lua 5.2

This adds support for Lua 5.2 in pbx_lua which is available on newer
operating systems.

(closes issue ASTERISK-23011)
Review: https://reviewboard.asterisk.org/r/3075/
Reported by: George Joseph
Patch by: George Joseph
........

Merged revisions 405090 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 405091 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 405124 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@405130 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 configure     | 116 +++++++++++++++++++++++++++++++++++++++++++++++++-
 configure.ac  |  11 ++++-
 pbx/pbx_lua.c |  11 ++++-
 3 files changed, 133 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index a185656459..815bb64c03 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.ac Revision: 403161 .
+# From configure.ac Revision: 403330 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.69 for asterisk trunk.
 #
@@ -27429,6 +27429,118 @@ $as_echo "#define HAVE_OPENH323 1" >>confdefs.h
 fi
 
 
+if test "x${PBX_LUA}" != "x1" -a "${USE_LUA}" != "no"; then
+   pbxlibdir=""
+   # if --with-LUA=DIR has been specified, use it.
+   if test "x${LUA_DIR}" != "x"; then
+      if test -d ${LUA_DIR}/lib; then
+         pbxlibdir="-L${LUA_DIR}/lib"
+      else
+         pbxlibdir="-L${LUA_DIR}"
+      fi
+   fi
+   pbxfuncname="luaL_newstate"
+   if test "x${pbxfuncname}" = "x" ; then   # empty lib, assume only headers
+      AST_LUA_FOUND=yes
+   else
+      ast_ext_lib_check_save_CFLAGS="${CFLAGS}"
+      CFLAGS="${CFLAGS} "
+      as_ac_Lib=`$as_echo "ac_cv_lib_lua5.2_${pbxfuncname}" | $as_tr_sh`
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ${pbxfuncname} in -llua5.2" >&5
+$as_echo_n "checking for ${pbxfuncname} in -llua5.2... " >&6; }
+if eval \${$as_ac_Lib+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-llua5.2 ${pbxlibdir} -lm $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char ${pbxfuncname} ();
+int
+main ()
+{
+return ${pbxfuncname} ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  eval "$as_ac_Lib=yes"
+else
+  eval "$as_ac_Lib=no"
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+eval ac_res=\$$as_ac_Lib
+	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
+$as_echo "$ac_res" >&6; }
+if eval test \"x\$"$as_ac_Lib"\" = x"yes"; then :
+  AST_LUA_FOUND=yes
+else
+  AST_LUA_FOUND=no
+fi
+
+      CFLAGS="${ast_ext_lib_check_save_CFLAGS}"
+   fi
+
+   # now check for the header.
+   if test "${AST_LUA_FOUND}" = "yes"; then
+      LUA_LIB="${pbxlibdir} -llua5.2 -lm"
+      # if --with-LUA=DIR has been specified, use it.
+      if test "x${LUA_DIR}" != "x"; then
+         LUA_INCLUDE="-I${LUA_DIR}/include"
+      fi
+      LUA_INCLUDE="${LUA_INCLUDE} "
+      if test "xlua5.2/lua.h" = "x" ; then	# no header, assume found
+         LUA_HEADER_FOUND="1"
+      else				# check for the header
+         ast_ext_lib_check_saved_CPPFLAGS="${CPPFLAGS}"
+         CPPFLAGS="${CPPFLAGS} ${LUA_INCLUDE}"
+         ac_fn_c_check_header_mongrel "$LINENO" "lua5.2/lua.h" "ac_cv_header_lua5_2_lua_h" "$ac_includes_default"
+if test "x$ac_cv_header_lua5_2_lua_h" = xyes; then :
+  LUA_HEADER_FOUND=1
+else
+  LUA_HEADER_FOUND=0
+fi
+
+
+         CPPFLAGS="${ast_ext_lib_check_saved_CPPFLAGS}"
+      fi
+      if test "x${LUA_HEADER_FOUND}" = "x0" ; then
+         LUA_LIB=""
+         LUA_INCLUDE=""
+      else
+         if test "x${pbxfuncname}" = "x" ; then		# only checking headers -> no library
+            LUA_LIB=""
+         fi
+         PBX_LUA=1
+         cat >>confdefs.h <<_ACEOF
+#define HAVE_LUA 1
+_ACEOF
+
+      fi
+   fi
+fi
+
+
+if test "x${PBX_LUA}" = "x1" ; then
+	if test x"${LUA_DIR}" = x; then
+		LUA_INCLUDE="${LUA_INCLUDE} -I/usr/include/lua5.2"
+	else
+		LUA_INCLUDE="${LUA_INCLUDE} -I${LUA_DIR}/lua5.2"
+	fi
+fi
+
+
 if test "x${PBX_LUA}" != "x1" -a "${USE_LUA}" != "no"; then
    pbxlibdir=""
    # if --with-LUA=DIR has been specified, use it.
@@ -27552,7 +27664,7 @@ if test "x${PBX_LUA}" != "x1" -a "${USE_LUA}" != "no"; then
          pbxlibdir="-L${LUA_DIR}"
       fi
    fi
-   pbxfuncname="luaL_register"
+   pbxfuncname="luaL_openlib"
    if test "x${pbxfuncname}" = "x" ; then   # empty lib, assume only headers
       AST_LUA_FOUND=yes
    else
diff --git a/configure.ac b/configure.ac
index ae5ddea092..fb14b2936b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2113,6 +2113,15 @@ if test "${PBX_PWLIB}" = "1" -a "${USE_OPENH323}" != "no" ; then
 		[${PWLIB_INCLUDE}], [${PWLIB_LIB}])
 fi
 
+AST_EXT_LIB_CHECK([LUA], [lua5.2], [luaL_newstate], [lua5.2/lua.h], [-lm])
+if test "x${PBX_LUA}" = "x1" ; then
+	if test x"${LUA_DIR}" = x; then
+		LUA_INCLUDE="${LUA_INCLUDE} -I/usr/include/lua5.2"
+	else
+		LUA_INCLUDE="${LUA_INCLUDE} -I${LUA_DIR}/lua5.2"
+	fi
+fi
+
 AST_EXT_LIB_CHECK([LUA], [lua5.1], [luaL_newstate], [lua5.1/lua.h], [-lm])
 if test "x${PBX_LUA}" = "x1" ; then
 	if test x"${LUA_DIR}" = x; then
@@ -2123,7 +2132,7 @@ if test "x${PBX_LUA}" = "x1" ; then
 fi
 
 # Some distributions (like SuSE) remove the 5.1 suffix.
-AST_EXT_LIB_CHECK([LUA], [lua], [luaL_register], [lua.h], [-lm])
+AST_EXT_LIB_CHECK([LUA], [lua], [luaL_openlib], [lua.h], [-lm])
 
 AST_EXT_LIB_CHECK([RADIUS], [radiusclient-ng], [rc_read_config], [radiusclient-ng.h])
 
diff --git a/pbx/pbx_lua.c b/pbx/pbx_lua.c
index f429a4a25f..c4492c7e5e 100644
--- a/pbx/pbx_lua.c
+++ b/pbx/pbx_lua.c
@@ -872,8 +872,11 @@ static int lua_sort_extensions(lua_State *L)
 		 * table in the extensions_order table */
 		for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) {
 			int exten = lua_gettop(L) - 1;
-
+#if LUA_VERSION_NUM < 502
 			lua_pushinteger(L, lua_objlen(L, context_order) + 1);
+#else
+			lua_pushinteger(L, lua_rawlen(L, context_order) + 1);
+#endif
 			lua_pushvalue(L, exten);
 			lua_settable(L, context_order);
 		}
@@ -1505,9 +1508,13 @@ static int lua_find_extension(lua_State *L, const char *context, const char *ext
 	lua_remove(L, -2);  /* remove the extensions order table */
 
 	context_order_table = lua_gettop(L);
-	
+
 	/* step through the extensions looking for a match */
+#if LUA_VERSION_NUM < 502
 	for (i = 1; i < lua_objlen(L, context_order_table) + 1; i++) {
+#else
+	for (i = 1; i < lua_rawlen(L, context_order_table) + 1; i++) {
+#endif
 		int e_index_copy, match = 0;
 		const char *e;
 
-- 
GitLab