From 3739aabda1323dc488c5150aed166e2fd37edf56 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 27 Aug 2009 17:53:51 +0200 Subject: [PATCH] lualib: allow to replace error handling function Signed-off-by: Julien Danjou --- common/lualib.h | 17 ++++++----------- luaa.c | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/common/lualib.h b/common/lualib.h index dce2d5d3..291d4b12 100644 --- a/common/lualib.h +++ b/common/lualib.h @@ -25,6 +25,9 @@ #include #include "common/util.h" +/** Lua function to call on dofuction() error */ +lua_CFunction lualib_dofunction_on_error; + #define luaA_checkfunction(L, n) \ do { \ if(!lua_isfunction(L, n)) \ @@ -79,17 +82,9 @@ luaA_absindex(lua_State *L, int ud) static inline int luaA_dofunction_error(lua_State *L) { - if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)")) - { - /* Move traceback before error */ - lua_insert(L, -2); - /* Insert sentence */ - lua_pushliteral(L, "\nerror: "); - /* Move it before error */ - lua_insert(L, -2); - lua_concat(L, 3); - } - return 1; + if(lualib_dofunction_on_error) + return lualib_dofunction_on_error(L); + return 0; } /** Execute an Lua function on top of the stack. diff --git a/luaa.c b/luaa.c index d4841723..7c94dac0 100644 --- a/luaa.c +++ b/luaa.c @@ -680,6 +680,22 @@ luaA_panic(lua_State *L) return 0; } +static int +luaA_dofunction_on_error(lua_State *L) +{ + if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)")) + { + /* Move traceback before error */ + lua_insert(L, -2); + /* Insert sentence */ + lua_pushliteral(L, "\nerror: "); + /* Move it before error */ + lua_insert(L, -2); + lua_concat(L, 3); + } + return 1; +} + /** Initialize the Lua VM * \param xdg An xdg handle to use to get XDG basedir. */ @@ -706,6 +722,9 @@ luaA_init(xdgHandle* xdg) /* Set panic function */ lua_atpanic(L, luaA_panic); + /* Set error handling function */ + lualib_dofunction_on_error = luaA_dofunction_on_error; + luaL_openlibs(L); luaA_fixups(L); -- 2.11.4.GIT