From 11a2421acaf2b39d19ee99933102c35e28fd13f8 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 13 Dec 2017 14:55:33 +1100 Subject: Use Uint8Array to back strings --- tests/test-suite/ltests.js | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'tests') diff --git a/tests/test-suite/ltests.js b/tests/test-suite/ltests.js index 8d89102..08bb603 100644 --- a/tests/test-suite/ltests.js +++ b/tests/test-suite/ltests.js @@ -60,8 +60,7 @@ const getstring = function(L, buff, pc) { while (pc.script[pc.offset] !== 0 && pc.offset < pc.script.length && delimits.indexOf(pc.script[pc.offset]) < 0) buff[i++] = pc.script[pc.offset++]; } - buff.length = i; - return buff; + return buff.subarray(0, i); }; const getindex = function(L, L1, pc) { @@ -100,7 +99,7 @@ const printstack = function(L) { const ops = "+-*%^/\\&|~<>_!".split('').map(e => e.charCodeAt(0)); const runJS = function(L, L1, pc) { - let buff = []; + let buff = new Uint8Array(300); let status = 0; if (!pc || !pc.script) return lauxlib.luaL_error(L, lua.to_luastring("attempt to runJS null script")); for (;;) { @@ -527,25 +526,15 @@ const udataval = function(L) { const d2s = function(L) { let d = lauxlib.luaL_checknumber(L, 1); - - let dv = new DataView(new ArrayBuffer(8)); - dv.setFloat64(0, d, true); - - let b = []; - for (let i = 0; i < 8; i++) - b.push(dv.getUint8(i, true)); - - lua.lua_pushlstring(L, b, 8); + let b = new ArrayBuffer(8); + new DataView(b).setFloat64(0, d, true); + lua.lua_pushlstring(L, new Uint8Array(b), 8); return 1; }; const s2d = function(L) { let b = lauxlib.luaL_checkstring(L, 1); - - let dv = new DataView(new ArrayBuffer(8)); - for (let i = 0; i < b.length; i++) - dv.setUint8(i, b[i], true); - + let dv = new DataView(b.buffer); lua.lua_pushnumber(L, dv.getFloat64(0, true)); return 1; }; -- cgit v1.2.3-70-g09d2 From af95e27d1b2d5f0f39534523778003dfd1fcf417 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 13 Dec 2017 17:17:43 +1100 Subject: src/lzio.js: Don't permit DataView returned from a lua_load reader any more --- src/lzio.js | 15 ++++----------- tests/lstrlib.js | 4 ++-- tests/tests.js | 2 +- 3 files changed, 7 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/src/lzio.js b/src/lzio.js index efd7909..a00a24b 100644 --- a/src/lzio.js +++ b/src/lzio.js @@ -48,22 +48,15 @@ class ZIO { const EOZ = -1; const luaZ_fill = function(z) { - let size; let buff = z.reader(z.L, z.data); if (buff === null) return EOZ; - if (buff instanceof DataView) { - z.buffer = new Uint8Array(buff.buffer, buff.byteOffset, buff.byteLength); - z.off = 0; - size = buff.byteLength - buff.byteOffset; - } else { - assert(buff instanceof Uint8Array, "Should only load binary of array of bytes"); - z.buffer = buff; - z.off = 0; - size = buff.length; - } + assert(buff instanceof Uint8Array, "Should only load binary of array of bytes"); + let size = buff.length; if (size === 0) return EOZ; + z.buffer = buff; + z.off = 0; z.n = size - 1; return z.buffer[z.off++]; }; diff --git a/tests/lstrlib.js b/tests/lstrlib.js index a55fca1..bdf68d8 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -442,9 +442,9 @@ test('string.dump', function (t) { lua.lua_call(L, 0, -1); - let dv = lua.lua_todataview(L, -1); + let str = lua.lua_tostring(L, -1); - lua.lua_load(L, function(L, s) { let r = s.dv; s.dv = null; return r; }, {dv: dv}, lua.to_luastring("test"), lua.to_luastring("binary")); + lua.lua_load(L, function(L, s) { let r = s.str; s.str = null; return r; }, {str: str}, lua.to_luastring("test"), lua.to_luastring("binary")); lua.lua_call(L, 0, -1); diff --git a/tests/tests.js b/tests/tests.js index 1bc4fc6..1ad9900 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -11,7 +11,7 @@ const toByteCode = function(luaCode) { return 0; }, b, false) !== 0) throw Error("unable to dump given function"); - return new DataView(Uint8Array.from(b).buffer); + return Uint8Array.from(b); }; const getState = function(luaCode) { -- cgit v1.2.3-70-g09d2 From b162ffe744bc407ad4035674f5c05848a460b58f Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 15 Dec 2017 14:50:14 +1100 Subject: src/lobject.js: Refactor luaO_chunkid to use Uint8Array.set --- src/lobject.js | 26 ++++++++++++++++++-------- src/luaconf.js | 2 +- tests/test-suite/db.js | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/src/lobject.js b/src/lobject.js index 5ca7cd3..c02bc91 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -294,7 +294,7 @@ class LocVar { } } -const RETS = defs.to_luastring("...", true); +const RETS = defs.to_luastring("..."); const PRE = defs.to_luastring("[string \""); const POS = defs.to_luastring("\"]"); @@ -305,27 +305,37 @@ const luaO_chunkid = function(source, bufflen) { if (l < bufflen) /* small enough? */ out = source.slice(1); else { /* truncate it */ - out = source.slice(1, bufflen); + out = source.slice(1, bufflen+1); } } else if (source[0] === char['@']) { /* file name */ if (l <= bufflen) /* small enough? */ out = source.slice(1); else { /* add '...' before rest of name */ + out = new Uint8Array(bufflen); + out.set(RETS); bufflen -= RETS.length; - out = Uint8Array.from(Array.from(RETS).concat(Array.from(source.subarray(1 + l - bufflen)))); + out.set(source.subarray(l - bufflen), RETS.length); } } else { /* string; format as [string "source"] */ + out = new Uint8Array(bufflen); let nli = source.indexOf(char['\n']); /* find first new line (if any) */ - out = Array.from(PRE); /* add prefix */ - bufflen -= PRE.length + RETS.length + POS.length + 1; /* save space for prefix+suffix+'\0' */ + out.set(PRE); /* add prefix */ + let out_i = PRE.length; + bufflen -= PRE.length + RETS.length + POS.length; /* save space for prefix+suffix */ if (l < bufflen && nli === -1) { /* small one-line source? */ - out = out.concat(Array.from(source), Array.from(POS)); /* keep it */ + out.set(source, out_i); /* keep it */ + out_i += source.length; } else { if (nli !== -1) l = nli; /* stop at first newline */ if (l > bufflen) l = bufflen; - out = out.concat(Array.from(source.subarray(0, l)), Array.from(RETS), Array.from(POS)); + out.set(source.subarray(0, l), out_i); + out_i += l; + out.set(RETS, out_i); + out_i += RETS.length; } - out = Uint8Array.from(out); + out.set(POS, out_i); + out_i += POS.length; + out = out.subarray(0, out_i); } return out; }; diff --git a/src/luaconf.js b/src/luaconf.js index 00663a6..5331720 100644 --- a/src/luaconf.js +++ b/src/luaconf.js @@ -19,7 +19,7 @@ const LUAI_MAXSTACK = 100000; @@ of a function in debug information. ** CHANGE it if you want a different size. */ -const LUA_IDSIZE = 60; +const LUA_IDSIZE = 60-1; /* fengari uses 1 less than lua as we don't embed the null byte */ const lua_integer2str = function(n) { return sprintf(LUA_INTEGER_FMT, n); diff --git a/tests/test-suite/db.js b/tests/test-suite/db.js index a2e3ce6..ff13ecb 100644 --- a/tests/test-suite/db.js +++ b/tests/test-suite/db.js @@ -64,7 +64,7 @@ test("[test-suite] db: getinfo, ...line...", function (t) { }); -test("[test-suite] db: test file ad string names truncation", function (t) { +test("[test-suite] db: test file and string names truncation", function (t) { let luaCode = ` a = "function f () end" local function dostring (s, x) return load(s, x)() end -- cgit v1.2.3-70-g09d2