Skip to content

Commit

Permalink
fix(xsnap): tolerate Symbols in console.log() arguments
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 committed Aug 6, 2021
1 parent 198e5c3 commit 36485f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/xsnap/lib/console-shim.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* global globalThis */

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
24 changes: 24 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,27 @@ 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 name = 'SES lockdown worker';
const vat = xsnap({ ...opts, name });
//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);
`);
await vat.evaluate(`
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);
t.pass();
});

0 comments on commit 36485f9

Please sign in to comment.