From 1d918cbbd59152fb0ad6e24005335f0dfa6913fd Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 19 Mar 2023 23:47:43 +0100 Subject: [PATCH] bootstrap: bind properties of the global console in the default snapshot If it safe to bind the properties of the global console when building the default snapshot as long as they are not actually accessed during snapshot building. Bind them early instead of doing it at run time. --- lib/internal/console/constructor.js | 41 +++-------------------------- lib/internal/console/global.js | 32 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index fc91bc9b3851d3..c405a51e2b63bd 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -90,7 +90,6 @@ const kIsConsole = Symbol('kIsConsole'); const kWriteToConsole = Symbol('kWriteToConsole'); const kBindProperties = Symbol('kBindProperties'); const kBindStreamsEager = Symbol('kBindStreamsEager'); -const kBindStreamsLazy = Symbol('kBindStreamsLazy'); const kUseStdout = Symbol('kUseStdout'); const kUseStderr = Symbol('kUseStderr'); @@ -192,38 +191,6 @@ ObjectDefineProperties(Console.prototype, { }); }, }, - [kBindStreamsLazy]: { - __proto__: null, - ...consolePropAttributes, - // Lazily load the stdout and stderr from an object so we don't - // create the stdio streams when they are not even accessed - value: function(object) { - let stdout; - let stderr; - ObjectDefineProperties(this, { - '_stdout': { - __proto__: null, - enumerable: false, - configurable: true, - get() { - if (!stdout) stdout = object.stdout; - return stdout; - }, - set(value) { stdout = value; }, - }, - '_stderr': { - __proto__: null, - enumerable: false, - configurable: true, - get() { - if (!stderr) { stderr = object.stderr; } - return stderr; - }, - set(value) { stderr = value; }, - }, - }); - }, - }, [kBindProperties]: { __proto__: null, ...consolePropAttributes, @@ -685,9 +652,6 @@ Console.prototype.error = Console.prototype.warn; Console.prototype.groupCollapsed = Console.prototype.group; function initializeGlobalConsole(globalConsole) { - globalConsole[kBindStreamsLazy](process); - globalConsole[kBindProperties](true, 'auto'); - const { namespace: { addSerializeCallback, @@ -714,12 +678,15 @@ function initializeGlobalConsole(globalConsole) { for (const key of inspectorConsoleKeys) { globalConsole[key] = undefined; } + // Reset the streams so they are re-initialized after the user-land + // snapshot is deserialized. + globalConsole._stdout = undefined; + globalConsole._stderr = undefined; }); } module.exports = { Console, - kBindStreamsLazy, kBindProperties, initializeGlobalConsole, formatTime, // exported for tests diff --git a/lib/internal/console/global.js b/lib/internal/console/global.js index 33654d8dbc6740..8d4c164e79b2f4 100644 --- a/lib/internal/console/global.js +++ b/lib/internal/console/global.js @@ -14,6 +14,7 @@ const { FunctionPrototypeBind, + ObjectDefineProperties, ReflectDefineProperty, ReflectGetOwnPropertyDescriptor, ReflectOwnKeys, @@ -21,6 +22,7 @@ const { const { Console, + kBindProperties, } = require('internal/console/constructor'); const globalConsole = { __proto__: {} }; @@ -41,6 +43,36 @@ for (const prop of ReflectOwnKeys(Console.prototype)) { ReflectDefineProperty(globalConsole, prop, desc); } +let stdout; +let stderr; + +// Lazily load the stdout and stderr from the process object so we don't +// create the stdio streams when they are not even accessed. +ObjectDefineProperties(globalConsole, { + '_stdout': { + __proto__: null, + enumerable: false, + configurable: true, + get() { + if (!stdout) stdout = process.stdout; + return stdout; + }, + set(value) { stdout = value; }, + }, + '_stderr': { + __proto__: null, + enumerable: false, + configurable: true, + get() { + if (!stderr) { stderr = process.stderr; } + return stderr; + }, + set(value) { stderr = value; }, + }, +}); + +globalConsole[kBindProperties](true, 'auto'); + // This is a legacy feature - the Console constructor is exposed on // the global console instance. globalConsole.Console = Console;