From 59e549150996ec4c8a049f893dad9ec95a4677e9 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 13 Feb 2017 14:37:01 +0100 Subject: Better use of module to avoid cyclic dependencies issues --- src/ldo.js | 55 ++++++++++++++++++++++++++++++------ src/lfunc.js | 10 +++---- src/llimit.js | 8 ++---- src/lobject.js | 12 ++++---- src/lopcodes.js | 48 +++++++++++++++---------------- src/lstate.js | 24 ++++++++-------- src/ltable.js | 6 +++- src/ltm.js | 14 ++++----- src/lua.js | 8 ++---- src/lvm.js | 88 +++++++++++++++------------------------------------------ 10 files changed, 130 insertions(+), 143 deletions(-) (limited to 'src') diff --git a/src/ldo.js b/src/ldo.js index 26a4a0f..63b949c 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -11,6 +11,7 @@ const CallInfo = lstate.CallInfo; const llimit = require('./llimit.js'); const ltm = require('./ltm.js'); const TMS = ltm.TMS; +const lvm = require('./lvm.js'); const nil = new TValue(CT.LUA_TNIL, null); @@ -141,11 +142,49 @@ const tryfuncTM = function(L, off, func) { L.stack[off] = tm; /* tag method is the new function to be called */ }; -module.exports = { - nil: nil, - luaD_precall: luaD_precall, - luaD_poscall: luaD_poscall, - moveresults: moveresults, - adjust_varargs: adjust_varargs, - tryfuncTM: tryfuncTM -}; \ No newline at end of file +/* +** Check appropriate error for stack overflow ("regular" overflow or +** overflow while handling stack overflow). If 'nCalls' is larger than +** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but +** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to +** allow overflow handling to work) +*/ +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); +}; + +/* +** Call a function (JS or Lua). The function to be called is at func. +** The arguments are on the stack, right after the function. +** When returns, all the results are on the stack, starting at the original +** function position. +*/ +const luaD_call = function(L, off, nResults) { + if (++L.nCcalls >= llimit.LUAI_MAXCCALLS) + stackerror(L); + if (!luaD_precall(L, off, nResults)) + lvm.luaV_execute(L); + L.nCcalls--; +}; + +/* +** Similar to 'luaD_call', but does not allow yields during the call +*/ +const luaD_callnoyield = function(L, off, nResults) { + L.nny++; + luaD_call(L, off, nResults); + L.nny--; +}; + +module.exports.nil = nil; +module.exports.luaD_precall = luaD_precall; +module.exports.luaD_poscall = luaD_poscall; +module.exports.moveresults = moveresults; +module.exports.adjust_varargs = adjust_varargs; +module.exports.tryfuncTM = tryfuncTM; +module.exports.stackerror = stackerror; +module.exports.luaD_call = luaD_call; +module.exports.luaD_callnoyield = luaD_callnoyield; \ No newline at end of file diff --git a/src/lfunc.js b/src/lfunc.js index ac09c5e..1a1597a 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -88,9 +88,7 @@ const luaF_close = function(L, level) { } }; -module.exports = { - Proto: Proto, - UpVal: UpVal, - findupval: findupval, - luaF_close: luaF_close -}; \ No newline at end of file +module.exports.Proto = Proto; +module.exports.UpVal = UpVal; +module.exports.findupval = findupval; +module.exports.luaF_close = luaF_close; \ No newline at end of file diff --git a/src/llimit.js b/src/llimit.js index 2cbfbb6..9c02eb0 100644 --- a/src/llimit.js +++ b/src/llimit.js @@ -1,8 +1,6 @@ /*jshint esversion: 6 */ "use strict"; -module.exports = { - LUAI_MAXCCALLS: 200, - LUA_MAXINTEGER: 2147483647, - LUA_MININTEGER: -2147483647 -}; \ No newline at end of file +module.exports.LUAI_MAXCCALLS = 200; +module.exports.LUA_MAXINTEGER = 2147483647; +module.exports.LUA_MININTEGER = -2147483647; \ No newline at end of file diff --git a/src/lobject.js b/src/lobject.js index e206bf1..725f63d 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -209,10 +209,8 @@ class Userdata extends TValue { } -module.exports = { - LClosure: LClosure, - TValue: TValue, - Table: Table, - TString: TString, - Userdata: Userdata -}; \ No newline at end of file +module.exports.LClosure = LClosure; +module.exports.TValue = TValue; +module.exports.Table = Table; +module.exports.TString = TString; +module.exports.Userdata = Userdata; \ No newline at end of file diff --git a/src/lopcodes.js b/src/lopcodes.js index 6bdfd7c..1350468 100644 --- a/src/lopcodes.js +++ b/src/lopcodes.js @@ -83,28 +83,26 @@ const INDEXK = function (r) { /* number of list items to accumulate before a SETLIST instruction */ const LFIELDS_PER_FLUSH = 50; -module.exports = { - OpCodes: OpCodes, - SIZE_C: SIZE_C, - SIZE_B: SIZE_B, - SIZE_Bx: SIZE_Bx, - SIZE_A: SIZE_A, - SIZE_Ax: SIZE_Ax, - SIZE_OP: SIZE_OP, - POS_OP: POS_OP, - POS_A: POS_A, - POS_C: POS_C, - POS_B: POS_B, - POS_Bx: POS_Bx, - POS_Ax: POS_Ax, - MAXARG_Bx: MAXARG_Bx, - MAXARG_sBx: MAXARG_sBx, - MAXARG_Ax: MAXARG_Ax, - MAXARG_A: MAXARG_A, - MAXARG_B: MAXARG_B, - MAXARG_C: MAXARG_C, - BITRK: BITRK, - ISK: ISK, - INDEXK: INDEXK, - LFIELDS_PER_FLUSH: LFIELDS_PER_FLUSH -}; \ No newline at end of file +module.exports.OpCodes = OpCodes; +module.exports.SIZE_C = SIZE_C; +module.exports.SIZE_B = SIZE_B; +module.exports.SIZE_Bx = SIZE_Bx; +module.exports.SIZE_A = SIZE_A; +module.exports.SIZE_Ax = SIZE_Ax; +module.exports.SIZE_OP = SIZE_OP; +module.exports.POS_OP = POS_OP; +module.exports.POS_A = POS_A; +module.exports.POS_C = POS_C; +module.exports.POS_B = POS_B; +module.exports.POS_Bx = POS_Bx; +module.exports.POS_Ax = POS_Ax; +module.exports.MAXARG_Bx = MAXARG_Bx; +module.exports.MAXARG_sBx = MAXARG_sBx; +module.exports.MAXARG_Ax = MAXARG_Ax; +module.exports.MAXARG_A = MAXARG_A; +module.exports.MAXARG_B = MAXARG_B; +module.exports.MAXARG_C = MAXARG_C; +module.exports.BITRK = BITRK; +module.exports.ISK = ISK; +module.exports.INDEXK = INDEXK; +module.exports.LFIELDS_PER_FLUSH = LFIELDS_PER_FLUSH; \ No newline at end of file diff --git a/src/lstate.js b/src/lstate.js index fcc3eb6..ed299b5 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -41,16 +41,14 @@ class lua_State { } -module.exports = { - lua_State: lua_State, - CallInfo: CallInfo, - CIST_OAH: (1<<0), /* original value of 'allowhook' */ - CIST_LUA: (1<<1), /* call is running a Lua function */ - CIST_HOOKED: (1<<2), /* call is running a debug hook */ - CIST_FRESH: (1<<3), /* call is running on a fresh invocation of luaV_execute */ - CIST_YPCALL: (1<<4), /* call is a yieldable protected call */ - CIST_TAIL: (1<<5), /* call was tail called */ - CIST_HOOKYIELD: (1<<6), /* last hook called yielded */ - CIST_LEQ: (1<<7), /* using __lt for __le */ - CIST_FIN: (1<<8) /* call is running a finalizer */ -}; \ No newline at end of file +module.exports.lua_State = lua_State; +module.exports.CallInfo = CallInfo; +module.exports.CIST_OAH = (1<<0); /* original value of 'allowhook' */ +module.exports.CIST_LUA = (1<<1); /* call is running a Lua function */ +module.exports.CIST_HOOKED = (1<<2); /* call is running a debug hook */ +module.exports.CIST_FRESH = (1<<3); /* call is running on a fresh invocation of luaV_execute */ +module.exports.CIST_YPCALL = (1<<4); /* call is a yieldable protected call */ +module.exports.CIST_TAIL = (1<<5); /* call was tail called */ +module.exports.CIST_HOOKYIELD = (1<<6); /* last hook called yielded */ +module.exports.CIST_LEQ = (1<<7); /* using __lt for __le */ +module.exports.CIST_FIN = (1<<8); /* call is running a finalizer */ \ No newline at end of file diff --git a/src/ltable.js b/src/ltable.js index 6e3af15..95ca5a5 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -1,9 +1,13 @@ /*jshint esversion: 6 */ "use strict"; +const assert = require('assert'); + const lobject = require('./lobject.js'); +const nil = require('./ldo.js').nil; const Table = lobject.Table; + /* ** Try to find a boundary in table 't'. A 'boundary' is an integer index ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). @@ -26,5 +30,5 @@ Table.prototype.luaH_getn = function() { /* else must find a boundary in hash part */ else if (hash.size === 0) return j; - else return j; // TODO: unbound_search(t, j) => but why ? + else return hash.get(j); }; \ No newline at end of file diff --git a/src/ltm.js b/src/ltm.js index 041e233..f963aa9 100644 --- a/src/ltm.js +++ b/src/ltm.js @@ -96,11 +96,9 @@ const luaT_gettmbyobj = function(L, o, event) { return mt ? mt.__index(mt, event) : ldo.nil; }; -module.exports = { - TMS: TMS, - luaT_callTM: luaT_callTM, - luaT_callbinTM: luaT_callbinTM, - luaT_trybinTM: luaT_trybinTM, - luaT_callorderTM: luaT_callorderTM, - luaT_gettmbyobj: luaT_gettmbyobj -}; \ No newline at end of file +module.exports.TMS = TMS; +module.exports.luaT_callTM = luaT_callTM; +module.exports.luaT_callbinTM = luaT_callbinTM; +module.exports.luaT_trybinTM = luaT_trybinTM; +module.exports.luaT_callorderTM = luaT_callorderTM; +module.exports.luaT_gettmbyobj = luaT_gettmbyobj; \ No newline at end of file diff --git a/src/lua.js b/src/lua.js index 1989133..33cf623 100644 --- a/src/lua.js +++ b/src/lua.js @@ -35,8 +35,6 @@ constant_types.LUA_TLCL = constant_types.LUA_TFUNCTION | (0 << 4); /* Lua closu constant_types.LUA_TLCF = constant_types.LUA_TFUNCTION | (1 << 4); /* light C function */ constant_types.LUA_TCCL = constant_types.LUA_TFUNCTION | (2 << 4); /* C closure */ -module.exports = { - constant_types: constant_types, - thread_status: thread_status, - LUA_MULTRET: -1 -}; \ No newline at end of file +module.exports.constant_types = constant_types; +module.exports.thread_status = thread_status; +module.exports.LUA_MULTRET = -1; \ No newline at end of file diff --git a/src/lvm.js b/src/lvm.js index 890c123..092559a 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -571,7 +571,7 @@ const luaV_execute = function(L) { L.stack[cb + 1] = L.stack[ra + 1]; L.stack[cb] = L.stack[ra]; L.top = cb + 3; /* func. + 2 args (state and index) */ - luaD_call(L, cb, i.C); + ldo.luaD_call(L, cb, i.C); base = ci.u.l.base; L.top = ci.top; i = ci.u.l.savedpc[ci.pcOff++]; @@ -728,7 +728,7 @@ const luaV_equalobj = function(L, t1, t2) { case CT.LUA_TUSERDATA: case CT.LUA_TTABLE: if (t1 === t2) return 1; - else if (L === null) return 1; + else if (L === null) return 0; // TODO: fasttm ? tm = ltm.luaT_gettmbyobj(L, t1, TMS.TM_EQ); @@ -743,7 +743,7 @@ const luaV_equalobj = function(L, t1, t2) { return 0; ltm.luaT_callTM(L, tm, t1, t2, L.top, 1); - return !L.stack[L.top].l_isfalse(); + return L.stack[L.top].l_isfalse() ? 0 : 1; }; const forlimit = function(obj, step) { @@ -937,65 +937,23 @@ const luaV_concat = function(L, total) { } while (total > 1); /* repeat until only 1 result left */ }; -/* -** Check appropriate error for stack overflow ("regular" overflow or -** overflow while handling stack overflow). If 'nCalls' is larger than -** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but -** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to -** allow overflow handling to work) -*/ -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); -}; - -/* -** Call a function (JS or Lua). The function to be called is at func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. -*/ -const luaD_call = function(L, off, nResults) { - if (++L.nCcalls >= llimit.LUAI_MAXCCALLS) - stackerror(L); - if (!ldo.luaD_precall(L, off, nResults)) - luaV_execute(L); - L.nCcalls--; -}; - -/* -** Similar to 'luaD_call', but does not allow yields during the call -*/ -const luaD_callnoyield = function(L, off, nResults) { - L.nny++; - luaD_call(L, off, nResults); - L.nny--; -}; - -module.exports = { - RA: RA, - RB: RB, - RC: RC, - RKB: RKB, - RKC: RKC, - luaV_execute: luaV_execute, - dojump: dojump, - donextjump: donextjump, - luaV_lessequal: luaV_lessequal, - luaV_lessthan: luaV_lessthan, - luaV_equalobj: luaV_equalobj, - forlimit: forlimit, - luaV_tointeger: luaV_tointeger, - tonumber: tonumber, - LTnum: LTnum, - LEnum: LEnum, - LEintfloat: LEintfloat, - LTintfloat: LTintfloat, - l_strcmp: l_strcmp, - luaV_objlen: luaV_objlen, - stackerror: stackerror, - luaD_call: luaD_call, - luaD_callnoyield: luaD_callnoyield, -}; \ No newline at end of file +module.exports.RA = RA; +module.exports.RB = RB; +module.exports.RC = RC; +module.exports.RKB = RKB; +module.exports.RKC = RKC; +module.exports.luaV_execute = luaV_execute; +module.exports.dojump = dojump; +module.exports.donextjump = donextjump; +module.exports.luaV_lessequal = luaV_lessequal; +module.exports.luaV_lessthan = luaV_lessthan; +module.exports.luaV_equalobj = luaV_equalobj; +module.exports.forlimit = forlimit; +module.exports.luaV_tointeger = luaV_tointeger; +module.exports.tonumber = tonumber; +module.exports.LTnum = LTnum; +module.exports.LEnum = LEnum; +module.exports.LEintfloat = LEintfloat; +module.exports.LTintfloat = LTintfloat; +module.exports.l_strcmp = l_strcmp; +module.exports.luaV_objlen = luaV_objlen; \ No newline at end of file -- cgit v1.2.3-70-g09d2