From 5131a5ab1f471655d6398748b1eaa9abd47c14da Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 17 Feb 2017 14:36:33 +0100 Subject: lua_pcall --- README.md | 2 +- src/lapi.js | 8 +++++++- src/ldo.js | 20 ++++++++++++++++++-- tests/lapi.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e2a9864..980f9e5 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ - [x] lua_load - [x] lua_call - [x] lua_callk + - [x] lua_pcall - [x] lua_setglobal - [x] lua_upvalueindex - [x] lua_createtable @@ -96,7 +97,6 @@ - [ ] lua_newuserdata - [ ] lua_next - [ ] lua_numbertointeger - - [ ] lua_pcall - [ ] lua_pcallk - [ ] lua_pushfstring - [ ] lua_pushglobaltable diff --git a/src/lapi.js b/src/lapi.js index 705fec4..1113eec 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -276,7 +276,7 @@ const lua_tonumber = function(L, idx) { }; const f_call = function(L, ud) { - ldo.luaD_callnoyield(L, ud.func, ud.nresults); + ldo.luaD_callnoyield(L, ud.funcOff, ud.nresults); }; const lua_type = function(L, idx) { @@ -387,6 +387,10 @@ const lua_pcallk = function(L, nargs, nresults, errfunc, ctx, k) { return status; }; +const lua_pcall = function(L, n, r, f) { + return lua_pcallk(L, n, r, f, 0, null); +} + module.exports.lua_pushvalue = lua_pushvalue; module.exports.lua_pushnil = lua_pushnil; module.exports.lua_pushnumber = lua_pushnumber; @@ -411,6 +415,8 @@ module.exports.lua_tostring = lua_tostring; module.exports.lua_load = lua_load; module.exports.lua_callk = lua_callk; module.exports.lua_call = lua_call; +module.exports.lua_pcallk = lua_pcallk; +module.exports.lua_pcall = lua_pcall; module.exports.lua_pop = lua_pop; module.exports.lua_setglobal = lua_setglobal; module.exports.lua_istable = lua_istable; diff --git a/src/ldo.js b/src/ldo.js index 632bc22..458ae46 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -19,6 +19,23 @@ const TMS = ltm.TMS; const nil = new TValue(CT.LUA_TNIL, null); +const seterrorobj = function(L, errcode, oldtop) { + switch (errcode) { + case TS.LUA_ERRMEM: { + L.stack[oldtop] = new TValue(CT.LUA_TLNGSTR, "not enough memory"); + break; + } + case TS.LUA_ERRERR: { + L.stack[oldtop] = new TValue(CT.LUA_TLNGSTR, "error in error handling"); + } + default: { + L.stack[oldtop] = L.stack[L.top - 1]; + } + } + + L.top = oldtop + 1; +}; + /* ** Prepares a function call: checks the stack, creates a new CallInfo ** entry, fills in the relevant information, calls hook if needed. @@ -221,7 +238,6 @@ const luaD_rawrunprotected = function(L, f, ud) { try { f(L, ud); } catch (e) { - console.log(e); if (lj.status == 0) lj.status = -1; } @@ -243,7 +259,7 @@ const luaD_pcall = function(L, func, u, old_top, ef) { if (status !== TS.LUA_OK) { lfunc.luaF_close(L, old_top); - // TODO: seterrorobj(L, status, oldtop); + seterrorobj(L, status, old_top); L.ci = old_ci; // TODO: L->allowhook = old_allowhooks; L.nny = old_nny; diff --git a/tests/lapi.js b/tests/lapi.js index 298328c..8a1db31 100644 --- a/tests/lapi.js +++ b/tests/lapi.js @@ -353,6 +353,63 @@ test('lua_call (calling a JS closure)', function (t) { }); +test('lua_pcall (calling a light JS function)', function (t) { + let L; + + t.plan(3); + + t.doesNotThrow(function () { + + let fn = function(L) { + lapi.lua_pushstring(L, "hello"); + return 1; + }; + + L = lauxlib.luaL_newstate(); + + lapi.lua_pushjsfunction(L, fn); + + lapi.lua_pcall(L, 0, 1, 0); + + }, "JS Lua program ran without error"); + + t.strictEqual( + lapi.lua_gettop(L), + 1, + "top is correct" + ); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "hello", + "top is correct" + ); +}); + + +test('lua_pcall that breaks', function (t) { + let L; + + t.plan(1); + + t.doesNotThrow(function () { + + let fn = function(L) { + return undefined_value; + }; + + L = lauxlib.luaL_newstate(); + + lapi.lua_pushjsfunction(L, fn); + + lapi.lua_pcall(L, 0, 1, 0); + + }, "JS Lua program ran without error"); + + console.log(L.stack[L.top - 1].value); +}); + + test('lua_pop', function (t) { let L; -- cgit v1.2.3-70-g09d2