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 --- README.md | 29 +++++++++++++---------------- src/lauxlib.js | 2 +- src/lbaselib.js | 4 ++-- src/ldo.js | 9 +++++---- src/lvm.js | 14 +++++++------- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 96ca465..dd2ba4f 100644 --- a/README.md +++ b/README.md @@ -68,13 +68,17 @@ - [x] lua_settop - [x] lua_tostring - [x] lua_rawequal + - [x] lua_error + - [x] lua_concat + - [x] lua_isstring + - [x] lua_istable + - [x] lua_remove + - [x] lua_rotate - [ ] lua_arith - [ ] lua_close - [ ] lua_compare - - [ ] lua_concat - [ ] lua_copy - [ ] lua_dump - - [ ] lua_error - [x] lua_gc (unvailable) - [x] lua_getallocf (unvailable) - [x] lua_getextraspace (unvailable) @@ -97,8 +101,6 @@ - [ ] lua_isnone - [ ] lua_isnoneornil - [ ] lua_isnumber - - [ ] lua_isstring - - [ ] lua_istable - [ ] lua_isthread - [ ] lua_isuserdata - [ ] lua_isyieldable @@ -117,10 +119,8 @@ - [ ] lua_rawseti - [ ] lua_rawsetp - [ ] lua_register - - [ ] lua_remove - [ ] lua_replace - [ ] lua_resume - - [ ] lua_rotate - [ ] lua_setallocf - [ ] lua_sethook - [ ] lua_seti @@ -151,9 +151,12 @@ - [x] luaL_openlibs - [x] luaL_getsubtable - [x] luaL_requiref - - [ ] luaL_Buffer - - [ ] luaL_Reg - - [ ] luaL_Stream + - [x] luaL_checkinteger + - [x] luaL_checklstring + - [x] luaL_opt + - [x] luaL_optinteger + - [x] luaL_optlstring + - [x] luaL_where - [ ] luaL_addchar - [ ] luaL_addlstring - [ ] luaL_addsize @@ -163,8 +166,6 @@ - [ ] luaL_argerror - [ ] luaL_buffinit - [ ] luaL_buffinitsize - - [ ] luaL_checkinteger - - [ ] luaL_checklstring - [ ] luaL_checknumber - [ ] luaL_checkoption - [ ] luaL_checkstring @@ -186,9 +187,6 @@ - [ ] luaL_newlib - [ ] luaL_newlibtable - [ ] luaL_newmetatable - - [ ] luaL_opt - - [ ] luaL_optinteger - - [ ] luaL_optlstring - [ ] luaL_optnumber - [ ] luaL_optstring - [ ] luaL_prepbuffer @@ -200,7 +198,6 @@ - [ ] luaL_testudata - [ ] luaL_traceback - [ ] luaL_unref - - [ ] luaL_where - [ ] Standard library - [ ] Base lib - [x] tostring @@ -211,10 +208,10 @@ - [x] rawset - [x] rawget - [x] type + - [x] error - [ ] assert - [ ] collectgarbage - [ ] dofile - - [ ] error - [ ] ipairs - [ ] loadfile - [ ] load 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-70-g09d2