Skip to content

Commit

Permalink
fix: fixed allocation calculation (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Apr 4, 2022
1 parent df27d03 commit 057b19f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
11 changes: 6 additions & 5 deletions lib/lexer.asm.js
@@ -1,4 +1,4 @@
let asm, asmBuffer, allocSize = 2<<18, addr;
let asm, asmBuffer, allocSize = 2<<19, addr;

const copy = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1 ? function (src, outBuf16) {
const len = src.length;
Expand All @@ -19,16 +19,17 @@ let source, name;
export function parse (_source, _name = '@') {
source = _source;
name = _name;
// 2 bytes per string code point
// + analysis space (2^17)
// remaining space is EMCC stack space (2^17)
const memBound = source.length * 2 + (2 << 18);
if (memBound > allocSize || !asm) {
while (memBound > allocSize) allocSize *= 2;
asmBuffer = new ArrayBuffer(allocSize);
copy(words, new Uint16Array(asmBuffer, 16, words.length));
asm = asmInit(typeof self !== 'undefined' ? self : global, {}, asmBuffer);
// 2 bytes per string code point
// + analysis space
// remaining space is stack space
addr = asm.su(source.length * 2 + (2 << 17));
// lexer.c bulk allocates string space + analysis space
addr = asm.su(allocSize - (2<<17));
}
const len = source.length + 1;
asm.ses(addr);
Expand Down
11 changes: 6 additions & 5 deletions src/lexer.asm.js
@@ -1,4 +1,4 @@
let asm, asmBuffer, allocSize = 2<<18, addr;
let asm, asmBuffer, allocSize = 2<<19, addr;

const copy = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1 ? function (src, outBuf16) {
const len = src.length;
Expand All @@ -19,16 +19,17 @@ let source, name;
export function parse (_source, _name = '@') {
source = _source;
name = _name;
// 2 bytes per string code point
// + analysis space (2^17)
// remaining space is EMCC stack space (2^17)
const memBound = source.length * 2 + (2 << 18);
if (memBound > allocSize || !asm) {
while (memBound > allocSize) allocSize *= 2;
asmBuffer = new ArrayBuffer(allocSize);
copy(words, new Uint16Array(asmBuffer, 16, words.length));
asm = asmInit(typeof self !== 'undefined' ? self : global, {}, asmBuffer);
// 2 bytes per string code point
// + analysis space
// remaining space is stack space
addr = asm.su(source.length * 2 + (2 << 17));
// lexer.c bulk allocates string space + analysis space
addr = asm.su(allocSize - (2<<17));
}
const len = source.length + 1;
asm.ses(addr);
Expand Down

0 comments on commit 057b19f

Please sign in to comment.