Skip to content
Snippets Groups Projects
Commit 067da200 authored by George Kiagiadakis's avatar George Kiagiadakis
Browse files

wplua: use the error handler also when loading chunks

... and improve its output
parent 0054559b
No related branches found
No related tags found
No related merge requests found
......@@ -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, &param_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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment