From dbd46b4e1d8b37534992e9339b29df308688b34c Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Wed, 29 May 2019 13:32:04 +0200 Subject: [PATCH] app_lua: exposed inter-module api --- src/modules/app_lua/app_lua_api.c | 14 ++++++ src/modules/app_lua/app_lua_api.h | 30 ++----------- src/modules/app_lua/app_lua_mod.c | 2 + src/modules/app_lua/modapi.h | 75 +++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 src/modules/app_lua/modapi.h diff --git a/src/modules/app_lua/app_lua_api.c b/src/modules/app_lua/app_lua_api.c index 6a4d8668f88..f80de0909e7 100644 --- a/src/modules/app_lua/app_lua_api.c +++ b/src/modules/app_lua/app_lua_api.c @@ -2016,3 +2016,17 @@ int app_lua_init_rpc(void) } return 0; } + +/** + * + */ +int bind_app_lua(app_lua_api_t* api) +{ + if (!api) { + ERR("Invalid parameter value\n"); + return -1; + } + api->env_get_f = sr_lua_env_get; + api->openlibs_register_f = app_lua_openlibs_register; + return 0; +} diff --git a/src/modules/app_lua/app_lua_api.h b/src/modules/app_lua/app_lua_api.h index 0348cc5a421..f6514937fab 100644 --- a/src/modules/app_lua/app_lua_api.h +++ b/src/modules/app_lua/app_lua_api.h @@ -29,6 +29,8 @@ #include "../../core/parser/msg_parser.h" #include "../../core/kemi.h" +#include "modapi.h" + /** * version variable stores a version counter for each script loaded. * This counter will be updated via RPC. @@ -39,15 +41,6 @@ typedef struct _sr_lua_script_ver unsigned int len; /* length of version array */ } sr_lua_script_ver_t; -typedef struct _sr_lua_env -{ - lua_State *L; - lua_State *LL; - struct sip_msg *msg; - unsigned int flags; - unsigned int nload; /* number of scripts loaded */ -} sr_lua_env_t; - typedef struct _sr_lua_load { char *script; @@ -55,8 +48,6 @@ typedef struct _sr_lua_load struct _sr_lua_load *next; } sr_lua_load_t; -typedef void (*app_lua_openlibs_f)(lua_State *L); - int app_lua_openlibs_register(app_lua_openlibs_f rfunc); sr_lua_env_t *sr_lua_env_get(void); @@ -65,12 +56,8 @@ int lua_sr_initialized(void); int lua_sr_init_mod(void); int lua_sr_init_child(void); void lua_sr_destroy(void); -int lua_sr_init_probe(void); -int lua_sr_reload_script(int pos); -int lua_sr_list_script(sr_lua_load_t **list); int sr_lua_load_script(char *script); -int sr_lua_reload_script(void); int sr_lua_reload_module(unsigned int reload); int app_lua_dostring(struct sip_msg *msg, char *script); @@ -81,21 +68,10 @@ int app_lua_run(sip_msg_t *msg, char *func, char *p1, char *p2, int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2, char *p3, int emode); -#define SRLUA_FALSE 0 -#define SRLUA_TRUE 1 -int app_lua_return_boolean(lua_State *L, int b); -int app_lua_return_false(lua_State *L); -int app_lua_return_true(lua_State *L); -int app_lua_return_int(lua_State *L, int v); -int app_lua_return_error(lua_State *L); - -void app_lua_dump_stack(lua_State *L); - -str* sr_kemi_lua_exit_string_get(void); - int sr_kemi_lua_exec_func(lua_State* L, int eidx); int app_lua_init_rpc(void); +int bind_app_lua(app_lua_api_t* api); #endif diff --git a/src/modules/app_lua/app_lua_mod.c b/src/modules/app_lua/app_lua_mod.c index d8e4ac1073b..5c3bebfd8ef 100644 --- a/src/modules/app_lua/app_lua_mod.c +++ b/src/modules/app_lua/app_lua_mod.c @@ -84,6 +84,8 @@ static cmd_export_t cmds[]={ 0, ANY_ROUTE}, {"lua_run", (cmd_function)w_app_lua_run3, 4, fixup_lua_run, 0, ANY_ROUTE}, + {"bind_app_lua", (cmd_function)bind_app_lua, 0, 0, 0, + ANY_ROUTE}, {0, 0, 0, 0, 0, 0} }; diff --git a/src/modules/app_lua/modapi.h b/src/modules/app_lua/modapi.h new file mode 100644 index 00000000000..b61835cdc5b --- /dev/null +++ b/src/modules/app_lua/modapi.h @@ -0,0 +1,75 @@ +/** + * Copyright (C) 2010-2016 Daniel-Constantin Mierla (asipto.com) + * + * This file is part of Kamailio, a free SIP server. + * + * Kamailio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version + * + * Kamailio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef _APP_LUA_MODAPI_H_ +#define _APP_LUA_MODAPI_H_ + +#include +#include +#include + +#include "../../core/sr_module.h" + +#define SRLUA_FALSE 0 +#define SRLUA_TRUE 1 + +typedef struct _sr_lua_env +{ + lua_State *L; + lua_State *LL; + struct sip_msg *msg; + unsigned int flags; + unsigned int nload; /* number of scripts loaded */ +} sr_lua_env_t; + +typedef int (*app_lua_openlibs_f)(lua_State *L); + +typedef sr_lua_env_t* (*app_lua_env_get_f)(void); +typedef int (*app_lua_openlibs_register_f)(app_lua_openlibs_f rfunc); + +typedef struct app_lua_api { + app_lua_env_get_f env_get_f; + app_lua_openlibs_register_f openlibs_register_f; +} app_lua_api_t; + +typedef int (*bind_app_lua_f)(app_lua_api_t* api); +int bind_app_lua(app_lua_api_t* api); + +/** + * @brief Load the app_lua API + */ +static inline int app_lua_load_api(app_lua_api_t *api) +{ + bind_app_lua_f bindapplua; + + bindapplua = (bind_app_lua_f)find_export("bind_app_lua", 0, 0); + if(bindapplua == 0) { + LM_ERR("cannot find bind_app_lua\n"); + return -1; + } + if(bindapplua(api)<0) { + LM_ERR("cannot bind app_lua api\n"); + return -1; + } + return 0; +} + +#endif