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 --- tests/lapi.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'tests/lapi.js') diff --git a/tests/lapi.js b/tests/lapi.js index b739860..1dbdd5c 100644 --- a/tests/lapi.js +++ b/tests/lapi.js @@ -481,3 +481,43 @@ test('lua_settable, lua_gettable', function (t) { "Correct element(s) on the stack" ); }); + +test('lua_atnativeerror', function(t) { + t.plan(7); + + let L = lauxlib.luaL_newstate(); + let errob = {}; + + lua.lua_pushcfunction(L, function(L) { + throw errob; + }); + t.strictEqual(lua.lua_pcall(L, 0, 0, 0), -1, "without a native error handler pcall should be -1"); + lua.lua_pop(L, 1); + + + lua.lua_atnativeerror(L, function(L) { + let e = lua.lua_touserdata(L, 1); + t.strictEqual(e, errob); + lua.lua_pushstring(L, lua.to_luastring("runtime error!")); + return 1; + }); + lua.lua_pushcfunction(L, function(L) { + throw errob; + }); + t.strictEqual(lua.lua_pcall(L, 0, 0, 0), lua.LUA_ERRRUN, "should return lua.LUA_ERRRUN"); + t.strictEqual(lua.lua_tojsstring(L, -1), "runtime error!"); + lua.lua_pop(L, 1); + + + lua.lua_atnativeerror(L, function(L) { + let e = lua.lua_touserdata(L, 1); + t.strictEqual(e, errob); + lauxlib.luaL_error(L, lua.to_luastring("runtime error!")); + }); + lua.lua_pushcfunction(L, function(L) { + throw errob; + }); + t.strictEqual(lua.lua_pcall(L, 0, 0, 0), lua.LUA_ERRRUN, "luaL_error from a native error handler should result in LUA_ERRRUN"); + t.strictEqual(lua.lua_tojsstring(L, -1), "runtime error!"); + lua.lua_pop(L, 1); +}); -- cgit v1.2.3-54-g00ecf