From 2a6993bf0e23e2a3759abb9d7127525577dc346c Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Sun, 12 Mar 2017 16:48:53 +0100 Subject: Testing 8-bit strings --- src/lapi.js | 3 +-- src/ldo.js | 2 +- src/llex.js | 2 +- src/lobject.js | 6 ++++++ src/lparser.js | 12 ++++++------ src/ltm.js | 32 +++++++++++++++++++++++++++++--- src/lua.js | 6 +++--- src/lvm.js | 29 +++++++++++++---------------- 8 files changed, 60 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 79cd6fb..96fe207 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -231,7 +231,6 @@ const lua_pushstring = function (L, s) { else { let ts = L.l_G.intern(lua.to_luastring(s)); L.stack[L.top] = ts; - s = ts.value; } L.top++; @@ -537,7 +536,7 @@ const lua_tolstring = function(L, idx) { if (!o.ttisstring() && !o.ttisnumber()) return null; - return o.ttisstring() ? String.fromCharCode(...o.value) : `${o.value}`; + return o.ttisstring() ? o.jsstring() : `${o.value}`; }; const lua_tostring = lua_tolstring; diff --git a/src/ldo.js b/src/ldo.js index e088d8d..7df4c62 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -532,7 +532,7 @@ const f_parser = function(L, p) { let c = p.z.getc(); /* read first character */ if (String.fromCharCode(c) === lua.LUA_SIGNATURE.charAt(0)) { checkmode(L, p.mode, "binary"); - cl = new BytecodeParser(L, p.z.buffer); + cl = new BytecodeParser(L, p.z.buffer).luaU_undump(); } else { checkmode(L, p.mode, "text"); cl = lparser.luaY_parser(L, p.z, p.buff, p.dyd, p.name, c); diff --git a/src/llex.js b/src/llex.js index fc3603f..e747fdd 100644 --- a/src/llex.js +++ b/src/llex.js @@ -596,7 +596,7 @@ const llex = function(ls, seminfo) { let ts = new TValue(CT.LUA_TLNGSTR, lua.to_luastring(ls.buff.buffer.join(''))); seminfo.ts = ts; - let kidx = luaX_tokens.slice(0, 22).indexOf(ts.value); + let kidx = luaX_tokens.slice(0, 22).indexOf(ts.jsstring()); if (kidx >= 0) /* reserved word? */ return kidx + FIRST_RESERVED; else diff --git a/src/lobject.js b/src/lobject.js index 92dd560..739db50 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -110,6 +110,10 @@ class TValue { return this.ttisnil() || (this.ttisboolean() && this.value === false); } + jsstring() { + return this.ttisstring() ? String.fromCharCode(...this.value) : null; + } + } const nil = new TValue(CT.LUA_TNIL, null); @@ -133,6 +137,8 @@ class Table extends TValue { } else if ([CT.LUA_TSHRSTR, CT.LUA_TLNGSTR].indexOf(key.type) > -1) { key = key.value.map(e => `${e}|`).join(''); } + } else if (typeof key === "string") { // To avoid + key = lua.to_luastring(key).map(e => `${e}|`).join(''); } return key; diff --git a/src/lparser.js b/src/lparser.js index 42337cc..1806bbe 100644 --- a/src/lparser.js +++ b/src/lparser.js @@ -273,7 +273,7 @@ const removevars = function(fs, tolevel) { const searchupvalue = function(fs, name) { let up = fs.f.upvalues; for (let i = 0; i < fs.nups; i++) { - if (up[i].name.value === name.value) + if (up[i].name.jsstring() === name.jsstring()) return i; } return -1; /* not found */ @@ -291,7 +291,7 @@ const newupvalue = function(fs, name, v) { const searchvar = function(fs, n) { for (let i = fs.nactvar - 1; i >= 0; i--) { - if (n.value === getlocvar(fs, i).varname.value) + if (n.jsstring() === getlocvar(fs, i).varname.jsstring()) return i; } @@ -383,7 +383,7 @@ const closegoto = function(ls, g, label) { let fs = ls.fs; let gl = ls.dyd.gt; let gt = gl.arr[g]; - assert(gt.name.value === label.name.value); + assert(gt.name.jsstring() === label.name.jsstring()); if (gt.nactvar < label.nactvar) { let vname = getlocvar(fs, gt.nactvar).varname; semerror(ls, ` at line ${gt.line} jumps into the scope of local '${vname.value}'`); @@ -405,7 +405,7 @@ const findlabel = function(ls, g) { /* check labels in current block for a match */ for (let i = bl.firstlabel; i < dyd.label.n; i++) { let lb = dyd.label.arr[i]; - if (lb.name.value === gt.name.value) { /* correct label? */ + if (lb.name.jsstring() === gt.name.jsstring()) { /* correct label? */ if (gt.nactvar > lb.nactvar && (bl.upval || dyd.label.n > bl.firstlabel)) lcode.luaK_patchclose(ls.fs, gt.pc, lb.nactvar); closegoto(ls, g, lb); /* close it */ @@ -434,7 +434,7 @@ const findgotos = function(ls, lb) { let gl = ls.dyd.gt; let i = ls.fs.bl.firstgoto; while (i < gl.n) { - if (gl.arr[i].name.value === lb.name.value) + if (gl.arr[i].name.jsstring() === lb.name.jsstring()) closegoto(ls, i, lb); else i++; @@ -1150,7 +1150,7 @@ const gotostat = function(ls, pc) { /* check for repeated labels on the same block */ const checkrepeated = function(fs, ll, label) { for (let i = fs.bl.firstlabel; i < ll.n; i++) { - if (label.value === ll.arr[i].name.value) { + if (label.jsstring() === ll.arr[i].name.jsstring()) { semerror(fs.ls, `label '${label}' already defined on line ${ll.arr[i].line}`); } } diff --git a/src/ltm.js b/src/ltm.js index 51ab8ec..6377c93 100644 --- a/src/ltm.js +++ b/src/ltm.js @@ -38,8 +38,34 @@ const TMS = { TM_LT: "__lt", TM_LE: "__le", TM_CONCAT: "__concat", - TM_CALL: "__call", - TM_N: 26 + TM_CALL: "__call" +}; + +const TMS8 = { + TM_INDEX: lua.to_luastring(TMS.TM_INDEX), + TM_NEWINDEX: lua.to_luastring(TMS.TM_NEWINDEX), + TM_GC: lua.to_luastring(TMS.TM_GC), + TM_MODE: lua.to_luastring(TMS.TM_MODE), + TM_LEN: lua.to_luastring(TMS.TM_LEN), + TM_EQ: lua.to_luastring(TMS.TM_EQ), /* last tag method with fast access */ + TM_ADD: lua.to_luastring(TMS.TM_ADD), + TM_SUB: lua.to_luastring(TMS.TM_SUB), + TM_MUL: lua.to_luastring(TMS.TM_MUL), + TM_MOD: lua.to_luastring(TMS.TM_MOD), + TM_POW: lua.to_luastring(TMS.TM_POW), + TM_DIV: lua.to_luastring(TMS.TM_DIV), + TM_IDIV: lua.to_luastring(TMS.TM_IDIV), + TM_BAND: lua.to_luastring(TMS.TM_BAND), + TM_BOR: lua.to_luastring(TMS.TM_BOR), + TM_BXOR: lua.to_luastring(TMS.TM_BXOR), + TM_SHL: lua.to_luastring(TMS.TM_SHL), + TM_SHR: lua.to_luastring(TMS.TM_SHR), + TM_UNM: lua.to_luastring(TMS.TM_UNM), + TM_BNOT: lua.to_luastring(TMS.TM_BNOT), + TM_LT: lua.to_luastring(TMS.TM_LT), + TM_LE: lua.to_luastring(TMS.TM_LE), + TM_CONCAT: lua.to_luastring(TMS.TM_CONCAT), + TM_CALL: lua.to_luastring(TMS.TM_CALL) }; const luaT_typenames_ = [ @@ -77,7 +103,7 @@ const luaT_objtypename = function(L, o) { || (o.ttisfulluserdata() && o.metatable !== null)) { let name = o.__index(o, '__name'); if (name.ttisstring()) - return String.fromCharCode(...name.value); + return name.jsstring(); } return ttypename(o.ttnov()); diff --git a/src/lua.js b/src/lua.js index 12d4e65..08bad58 100644 --- a/src/lua.js +++ b/src/lua.js @@ -133,8 +133,8 @@ class lua_Debug { } const to_luastring = function(str, maxBytesToWrite) { - maxBytesToWrite = maxBytesToWrite !== undefined ? maxBytesToWrite : str.length; - let outU8Array = new Array(maxBytesToWrite); + maxBytesToWrite = maxBytesToWrite !== undefined ? maxBytesToWrite : str.length + 1; + let outU8Array = []; if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes. return 0; @@ -184,7 +184,7 @@ const to_luastring = function(str, maxBytesToWrite) { } } // Null-terminate the pointer to the buffer. - outU8Array[outIdx] = 0; + // outU8Array[outIdx] = 0; return outU8Array; }; diff --git a/src/lvm.js b/src/lvm.js index 3cf3fb6..873cd20 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -856,8 +856,7 @@ const luaV_tointeger = function(obj, mode) { } else if (obj.ttisinteger()) { return obj.value|0; } else if (obj.ttisstring()) { - let str = String.fromCharCode(...obj.value); - return luaV_tointeger(new TValue(CT.LUA_TNUMFLT, parseFloat(str)), mode); // TODO: luaO_str2num + return luaV_tointeger(new TValue(CT.LUA_TNUMFLT, parseFloat(obj.jsstring())), mode); // TODO: luaO_str2num } return false; @@ -921,8 +920,8 @@ const LTintfloat = function(l, r) { const l_strcmp = function(ls, rs) { // TODO: lvm.c:248 static int l_strcmp (const TString *ls, const TString *rs) - ls = String.fromCharCode(...ls.value); - rs = String.fromCharCode(...rs.value); + ls = ls.jsstring(); + rs = rs.jsstring(); return ls === rs ? 0 : (ls < rs ? -1 : 1); }; @@ -955,11 +954,11 @@ const luaV_objlen = function(L, ra, rb) { const tostring = function(L, i) { let o = L.stack[i]; - let str = `${o.value}`; - str = lua.to_luastring(str, str.length); - if (o.ttisstring() || (o.ttisnumber() && !isNaN(str))) { - L.stack[i] = L.l_G.intern(str); + if (o.ttisstring()) return true; + + if (o.ttisnumber() && !isNaN(o.value)) { + L.stack[i] = L.l_G.intern(lua.to_luastring(`${o.value}`)); return true; } @@ -975,18 +974,16 @@ const luaV_concat = function(L, total) { do { let top = L.top; let n = 2; /* number of elements handled in this pass (at least 2) */ - let v = L.stack[top-2]; - let v2 = L.stack[top-1]; - if (!(v.ttisstring() || v.ttisnumber()) || !tostring(L, top - 2)) // TODO: tostring - ltm.luaT_trybinTM(L, v, v2, top-2, ltm.TMS.TM_CONCAT); - else if (v2.ttisstring() && v2.value.length === 0) + if (!(L.stack[top-2].ttisstring() || L.stack[top-2].ttisnumber()) || !tostring(L, top - 1)) + ltm.luaT_trybinTM(L, L.stack[top-2], L.stack[top-1], top-2, ltm.TMS.TM_CONCAT); + else if (L.stack[top-1].ttisstring() && L.stack[top-1].value.length === 0) tostring(L, top - 2); - else if (v.ttisstring() && v.value.length === 0) + else if (L.stack[top-2].ttisstring() && L.stack[top-2].value.length === 0) L.stack[top - 2] = L.stack[top - 1]; else { /* at least two non-empty string values; get as many as possible */ - let tl = v.value.length; + let tl = L.stack[top-2].value.length; /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, top - n - 1); n++) { let l = L.stack[top - n - 1].value.length; @@ -1018,7 +1015,7 @@ const gettable = function(L, table, key, ra, recur) { let element = table.__index(table, key); if (!element.ttisnil()) { - L.stack[ra] = table.__index(table, key); + L.stack[ra] = element; } else { luaV_finishget(L, table, key, ra, element, recur); } -- cgit v1.2.3-54-g00ecf