From 98e485a290fb6caedc3374d05d9053ef07003658 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 13 Oct 2020 06:40:18 -0700 Subject: [PATCH 1/5] support big endian --- src/lexer.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lexer.js b/src/lexer.js index f5e01c6..411d601 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -2,17 +2,21 @@ const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'pr let wasm; +const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1; + export function parse (source, name = '@') { if (!wasm) throw new Error('Not initialized'); + const len = source.length + 1 + (source.length + 1) % 2; + // need 2 bytes per code point plus analysis space so we double again - const extraMem = (wasm.__heap_base.value || wasm.__heap_base) + source.length * 4 - wasm.memory.buffer.byteLength; + const extraMem = (wasm.__heap_base.value || wasm.__heap_base) + len * 4 - wasm.memory.buffer.byteLength; if (extraMem > 0) wasm.memory.grow(Math.ceil(extraMem / 65536)); - const addr = wasm.sa(source.length); - copy(source, new Uint16Array(wasm.memory.buffer, addr, source.length + 1)); + const addr = wasm.sa(len); + (isLE ? copyLE : copyBE)(source, new Uint16Array(wasm.memory.buffer, addr, len)); if (!wasm.parseCJS(addr, source.length, 0, 0)) throw Object.assign(new Error(`Parse error ${name}${wasm.e()}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`), { idx: wasm.e() }); @@ -29,7 +33,17 @@ export function parse (source, name = '@') { return { exports: [...exports], reexports: [...reexports] }; } -function copy (src, outBuf16) { +function copyBE (src, outBuf16) { + const len = src.length / 2; + let i = 0; + while (i < len) { + outBuf16[i] = src.charCodeAt(i * 2 + 1); + outBuf16[i + 1] = src.charCodeAt(i * 2); + i++; + } +} + +function copyLE (src, outBuf16) { const len = src.length; let i = 0; while (i < len) From d3320aff2a902b8de42b31fefd633fa28b211fe0 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 13 Oct 2020 12:58:14 -0700 Subject: [PATCH 2/5] second attempt --- src/lexer.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lexer.js b/src/lexer.js index 411d601..f4f60dd 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -37,8 +37,10 @@ function copyBE (src, outBuf16) { const len = src.length / 2; let i = 0; while (i < len) { - outBuf16[i] = src.charCodeAt(i * 2 + 1); - outBuf16[i + 1] = src.charCodeAt(i * 2); + const ch1 = src.charCodeAt(i * 2 + 1); + outBuf16[i] = (ch1 & 0xff) << 8 | (ch1 & 0xff0) >> 8; + const ch2 = src.charCodeAt(i * 2); + outBuf16[i + 1] = (ch2 & 0xff) << 8 | (ch2 & 0xff0) >> 8; i++; } } From 43d35ba53228998fba8b8442d2b4db870a250fcb Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 13 Oct 2020 13:02:09 -0700 Subject: [PATCH 3/5] fixup iterator --- src/lexer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lexer.js b/src/lexer.js index f4f60dd..6f2bed4 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -34,14 +34,14 @@ export function parse (source, name = '@') { } function copyBE (src, outBuf16) { - const len = src.length / 2; + const len = src.length; let i = 0; while (i < len) { const ch1 = src.charCodeAt(i * 2 + 1); - outBuf16[i] = (ch1 & 0xff) << 8 | (ch1 & 0xff0) >> 8; + outBuf16[i * 2] = (ch1 & 0xff) << 8 | (ch1 & 0xff0) >> 8; const ch2 = src.charCodeAt(i * 2); - outBuf16[i + 1] = (ch2 & 0xff) << 8 | (ch2 & 0xff0) >> 8; - i++; + outBuf16[i * 2 + 1] = (ch2 & 0xff) << 8 | (ch2 & 0xff0) >> 8; + i += 2; } } From 4e16c060300d0bff7d80f9ab47f9d0c67ad9a706 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 13 Oct 2020 13:12:56 -0700 Subject: [PATCH 4/5] fixes --- src/lexer.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/lexer.js b/src/lexer.js index 6f2bed4..8c96ef4 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -37,11 +37,8 @@ function copyBE (src, outBuf16) { const len = src.length; let i = 0; while (i < len) { - const ch1 = src.charCodeAt(i * 2 + 1); - outBuf16[i * 2] = (ch1 & 0xff) << 8 | (ch1 & 0xff0) >> 8; - const ch2 = src.charCodeAt(i * 2); - outBuf16[i * 2 + 1] = (ch2 & 0xff) << 8 | (ch2 & 0xff0) >> 8; - i += 2; + const ch = src.charCodeAt(i); + outBuf16[i++] = (ch & 0xff) << 8 | (ch & 0xff0) >> 8; } } From 7c86b914d3a2f1d933c48d18ad85b59122bd7a78 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 13 Oct 2020 13:19:17 -0700 Subject: [PATCH 5/5] finally working --- src/lexer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexer.js b/src/lexer.js index 8c96ef4..4aee529 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -8,7 +8,7 @@ export function parse (source, name = '@') { if (!wasm) throw new Error('Not initialized'); - const len = source.length + 1 + (source.length + 1) % 2; + const len = source.length + 1; // need 2 bytes per code point plus analysis space so we double again const extraMem = (wasm.__heap_base.value || wasm.__heap_base) + len * 4 - wasm.memory.buffer.byteLength;