From c307e3e4be4e02de86103cd48d28f5ba3b18628d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 17 Sep 2020 18:42:36 +0200 Subject: [PATCH] refactor: use Symbol.for instead of Symbol in cli/rt/ (#7537) --- cli/rt/02_console.js | 13 +++++++++++++ cli/rt/11_streams.js | 7 +++---- cli/rt/11_url.js | 3 +-- cli/rt/20_headers.js | 3 +-- cli/rt/20_streams_queuing_strategy.js | 2 +- cli/rt/40_performance.js | 6 +++--- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index b07ccf1872..b6f5bc74be 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -764,6 +764,19 @@ return String(value[customInspect]()); } catch {} } + // This non-unique symbol is used to support op_crates, ie. + // in op_crates/web we don't want to depend on unique "Deno.customInspect" + // symbol defined in the public API. Internal only, shouldn't be used + // by users. + const nonUniqueCustomInspect = Symbol.for("Deno.customInspect"); + if ( + nonUniqueCustomInspect in value && + typeof value[nonUniqueCustomInspect] === "function" + ) { + try { + return String(value[nonUniqueCustomInspect]()); + } catch {} + } if (value instanceof Error) { return String(value.stack); } else if (Array.isArray(value)) { diff --git a/cli/rt/11_streams.js b/cli/rt/11_streams.js index 7f8af19e2d..630878e74e 100644 --- a/cli/rt/11_streams.js +++ b/cli/rt/11_streams.js @@ -11,7 +11,8 @@ const { cloneValue, setFunctionName } = window.__bootstrap.webUtil; const { assert, AssertionError } = window.__bootstrap.util; - const { customInspect, inspect } = window.__bootstrap.console; + + const customInspect = Symbol.for("Deno.customInspect"); const sym = { abortAlgorithm: Symbol("abortAlgorithm"), @@ -636,9 +637,7 @@ } [customInspect]() { - return `${this.constructor.name} {\n readable: ${ - inspect(this.readable) - }\n writable: ${inspect(this.writable)}\n}`; + return this.constructor.name; } } diff --git a/cli/rt/11_url.js b/cli/rt/11_url.js index fee40ebcf9..99f4ba0e10 100644 --- a/cli/rt/11_url.js +++ b/cli/rt/11_url.js @@ -2,7 +2,6 @@ ((window) => { const core = window.Deno.core; - const { customInspect } = window.__bootstrap.console; const { isIterable, requiredArguments } = window.__bootstrap.webUtil; /** https://url.spec.whatwg.org/#idna */ @@ -492,7 +491,7 @@ class URL { #searchParams = null; - [customInspect]() { + [Symbol.for("Deno.customInspect")]() { const keys = [ "href", "origin", diff --git a/cli/rt/20_headers.js b/cli/rt/20_headers.js index 7b9ef8c8ec..ccde77e8d4 100644 --- a/cli/rt/20_headers.js +++ b/cli/rt/20_headers.js @@ -3,7 +3,6 @@ ((window) => { const { DomIterableMixin } = window.__bootstrap.domIterable; const { requiredArguments } = window.__bootstrap.webUtil; - const { customInspect } = window.__bootstrap.console; // From node-fetch // Copyright (c) 2016 David Frank. MIT License. @@ -194,7 +193,7 @@ } } - [customInspect]() { + [Symbol.for("Deno.customInspect")]() { let length = this[headersData].length; let output = ""; for (const [key, value] of this[headersData]) { diff --git a/cli/rt/20_streams_queuing_strategy.js b/cli/rt/20_streams_queuing_strategy.js index cbd30664fa..af32c7d2ee 100644 --- a/cli/rt/20_streams_queuing_strategy.js +++ b/cli/rt/20_streams_queuing_strategy.js @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { customInspect } = window.__bootstrap.console; + const customInspect = Symbol.for("Deno.customInspect"); class CountQueuingStrategy { constructor({ highWaterMark }) { diff --git a/cli/rt/40_performance.js b/cli/rt/40_performance.js index 768c43a6af..a785d23b12 100644 --- a/cli/rt/40_performance.js +++ b/cli/rt/40_performance.js @@ -2,9 +2,9 @@ ((window) => { const { opNow } = window.__bootstrap.timers; - const { customInspect, inspect } = window.__bootstrap.console; const { cloneValue } = window.__bootstrap.webUtil; + const customInspect = Symbol.for("Deno.customInspect"); let performanceEntries = []; function findMostRecent( @@ -130,7 +130,7 @@ [customInspect]() { return this.detail ? `${this.constructor.name} {\n detail: ${ - inspect(this.detail, { depth: 3 }) + JSON.stringify(this.detail, null, 2) },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; } @@ -170,7 +170,7 @@ [customInspect]() { return this.detail ? `${this.constructor.name} {\n detail: ${ - inspect(this.detail, { depth: 3 }) + JSON.stringify(this.detail, null, 2) },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; }