From 790d98595f17f01d9e2599a2b749e3104441a3a6 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Mon, 20 Sep 2021 15:47:53 -0500 Subject: [PATCH] feat(xsnap): use SES assert.quote to format console args fixes #3844 --- packages/xsnap/lib/console-shim.js | 27 ++++++++++++++++----------- packages/xsnap/lib/ses-boot-debug.js | 4 +++- packages/xsnap/lib/ses-boot.js | 4 +++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/xsnap/lib/console-shim.js b/packages/xsnap/lib/console-shim.js index 7216a5b42ec3..d34c107683f5 100644 --- a/packages/xsnap/lib/console-shim.js +++ b/packages/xsnap/lib/console-shim.js @@ -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() : '', typeof a); - }); - } + // eslint-disable-next-line + print(...args.map(v => typeof v === 'string' ? v : quote(v))); } const noop = _ => {}; @@ -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; diff --git a/packages/xsnap/lib/ses-boot-debug.js b/packages/xsnap/lib/ses-boot-debug.js index a5de25639a3f..fc1216a8972c 100644 --- a/packages/xsnap/lib/ses-boot-debug.js +++ b/packages/xsnap/lib/ses-boot-debug.js @@ -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); diff --git a/packages/xsnap/lib/ses-boot.js b/packages/xsnap/lib/ses-boot.js index de6878a5f30d..318bf8cc648d 100644 --- a/packages/xsnap/lib/ses-boot.js +++ b/packages/xsnap/lib/ses-boot.js @@ -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);