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 --- src/lobject.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/lobject.js') diff --git a/src/lobject.js b/src/lobject.js index 707cdf8..18adb27 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -312,19 +312,20 @@ const luaO_chunkid = function(source, bufflen) { out = source.slice(1); else { /* add '...' before rest of name */ bufflen -= RETS.length; - out = RETS.concat(source.slice(1 + l - bufflen)); + out = Uint8Array.from(Array.from(RETS).concat(Array.from(source.subarray(1 + l - bufflen)))); } } else { /* string; format as [string "source"] */ let nli = source.indexOf(char['\n']); /* find first new line (if any) */ - out = PRE; /* add prefix */ + out = Array.from(PRE); /* add prefix */ bufflen -= PRE.length + RETS.length + POS.length + 1; /* save space for prefix+suffix+'\0' */ if (l < bufflen && nli === -1) { /* small one-line source? */ - out = out.concat(source, POS); /* keep it */ + out = out.concat(Array.from(source), Array.from(POS)); /* keep it */ } else { if (nli !== -1) l = nli; /* stop at first newline */ if (l > bufflen) l = bufflen; - out = out.concat(source.slice(0, l), RETS, POS); + out = out.concat(Array.from(source.subarray(0, l)), Array.from(RETS), Array.from(POS)); } + out = Uint8Array.from(out); } return out; }; -- cgit v1.2.3-54-g00ecf From 17e4cff6514c75920462397c227408c21336d1ae Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 13 Dec 2017 14:56:26 +1100 Subject: src/: Use .subarray instead of .slice (optimisation) --- src/lauxlib.js | 2 +- src/llex.js | 4 ++-- src/lobject.js | 6 +++--- src/lundump.js | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/lobject.js') diff --git a/src/lauxlib.js b/src/lauxlib.js index 6acf57f..3b2be6e 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -62,7 +62,7 @@ const pushglobalfuncname = function(L, ar) { if (findfield(L, top + 1, 2)) { let name = lua.lua_tostring(L, -1); if (name[0] === "_".charCodeAt(0) && name[1] === "G".charCodeAt(0) && name[2] === ".".charCodeAt(0)) { /* name start with '_G.'? */ - lua.lua_pushstring(L, name.slice(3)); /* push name without prefix */ + lua.lua_pushstring(L, name.subarray(3)); /* push name without prefix */ lua.lua_remove(L, -2); /* remove original name */ } lua.lua_copy(L, -1, top + 1); /* move name to proper place */ diff --git a/src/llex.js b/src/llex.js index 9e4bed4..438f288 100644 --- a/src/llex.js +++ b/src/llex.js @@ -328,7 +328,7 @@ const read_long_string = function(ls, seminfo, sep) { } if (seminfo) - seminfo.ts = luaX_newstring(ls, ls.buff.buffer.slice(2 + sep, ls.buff.n - (2 + sep))); + seminfo.ts = luaX_newstring(ls, ls.buff.buffer.subarray(2 + sep, ls.buff.n - (2 + sep))); }; const esccheck = function(ls, c, msg) { @@ -453,7 +453,7 @@ const read_string = function(ls, del, seminfo) { } save_and_next(ls); /* skip delimiter */ - seminfo.ts = luaX_newstring(ls, ls.buff.buffer.slice(1, ls.buff.n-1)); + seminfo.ts = luaX_newstring(ls, ls.buff.buffer.subarray(1, ls.buff.n-1)); }; const token_to_index = Object.create(null); /* don't want to return true for e.g. 'hasOwnProperty' */ diff --git a/src/lobject.js b/src/lobject.js index 18adb27..5ca7cd3 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -531,7 +531,7 @@ const luaO_pushvfstring = function(L, fmt, argp) { for (;;) { e = fmt.indexOf(char['%'], i); if (e == -1) break; - pushstr(L, fmt.slice(i, e)); + pushstr(L, fmt.subarray(i, e)); switch(fmt[e+1]) { case char['s']: { let s = argp[a++]; @@ -540,7 +540,7 @@ const luaO_pushvfstring = function(L, fmt, argp) { /* respect null terminator */ let i = s.indexOf(0); if (i !== -1) - s = s.slice(0, i); + s = s.subarray(0, i); } pushstr(L, s); break; @@ -609,7 +609,7 @@ const luaO_pushvfstring = function(L, fmt, argp) { i = e + 2; } ldo.luaD_checkstack(L, 1); - pushstr(L, fmt.slice(i)); + pushstr(L, fmt.subarray(i)); if (n > 0) lvm.luaV_concat(L, n+1); return L.stack[L.top-1].svalue(); }; diff --git a/src/lundump.js b/src/lundump.js index 7461813..e49c376 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -25,7 +25,7 @@ class BytecodeParser { assert(defs.is_luastring(name)); if (name[0] == defs.char["@"] || name[0] == defs.char["="]) - this.name = name.slice(1); + this.name = name.subarray(1); else if (name[0] == defs.LUA_SIGNATURE.charCodeAt(0)) this.name = defs.to_luastring("binary string", true); else -- cgit v1.2.3-54-g00ecf 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 'src/lobject.js') 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-54-g00ecf