From 41b69e3cf469ea683592e20278f0b04c413ff34f Mon Sep 17 00:00:00 2001 From: legendecas Date: Wed, 18 May 2022 23:30:49 +0800 Subject: [PATCH] src,lib: migrate to console on context's extra binding Since `globalThis.console` is not an ECMAScript defined builtin, V8's globally installed `console` implementation is been moved to the context's extra binding object. We need to migrate to that one before the globally installed console object is removed in V8. PR-URL: https://github.com/nodejs/node/pull/43142 Reviewed-By: Joyee Cheung Reviewed-By: Minwoo Jung --- lib/inspector.js | 7 +++---- lib/internal/bootstrap/browser.js | 10 +++------- lib/internal/util/inspector.js | 12 ++---------- src/inspector_js_api.cc | 12 ++++++++++++ 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/inspector.js b/lib/inspector.js index 58d8dbe45c7213..5ffb2d9d6658e0 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -37,7 +37,8 @@ const { open, url, isEnabled, - waitForDebugger + waitForDebugger, + console, } = internalBinding('inspector'); const connectionSymbol = Symbol('connectionProperty'); @@ -188,8 +189,6 @@ module.exports = { close: process._debugEnd, url, waitForDebugger: inspectorWaitForDebugger, - // This is dynamically added during bootstrap, - // where the console from the VM is still available - console: require('internal/util/inspector').consoleFromVM, + console, Session }; diff --git a/lib/internal/bootstrap/browser.js b/lib/internal/bootstrap/browser.js index 49d69b939687ca..5704dedecb2ef8 100644 --- a/lib/internal/bootstrap/browser.js +++ b/lib/internal/bootstrap/browser.js @@ -12,11 +12,9 @@ const { } = require('internal/util'); const config = internalBinding('config'); -// Override global console from the one provided by the VM -// to the one implemented by Node.js // https://console.spec.whatwg.org/#console-namespace exposeNamespace(globalThis, 'console', - createGlobalConsole(globalThis.console)); + createGlobalConsole()); const { URL, URLSearchParams } = require('internal/url'); // https://url.spec.whatwg.org/#url @@ -71,16 +69,14 @@ defineOperation(globalThis, 'setTimeout', timers.setTimeout); defineReplacableAttribute(globalThis, 'performance', require('perf_hooks').performance); -function createGlobalConsole(consoleFromVM) { +function createGlobalConsole() { const consoleFromNode = require('internal/console/global'); if (config.hasInspector) { const inspector = require('internal/util/inspector'); - // This will be exposed by `require('inspector').console` later. - inspector.consoleFromVM = consoleFromVM; // TODO(joyeecheung): postpone this until the first time inspector // is activated. - inspector.wrapConsole(consoleFromNode, consoleFromVM); + inspector.wrapConsole(consoleFromNode); const { setConsoleExtensionInstaller } = internalBinding('inspector'); // Setup inspector command line API. setConsoleExtensionInstaller(inspector.installConsoleExtensions); diff --git a/lib/internal/util/inspector.js b/lib/internal/util/inspector.js index a94289b17d012e..5bc4fe5351ac86 100644 --- a/lib/internal/util/inspector.js +++ b/lib/internal/util/inspector.js @@ -38,8 +38,8 @@ function installConsoleExtensions(commandLineApi) { } // Wrap a console implemented by Node.js with features from the VM inspector -function wrapConsole(consoleFromNode, consoleFromVM) { - const { consoleCall } = internalBinding('inspector'); +function wrapConsole(consoleFromNode) { + const { consoleCall, console: consoleFromVM } = internalBinding('inspector'); for (const key of ObjectKeys(consoleFromVM)) { // If global console has the same method as inspector console, // then wrap these two methods into one. Native wrapper will preserve @@ -61,16 +61,8 @@ function wrapConsole(consoleFromNode, consoleFromVM) { } } -// Stores the console from VM, should be set during bootstrap. -let consoleFromVM; module.exports = { installConsoleExtensions, sendInspectorCommand, wrapConsole, - get consoleFromVM() { - return consoleFromVM; - }, - set consoleFromVM(val) { - consoleFromVM = val; - } }; diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index 4ebb8acd689d58..298066dfc759ba 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -343,6 +343,18 @@ void Initialize(Local target, Local unused, env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper); env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled); + Local console_string = + FIXED_ONE_BYTE_STRING(env->isolate(), "console"); + + // Grab the console from the binding object and expose those to our binding + // layer. + Local binding = context->GetExtrasBindingObject(); + target + ->Set(context, + console_string, + binding->Get(context, console_string).ToLocalChecked()) + .Check(); + JSBindingsConnection::Bind(env, target); JSBindingsConnection::Bind(env, target); }