mirror of
https://gitlab.freedesktop.org/pipewire/wireplumber.git
synced 2026-05-05 10:08:15 +02:00
wplua: use the error handler also when loading chunks
... and improve its output
This commit is contained in:
parent
0054559b91
commit
067da200fc
3 changed files with 44 additions and 45 deletions
|
|
@ -7,6 +7,7 @@
|
|||
*/
|
||||
|
||||
#include "wplua.h"
|
||||
#include "private.h"
|
||||
#include <wp/wp.h>
|
||||
|
||||
typedef struct _WpLuaClosure WpLuaClosure;
|
||||
|
|
@ -16,47 +17,6 @@ struct _WpLuaClosure
|
|||
int func_ref;
|
||||
};
|
||||
|
||||
static int
|
||||
_wplua_closure_errhandler (lua_State *L)
|
||||
{
|
||||
wp_warning ("%s", lua_tostring (L, -1));
|
||||
lua_pop (L, 1);
|
||||
|
||||
luaL_traceback (L, L, "traceback:\n", 1);
|
||||
wp_warning ("%s", lua_tostring (L, -1));
|
||||
lua_pop (L, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_wplua_closure_pcall (lua_State *L, int nargs, int nret)
|
||||
{
|
||||
int hpos = lua_gettop (L) - nargs;
|
||||
int ret = LUA_OK;
|
||||
|
||||
lua_pushcfunction (L, _wplua_closure_errhandler);
|
||||
lua_insert (L, hpos);
|
||||
|
||||
ret = lua_pcall (L, nargs, nret, hpos);
|
||||
switch (ret) {
|
||||
case LUA_ERRMEM:
|
||||
wp_critical ("not enough memory");
|
||||
break;
|
||||
case LUA_ERRERR:
|
||||
wp_critical ("error running the message handler");
|
||||
break;
|
||||
case LUA_ERRGCMM:
|
||||
wp_critical ("error running __gc");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
lua_remove (L, hpos);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
_wplua_closure_marshal (GClosure *closure, GValue *return_value,
|
||||
guint n_param_values, const GValue *param_values,
|
||||
|
|
@ -81,7 +41,7 @@ _wplua_closure_marshal (GClosure *closure, GValue *return_value,
|
|||
wplua_gvalue_to_lua (L, ¶m_values[i]);
|
||||
|
||||
/* call in protected mode */
|
||||
int res = _wplua_closure_pcall (L, n_param_values, return_value ? 1 : 0);
|
||||
int res = _wplua_pcall (L, n_param_values, return_value ? 1 : 0);
|
||||
|
||||
/* handle the result */
|
||||
if (res == LUA_OK && return_value && lua_gettop (L) >= 1)
|
||||
|
|
|
|||
|
|
@ -29,6 +29,9 @@ gboolean _wplua_isgvalue_userdata (lua_State *L, int idx, GType type);
|
|||
int _wplua_gvalue_userdata___gc (lua_State *L);
|
||||
int _wplua_gvalue_userdata___eq (lua_State *L);
|
||||
|
||||
/* wplua.c */
|
||||
int _wplua_pcall (lua_State *L, int nargs, int nret);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -42,6 +42,43 @@ _wplua_openlibs (lua_State *L)
|
|||
}
|
||||
}
|
||||
|
||||
static int
|
||||
_wplua_errhandler (lua_State *L)
|
||||
{
|
||||
luaL_traceback (L, L, NULL, 1);
|
||||
wp_warning ("%s\n%s", lua_tostring (L, -2), lua_tostring (L, -1));
|
||||
lua_pop (L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
_wplua_pcall (lua_State *L, int nargs, int nret)
|
||||
{
|
||||
int hpos = lua_gettop (L) - nargs;
|
||||
int ret = LUA_OK;
|
||||
|
||||
lua_pushcfunction (L, _wplua_errhandler);
|
||||
lua_insert (L, hpos);
|
||||
|
||||
ret = lua_pcall (L, nargs, nret, hpos);
|
||||
switch (ret) {
|
||||
case LUA_ERRMEM:
|
||||
wp_critical ("not enough memory");
|
||||
break;
|
||||
case LUA_ERRERR:
|
||||
wp_critical ("error running the message handler");
|
||||
break;
|
||||
case LUA_ERRGCMM:
|
||||
wp_critical ("error running __gc");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
lua_remove (L, hpos);
|
||||
return ret;
|
||||
}
|
||||
|
||||
lua_State *
|
||||
wplua_new (void)
|
||||
{
|
||||
|
|
@ -152,11 +189,10 @@ _wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
ret = lua_pcall (L, sandbox, 0, 0);
|
||||
ret = _wplua_pcall (L, sandbox, 0);
|
||||
if (ret != LUA_OK) {
|
||||
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME,
|
||||
"Failed to run: %s", lua_tostring (L, -1));
|
||||
lua_pop (L, 1);
|
||||
"Runtime error while loading '%s'", name);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue