From 82ef443de9a3ba53b4a5b9abd161ddc164776a59 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 21 Feb 2017 12:53:41 +0100 Subject: Use correct luaG errors instead of throwing Errors --- src/lauxlib.js | 2 +- src/lbaselib.js | 4 ++-- src/ldo.js | 9 +++++---- src/lvm.js | 14 +++++++------- 4 files changed, 15 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/lauxlib.js b/src/lauxlib.js index b879446..3ebcc7d 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -14,7 +14,7 @@ const LUA_LOADED_TABLE = "_LOADED" const panic = function(L) { let msg = `PANIC: unprotected error in call to Lua API (${lapi.lua_tostring(L, -1)})`; console.error(msg); - throw new Error(msg); + return 0; }; // const luaL_argerror = function(L, arg, extramsg) { diff --git a/src/lbaselib.js b/src/lbaselib.js index 16c63bf..205d57a 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -19,7 +19,7 @@ const luaB_print = function(L) { lapi.lua_call(L, 1, 1); let s = lapi.lua_tolstring(L, -1); if (s === null) - throw new Error("'tostring' must return a string to 'print"); + return lauxlib.luaL_error(L, "'tostring' must return a string to 'print'"); if (i > 1) s = `\t${s}`; str = `${str}${s}`; lapi.lua_pop(L, 1); @@ -51,7 +51,7 @@ const luaB_setmetatable = function(L) { lauxlib.luaL_checktype(L, 1, CT.LUA_TTABLE); lauxlib.luaL_argcheck(L, t === CT.LUA_TNIL || t === CT.LUA_TTABLE, 2, "nil or table expected"); if (lauxlib.luaL_getmetafield(L, 1, "__metatable") !== CT.LUA_TNIL) - throw new Error("cannot change a protected metatable"); + return lauxlib.luaL_error(L, "cannot change a protected metatable"); lapi.lua_settop(L, 2); lapi.lua_setmetatable(L, 1); return 1; diff --git a/src/ldo.js b/src/ldo.js index bf07c8c..4333f5e 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -11,6 +11,7 @@ const ltm = require('./ltm.js'); const lvm = require('./lvm.js'); const lfunc = require('./lfunc.js'); const BytecodeParser = require('./lundump.js'); +const ldebug = require('./ldebug.js'); const CT = lua.constant_types; const TS = lua.thread_status; const LUA_MULTRET = lua.LUA_MULTRET; @@ -190,7 +191,7 @@ const adjust_varargs = function(L, p, actual) { const tryfuncTM = function(L, off, func) { let tm = ltm.luaT_gettmbyobj(L, func, ltm.TMS.TM_CALL); if (!tm.ttisfunction(tm)) - throw new Error("__call metatable member is not a function"); // TODO: luaG_typeerror + ldebug.luaG_typeerror(L, func, "call"); /* Open a hole inside the stack at 'func' */ for (let p = L.top; p > off; p--) L.stack[p] = L.stack[p-1]; @@ -207,9 +208,9 @@ const tryfuncTM = function(L, off, func) { */ const stackerror = function(L) { if (L.nCcalls === llimit.LUAI_MAXCCALLS) - throw new Error("JS stack overflow"); - else if (L.nCcalls >= llimit.LUAI_MAXCCALLS + (llimit.LUAI_MAXCCALLS >> 3)) /* error while handing stack error */ - throw new Error("stack overflow"); // TODO: luaD_throw(L, LUA_ERRERR); + ldebug.luaG_runerror(L, "JS stack overflow"); + else if (L.nCcalls >= llimit.LUAI_MAXCCALLS + (llimit.LUAI_MAXCCALLS >> 3)) + luaD_throw(L, TS.LUA_ERRERR); /* error while handing stack error */ }; /* diff --git a/src/lvm.js b/src/lvm.js index c74fa45..a31e0c7 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -531,19 +531,19 @@ const luaV_execute = function(L) { let nstep = tonumber(pstep); if (nlimit === false) - throw new Error("'for' limit must be a number"); + ldebug.luaG_runerror(L, "'for' limit must be a number"); plimit.type = CT.LUA_TNUMFLT; plimit.value = nlimit if (nstep === false) - throw new Error("'for' step must be a number"); + ldebug.luaG_runerror(L, "'for' step must be a number"); pstep.type = CT.LUA_TNUMFLT; pstep.value = nstep if (ninit === false) - throw new Error("'for' initial value must be a number"); + ldebug.luaG_runerror(L, "'for' initial value must be a number"); init.type = CT.LUA_TNUMFLT; init.value = ninit - nstep; @@ -662,7 +662,7 @@ const luaV_lessthan = function(L, l, r) { else { let res = ltm.luaT_callorderTM(L, l, r, ltm.TMS.TM_LT); if (res < 0) - throw new Error("TM order error"); // TODO: luaG_ordererror + ldebug.luaG_ordererror(L, l, r); return res; } }; @@ -684,7 +684,7 @@ const luaV_lessequal = function(L, l, r) { res = ltm.luaT_callorderTM(L, l, r, ltm.TMS.TM_LT); L.ci.callstatus ^= lstate.CIST_LEQ; /* clear mark */ if (res < 0) - throw new Error("TM order error"); // TODO: luaG_ordererror + ldebug.luaG_ordererror(L, l, r); return res !== 1 ? 1 : 0; /* result is negated */ }; @@ -931,7 +931,7 @@ const gettable = function(L, table, key, ra, recur) { recur = recur ? recur : 0; if (recur >= MAXTAGRECUR) - throw new Error("'__index' chain too long; possible loop"); // TODO: luaG_runerror + ldebug.luaG_runerror(L, "'__index' chain too long; possible loop"); if (table.ttistable()) { let element = table.__index(table, key); @@ -974,7 +974,7 @@ const settable = function(L, table, key, v, recur) { recur = recur ? recur : 0; if (recur >= MAXTAGRECUR) - throw new Error("'__newindex' chain too long; possible loop"); // TODO: luaG_runerror + ldebug.luaG_runerror(L, "'__newindex' chain too long; possible loop"); if (table.ttistable()) { let element = table.__index(table, key); -- cgit v1.2.3-54-g00ecf