From c1824a99035a231172f3c10ae3ee24a3e6330260 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 22 Feb 2017 07:42:36 +0100 Subject: xpcall calls msgh --- README.md | 5 ++--- src/lapi.js | 20 +++++++++++++++++--- src/lbaselib.js | 10 +++++----- src/ldebug.js | 2 +- tests/lbaselib.js | 15 ++++++++++----- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a7d0124..e145ed7 100644 --- a/README.md +++ b/README.md @@ -210,19 +210,18 @@ - [x] type - [x] error - [x] pcall + - [x] xpcall + - [x] collectgarbage (unavailable) - [ ] assert - - [ ] collectgarbage - [ ] dofile - [ ] ipairs - [ ] loadfile - [ ] load - - [ ] loadstring - [ ] next - [ ] pairs - [ ] rawlen - [ ] select - [ ] tonumber - - [ ] xpcall - [ ] ... - [ ] Debug (errors) - [ ] DOM API binding diff --git a/src/lapi.js b/src/lapi.js index 9059ee9..c091150 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -548,9 +548,9 @@ const lua_pcallk = function(L, nargs, nresults, errfunc, ctx, k) { if (errfunc === 0) func = 0; else { - let o = index2addr(L, errfunc); + // let o = index2addr(L, errfunc); // TODO: api_checkstackindex(L, errfunc, o); - func = errfunc; + func = index2addr_(L, errfunc); } c.funcOff = L.top - (nargs + 1); /* function to be called */ @@ -605,6 +605,17 @@ const lua_concat = function(L, n) { } }; +// This functions are only there for compatibility purposes +const lua_gc = function () {}; + +const lua_getallocf = function () { + console.warn("lua_getallocf is not available and will always return null"); + return null; +}; +const lua_getextraspace = function () { + console.warn("lua_getextraspace is not available and will always return null"); + return null; +}; module.exports.lua_pushvalue = lua_pushvalue; module.exports.lua_pushnil = lua_pushnil; @@ -659,4 +670,7 @@ module.exports.lua_settop = lua_settop; module.exports.lua_rawequal = lua_rawequal; module.exports.lua_concat = lua_concat; module.exports.lua_error = lua_error; -module.exports.lua_insert = lua_insert; \ No newline at end of file +module.exports.lua_insert = lua_insert; +module.exports.lua_gc = lua_gc; +module.exports.lua_getallocf = lua_getallocf; +module.exports.lua_getextraspace = lua_getextraspace; \ No newline at end of file diff --git a/src/lbaselib.js b/src/lbaselib.js index ac0ac4c..b3dcd12 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -131,10 +131,10 @@ const luaB_pcall = function(L) { */ const luaB_xpcall = function(L) { let n = lapi.lua_gettop(L); - lauxlib.luaL_checktype(L, 2, CT.LUA_TFUNCTION); - lapi.lua_pushboolean(L, 1); - lapi.lua_pushvalue(L, 1); - lapi.lua_rotate(L, 3, 2); + lauxlib.luaL_checktype(L, 2, CT.LUA_TFUNCTION); /* check error function */ + lapi.lua_pushboolean(L, 1); /* first result */ + lapi.lua_pushvalue(L, 1); /* function */ + lapi.lua_rotate(L, 3, 2); /* move them below function's arguments */ let status = lapi.lua_pcallk(L, n - 2, lua.LUA_MULTRET, 2, 2, finishpcall); return finishpcall(L, status, 2); }; @@ -167,4 +167,4 @@ const luaopen_base = function(L) { return 1; }; -module.exports.luaopen_base = luaopen_base; +module.exports.luaopen_base = luaopen_base; diff --git a/src/ldebug.js b/src/ldebug.js index be122e0..f6d0633 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -485,7 +485,7 @@ const luaG_errormsg = function(L) { if (L.errfunc !== 0) { /* is there an error handling function? */ let errfunc = L.errfunc; L.stack[L.top] = L.stack[L.top - 1]; - L.stack[L.top - 1] = errfunc; + L.stack[L.top - 1] = L.stack[errfunc]; L.top++; ldo.luaD_callnoyield(L, L.top - 2, 1); } diff --git a/tests/lbaselib.js b/tests/lbaselib.js index cca3ebe..85563da 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -338,7 +338,7 @@ test('xpcall', function (t) { return xpcall(willFail, msgh) `, L; - t.plan(1); + t.plan(3); t.doesNotThrow(function () { @@ -356,8 +356,13 @@ test('xpcall', function (t) { console.log(lapi.lua_tostring(L, -1)); - // t.ok( - // lapi.lua_tostring(L, -1).endsWith("you fucked up"), - // "Error is on the stack" - // ) + t.ok( + lapi.lua_tostring(L, -1).startsWith("Something's wrong:"), + "msgh was called and modified the error" + ) + + t.ok( + lapi.lua_tostring(L, -1).endsWith("you fucked up"), + "msgh was called and modified the error" + ) }); \ No newline at end of file -- cgit v1.2.3-70-g09d2