Skip to content

Support big endian #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// 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() });
Expand All @@ -29,7 +33,16 @@ export function parse (source, name = '@') {
return { exports: [...exports], reexports: [...reexports] };
}

function copy (src, outBuf16) {
function copyBE (src, outBuf16) {
const len = src.length;
let i = 0;
while (i < len) {
const ch = src.charCodeAt(i);
outBuf16[i++] = (ch & 0xff) << 8 | (ch & 0xff0) >> 8;
Copy link
Contributor

@ExE-Boss ExE-Boss Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a 0, which causes the most significant nibble to be discared:

Suggested change
outBuf16[i++] = (ch & 0xff) << 8 | (ch & 0xff0) >> 8;
outBuf16[i++] = (ch & 0xff) << 8 | (ch & 0xff00) >> 8;

Copy link
Collaborator Author

@guybedford guybedford Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this, it wasn't causing an issue because the right shift drops these bits anyway, the version on master has the fix.

}
}

function copyLE (src, outBuf16) {
const len = src.length;
let i = 0;
while (i < len)
Expand Down