Browse code

app_lua_sr: support for lua 5.4.x

Daniel-Constantin Mierla authored on 26/01/2022 15:25:56
Showing 1 changed files
... ...
@@ -45,8 +45,70 @@
45 45
 #include "app_lua_sr_api.h"
46 46
 #include "app_lua_sr_exp.h"
47 47
 
48
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
49
+#define luaL_openlib ksr_luaL_openlib
50
+#endif
51
+
52
+
48 53
 extern app_lua_api_t _app_lua_api;
49 54
 
55
+
56
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
57
+/**
58
+ *
59
+ */
60
+void ksr_luaL_openlib_mode(lua_State *L, const char *libname,
61
+			     const luaL_Reg *lfuncs, int nup, int mode)
62
+{
63
+	char modname[256];
64
+	char *submod = NULL;
65
+	int tidx = 0;
66
+	if(mode) {
67
+		/* support for registering 'module.submodule' functions
68
+		 * - 'module' functions must be registered first  */
69
+		if(strlen(libname)>254) {
70
+			LM_ERR("module name is too long [%s]\n", libname);
71
+			return;
72
+		}
73
+		strcpy(modname, libname);
74
+		submod = strchr(modname, '.');
75
+		if(submod != NULL) {
76
+			*submod = '\0';
77
+			submod++;
78
+		}
79
+		lua_getglobal(L, modname);
80
+		if (lua_isnil(L, -1)) {
81
+			if(submod != NULL) {
82
+				LM_ERR("main module not registered yet [%s]\n", libname);
83
+				return;
84
+			}
85
+			lua_pop(L, 1);
86
+			lua_newtable(L);
87
+			luaL_setfuncs(L, lfuncs, 0);
88
+			lua_setglobal(L, modname);
89
+			return;
90
+		}
91
+		tidx = lua_gettop(L);
92
+		lua_newtable(L);
93
+		luaL_setfuncs(L, lfuncs, 0);
94
+		lua_setfield(L, tidx, submod);
95
+		return;
96
+	}
97
+	lua_newtable(L);
98
+	luaL_setfuncs(L, lfuncs, 0);
99
+	lua_setglobal(L, libname);
100
+}
101
+
102
+/**
103
+ *
104
+ */
105
+void ksr_luaL_openlib(lua_State *L, const char *libname,
106
+			     const luaL_Reg *lfuncs, int nup)
107
+{
108
+	ksr_luaL_openlib_mode(L, libname, lfuncs, nup, 1);
109
+}
110
+#endif
111
+
50 112
 /**
51 113
  *
52 114
  */