mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-07-15 11:29:56 +00:00
FS-3161
This commit is contained in:
parent
00a2b18541
commit
c49c1fdea9
@ -3,6 +3,10 @@
|
|||||||
#include "freeswitch_lua.h"
|
#include "freeswitch_lua.h"
|
||||||
using namespace LUA;
|
using namespace LUA;
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
int docall(lua_State * L, int narg, int clear, int perror);
|
||||||
|
};
|
||||||
|
|
||||||
Session::Session():CoreSession()
|
Session::Session():CoreSession()
|
||||||
{
|
{
|
||||||
cb_function = cb_arg = hangup_func_str = hangup_func_arg = NULL;
|
cb_function = cb_arg = hangup_func_str = hangup_func_arg = NULL;
|
||||||
@ -137,7 +141,7 @@ void Session::do_hangup_hook()
|
|||||||
arg_count++;
|
arg_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_call(L, arg_count, 1);
|
docall(L, arg_count, 1, 1);
|
||||||
err = lua_tostring(L, -1);
|
err = lua_tostring(L, -1);
|
||||||
|
|
||||||
if (!zstr(err)) {
|
if (!zstr(err)) {
|
||||||
@ -273,7 +277,8 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp
|
|||||||
arg_count++;
|
arg_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_call(L, arg_count, 1);
|
docall(L, arg_count, 0, 1);
|
||||||
|
|
||||||
ret = lua_tostring(L, -1);
|
ret = lua_tostring(L, -1);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|
||||||
@ -297,7 +302,7 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp
|
|||||||
arg_count++;
|
arg_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_call(L, arg_count, 1);
|
docall(L, arg_count, 1, 1);
|
||||||
ret = lua_tostring(L, -1);
|
ret = lua_tostring(L, -1);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|
||||||
@ -374,7 +379,7 @@ int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv)
|
|||||||
lua_settable(lua_fun->L, -3);
|
lua_settable(lua_fun->L, -3);
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_call(lua_fun->L, 1, 1); /* 1 in, 1 out */
|
docall(lua_fun->L, 1, 1, 1); /* 1 in, 1 out */
|
||||||
|
|
||||||
if (lua_isnumber(lua_fun->L, -1)) {
|
if (lua_isnumber(lua_fun->L, -1)) {
|
||||||
if (lua_tonumber(lua_fun->L, -1) != 0) {
|
if (lua_tonumber(lua_fun->L, -1) != 0) {
|
||||||
|
@ -80,7 +80,7 @@ static int traceback(lua_State * L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int docall(lua_State * L, int narg, int clear)
|
int docall(lua_State * L, int narg, int clear, int perror)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
int base = lua_gettop(L) - narg; /* function index */
|
int base = lua_gettop(L) - narg; /* function index */
|
||||||
@ -92,13 +92,22 @@ static int docall(lua_State * L, int narg, int clear)
|
|||||||
|
|
||||||
lua_remove(L, base); /* remove traceback function */
|
lua_remove(L, base); /* remove traceback function */
|
||||||
/* force a complete garbage collection in case of errors */
|
/* force a complete garbage collection in case of errors */
|
||||||
if (status != 0)
|
if (status != 0) {
|
||||||
lua_gc(L, LUA_GCCOLLECT, 0);
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status && perror) {
|
||||||
|
const char *err = lua_tostring(L, -1);
|
||||||
|
if (!zstr(err)) {
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n", err);
|
||||||
|
}
|
||||||
|
lua_pop(L, 1); /* pop error message from the stack */
|
||||||
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static lua_State *lua_init(void)
|
static lua_State *lua_init(void)
|
||||||
{
|
{
|
||||||
lua_State *L = lua_open();
|
lua_State *L = lua_open();
|
||||||
@ -111,7 +120,7 @@ static lua_State *lua_init(void)
|
|||||||
luaopen_freeswitch(L);
|
luaopen_freeswitch(L);
|
||||||
lua_gc(L, LUA_GCRESTART, 0);
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
lua_atpanic(L, panic);
|
lua_atpanic(L, panic);
|
||||||
error = luaL_loadbuffer(L, buff, strlen(buff), "line") || docall(L, 0, 1);
|
error = luaL_loadbuffer(L, buff, strlen(buff), "line") || docall(L, 0, 1, 0);
|
||||||
}
|
}
|
||||||
return L;
|
return L;
|
||||||
}
|
}
|
||||||
@ -128,7 +137,7 @@ static int lua_parse_and_execute(lua_State * L, char *input_code)
|
|||||||
|
|
||||||
if (*input_code == '~') {
|
if (*input_code == '~') {
|
||||||
char *buff = input_code + 1;
|
char *buff = input_code + 1;
|
||||||
error = luaL_loadbuffer(L, buff, strlen(buff), "line") || docall(L, 0, 1); //lua_pcall(L, 0, 0, 0);
|
error = luaL_loadbuffer(L, buff, strlen(buff), "line") || docall(L, 0, 1, 0); //lua_pcall(L, 0, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
char *args = strchr(input_code, ' ');
|
char *args = strchr(input_code, ' ');
|
||||||
if (args) {
|
if (args) {
|
||||||
@ -152,14 +161,14 @@ static int lua_parse_and_execute(lua_State * L, char *input_code)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (code) {
|
if (code) {
|
||||||
error = luaL_loadbuffer(L, code, strlen(code), "line") || docall(L, 0, 1);
|
error = luaL_loadbuffer(L, code, strlen(code), "line") || docall(L, 0, 1, 0);
|
||||||
switch_safe_free(code);
|
switch_safe_free(code);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Force empty argv table
|
// Force empty argv table
|
||||||
char *code = NULL;
|
char *code = NULL;
|
||||||
code = switch_mprintf("argv = {[0]='%s'};", input_code);
|
code = switch_mprintf("argv = {[0]='%s'};", input_code);
|
||||||
error = luaL_loadbuffer(L, code, strlen(code), "line") || docall(L, 0, 1);
|
error = luaL_loadbuffer(L, code, strlen(code), "line") || docall(L, 0, 1, 0);
|
||||||
switch_safe_free(code);
|
switch_safe_free(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +180,7 @@ static int lua_parse_and_execute(lua_State * L, char *input_code)
|
|||||||
switch_assert(fdup);
|
switch_assert(fdup);
|
||||||
file = fdup;
|
file = fdup;
|
||||||
}
|
}
|
||||||
error = luaL_loadfile(L, file) || docall(L, 0, 1);
|
error = luaL_loadfile(L, file) || docall(L, 0, 1, 0);
|
||||||
switch_safe_free(fdup);
|
switch_safe_free(fdup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user