Skip to content

Commit

Permalink
fix(xsnap): tolerate Symbols in console.log() arguments (#3618)
Browse files Browse the repository at this point in the history
The xsnap `print()` doesn't know how to print a Symbol, so until that's
fixed, here's a workaround in our `tryPrint()` function. This is the backend
of all console.log calls within the XS process.

refs Moddable-OpenSource/moddable#679
  • Loading branch information
warner authored Aug 7, 2021
1 parent 198e5c3 commit 314ee93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/xsnap/lib/console-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
function tryPrint(...args) {
try {
// eslint-disable-next-line
print(...args);
print(...args.map(arg => typeof arg === 'symbol' ? arg.toString() : arg));
} catch (err) {
// eslint-disable-next-line
print('cannot print:', err.message);
args.forEach((a, i) => {
// eslint-disable-next-line
print(` ${i}:`, a.toString ? a.toString() : '<no .toString>', typeof a);
});
}
}

Expand Down
19 changes: 19 additions & 0 deletions packages/xsnap/test/test-boot-lockdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,22 @@ test('TextDecoder under xsnap handles TypedArray and subarrays', async t => {
t.assert(pass);
}
});

test('console - symbols', async t => {
// our console-shim.js handles Symbol specially
const bootScript = await ld.asset('../dist/bundle-ses-boot.umd.js');
const opts = options(io);
const vat = xsnap(opts);
await vat.evaluate(bootScript);
t.deepEqual([], opts.messages);
await vat.evaluate(`
const encoder = new TextEncoder();
globalThis.send = msg => issueCommand(encoder.encode(JSON.stringify(msg)).buffer);
console.log('console:', 123);
console.log('console:', Symbol('anonymous'));
console.log('console:', Symbol.for('registered'));
send('ok');
`);
await vat.close();
t.deepEqual(['"ok"'], opts.messages);
});

0 comments on commit 314ee93

Please sign in to comment.