From 4a0aa6eecf10432453c22031c247cf24819f1040 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 12 May 2017 14:52:14 +1000 Subject: Add facility for a user provided (state-global) native error handler --- src/ldo.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/ldo.js') diff --git a/src/ldo.js b/src/ldo.js index bb34216..c4619e5 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -273,12 +273,26 @@ const luaD_rawrunprotected = function(L, f, ud) { } catch (e) { if (lj.status === TS.LUA_OK) { /* error was not thrown via luaD_throw, i.e. it is a JS error */ - lj.status = -1; - /* run error handler (if possible) with error object as light userdata */ - try { - lapi.lua_pushlightuserdata(L, e); - ldebug.luaG_errormsg(L); - } catch (e) {} + /* run user error handler (if it exists) */ + let atnativeerror = L.l_G.atnativeerror; + if (atnativeerror) { + try { + lj.status = TS.LUA_OK; + + lapi.lua_pushcfunction(L, atnativeerror); + lapi.lua_pushlightuserdata(L, e); + luaD_callnoyield(L, L.top - 2, 1); + + lj.status = TS.LUA_ERRRUN; + } catch(e2) { + if (lj.status === TS.LUA_OK) { + /* also failed */ + lj.status = -1; + } + } + } else { + lj.status = -1; + } } } -- cgit v1.2.3-54-g00ecf