Skip to content

Commit 1f55735

Browse files
authored
compiler: add snapshot test for heap contents (#584)
1 parent 84a0785 commit 1f55735

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Snapshot report for `test/test-wasm.js`
2+
3+
The actual snapshot is saved in `test-wasm.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## snapshot: raw heap after match
8+
9+
> Snapshot 1
10+
11+
'0000020c 00*2 00000810 000007f0 00*127 0000000c 00620061 00*2 0000000c 000007a0 00*2 0000002c 00*11 0000024c 00*147 0000004c 00000810 00*18 0000001c 00000001 00000001 00000008 ffffffff 00000003 00*2 0000001c 00000002 00000002 00*1 ffffffff 00000003 000007f0 00*1'
236 Bytes
Binary file not shown.

packages/compiler/test/test-wasm.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,46 @@ test('bindings chunks contain valid CST nodes and tagged terminals', async t =>
19651965
t.true(totalNonZero >= 1, 'should have found at least one non-zero binding');
19661966
});
19671967

1968+
// RLE-compress a Uint8Array: non-zero i32 words shown as hex, runs of
1969+
// zero words collapsed to "00*N".
1970+
function rleHexDump(bytes) {
1971+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1972+
const parts = [];
1973+
let i = 0;
1974+
while (i < bytes.length) {
1975+
if (i + 4 <= bytes.length && view.getUint32(i, true) === 0) {
1976+
let zeroWords = 0;
1977+
while (i + 4 <= bytes.length && view.getUint32(i, true) === 0) {
1978+
zeroWords++;
1979+
i += 4;
1980+
}
1981+
parts.push(`00*${zeroWords}`);
1982+
} else {
1983+
const word = view.getUint32(i, true);
1984+
parts.push(word.toString(16).padStart(8, '0'));
1985+
i += 4;
1986+
}
1987+
}
1988+
return parts.join(' ');
1989+
}
1990+
1991+
test('snapshot: raw heap after match', async t => {
1992+
const g = ohm.grammar('G { start = "a" b\nb = "b" }');
1993+
const wasmGrammar = await toWasmGrammar(g);
1994+
1995+
const {memory, __offset} = wasmGrammar._instance.exports;
1996+
1997+
wasmGrammar.match('ab').use(r => {
1998+
t.true(r.succeeded());
1999+
2000+
// Capture the heap region allocated during this match.
2001+
const heapStart = r._heapWatermark;
2002+
const heapEnd = __offset.value;
2003+
const bytes = new Uint8Array(memory.buffer, heapStart, heapEnd - heapStart);
2004+
t.snapshot(rleHexDump(bytes));
2005+
});
2006+
});
2007+
19682008
test('chunkedBindings: false', async t => {
19692009
const g = ohm.grammar('G { Start = letter+ ";" }');
19702010
const wasmGrammar = await toWasmGrammar(g, {chunkedBindings: false});

0 commit comments

Comments
 (0)