Browse code

app_lua: enclose ksr_luaL_openlib() between defines

- uses functions not available in lua 5.1

Daniel-Constantin Mierla authored on 23/01/2022 18:48:26
Showing 1 changed files
... ...
@@ -188,6 +188,7 @@ int sr_lua_reload_module(unsigned int reload)
188 188
 	return 0;
189 189
 }
190 190
 
191
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
191 192
 /**
192 193
  *
193 194
  */
... ...
@@ -241,6 +242,7 @@ void ksr_luaL_openlib(lua_State *L, const char *libname,
241 242
 {
242 243
 	ksr_luaL_openlib_mode(L, libname, lfuncs, nup, 1);
243 244
 }
245
+#endif
244 246
 
245 247
 /**
246 248
  *
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
 /**
Browse code

app_lua: added internal alternative to luaL_openlib()

- it was deprecated in Lua API, no longer available in newer versions [wip]

Daniel-Constantin Mierla authored on 17/01/2022 07:29:13
Showing 1 changed files
... ...
@@ -184,6 +184,34 @@ int sr_lua_reload_module(unsigned int reload)
184 184
 	return 0;
185 185
 }
186 186
 
187
+/**
188
+ *
189
+ */
190
+void ksr_luaL_openlib_mode(lua_State *L, const char *libname,
191
+			     const luaL_Reg *lfuncs, int nup, int mode)
192
+{
193
+	if(mode) {
194
+		lua_getglobal(L, libname);
195
+		if (lua_isnil(L, -1)) {
196
+			lua_pop(L, 1);
197
+			lua_newtable(L);
198
+		}
199
+	} else {
200
+		lua_newtable(L);
201
+	}
202
+	luaL_setfuncs(L, lfuncs, 0);
203
+	lua_setglobal(L, libname);
204
+}
205
+
206
+/**
207
+ *
208
+ */
209
+void ksr_luaL_openlib(lua_State *L, const char *libname,
210
+			     const luaL_Reg *lfuncs, int nup)
211
+{
212
+	ksr_luaL_openlib_mode(L, libname, lfuncs, nup, 0);
213
+}
214
+
187 215
 /**
188 216
  *
189 217
  */
Browse code

app_lua: update to handle xval return for functions with 3 parameters

- fix for functions like KSR.sqlops.sql_result_get()

Daniel-Constantin Mierla authored on 09/12/2021 19:37:51
Showing 1 changed files
... ...
@@ -1088,77 +1088,104 @@ int sr_kemi_lua_exec_func_ex(lua_State* L, sr_kemi_t *ket, int pdelta)
1088 1088
 			}
1089 1089
 		break;
1090 1090
 		case 3:
