Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap: bind properties of the global console in the default snapshot #47171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 4 additions & 37 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions lib/internal/console/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

const {
FunctionPrototypeBind,
ObjectDefineProperties,
ReflectDefineProperty,
ReflectGetOwnPropertyDescriptor,
ReflectOwnKeys,
} = primordials;

const {
Console,
kBindProperties,
} = require('internal/console/constructor');

const globalConsole = { __proto__: {} };
Expand All @@ -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;
Expand Down