diff options
-rw-r--r-- | src/lstate.js | 5 | ||||
-rw-r--r-- | src/ltm.js | 90 |
2 files changed, 61 insertions, 34 deletions
diff --git a/src/lstate.js b/src/lstate.js index f2e17cd..664ef27 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -9,7 +9,7 @@ const ldo = require('./ldo.js'); const lapi = require('./lapi.js'); const ltable = require('./ltable.js'); const lfunc = require('./lfunc.js'); -const luaT_init = require('./ltm.js').luaT_init; +const ltm = require('./ltm.js'); const CT = defs.constant_types; const TS = defs.thread_status; const LUA_NUMTAGS = defs.LUA_NUMTAGS; @@ -68,6 +68,7 @@ class global_State { this.l_registry = new lobject.TValue(CT.LUA_TNIL, null); this.panic = null; this.version = null; + this.tmname = new Array(ltm.TMS.TM_N); this.mt = new Array(LUA_NUMTAGS); } @@ -105,7 +106,7 @@ const f_luaopen = function(L) { let g = L.l_G; stack_init(L, L); init_registry(L, g); - luaT_init(L); + ltm.luaT_init(L); g.version = lapi.lua_version(null); }; @@ -13,34 +13,6 @@ const ldebug = require('./ldebug.js'); const lvm = require('./lvm.js'); const CT = defs.constant_types; - -const TMS = { - TM_INDEX: defs.to_luastring("__index", true), - TM_NEWINDEX: defs.to_luastring("__newindex", true), - TM_GC: defs.to_luastring("__gc", true), - TM_MODE: defs.to_luastring("__mode", true), - TM_LEN: defs.to_luastring("__len", true), - TM_EQ: defs.to_luastring("__eq", true), /* last tag method with fast access */ - TM_ADD: defs.to_luastring("__add", true), - TM_SUB: defs.to_luastring("__sub", true), - TM_MUL: defs.to_luastring("__mul", true), - TM_MOD: defs.to_luastring("__mod", true), - TM_POW: defs.to_luastring("__pow", true), - TM_DIV: defs.to_luastring("__div", true), - TM_IDIV: defs.to_luastring("__idiv", true), - TM_BAND: defs.to_luastring("__band", true), - TM_BOR: defs.to_luastring("__bor", true), - TM_BXOR: defs.to_luastring("__bxor", true), - TM_SHL: defs.to_luastring("__shl", true), - TM_SHR: defs.to_luastring("__shr", true), - TM_UNM: defs.to_luastring("__unm", true), - TM_BNOT: defs.to_luastring("__bnot", true), - TM_LT: defs.to_luastring("__lt", true), - TM_LE: defs.to_luastring("__le", true), - TM_CONCAT: defs.to_luastring("__concat", true), - TM_CALL: defs.to_luastring("__call", true) -}; - const luaT_typenames_ = [ "no value", "nil", @@ -59,10 +31,64 @@ const ttypename = function(t) { return luaT_typenames_[t + 1]; }; + +/* +* WARNING: if you change the order of this enumeration, +* grep "ORDER TM" and "ORDER OP" +*/ +const TMS = { + TM_INDEX: 0, + TM_NEWINDEX: 1, + TM_GC: 2, + TM_MODE: 3, + TM_LEN: 4, + TM_EQ: 5, /* last tag method with fast access */ + TM_ADD: 6, + TM_SUB: 7, + TM_MUL: 8, + TM_MOD: 9, + TM_POW: 10, + TM_DIV: 11, + TM_IDIV: 12, + TM_BAND: 13 , + TM_BOR: 14, + TM_BXOR: 15, + TM_SHL: 16, + TM_SHR: 17, + TM_UNM: 18, + TM_BNOT: 19, + TM_LT: 20, + TM_LE: 21, + TM_CONCAT: 22, + TM_CALL: 23, + TM_N: 24 /* number of elements in the enum */ +}; + const luaT_init = function(L) { - L.l_G.tmname = []; - for (let event in TMS) - L.l_G.tmname.push(new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_new(L, TMS[event]))); + L.l_G.tmname[TMS.TM_INDEX] = new lstring.luaS_new(L, defs.to_luastring("__index", true)); + L.l_G.tmname[TMS.TM_NEWINDEX] = new lstring.luaS_new(L, defs.to_luastring("__newindex", true)); + L.l_G.tmname[TMS.TM_GC] = new lstring.luaS_new(L, defs.to_luastring("__gc", true)); + L.l_G.tmname[TMS.TM_MODE] = new lstring.luaS_new(L, defs.to_luastring("__mode", true)); + L.l_G.tmname[TMS.TM_LEN] = new lstring.luaS_new(L, defs.to_luastring("__len", true)); + L.l_G.tmname[TMS.TM_EQ] = new lstring.luaS_new(L, defs.to_luastring("__eq", true)); + L.l_G.tmname[TMS.TM_ADD] = new lstring.luaS_new(L, defs.to_luastring("__add", true)); + L.l_G.tmname[TMS.TM_SUB] = new lstring.luaS_new(L, defs.to_luastring("__sub", true)); + L.l_G.tmname[TMS.TM_MUL] = new lstring.luaS_new(L, defs.to_luastring("__mul", true)); + L.l_G.tmname[TMS.TM_MOD] = new lstring.luaS_new(L, defs.to_luastring("__mod", true)); + L.l_G.tmname[TMS.TM_POW] = new lstring.luaS_new(L, defs.to_luastring("__pow", true)); + L.l_G.tmname[TMS.TM_DIV] = new lstring.luaS_new(L, defs.to_luastring("__div", true)); + L.l_G.tmname[TMS.TM_IDIV] = new lstring.luaS_new(L, defs.to_luastring("__idiv", true)); + L.l_G.tmname[TMS.TM_BAND] = new lstring.luaS_new(L, defs.to_luastring("__band", true)); + L.l_G.tmname[TMS.TM_BOR] = new lstring.luaS_new(L, defs.to_luastring("__bor", true)); + L.l_G.tmname[TMS.TM_BXOR] = new lstring.luaS_new(L, defs.to_luastring("__bxor", true)); + L.l_G.tmname[TMS.TM_SHL] = new lstring.luaS_new(L, defs.to_luastring("__shl", true)); + L.l_G.tmname[TMS.TM_SHR] = new lstring.luaS_new(L, defs.to_luastring("__shr", true)); + L.l_G.tmname[TMS.TM_UNM] = new lstring.luaS_new(L, defs.to_luastring("__unm", true)); + L.l_G.tmname[TMS.TM_BNOT] = new lstring.luaS_new(L, defs.to_luastring("__bnot", true)); + L.l_G.tmname[TMS.TM_LT] = new lstring.luaS_new(L, defs.to_luastring("__lt", true)); + L.l_G.tmname[TMS.TM_LE] = new lstring.luaS_new(L, defs.to_luastring("__le", true)); + L.l_G.tmname[TMS.TM_CONCAT] = new lstring.luaS_new(L, defs.to_luastring("__concat", true)); + L.l_G.tmname[TMS.TM_CALL] = new lstring.luaS_new(L, defs.to_luastring("__call", true)); }; /* @@ -150,7 +176,7 @@ const luaT_gettmbyobj = function(L, o, event) { mt = L.l_G.mt[o.ttnov()]; } - return mt ? ltable.luaH_getstr(mt, event) : lobject.luaO_nilobject; + return mt ? ltable.luaH_getstr(mt, L.l_G.tmname[event]) : lobject.luaO_nilobject; }; module.exports.TMS = TMS; |