diff --git a/src/mod/languages/mod_lua/freeswitch_lua.cpp b/src/mod/languages/mod_lua/freeswitch_lua.cpp index b388f2e491..c3a63f5396 100644 --- a/src/mod/languages/mod_lua/freeswitch_lua.cpp +++ b/src/mod/languages/mod_lua/freeswitch_lua.cpp @@ -3,6 +3,10 @@ #include "freeswitch_lua.h" using namespace LUA; +extern "C" { + int docall(lua_State * L, int narg, int clear, int perror); +}; + Session::Session():CoreSession() { cb_function = cb_arg = hangup_func_str = hangup_func_arg = NULL; @@ -137,7 +141,7 @@ void Session::do_hangup_hook() arg_count++; } - lua_call(L, arg_count, 1); + docall(L, arg_count, 1, 1); err = lua_tostring(L, -1); if (!zstr(err)) { @@ -273,7 +277,8 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp arg_count++; } - lua_call(L, arg_count, 1); + docall(L, arg_count, 0, 1); + ret = lua_tostring(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++; } - lua_call(L, arg_count, 1); + docall(L, arg_count, 1, 1); ret = lua_tostring(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_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_tonumber(lua_fun->L, -1) != 0) { diff --git a/src/mod/languages/mod_lua/mod_lua.cpp b/src/mod/languages/mod_lua/mod_lua.cpp index 56e49d5f5e..ee1e3e2104 100644 --- a/src/mod/languages/mod_lua/mod_lua.cpp +++ b/src/mod/languages/mod_lua/mod_lua.cpp @@ -80,7 +80,7 @@ static int traceback(lua_State * L) 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 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 */ /* force a complete garbage collection in case of errors */ - if (status != 0) + if (status != 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; } - static lua_State *lua_init(void) { lua_State *L = lua_open(); @@ -111,7 +120,7 @@ static lua_State *lua_init(void) luaopen_freeswitch(L); lua_gc(L, LUA_GCRESTART, 0); 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; } @@ -128,7 +137,7 @@ static int lua_parse_and_execute(lua_State * L, char *input_code) if (*input_code == '~') { 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 { char *args = strchr(input_code, ' '); if (args) { @@ -152,14 +161,14 @@ static int lua_parse_and_execute(lua_State * L, char *input_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); } } else { // Force empty argv table char *code = NULL; 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); } @@ -171,7 +180,7 @@ static int lua_parse_and_execute(lua_State * L, char *input_code) switch_assert(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); } }