Browse code

app_lua: support for lua 5.4.x

- GH #2728

Daniel-Constantin Mierla authored on 18/01/2022 14:19:52
Showing 1 changed files
... ...
@@ -42,6 +42,10 @@
42 42
 
43 43
 #define KSR_APP_LUA_LOG_EXPORTS (1<<0)
44 44
 
45
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
46
+#define luaL_openlib ksr_luaL_openlib
47
+#endif
48
+
45 49
 extern int _ksr_app_lua_log_mode;
46 50
 
47 51
 void lua_sr_kemi_register_libs(lua_State *L);
... ...
@@ -190,15 +194,41 @@ int sr_lua_reload_module(unsigned int reload)
190 194
 void ksr_luaL_openlib_mode(lua_State *L, const char *libname,
191 195
 			     const luaL_Reg *lfuncs, int nup, int mode)
192 196
 {
197
+	char modname[256];
198
+	char *submod = NULL;
199
+	int tidx = 0;
193 200
 	if(mode) {
194
-		lua_getglobal(L, libname);
201
+		/* support for registering 'module.submodule' functions
202
+		 * - 'module' functions must be registered first  */
203
+		if(strlen(libname)>254) {
204
+			LM_ERR("module name is too long [%s]\n", libname);
205
+			return;
206
+		}
207
+		strcpy(modname, libname);
208
+		submod = strchr(modname, '.');
209
+		if(submod != NULL) {
210
+			*submod = '\0';
211
+			submod++;
212
+		}
213
+		lua_getglobal(L, modname);
195 214
 		if (lua_isnil(L, -1)) {
215
+			if(submod != NULL) {
216
+				LM_ERR("main module not registered yet [%s]\n", libname);
217
+				return;
218
+			}
196 219
 			lua_pop(L, 1);
197 220
 			lua_newtable(L);
221
+			luaL_setfuncs(L, lfuncs, 0);
222
+			lua_setglobal(L, modname);
223
+			return;
198 224
 		}
199
-	} else {
225
+		tidx = lua_gettop(L);
200 226
 		lua_newtable(L);
227
+		luaL_setfuncs(L, lfuncs, 0);
228
+		lua_setfield(L, tidx, submod);
229
+		return;
201 230
 	}
231
+	lua_newtable(L);
202 232
 	luaL_setfuncs(L, lfuncs, 0);
203 233
 	lua_setglobal(L, libname);
204 234
 }
... ...
@@ -209,7 +239,7 @@ void ksr_luaL_openlib_mode(lua_State *L, const char *libname,
209 239
 void ksr_luaL_openlib(lua_State *L, const char *libname,
210 240
 			     const luaL_Reg *lfuncs, int nup)
211 241
 {
212
-	ksr_luaL_openlib_mode(L, libname, lfuncs, nup, 0);
242
+	ksr_luaL_openlib_mode(L, libname, lfuncs, nup, 1);
213 243
 }
214 244
 
215 245
 /**