From 636b1d8a5a566fac0c9c61fed44049c659435a56 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 8 May 2017 18:26:14 +1000 Subject: Introduce a TString class --- src/lapi.js | 2 +- src/ldebug.js | 8 ++++---- src/ldump.js | 4 ++-- src/lfunc.js | 2 +- src/llex.js | 2 +- src/lobject.js | 4 ++-- src/lstring.js | 28 +++++++++++++++++++++++++--- src/ltable.js | 2 +- src/lvm.js | 8 ++++---- 9 files changed, 41 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 38c8bd7..97cb061 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -512,7 +512,7 @@ const aux_upvalue = function(L, fi, n) { if (!(1 <= n && n <= p.upvalues.length)) return null; let name = p.upvalues[n-1].name; return { - name: name ? name : defs.to_luastring("(*no name)", true), + name: name ? name.getstr() : defs.to_luastring("(*no name)", true), val: f.upvals[n-1].val() }; } diff --git a/src/ldebug.js b/src/ldebug.js index af9b9a9..6a16b9c 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -82,7 +82,7 @@ const upvalname = function(p, uv) { assert(uv < p.upvalues.length); let s = p.upvalues[uv].name; if (s === null) return ["?".charCodeAt(0)]; - return s; + return s.getstr(); }; const findvararg = function(ci, n) { @@ -167,7 +167,7 @@ const funcinfo = function(ar, cl) { ar.what = "J"; } else { let p = cl.p; - ar.source = p.source ? p.source : defs.to_luastring("=?", true); + ar.source = p.source ? p.source.getstr() : defs.to_luastring("=?", true); ar.linedefined = p.linedefined; ar.lastlinedefined = p.lastlinedefined; ar.what = ar.linedefined === 0 ? defs.to_luastring("main", true) : defs.to_luastring("Lua", true); @@ -491,7 +491,7 @@ const funcnamefromcode = function(L, ci) { return null; /* cannot find a reasonable name */ } - r.name = L.l_G.tmname[tm]; + r.name = L.l_G.tmname[tm].getstr(); r.funcname = defs.to_luastring("metamethod", true); return r; }; @@ -569,7 +569,7 @@ const luaG_ordererror = function(L, p1, p2) { const luaG_addinfo = function(L, msg, src, line) { let buff; if (src) - buff = lobject.luaO_chunkid(src, luaconf.LUA_IDSIZE); + buff = lobject.luaO_chunkid(src.getstr(), luaconf.LUA_IDSIZE); else buff = ['?'.charCodeAt(0)]; diff --git a/src/ldump.js b/src/ldump.js index 99780f6..a2abbed 100644 --- a/src/ldump.js +++ b/src/ldump.js @@ -66,8 +66,8 @@ const DumpString = function(s, D) { if (s === null) DumpByte(0, D); else { - let size = s.length + 1; - let str = s; + let size = s.tsslen() + 1; + let str = s.getstr(); if (size < 0xFF) DumpByte(size, D); else { diff --git a/src/lfunc.js b/src/lfunc.js index 161724e..40c0905 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -114,7 +114,7 @@ const luaF_getlocalname = function(f, local_number, pc) { if (pc < f.locvars[i].endpc) { /* is variable active? */ local_number--; if (local_number === 0) - return f.locvars[i].varname; + return f.locvars[i].varname.getstr(); } } return null; /* not found */ diff --git a/src/llex.js b/src/llex.js index e3349ce..220f45d 100644 --- a/src/llex.js +++ b/src/llex.js @@ -629,7 +629,7 @@ const llex = function(ls, seminfo) { let ts = luaX_newstring(ls, ls.buff.buffer); seminfo.ts = ts; - let kidx = luaX_tokens.slice(0, 22).indexOf(defs.to_jsstring(ts)); + let kidx = luaX_tokens.slice(0, 22).indexOf(defs.to_jsstring(ts.getstr())); if (kidx >= 0) /* reserved word? */ return kidx + FIRST_RESERVED; else diff --git a/src/lobject.js b/src/lobject.js index 77c63d0..4b83bdb 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -161,11 +161,11 @@ class TValue { } svalue() { - return this.tsvalue(); + return this.tsvalue().getstr(); } vslen() { - return this.tsvalue().length; + return this.tsvalue().tsslen(); } jsstring(from, to) { diff --git a/src/lstring.js b/src/lstring.js index af434c6..3e94171 100644 --- a/src/lstring.js +++ b/src/lstring.js @@ -1,20 +1,41 @@ "use strict"; +const assert = require("assert"); + const defs = require('./defs.js'); +class TString { + + constructor(L, str) { + this.realstring = str; + } + + getstr() { + return this.realstring; + } + + tsslen() { + return this.realstring.length; + } + +} + const luaS_eqlngstr = function(a, b) { - return a == b || (a.length == b.length && a.join() == b.join()); + assert(a instanceof TString); + assert(b instanceof TString); + return a == b || (a.realstring.length == b.realstring.length && a.realstring.join() == b.realstring.join()); }; /* converts strings (arrays) to a consistent map key */ const luaS_hash = function(str) { - return str.map(e => `${e}|`).join(''); + assert(str instanceof TString); + return str.realstring.map(e => `${e}|`).join(''); }; /* variant that takes ownership of array */ const luaS_bless = function(L, str) { Object.freeze(str); - return str; + return new TString(L, str); }; /* makes a copy */ @@ -32,3 +53,4 @@ module.exports.luaS_hash = luaS_hash; module.exports.luaS_bless = luaS_bless; module.exports.luaS_new = luaS_new; module.exports.luaS_newliteral = luaS_newliteral; +module.exports.TString = TString; diff --git a/src/ltable.js b/src/ltable.js index de4c686..a2987d3 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -53,7 +53,7 @@ const luaH_getint = function(t, key) { }; const luaH_getstr = function(t, key) { - assert(Array.isArray(key)); + assert(key instanceof lstring.TString); return getgeneric(t, lstring.luaS_hash(key)); }; diff --git a/src/lvm.js b/src/lvm.js index 1ea4f65..1861815 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -916,11 +916,11 @@ const LTintfloat = function(l, r) { ** The code is a little tricky because it allows '\0' in the strings. */ const l_strcmp = function(ls, rs) { - let l = ls; - let ll = ls.length; + let l = ls.getstr(); + let ll = ls.tsslen(); let jl = defs.to_jsstring(l); - let r = rs; - let lr = rs.length; + let r = rs.getstr(); + let lr = rs.tsslen(); let jr = defs.to_jsstring(r); for (;;) { let temp = jl === jr; // TODO: strcoll ? -- cgit v1.2.3-70-g09d2