Skip to content

Commit

Permalink
feat(xsnap): use SES assert.quote to format console args
Browse files Browse the repository at this point in the history
fixes #3844
  • Loading branch information
dckc committed Sep 20, 2021
1 parent 99d66bf commit 790d985
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
27 changes: 16 additions & 11 deletions packages/xsnap/lib/console-shim.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/* global globalThis */

// We use setQuote() below to break the cycle
// where SES requires console and console is
// implemented using assert.quote from SES.
let quote = v => (typeof v === 'string' ? v : '[?]');

function tryPrint(...args) {
try {
// eslint-disable-next-line
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);
});
}
// eslint-disable-next-line
print(...args.map(v => typeof v === 'string' ? v : quote(v)));
}

const noop = _ => {};
Expand Down Expand Up @@ -52,4 +49,12 @@ const console = {
timeStamp: noop,
};

let quoteSet = false;

export function setQuote(f) {
if (quoteSet) throw TypeError('quote already set');
quote = f;
quoteSet = true;
}

globalThis.console = console;
4 changes: 3 additions & 1 deletion packages/xsnap/lib/ses-boot-debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import './console-shim.js';
import { setQuote } from './console-shim.js';
import '@agoric/eventual-send/shim.js';
import './lockdown-shim-debug.js';

setQuote(assert.quote);

harden(console);
4 changes: 3 additions & 1 deletion packages/xsnap/lib/ses-boot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import './console-shim.js';
import { setQuote } from './console-shim.js';
import '@agoric/eventual-send/shim.js';
import './lockdown-shim.js';

setQuote(assert.quote);

harden(console);

0 comments on commit 790d985

Please sign in to comment.