e26d1f4
diff --git a/configure b/configure
e26d1f4
index 37cd437..0a3c27b 100755
e26d1f4
--- a/configure
e26d1f4
+++ b/configure
e26d1f4
@@ -26334,7 +26334,7 @@ if test "x${PBX_LUA}" != "x1" -a "${USE_LUA}" != "no"; then
e26d1f4
          pbxlibdir="-L${LUA_DIR}"
e26d1f4
       fi
e26d1f4
    fi
e26d1f4
-   pbxfuncname="luaL_register"
e26d1f4
+   pbxfuncname="luaL_openlib"
e26d1f4
    if test "x${pbxfuncname}" = "x" ; then   # empty lib, assume only headers
e26d1f4
       AST_LUA_FOUND=yes
e26d1f4
    else
e26d1f4
diff --git a/configure.ac b/configure.ac
e26d1f4
index 9237ba3..b32cf25 100644
e26d1f4
--- a/configure.ac
e26d1f4
+++ b/configure.ac
e26d1f4
@@ -2098,7 +2098,7 @@ if test "x${PBX_LUA}" = "x1" ; then
f962b64
 fi
f962b64
 
f962b64
 # Some distributions (like SuSE) remove the 5.1 suffix.
f962b64
-AST_EXT_LIB_CHECK([LUA], [lua], [luaL_register], [lua.h], [-lm])
f962b64
+AST_EXT_LIB_CHECK([LUA], [lua], [luaL_openlib], [lua.h], [-lm])
f962b64
 
f962b64
 AST_EXT_LIB_CHECK([RADIUS], [radiusclient-ng], [rc_read_config], [radiusclient-ng.h])
f962b64
 
e26d1f4
diff --git a/pbx/pbx_lua.c b/pbx/pbx_lua.c
e26d1f4
index 1b91827..579bc76 100644
e26d1f4
--- a/pbx/pbx_lua.c
e26d1f4
+++ b/pbx/pbx_lua.c
e26d1f4
@@ -873,8 +873,11 @@ static int lua_sort_extensions(lua_State *L)
f962b64
 		 * table in the extensions_order table */
f962b64
 		for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) {
f962b64
 			int exten = lua_gettop(L) - 1;
f962b64
-
f962b64
+#if LUA_VERSION_NUM < 502
f962b64
 			lua_pushinteger(L, lua_objlen(L, context_order) + 1);
f962b64
+#else
f962b64
+			lua_pushinteger(L, lua_rawlen(L, context_order) + 1);
f962b64
+#endif
f962b64
 			lua_pushvalue(L, exten);
f962b64
 			lua_settable(L, context_order);
f962b64
 		}
e26d1f4
@@ -1508,7 +1511,11 @@ static int lua_find_extension(lua_State *L, const char *context, const char *ext
f962b64
 	context_order_table = lua_gettop(L);
f962b64
 	
f962b64
 	/* step through the extensions looking for a match */
f962b64
+#if LUA_VERSION_NUM < 502
f962b64
 	for (i = 1; i < lua_objlen(L, context_order_table) + 1; i++) {
f962b64
+#else
f962b64
+	for (i = 1; i < lua_rawlen(L, context_order_table) + 1; i++) {
f962b64
+#endif
f962b64
 		int e_index_copy, match = 0;
f962b64
 		const char *e;
f962b64