lualib: allow to replace error handling function

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-08-27 17:53:51 +02:00
parent 3f259d0ed2
commit 3739aabda1
2 changed files with 25 additions and 11 deletions

View File

@ -25,6 +25,9 @@
#include <lauxlib.h> #include <lauxlib.h>
#include "common/util.h" #include "common/util.h"
/** Lua function to call on dofuction() error */
lua_CFunction lualib_dofunction_on_error;
#define luaA_checkfunction(L, n) \ #define luaA_checkfunction(L, n) \
do { \ do { \
if(!lua_isfunction(L, n)) \ if(!lua_isfunction(L, n)) \
@ -79,17 +82,9 @@ luaA_absindex(lua_State *L, int ud)
static inline int static inline int
luaA_dofunction_error(lua_State *L) luaA_dofunction_error(lua_State *L)
{ {
if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)")) if(lualib_dofunction_on_error)
{ return lualib_dofunction_on_error(L);
/* Move traceback before error */ return 0;
lua_insert(L, -2);
/* Insert sentence */
lua_pushliteral(L, "\nerror: ");
/* Move it before error */
lua_insert(L, -2);
lua_concat(L, 3);
}
return 1;
} }
/** Execute an Lua function on top of the stack. /** Execute an Lua function on top of the stack.

19
luaa.c
View File

@ -680,6 +680,22 @@ luaA_panic(lua_State *L)
return 0; 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 /** Initialize the Lua VM
* \param xdg An xdg handle to use to get XDG basedir. * \param xdg An xdg handle to use to get XDG basedir.
*/ */
@ -706,6 +722,9 @@ luaA_init(xdgHandle* xdg)
/* Set panic function */ /* Set panic function */
lua_atpanic(L, luaA_panic); lua_atpanic(L, luaA_panic);
/* Set error handling function */
lualib_dofunction_on_error = luaA_dofunction_on_error;
luaL_openlibs(L); luaL_openlibs(L);
luaA_fixups(L); luaA_fixups(L);