Browse Source

lib: refactor console startup

PR-URL: https://github.com/nodejs/node/pull/15111
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
canary-base
Ruben Bridgewater 7 years ago
parent
commit
4bcb1c3f75
No known key found for this signature in database GPG Key ID: F07496B3EB3C1762
  1. 88
      lib/internal/bootstrap_node.js

88
lib/internal/bootstrap_node.js

@ -26,13 +26,19 @@
setupProcessICUVersions(); setupProcessICUVersions();
setupGlobalVariables(); setupGlobalVariables();
const _process = NativeModule.require('internal/process');
_process.setupConfig(NativeModule._source);
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/stdio').setup();
const browserGlobals = !process._noBrowserGlobals; const browserGlobals = !process._noBrowserGlobals;
if (browserGlobals) { if (browserGlobals) {
setupGlobalTimeouts(); setupGlobalTimeouts();
setupGlobalConsole(); setupGlobalConsole();
} }
const _process = NativeModule.require('internal/process');
const perf = process.binding('performance'); const perf = process.binding('performance');
const { const {
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE, NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
@ -50,26 +56,6 @@
_process.setup_performance(); _process.setup_performance();
_process.setup_cpuUsage(); _process.setup_cpuUsage();
_process.setupMemoryUsage(); _process.setupMemoryUsage();
_process.setupConfig(NativeModule._source);
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/stdio').setup();
if (browserGlobals) {
// Instantiate eagerly in case the first call is under stack overflow
// conditions where instantiation doesn't work.
const console = global.console;
console.assert;
console.clear;
console.count;
console.countReset;
console.dir;
console.error;
console.log;
console.time;
console.timeEnd;
console.trace;
console.warn;
}
_process.setupKillAndExit(); _process.setupKillAndExit();
_process.setupSignalHandlers(); _process.setupSignalHandlers();
if (global.__coverage__) if (global.__coverage__)
@ -319,62 +305,50 @@
function setupGlobalConsole() { function setupGlobalConsole() {
const originalConsole = global.console; const originalConsole = global.console;
let console; const Module = NativeModule.require('module');
// Setup Node.js global.console
const wrappedConsole = NativeModule.require('console');
Object.defineProperty(global, 'console', { Object.defineProperty(global, 'console', {
configurable: true, configurable: true,
enumerable: true, enumerable: true,
get: function() { get() {
if (!console) { return wrappedConsole;
console = (originalConsole === undefined) ?
NativeModule.require('console') :
installInspectorConsole(originalConsole);
}
return console;
} }
}); });
setupInspectorCommandLineAPI(); setupInspector(originalConsole, wrappedConsole, Module);
} }
function installInspectorConsole(globalConsole) { function setupInspector(originalConsole, wrappedConsole, Module) {
const wrappedConsole = NativeModule.require('console'); const { addCommandLineAPI, consoleCall } = process.binding('inspector');
const inspector = process.binding('inspector'); if (!consoleCall) {
if (!inspector.consoleCall) { return;
return wrappedConsole;
} }
// Setup inspector command line API
const { makeRequireFunction } = NativeModule.require('internal/module');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);
const consoleAPIModule = new Module('<inspector console>');
consoleAPIModule.paths =
Module._nodeModulePaths(cwd).concat(Module.globalPaths);
addCommandLineAPI('require', makeRequireFunction(consoleAPIModule));
const config = {}; const config = {};
for (const key of Object.keys(wrappedConsole)) { for (const key of Object.keys(wrappedConsole)) {
if (!globalConsole.hasOwnProperty(key)) if (!originalConsole.hasOwnProperty(key))
continue; continue;
// If global console has the same method as inspector console, // If global console has the same method as inspector console,
// then wrap these two methods into one. Native wrapper will preserve // then wrap these two methods into one. Native wrapper will preserve
// the original stack. // the original stack.
wrappedConsole[key] = inspector.consoleCall.bind(wrappedConsole, wrappedConsole[key] = consoleCall.bind(wrappedConsole,
globalConsole[key], originalConsole[key],
wrappedConsole[key], wrappedConsole[key],
config); config);
} }
for (const key of Object.keys(globalConsole)) { for (const key of Object.keys(originalConsole)) {
if (wrappedConsole.hasOwnProperty(key)) if (wrappedConsole.hasOwnProperty(key))
continue; continue;
wrappedConsole[key] = globalConsole[key]; wrappedConsole[key] = originalConsole[key];
}
return wrappedConsole;
} }
function setupInspectorCommandLineAPI() {
const { addCommandLineAPI } = process.binding('inspector');
if (!addCommandLineAPI) return;
const Module = NativeModule.require('module');
const { makeRequireFunction } = NativeModule.require('internal/module');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);
const consoleAPIModule = new Module('<inspector console>');
consoleAPIModule.paths =
Module._nodeModulePaths(cwd).concat(Module.globalPaths);
addCommandLineAPI('require', makeRequireFunction(consoleAPIModule));
} }
function setupProcessFatal() { function setupProcessFatal() {

Loading…
Cancel
Save