MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const { spawnSync } = require('child_process');
2
3function assert(condition, message) {
4 if (!condition) throw new Error(message);
5}
6
7function run(source) {
8 const result = spawnSync(process.execPath, ['-e', source], { encoding: 'utf8' });
9 if (result.error) throw result.error;
10 assert(
11 result.status === 0,
12 `expected console.inspect probe to exit 0, got ${result.status}\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`
13 );
14 return result.stdout;
15}
16
17const flat = run('console.inspect("hello world")');
18assert(flat.includes('<String flat '), `expected flat string internals, got ${JSON.stringify(flat)}`);
19assert(/value=0x[0-9a-f]+/.test(flat), `expected raw value bits, got ${JSON.stringify(flat)}`);
20assert(/data=0x[0-9a-f]+/.test(flat), `expected raw data bits, got ${JSON.stringify(flat)}`);
21assert(/ptr=0x[0-9a-f]+/.test(flat), `expected flat string pointer, got ${JSON.stringify(flat)}`);
22assert(flat.includes('len=11'), `expected flat string length, got ${JSON.stringify(flat)}`);
23assert(flat.includes('ascii=yes'), `expected ASCII state, got ${JSON.stringify(flat)}`);
24assert(flat.includes('bytes="hello world"'), `expected byte preview, got ${JSON.stringify(flat)}`);
25
26const rope = run('console.inspect("a" + "b")');
27assert(rope.includes('<String rope '), `expected rope string internals, got ${JSON.stringify(rope)}`);
28assert(rope.includes('depth=1'), `expected rope depth, got ${JSON.stringify(rope)}`);
29assert(rope.includes('cached=undefined'), `expected untouched rope cache, got ${JSON.stringify(rope)}`);
30assert(rope.includes('left: <String flat '), `expected left flat leaf, got ${JSON.stringify(rope)}`);
31assert(rope.includes('right: <String flat '), `expected right flat leaf, got ${JSON.stringify(rope)}`);
32
33console.log('console.inspect string internals ok');