#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;
arg_count++;
}
- lua_call(L, arg_count, 1);
+ docall(L, arg_count, 1, 1);
err = lua_tostring(L, -1);
if (!zstr(err)) {
arg_count++;
}
- lua_call(L, arg_count, 1);
+ docall(L, arg_count, 0, 1);
+
ret = lua_tostring(L, -1);
lua_pop(L, 1);
arg_count++;
}
- lua_call(L, arg_count, 1);
+ docall(L, arg_count, 1, 1);
ret = lua_tostring(L, -1);
lua_pop(L, 1);
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) {
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 */
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();
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;
}
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) {
}
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);
}
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);
}
}