Skip to content

Commit 41b69e3

Browse files
legendecasbengl
authored andcommitted
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: #43142 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 9539cfa commit 41b69e3

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

lib/inspector.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const {
3737
open,
3838
url,
3939
isEnabled,
40-
waitForDebugger
40+
waitForDebugger,
41+
console,
4142
} = internalBinding('inspector');
4243

4344
const connectionSymbol = Symbol('connectionProperty');
@@ -188,8 +189,6 @@ module.exports = {
188189
close: process._debugEnd,
189190
url,
190191
waitForDebugger: inspectorWaitForDebugger,
191-
// This is dynamically added during bootstrap,
192-
// where the console from the VM is still available
193-
console: require('internal/util/inspector').consoleFromVM,
192+
console,
194193
Session
195194
};

lib/internal/bootstrap/browser.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ const {
1212
} = require('internal/util');
1313
const config = internalBinding('config');
1414

15-
// Override global console from the one provided by the VM
16-
// to the one implemented by Node.js
1715
// https://console.spec.whatwg.org/#console-namespace
1816
exposeNamespace(globalThis, 'console',
19-
createGlobalConsole(globalThis.console));
17+
createGlobalConsole());
2018

2119
const { URL, URLSearchParams } = require('internal/url');
2220
// https://url.spec.whatwg.org/#url
@@ -71,16 +69,14 @@ defineOperation(globalThis, 'setTimeout', timers.setTimeout);
7169
defineReplacableAttribute(globalThis, 'performance',
7270
require('perf_hooks').performance);
7371

74-
function createGlobalConsole(consoleFromVM) {
72+
function createGlobalConsole() {
7573
const consoleFromNode =
7674
require('internal/console/global');
7775
if (config.hasInspector) {
7876
const inspector = require('internal/util/inspector');
79-
// This will be exposed by `require('inspector').console` later.
80-
inspector.consoleFromVM = consoleFromVM;
8177
// TODO(joyeecheung): postpone this until the first time inspector
8278
// is activated.
83-
inspector.wrapConsole(consoleFromNode, consoleFromVM);
79+
inspector.wrapConsole(consoleFromNode);
8480
const { setConsoleExtensionInstaller } = internalBinding('inspector');
8581
// Setup inspector command line API.
8682
setConsoleExtensionInstaller(inspector.installConsoleExtensions);

lib/internal/util/inspector.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function installConsoleExtensions(commandLineApi) {
3838
}
3939

4040
// Wrap a console implemented by Node.js with features from the VM inspector
41-
function wrapConsole(consoleFromNode, consoleFromVM) {
42-
const { consoleCall } = internalBinding('inspector');
41+
function wrapConsole(consoleFromNode) {
42+
const { consoleCall, console: consoleFromVM } = internalBinding('inspector');
4343
for (const key of ObjectKeys(consoleFromVM)) {
4444
// If global console has the same method as inspector console,
4545
// then wrap these two methods into one. Native wrapper will preserve
@@ -61,16 +61,8 @@ function wrapConsole(consoleFromNode, consoleFromVM) {
6161
}
6262
}
6363

64-
// Stores the console from VM, should be set during bootstrap.
65-
let consoleFromVM;
6664
module.exports = {
6765
installConsoleExtensions,
6866
sendInspectorCommand,
6967
wrapConsole,
70-
get consoleFromVM() {
71-
return consoleFromVM;
72-
},
73-
set consoleFromVM(val) {
74-
consoleFromVM = val;
75-
}
7668
};

src/inspector_js_api.cc

+12
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,18 @@ void Initialize(Local<Object> target, Local<Value> unused,
343343
env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper);
344344
env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled);
345345

346+
Local<String> console_string =
347+
FIXED_ONE_BYTE_STRING(env->isolate(), "console");
348+
349+
// Grab the console from the binding object and expose those to our binding
350+
// layer.
351+
Local<Object> binding = context->GetExtrasBindingObject();
352+
target
353+
->Set(context,
354+
console_string,
355+
binding->Get(context, console_string).ToLocalChecked())
356+
.Check();
357+
346358
JSBindingsConnection<LocalConnection>::Bind(env, target);
347359
JSBindingsConnection<MainThreadConnection>::Bind(env, target);
348360
}

0 commit comments

Comments
 (0)