From 653f4a07af6506e0b8ef71e8957976b9559f67e4 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 21 Feb 2017 15:24:37 +0100 Subject: pcall, xpcall --- tests/lbaselib.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests') diff --git a/tests/lbaselib.js b/tests/lbaselib.js index b55afde..cca3ebe 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -290,4 +290,74 @@ test('error, protected', function (t) { lapi.lua_tostring(L, -1).endsWith("you fucked up"), "Error is on the stack" ) +}); + + +test('pcall', function (t) { + let luaCode = ` + local willFail = function () + error("you fucked up") + end + + return pcall(willFail) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-pcall"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.ok( + lapi.lua_tostring(L, -1).endsWith("you fucked up"), + "Error is on the stack" + ) +}); + + +test('xpcall', function (t) { + let luaCode = ` + local willFail = function () + error("you fucked up") + end + + local msgh = function (err) + return "Something's wrong: " .. err + end + + return xpcall(willFail, msgh) + `, L; + + t.plan(1); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-pcall"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + console.log(lapi.lua_tostring(L, -1)); + + // t.ok( + // lapi.lua_tostring(L, -1).endsWith("you fucked up"), + // "Error is on the stack" + // ) }); \ No newline at end of file -- cgit v1.2.3-70-g09d2