1091
-			if(ket->ptypes[0]==SR_KEMIP_INT) {
1092
-				if(ket->ptypes[1]==SR_KEMIP_INT) {
1093
-					if(ket->ptypes[2]==SR_KEMIP_INT) {
1094
-						ret = ((sr_kemi_fmnnn_f)(ket->func))(env_L->msg,
1095
-								vps[0].n, vps[1].n, vps[2].n);
1096
-						return sr_kemi_lua_return_int(L, ket, ret);
1097
-					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1098
-						ret = ((sr_kemi_fmnns_f)(ket->func))(env_L->msg,
1099
-								vps[0].n, vps[1].n, &vps[2].s);
1100
-						return sr_kemi_lua_return_int(L, ket, ret);
1101
-					} else {
1102
-						LM_ERR("invalid parameters for: %.*s\n",
1103
-								fname->len, fname->s);
1104
-						return app_lua_return_false(L);
1105
-					}
1106
-				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
1107
-					if(ket->ptypes[2]==SR_KEMIP_INT) {
1108
-						ret = ((sr_kemi_fmnsn_f)(ket->func))(env_L->msg,
1109
-								vps[0].n, &vps[1].s, vps[2].n);
1110
-						return sr_kemi_lua_return_int(L, ket, ret);
1111
-					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1112
-						ret = ((sr_kemi_fmnss_f)(ket->func))(env_L->msg,
1113
-								vps[0].n, &vps[1].s, &vps[2].s);
1114
-						return sr_kemi_lua_return_int(L, ket, ret);
1115
-					} else {
1116
-						LM_ERR("invalid parameters for: %.*s\n",
1117
-								fname->len, fname->s);
1118
-						return app_lua_return_false(L);
1119
-					}
1091
+			if(ket->ptypes[0]==SR_KEMIP_STR
1092
+					&& ket->ptypes[1]==SR_KEMIP_STR
1093
+					&& ket->ptypes[2]==SR_KEMIP_STR) {
1094
+				if(ket->rtype==SR_KEMIP_XVAL) {
1095
+					xret = ((sr_kemi_xfmsss_f)(ket->func))(env_L->msg,
1096
+						&vps[0].s, &vps[1].s, &vps[2].s);
1097
+					return sr_kemi_lua_return_xval(L, ket, xret);
1120 1098
 				} else {
1121
-					LM_ERR("invalid parameters for: %.*s\n",
1122
-							fname->len, fname->s);
1123
-					return app_lua_return_false(L);
1099
+					ret = ((sr_kemi_fmsss_f)(ket->func))(env_L->msg,
1100
+						&vps[0].s, &vps[1].s, &vps[2].s);
1101
+					return sr_kemi_lua_return_int(L, ket, ret);
1124 1102
 				}
1125
-			} else if(ket->ptypes[0]==SR_KEMIP_STR) {
1126
-				if(ket->ptypes[1]==SR_KEMIP_INT) {
1127
-					if(ket->ptypes[2]==SR_KEMIP_INT) {
1128
-						ret = ((sr_kemi_fmsnn_f)(ket->func))(env_L->msg,
1129
-								&vps[0].s, vps[1].n, vps[2].n);
1130
-						return sr_kemi_lua_return_int(L, ket, ret);
1131
-					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1132
-						ret = ((sr_kemi_fmsns_f)(ket->func))(env_L->msg,
1133
-								&vps[0].s, vps[1].n, &vps[2].s);
1134
-						return sr_kemi_lua_return_int(L, ket, ret);
1135
-					} else {
1136
-						LM_ERR("invalid parameters for: %.*s\n",
1137
-								fname->len, fname->s);
1138
-						return app_lua_return_false(L);
1139
-					}
1140
-				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
1141
-					if(ket->ptypes[2]==SR_KEMIP_INT) {
1142
-						ret = ((sr_kemi_fmssn_f)(ket->func))(env_L->msg,
1143
-								&vps[0].s, &vps[1].s, vps[2].n);
1144
-						return sr_kemi_lua_return_int(L, ket, ret);
1145
-					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1146
-						ret = ((sr_kemi_fmsss_f)(ket->func))(env_L->msg,
1147
-								&vps[0].s, &vps[1].s, &vps[2].s);
1148
-						return sr_kemi_lua_return_int(L, ket, ret);
1149
-					} else {
1150
-						LM_ERR("invalid parameters for: %.*s\n",
1151
-								fname->len, fname->s);
1152
-						return app_lua_return_false(L);
1153
-					}
1103
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1104
+					&& ket->ptypes[1]==SR_KEMIP_STR
1105
+					&& ket->ptypes[2]==SR_KEMIP_INT) {
1106
+				if(ket->rtype==SR_KEMIP_XVAL) {
1107
+					xret = ((sr_kemi_xfmssn_f)(ket->func))(env_L->msg,
1108
+						&vps[0].s, &vps[1].s, vps[2].n);
1109
+					return sr_kemi_lua_return_xval(L, ket, xret);
1154 1110
 				} else {
1155
-					LM_ERR("invalid parameters for: %.*s\n",
1156
-							fname->len, fname->s);
1157
-					return app_lua_return_false(L);
1111
+					ret = ((sr_kemi_fmssn_f)(ket->func))(env_L->msg,
1112
+						&vps[0].s, &vps[1].s, vps[2].n);
1113
+					return sr_kemi_lua_return_int(L, ket, ret);
1114
+				}
1115
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1116
+					&& ket->ptypes[1]==SR_KEMIP_INT
1117
+					&& ket->ptypes[2]==SR_KEMIP_STR) {
1118
+				if(ket->rtype==SR_KEMIP_XVAL) {
1119
+					xret = ((sr_kemi_xfmsns_f)(ket->func))(env_L->msg,
1120
+						&vps[0].s, vps[1].n, &vps[2].s);
1121
+					return sr_kemi_lua_return_xval(L, ket, xret);
1122
+				} else {
1123
+					ret = ((sr_kemi_fmsns_f)(ket->func))(env_L->msg,
1124
+						&vps[0].s, vps[1].n, &vps[2].s);
1125
+					return sr_kemi_lua_return_int(L, ket, ret);
1126
+				}
1127
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1128
+					&& ket->ptypes[1]==SR_KEMIP_INT
1129
+					&& ket->ptypes[2]==SR_KEMIP_INT) {
1130
+				if(ket->rtype==SR_KEMIP_XVAL) {
1131
+					xret = ((sr_kemi_xfmsnn_f)(ket->func))(env_L->msg,
1132
+						&vps[0].s, vps[1].n, vps[2].n);
1133
+					return sr_kemi_lua_return_xval(L, ket, xret);
1134
+				} else {
1135
+					ret = ((sr_kemi_fmsnn_f)(ket->func))(env_L->msg,
1136
+						&vps[0].s, vps[1].n, vps[2].n);
1137
+					return sr_kemi_lua_return_int(L, ket, ret);
1138
+				}
1139
+			} else if(ket->ptypes[0]==SR_KEMIP_INT
1140
+					&& ket->ptypes[1]==SR_KEMIP_STR
1141
+					&& ket->ptypes[2]==SR_KEMIP_STR) {
1142
+				if(ket->rtype==SR_KEMIP_XVAL) {
1143
+					xret = ((sr_kemi_xfmnss_f)(ket->func))(env_L->msg,
1144
+						vps[0].n, &vps[1].s, &vps[2].s);
1145
+					return sr_kemi_lua_return_xval(L, ket, xret);
1146
+				} else {
1147
+					ret = ((sr_kemi_fmnss_f)(ket->func))(env_L->msg,
1148
+						vps[0].n, &vps[1].s, &vps[2].s);
1149
+					return sr_kemi_lua_return_int(L, ket, ret);
1150
+				}
1151
+			} else if(ket->ptypes[0]==SR_KEMIP_INT
1152
+					&& ket->ptypes[1]==SR_KEMIP_STR
1153
+					&& ket->ptypes[2]==SR_KEMIP_INT) {
1154
+				if(ket->rtype==SR_KEMIP_XVAL) {
1155
+					xret = ((sr_kemi_xfmnsn_f)(ket->func))(env_L->msg,
1156
+						vps[0].n, &vps[1].s, vps[2].n);
1157
+					return sr_kemi_lua_return_xval(L, ket, xret);
1158
+				} else {
1159
+					ret = ((sr_kemi_fmnsn_f)(ket->func))(env_L->msg,
1160
+						vps[0].n, &vps[1].s, vps[2].n);
1161
+					return sr_kemi_lua_return_int(L, ket, ret);
1162
+				}
1163
+			} else if(ket->ptypes[0]==SR_KEMIP_INT
1164
+					&& ket->ptypes[1]==SR_KEMIP_INT
1165
+					&& ket->ptypes[2]==SR_KEMIP_STR) {
1166
+				if(ket->rtype==SR_KEMIP_XVAL) {
1167
+					xret = ((sr_kemi_xfmnns_f)(ket->func))(env_L->msg,
1168
+						vps[0].n, vps[1].n, &vps[2].s);
1169
+					return sr_kemi_lua_return_xval(L, ket, xret);
1170
+				} else {
1171
+					ret = ((sr_kemi_fmnns_f)(ket->func))(env_L->msg,
1172
+						vps[0].n, vps[1].n, &vps[2].s);
1173
+					return sr_kemi_lua_return_int(L, ket, ret);
1174
+				}
1175
+			} else if(ket->ptypes[0]==SR_KEMIP_INT
1176
+					&& ket->ptypes[1]==SR_KEMIP_INT
1177
+					&& ket->ptypes[2]==SR_KEMIP_INT) {
1178
+				if(ket->rtype==SR_KEMIP_XVAL) {
1179
+					xret = ((sr_kemi_xfmnnn_f)(ket->func))(env_L->msg,
1180
+						vps[0].n, vps[1].n, vps[2].n);
1181
+					return sr_kemi_lua_return_xval(L, ket, xret);
1182
+				} else {
1183
+					ret = ((sr_kemi_fmnnn_f)(ket->func))(env_L->msg,
1184
+						vps[0].n, vps[1].n, vps[2].n);
1185
+					return sr_kemi_lua_return_int(L, ket, ret);
1158 1186
 				}
1159 1187
 			} else {
1160
-				LM_ERR("invalid parameters for: %.*s\n",
1161
-						fname->len, fname->s);
1188
+				LM_ERR("invalid parameters for: %.*s\n", fname->len, fname->s);
1162 1189
 				return app_lua_return_false(L);
1163 1190
 			}
1164 1191
 		break;
Browse code

app_lua: init timeval structures

Daniel-Constantin Mierla authored on 07/04/2021 08:29:24
Showing 1 changed files
... ...
@@ -1605,7 +1605,7 @@ int sr_kemi_lua_exec_func(lua_State* L, int eidx)
1605 1605
 {
1606 1606
 	sr_kemi_t *ket;
1607 1607
 	int ret;
1608
-	struct timeval tvb, tve;
1608
+	struct timeval tvb = {0}, tve = {0};
1609 1609
 	struct timezone tz;
1610 1610
 	unsigned int tdiff;
1611 1611
 	lua_Debug dinfo;
Browse code

app_lua: Fixed error 'too many parameters'

- Calling function KSR.secsipid.secsipid_add_identity() has six parameters, is giving error 'too many parameters'

Miteshkumar Thakkar authored on 23/03/2021 15:05:20 • Daniel-Constantin Mierla committed on 23/03/2021 16:27:32
Showing 1 changed files
... ...
@@ -969,7 +969,7 @@ int sr_kemi_lua_exec_func_ex(lua_State* L, sr_kemi_t *ket, int pdelta)
969 969
 		return app_lua_return_false(L);
970 970
 	}
971 971
 
972
-	if(argc>=SR_KEMI_PARAMS_MAX+pdelta) {
972
+	if(argc>SR_KEMI_PARAMS_MAX+pdelta) {
973 973
 		LM_ERR("too many parameters for: %.*s.%.*s\n",
974 974
 				mname->len, mname->s, fname->len, fname->s);
975 975
 		return app_lua_return_false(L);
Browse code

app_lua: add support for ARRAY and DICT types

Victor Seva authored on 13/04/2020 15:10:01
Showing 1 changed files
... ...
@@ -806,6 +806,85 @@ int sr_kemi_lua_return_int(lua_State* L, sr_kemi_t *ket, int rc)
806 806
 	return app_lua_return_false(L);
807 807
 }
808 808
 
809
+void sr_kemi_lua_push_dict_item(lua_State *L, sr_kemi_dict_item_t *item);
810
+
811
+/**
812
+ * creates and push a table to the lua stack with
813
+ * the elements of the list
814
+ */
815
+void sr_kemi_lua_push_array(lua_State *L, sr_kemi_dict_item_t *item) {
816
+	int i = 1;
817
+	sr_kemi_dict_item_t *k;
818
+	if(!item) {
819
+		LM_CRIT("BUG: dict field empty\n");
820
+		return;
821
+	}
822
+	if (item->vtype == SR_KEMIP_ARRAY) {
823
+		k = item->v.dict;
824
+	} else {
825
+		k = item;
826
+	}
827
+	if(k) {
828
+		lua_newtable(L);
829
+	}
830
+	while(k){
831
+		lua_pushnumber(L, i++);
832
+		sr_kemi_lua_push_dict_item(L, k);
833
+		lua_settable(L, -3);
834
+		k = k->next;
835
+	}
836
+}
837
+
838
+void sr_kemi_lua_push_dict(lua_State *L, sr_kemi_dict_item_t *item) {
839
+	sr_kemi_dict_item_t *k = item;
840
+	if(!item) {
841
+		LM_CRIT("BUG: dict field empty\n");
842
+		return;
843
+	}
844
+	lua_newtable(L);
845
+	while(k){
846
+		sr_kemi_lua_push_dict_item(L, k->v.dict);
847
+		lua_setfield(L, -2, k->name.s);
848
+		k = k->next;
849
+	}
850
+}
851
+
852
+void
853
+sr_kemi_lua_push_dict_item(lua_State *L, sr_kemi_dict_item_t *item)
854
+{
855
+	switch(item->vtype) {
856
+		case SR_KEMIP_NONE:
857
+			LM_CRIT("BUG: vtype is NONE\n");
858
+			lua_pushnil(L);
859
+		break;
860
+		case SR_KEMIP_INT:
861
+			lua_pushinteger(L, item->v.n);
862
+		break;
863
+		case SR_KEMIP_STR:
864
+			lua_pushlstring(L, item->v.s.s, item->v.s.len);
865
+		break;
866
+		case SR_KEMIP_BOOL:
867
+			if(item->v.n!=SR_KEMI_FALSE) {
868
+				lua_pushboolean(L, SRLUA_TRUE);
869
+			} else {
870
+				lua_pushboolean(L, SRLUA_FALSE);
871
+			}
872
+		break;
873
+		case SR_KEMIP_NULL:
874
+			lua_pushnil(L);
875
+		break;
876
+		case SR_KEMIP_ARRAY:
877
+			sr_kemi_lua_push_array(L, item);
878
+		break;
879
+		case SR_KEMIP_DICT:
880
+			sr_kemi_lua_push_dict(L, item);
881
+		break;
882
+		default:
883
+			LM_DBG("unknown type:%d\n", item->vtype);
884
+			/* unknown type - return false */
885
+			lua_pushboolean(L, SRLUA_FALSE);
886
+	}
887
+}
809 888
 
810 889
 /**
811 890
  *
... ...
@@ -835,6 +914,14 @@ int sr_kemi_lua_return_xval(lua_State* L, sr_kemi_t *ket, sr_kemi_xval_t *rx)
835 914
 		case SR_KEMIP_NULL:
836 915
 			lua_pushnil(L);
837 916
 			return 1;
917
+		case SR_KEMIP_ARRAY:
918
+			sr_kemi_lua_push_array(L, rx->v.dict);
919
+			sr_kemi_xval_free(rx);
920
+			return 1;
921
+		case SR_KEMIP_DICT:
922
+			sr_kemi_lua_push_dict_item(L, rx->v.dict);
923
+			sr_kemi_xval_free(rx);
924
+			return 1;
838 925
 		default:
839 926
 			/* unknown type - return false */
840 927
 			lua_pushboolean(L, SRLUA_FALSE);
Browse code

app_lua: exposed inter-module api

Daniel-Constantin Mierla authored on 29/05/2019 11:32:04
Showing 1 changed files
... ...
@@ -2016,3 +2016,17 @@ int app_lua_init_rpc(void)
2016 2016
 	}
2017 2017
 	return 0;
2018 2018
 }
2019
+
2020
+/**
2021
+ *
2022
+ */
2023
+int bind_app_lua(app_lua_api_t* api)
2024
+{
2025
+	if (!api) {
2026
+		ERR("Invalid parameter value\n");
2027
+		return -1;
2028
+	}
2029
+	api->env_get_f = sr_lua_env_get;
2030
+	api->openlibs_register_f = app_lua_openlibs_register;
2031
+	return 0;
2032
+}
Browse code

app_lua: removed old Lua sr module exports

- exporting only KSR via KEMI framework
- Lua sr module is relocated to app_lua_sr kamailio module, to be
obsoleted and removed over the time
- export KSR.pv submodule using implementation from core

Daniel-Constantin Mierla authored on 29/05/2019 09:18:20
Showing 1 changed files
... ...
@@ -32,13 +32,30 @@
32 32
 #include "../../core/data_lump.h"
33 33
 #include "../../core/data_lump_rpl.h"
34 34
 #include "../../core/strutils.h"
35
+#include "../../core/rpc.h"
36
+#include "../../core/rpc_lookup.h"
35 37
 
36 38
 #include "app_lua_api.h"
37
-#include "app_lua_sr.h"
38
-#include "app_lua_exp.h"
39
+#include "app_lua_kemi_export.h"
39 40
 
41
+#define KSRVERSION "2.0"
40 42
 
41
-#define SRVERSION "1.0"
43
+#define KSR_APP_LUA_LOG_EXPORTS (1<<0)
44
+
45
+extern int _ksr_app_lua_log_mode;
46
+
47
+void lua_sr_kemi_register_libs(lua_State *L);
48
+
49
+static app_lua_openlibs_f app_lua_openlibs_cb = NULL;
50
+
51
+/**
52
+ *
53
+ */
54
+int app_lua_openlibs_register(app_lua_openlibs_f rfunc)
55
+{
56
+	app_lua_openlibs_cb = rfunc;
57
+	return 0;
58
+}
42 59
 
43 60
 /**
44 61
  * reload enabled param
... ...
@@ -151,17 +168,6 @@ int sr_lua_load_script(char *script)
151 168
 	return 0;
152 169
 }
153 170
 
154
-
155
-/**
156
- *
157
- */
158
-int sr_lua_register_module(char *mname)
159
-{
160
-	if(lua_sr_exp_register_mod(mname)==0)
161
-		return 0;
162
-	return -1;
163
-}
164
-
165 171
 /**
166 172
  *
167 173
  */
... ...
@@ -183,8 +189,9 @@ int sr_lua_reload_module(unsigned int reload)
183 189
  */
184 190
 void lua_sr_openlibs(lua_State *L)
185 191
 {
186
-	lua_sr_core_openlibs(L);
187
-	lua_sr_exp_openlibs(L);
192
+	if(app_lua_openlibs_cb!=NULL) {
193
+		app_lua_openlibs_cb(L);
194
+	}
188 195
 	lua_sr_kemi_register_libs(L);
189 196
 }
190 197
 
... ...
@@ -194,15 +201,12 @@ void lua_sr_openlibs(lua_State *L)
194 201
 int lua_sr_init_mod(void)
195 202
 {
196 203
 	/* allocate shm */
197
-	if(lua_sr_alloc_script_ver()<0)
198
-	{
204
+	if(lua_sr_alloc_script_ver()<0) {
199 205
 		LM_CRIT("failed to alloc shm for version\n");
200 206
 		return -1;
201 207
 	}
202 208
 
203 209
 	memset(&_sr_L_env, 0, sizeof(sr_lua_env_t));
204
-	if(lua_sr_exp_init_mod()<0)
205
-		return -1;
206 210
 
207 211
 	return 0;
208 212
 
... ...
@@ -228,7 +232,7 @@ int lua_sr_init_probe(void)
228 232
 	lua_sr_openlibs(L);
229 233
 
230 234
 	/* force loading lua lib now */
231
-	if(luaL_dostring(L, "sr.probe()")!=0)
235
+	if(luaL_dostring(L, "KSR.x.probe()")!=0)
232 236
 	{
233 237
 		txt = (char*)lua_tostring(L, -1);
234 238
 		LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
... ...
@@ -278,13 +282,13 @@ int lua_sr_init_child(void)
278 282
 	luaL_openlibs(_sr_L_env.L);
279 283
 	lua_sr_openlibs(_sr_L_env.L);
280 284
 
281
-	/* set SR lib version */
285
+	/* set KSR lib version */
282 286
 #if LUA_VERSION_NUM >= 502
283
-	lua_pushstring(_sr_L_env.L, SRVERSION);
284
-	lua_setglobal(_sr_L_env.L, "SRVERSION");
287
+	lua_pushstring(_sr_L_env.L, KSRVERSION);
288
+	lua_setglobal(_sr_L_env.L, "KSRVERSION");
285 289
 #else
286
-	lua_pushstring(_sr_L_env.L, "SRVERSION");
287
-	lua_pushstring(_sr_L_env.L, SRVERSION);
290
+	lua_pushstring(_sr_L_env.L, "KSRVERSION");
291
+	lua_pushstring(_sr_L_env.L, KSRVERSION);
288 292
 	lua_settable(_sr_L_env.L, LUA_GLOBALSINDEX);
289 293
 #endif
290 294
 	if(_sr_lua_load_list != NULL)
... ...
@@ -300,15 +304,15 @@ int lua_sr_init_child(void)
300 304
 
301 305
 		/* set SR lib version */
302 306
 #if LUA_VERSION_NUM >= 502
303
-		lua_pushstring(_sr_L_env.LL, SRVERSION);
304
-		lua_setglobal(_sr_L_env.LL, "SRVERSION");
307
+		lua_pushstring(_sr_L_env.LL, KSRVERSION);
308
+		lua_setglobal(_sr_L_env.LL, "KSRVERSION");
305 309
 #else
306
-		lua_pushstring(_sr_L_env.LL, "SRVERSION");
307
-		lua_pushstring(_sr_L_env.LL, SRVERSION);
310
+		lua_pushstring(_sr_L_env.LL, "KSRVERSION");
311
+		lua_pushstring(_sr_L_env.LL, KSRVERSION);
308 312
 		lua_settable(_sr_L_env.LL, LUA_GLOBALSINDEX);
309 313
 #endif
310 314
 		/* force loading lua lib now */
311
-		if(luaL_dostring(_sr_L_env.LL, "sr.probe()")!=0)
315
+		if(luaL_dostring(_sr_L_env.LL, "KSR.x.probe()")!=0)
312 316
 		{
313 317
 			txt = (char*)lua_tostring(_sr_L_env.LL, -1);
314 318
 			LM_ERR("error initializing Lua: %s\n", (txt)?txt:"unknown");
... ...
@@ -786,3 +790,1229 @@ void app_lua_dump_stack(lua_State *L)
786 790
 		}
787 791
 	}
788 792
 }
793
+
794
+/**
795
+ *
796
+ */
797
+int sr_kemi_lua_return_int(lua_State* L, sr_kemi_t *ket, int rc)
798
+{
799
+	if(ket->rtype==SR_KEMIP_INT) {
800
+		lua_pushinteger(L, rc);
801
+		return 1;
802
+	}
803
+	if(ket->rtype==SR_KEMIP_BOOL && rc!=SR_KEMI_FALSE) {
804
+		return app_lua_return_true(L);
805
+	}
806
+	return app_lua_return_false(L);
807
+}
808
+
809
+
810
+/**
811
+ *
812
+ */
813
+int sr_kemi_lua_return_xval(lua_State* L, sr_kemi_t *ket, sr_kemi_xval_t *rx)
814
+{
815
+	switch(rx->vtype) {
816
+		case SR_KEMIP_NONE:
817
+			return 0;
818
+		case SR_KEMIP_INT:
819
+			lua_pushinteger(L, rx->v.n);
820
+			return 1;
821
+		case SR_KEMIP_STR:
822
+			lua_pushlstring(L, rx->v.s.s, rx->v.s.len);
823
+			return 1;
824
+		case SR_KEMIP_BOOL:
825
+			if(rx->v.n!=SR_KEMI_FALSE) {
826
+				lua_pushboolean(L, SRLUA_TRUE);
827
+			} else {
828
+				lua_pushboolean(L, SRLUA_FALSE);
829
+			}
830
+			return 1;
831
+		case SR_KEMIP_XVAL:
832
+			/* unknown content - return false */
833
+			lua_pushboolean(L, SRLUA_FALSE);
834
+			return 1;
835
+		case SR_KEMIP_NULL:
836
+			lua_pushnil(L);
837
+			return 1;
838
+		default:
839
+			/* unknown type - return false */
840
+			lua_pushboolean(L, SRLUA_FALSE);
841
+			return 1;
842
+	}
843
+}
844
+
845
+/**
846
+ *
847
+ */
848
+int sr_kemi_lua_exec_func_ex(lua_State* L, sr_kemi_t *ket, int pdelta)
849
+{
850
+	int i;
851
+	int argc;
852
+	int ret;
853
+	str *fname;
854
+	str *mname;
855
+	sr_kemi_val_t vps[SR_KEMI_PARAMS_MAX];
856
+	sr_lua_env_t *env_L;
857
+	sr_kemi_xval_t *xret;
858
+
859
+	env_L = sr_lua_env_get();
860
+
861
+	if(env_L==NULL || env_L->msg==NULL || ket==NULL) {
862
+		LM_ERR("invalid Lua environment attributes or parameters\n");
863
+		return app_lua_return_false(L);
864
+	}
865
+
866
+	fname = &ket->fname;
867
+	mname = &ket->mname;
868
+
869
+	argc = lua_gettop(L);
870
+	if(argc==pdelta && ket->ptypes[0]==SR_KEMIP_NONE) {
871
+		if(ket->rtype==SR_KEMIP_XVAL) {
872
+			xret = ((sr_kemi_xfm_f)(ket->func))(env_L->msg);
873
+			return sr_kemi_lua_return_xval(L, ket, xret);
874
+		} else {
875
+			ret = ((sr_kemi_fm_f)(ket->func))(env_L->msg);
876
+			return sr_kemi_lua_return_int(L, ket, ret);
877
+		}
878
+	}
879
+	if(argc==pdelta && ket->ptypes[0]!=SR_KEMIP_NONE) {
880
+		LM_ERR("invalid number of parameters for: %.*s.%.*s\n",
881
+				mname->len, mname->s, fname->len, fname->s);
882
+		return app_lua_return_false(L);
883
+	}
884
+
885
+	if(argc>=SR_KEMI_PARAMS_MAX+pdelta) {
886
+		LM_ERR("too many parameters for: %.*s.%.*s\n",
887
+				mname->len, mname->s, fname->len, fname->s);
888
+		return app_lua_return_false(L);
889
+	}
890
+
891
+	memset(vps, 0, SR_KEMI_PARAMS_MAX*sizeof(sr_kemi_val_t));
892
+	for(i=0; i<SR_KEMI_PARAMS_MAX; i++) {
893
+		if(ket->ptypes[i]==SR_KEMIP_NONE) {
894
+			break;
895
+		}
896
+		if(argc<i+pdelta+1) {
897
+			LM_ERR("not enough parameters for: %.*s.%.*s\n",
898
+					mname->len, mname->s, fname->len, fname->s);
899
+			return app_lua_return_false(L);
900
+		}
901
+		if(ket->ptypes[i]==SR_KEMIP_STR) {
902
+			vps[i].s.s = (char*)lua_tostring(L, i+pdelta+1);
903
+			if(vps[i].s.s!=NULL) {
904
+				if(lua_isstring(L, i+pdelta+1)) {
905
+#if LUA_VERSION_NUM > 501
906
+					vps[i].s.len = lua_rawlen(L, i+pdelta+1);
907
+#else
908
+					vps[i].s.len = lua_strlen(L, i+pdelta+1);
909
+#endif
910
+				} else {
911
+					vps[i].s.len = strlen(vps[i].s.s);
912
+				}
913
+			} else {
914
+				vps[i].s.len = 0;
915
+			}
916
+			LM_DBG("param[%d] for: %.*s is str: %.*s\n", i,
917
+				fname->len, fname->s, vps[i].s.len, vps[i].s.s);
918
+		} else if(ket->ptypes[i]==SR_KEMIP_INT) {
919
+			vps[i].n = lua_tointeger(L, i+pdelta+1);
920
+			LM_DBG("param[%d] for: %.*s is int: %d\n", i,
921
+				fname->len, fname->s, vps[i].n);
922
+		} else {
923
+			LM_ERR("unknown parameter type %d (%d)\n", ket->ptypes[i], i);
924
+			return app_lua_return_false(L);
925
+		}
926
+	}
927
+
928
+	switch(i) {
929
+		case 1:
930
+			if(ket->ptypes[0]==SR_KEMIP_INT) {
931
+				if(ket->rtype==SR_KEMIP_XVAL) {
932
+					xret = ((sr_kemi_xfmn_f)(ket->func))(env_L->msg, vps[0].n);
933
+					return sr_kemi_lua_return_xval(L, ket, xret);
934
+				} else {
935
+					ret = ((sr_kemi_fmn_f)(ket->func))(env_L->msg, vps[0].n);
936
+					return sr_kemi_lua_return_int(L, ket, ret);
937
+				}
938
+			} else if(ket->ptypes[0]==SR_KEMIP_STR) {
939
+				if(ket->rtype==SR_KEMIP_XVAL) {
940
+					xret = ((sr_kemi_xfms_f)(ket->func))(env_L->msg, &vps[0].s);
941
+					return sr_kemi_lua_return_xval(L, ket, xret);
942
+				} else {
943
+					ret = ((sr_kemi_fms_f)(ket->func))(env_L->msg, &vps[0].s);
944
+					return sr_kemi_lua_return_int(L, ket, ret);
945
+				}
946
+			} else {
947
+				LM_ERR("invalid parameters for: %.*s\n",
948
+						fname->len, fname->s);
949
+				return app_lua_return_false(L);
950
+			}
951
+		break;
952
+		case 2:
953
+			if(ket->ptypes[0]==SR_KEMIP_INT) {
954
+				if(ket->ptypes[1]==SR_KEMIP_INT) {
955
+					if(ket->rtype==SR_KEMIP_XVAL) {
956
+						xret = ((sr_kemi_xfmnn_f)(ket->func))(env_L->msg, vps[0].n, vps[1].n);
957
+						return sr_kemi_lua_return_xval(L, ket, xret);
958
+					} else {
959
+						ret = ((sr_kemi_fmnn_f)(ket->func))(env_L->msg, vps[0].n, vps[1].n);
960
+						return sr_kemi_lua_return_int(L, ket, ret);
961
+					}
962
+				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
963
+					if(ket->rtype==SR_KEMIP_XVAL) {
964
+						xret = ((sr_kemi_xfmns_f)(ket->func))(env_L->msg, vps[0].n, &vps[1].s);
965
+						return sr_kemi_lua_return_xval(L, ket, xret);
966
+					} else {
967
+						ret = ((sr_kemi_fmns_f)(ket->func))(env_L->msg, vps[0].n, &vps[1].s);
968
+						return sr_kemi_lua_return_int(L, ket, ret);
969
+					}
970
+				} else {
971
+					LM_ERR("invalid parameters for: %.*s\n",
972
+							fname->len, fname->s);
973
+					return app_lua_return_false(L);
974
+				}
975
+			} else if(ket->ptypes[0]==SR_KEMIP_STR) {
976
+				if(ket->ptypes[1]==SR_KEMIP_INT) {
977
+					if(ket->rtype==SR_KEMIP_XVAL) {
978
+						xret = ((sr_kemi_xfmsn_f)(ket->func))(env_L->msg, &vps[0].s, vps[1].n);
979
+						return sr_kemi_lua_return_xval(L, ket, xret);
980
+					} else {
981
+						ret = ((sr_kemi_fmsn_f)(ket->func))(env_L->msg, &vps[0].s, vps[1].n);
982
+						return sr_kemi_lua_return_int(L, ket, ret);
983
+					}
984
+				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
985
+					if(ket->rtype==SR_KEMIP_XVAL) {
986
+						xret = ((sr_kemi_xfmss_f)(ket->func))(env_L->msg, &vps[0].s, &vps[1].s);
987
+						return sr_kemi_lua_return_xval(L, ket, xret);
988
+					} else {
989
+						ret = ((sr_kemi_fmss_f)(ket->func))(env_L->msg, &vps[0].s, &vps[1].s);
990
+						return sr_kemi_lua_return_int(L, ket, ret);
991
+					}
992
+				} else {
993
+					LM_ERR("invalid parameters for: %.*s\n",
994
+							fname->len, fname->s);
995
+					return app_lua_return_false(L);
996
+				}
997
+			} else {
998
+				LM_ERR("invalid parameters for: %.*s\n",
999
+						fname->len, fname->s);
1000
+				return app_lua_return_false(L);
1001
+			}
1002
+		break;
1003
+		case 3:
1004
+			if(ket->ptypes[0]==SR_KEMIP_INT) {
1005
+				if(ket->ptypes[1]==SR_KEMIP_INT) {
1006
+					if(ket->ptypes[2]==SR_KEMIP_INT) {
1007
+						ret = ((sr_kemi_fmnnn_f)(ket->func))(env_L->msg,
1008
+								vps[0].n, vps[1].n, vps[2].n);
1009
+						return sr_kemi_lua_return_int(L, ket, ret);
1010
+					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1011
+						ret = ((sr_kemi_fmnns_f)(ket->func))(env_L->msg,
1012
+								vps[0].n, vps[1].n, &vps[2].s);
1013
+						return sr_kemi_lua_return_int(L, ket, ret);
1014
+					} else {
1015
+						LM_ERR("invalid parameters for: %.*s\n",
1016
+								fname->len, fname->s);
1017
+						return app_lua_return_false(L);
1018
+					}
1019
+				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
1020
+					if(ket->ptypes[2]==SR_KEMIP_INT) {
1021
+						ret = ((sr_kemi_fmnsn_f)(ket->func))(env_L->msg,
1022
+								vps[0].n, &vps[1].s, vps[2].n);
1023
+						return sr_kemi_lua_return_int(L, ket, ret);
1024
+					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1025
+						ret = ((sr_kemi_fmnss_f)(ket->func))(env_L->msg,
1026
+								vps[0].n, &vps[1].s, &vps[2].s);
1027
+						return sr_kemi_lua_return_int(L, ket, ret);
1028
+					} else {
1029
+						LM_ERR("invalid parameters for: %.*s\n",
1030
+								fname->len, fname->s);
1031
+						return app_lua_return_false(L);
1032
+					}
1033
+				} else {
1034
+					LM_ERR("invalid parameters for: %.*s\n",
1035
+							fname->len, fname->s);
1036
+					return app_lua_return_false(L);
1037
+				}
1038
+			} else if(ket->ptypes[0]==SR_KEMIP_STR) {
1039
+				if(ket->ptypes[1]==SR_KEMIP_INT) {
1040
+					if(ket->ptypes[2]==SR_KEMIP_INT) {
1041
+						ret = ((sr_kemi_fmsnn_f)(ket->func))(env_L->msg,
1042
+								&vps[0].s, vps[1].n, vps[2].n);
1043
+						return sr_kemi_lua_return_int(L, ket, ret);
1044
+					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1045
+						ret = ((sr_kemi_fmsns_f)(ket->func))(env_L->msg,
1046
+								&vps[0].s, vps[1].n, &vps[2].s);
1047
+						return sr_kemi_lua_return_int(L, ket, ret);
1048
+					} else {
1049
+						LM_ERR("invalid parameters for: %.*s\n",
1050
+								fname->len, fname->s);
1051
+						return app_lua_return_false(L);
1052
+					}
1053
+				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
1054
+					if(ket->ptypes[2]==SR_KEMIP_INT) {
1055
+						ret = ((sr_kemi_fmssn_f)(ket->func))(env_L->msg,
1056
+								&vps[0].s, &vps[1].s, vps[2].n);
1057
+						return sr_kemi_lua_return_int(L, ket, ret);
1058
+					} else if(ket->ptypes[2]==SR_KEMIP_STR) {
1059
+						ret = ((sr_kemi_fmsss_f)(ket->func))(env_L->msg,
1060
+								&vps[0].s, &vps[1].s, &vps[2].s);
1061
+						return sr_kemi_lua_return_int(L, ket, ret);
1062
+					} else {
1063
+						LM_ERR("invalid parameters for: %.*s\n",
1064
+								fname->len, fname->s);
1065
+						return app_lua_return_false(L);
1066
+					}
1067
+				} else {
1068
+					LM_ERR("invalid parameters for: %.*s\n",
1069
+							fname->len, fname->s);
1070
+					return app_lua_return_false(L);
1071
+				}
1072
+			} else {
1073
+				LM_ERR("invalid parameters for: %.*s\n",
1074
+						fname->len, fname->s);
1075
+				return app_lua_return_false(L);
1076
+			}
1077
+		break;
1078
+		case 4:
1079
+			if(ket->ptypes[0]==SR_KEMIP_STR
1080
+					&& ket->ptypes[1]==SR_KEMIP_STR
1081
+					&& ket->ptypes[2]==SR_KEMIP_STR
1082
+					&& ket->ptypes[3]==SR_KEMIP_STR) {
1083
+				ret = ((sr_kemi_fmssss_f)(ket->func))(env_L->msg,
1084
+						&vps[0].s, &vps[1].s, &vps[2].s, &vps[3].s);
1085
+				return sr_kemi_lua_return_int(L, ket, ret);
1086
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1087
+					&& ket->ptypes[1]==SR_KEMIP_STR
1088
+					&& ket->ptypes[2]==SR_KEMIP_STR
1089
+					&& ket->ptypes[3]==SR_KEMIP_INT) {
1090
+				ret = ((sr_kemi_fmsssn_f)(ket->func))(env_L->msg,
1091
+						&vps[0].s, &vps[1].s, &vps[2].s, vps[3].n);
1092
+				return sr_kemi_lua_return_int(L, ket, ret);
1093
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1094
+					&& ket->ptypes[1]==SR_KEMIP_STR
1095
+					&& ket->ptypes[2]==SR_KEMIP_INT
1096
+					&& ket->ptypes[3]==SR_KEMIP_STR) {
1097
+				ret = ((sr_kemi_fmssns_f)(ket->func))(env_L->msg,
1098
+						&vps[0].s, &vps[1].s, vps[2].n, &vps[3].s);
1099
+				return sr_kemi_lua_return_int(L, ket, ret);
1100
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1101
+					&& ket->ptypes[1]==SR_KEMIP_STR
1102
+					&& ket->ptypes[2]==SR_KEMIP_INT
1103
+					&& ket->ptypes[3]==SR_KEMIP_INT) {
1104
+				ret = ((sr_kemi_fmssnn_f)(ket->func))(env_L->msg,
1105
+						&vps[0].s, &vps[1].s, vps[2].n, vps[3].n);
1106
+				return sr_kemi_lua_return_int(L, ket, ret);
1107
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1108
+					&& ket->ptypes[1]==SR_KEMIP_INT
1109
+					&& ket->ptypes[2]==SR_KEMIP_STR
1110
+					&& ket->ptypes[3]==SR_KEMIP_STR) {
1111
+				ret = ((sr_kemi_fmsnss_f)(ket->func))(env_L->msg,
1112
+						&vps[0].s, vps[1].n, &vps[2].s, &vps[3].s);
1113
+				return sr_kemi_lua_return_int(L, ket, ret);
1114
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1115
+					&& ket->ptypes[1]==SR_KEMIP_INT
1116
+					&& ket->ptypes[2]==SR_KEMIP_STR
1117
+					&& ket->ptypes[3]==SR_KEMIP_INT) {
1118
+				ret = ((sr_kemi_fmsnsn_f)(ket->func))(env_L->msg,
1119
+						&vps[0].s, vps[1].n, &vps[2].s, vps[3].n);
1120
+				return sr_kemi_lua_return_int(L, ket, ret);
1121
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1122
+					&& ket->ptypes[1]==SR_KEMIP_INT
1123
+					&& ket->ptypes[2]==SR_KEMIP_INT
1124
+					&& ket->ptypes[3]==SR_KEMIP_STR) {
1125
+				ret = ((sr_kemi_fmsnns_f)(ket->func))(env_L->msg,
1126
+						&vps[0].s, vps[1].n, vps[2].n, &vps[3].s);
1127
+				return sr_kemi_lua_return_int(L, ket, ret);
1128
+			} else if(ket->ptypes[0]==SR_KEMIP_STR
1129
+					&& ket->ptypes[1]==SR_KEMIP_INT
1130
+					&& ket->ptypes[2]==SR_KEMIP_INT
1131
+					&& ket->ptypes[3]==SR_KEMIP_INT) {
1132
+				ret = ((sr_kemi_fmsnnn_f)(ket->func))(env_L->msg,
1133
+						&vps[0].s, vps[1].n, vps[2].n, vps[3].n);