From a7ddc2da1f3b8474036f444d8169f236952c1c1c Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 28 Aug 2017 21:48:54 -0300 Subject: [PATCH 1/3] lib: refactor console startup --- lib/internal/bootstrap_node.js | 89 ++++++++++++---------------------- 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 9d776674d2edab..835ad820256cda 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -26,11 +26,6 @@ setupProcessICUVersions(); setupGlobalVariables(); - const browserGlobals = !process._noBrowserGlobals; - if (browserGlobals) { - setupGlobalTimeouts(); - setupGlobalConsole(); - } const _process = NativeModule.require('internal/process'); const perf = process.binding('performance'); @@ -54,21 +49,11 @@ NativeModule.require('internal/process/warning').setup(); NativeModule.require('internal/process/next_tick').setup(); NativeModule.require('internal/process/stdio').setup(); + + const browserGlobals = !process._noBrowserGlobals; 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; + setupGlobalTimeouts(); + setupGlobalConsole(); } _process.setupKillAndExit(); _process.setupSignalHandlers(); @@ -312,62 +297,48 @@ function setupGlobalConsole() { const originalConsole = global.console; - let console; + // Setup inspector command line API + const { addCommandLineAPI, consoleCall } = process.binding('inspector'); + const Module = NativeModule.require('module'); + const { makeRequireFunction } = NativeModule.require('internal/module'); + const path = NativeModule.require('path'); + const cwd = tryGetCwd(path); + + const consoleAPIModule = new Module(''); + consoleAPIModule.paths = + Module._nodeModulePaths(cwd).concat(Module.globalPaths); + + addCommandLineAPI('require', makeRequireFunction(consoleAPIModule)); + + // Setup Node.js global.console + const wrappedConsole = NativeModule.require('console'); Object.defineProperty(global, 'console', { configurable: true, enumerable: true, - get: function() { - if (!console) { - console = (originalConsole === undefined) ? - NativeModule.require('console') : - installInspectorConsole(originalConsole); - } - return console; + get() { + return wrappedConsole; } }); - setupInspectorCommandLineAPI(); - } - - function installInspectorConsole(globalConsole) { - const wrappedConsole = NativeModule.require('console'); - const inspector = process.binding('inspector'); - if (!inspector.consoleCall) { - return wrappedConsole; + if (!consoleCall) { + return; } const config = {}; for (const key of Object.keys(wrappedConsole)) { - if (!globalConsole.hasOwnProperty(key)) + if (!originalConsole.hasOwnProperty(key)) continue; // If global console has the same method as inspector console, // then wrap these two methods into one. Native wrapper will preserve // the original stack. - wrappedConsole[key] = inspector.consoleCall.bind(wrappedConsole, - globalConsole[key], - wrappedConsole[key], - config); + wrappedConsole[key] = consoleCall.bind(wrappedConsole, + originalConsole[key], + wrappedConsole[key], + config); } - for (const key of Object.keys(globalConsole)) { + for (const key of Object.keys(originalConsole)) { if (wrappedConsole.hasOwnProperty(key)) 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(''); - consoleAPIModule.paths = - Module._nodeModulePaths(cwd).concat(Module.globalPaths); - - addCommandLineAPI('require', makeRequireFunction(consoleAPIModule)); } function setupProcessFatal() { From 0da4d6d104963128fcf26b7e0d886d0a3bce74c9 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 31 Aug 2017 19:48:59 -0300 Subject: [PATCH 2/3] [squash] address comments --- lib/internal/bootstrap_node.js | 43 +++++++++++++++++----------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 835ad820256cda..cd8fb3662034f3 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -28,6 +28,17 @@ 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; + if (browserGlobals) { + setupGlobalTimeouts(); + setupGlobalConsole(); + } + const perf = process.binding('performance'); const { NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE, @@ -45,16 +56,6 @@ _process.setup_performance(); _process.setup_cpuUsage(); _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(); - - const browserGlobals = !process._noBrowserGlobals; - if (browserGlobals) { - setupGlobalTimeouts(); - setupGlobalConsole(); - } _process.setupKillAndExit(); _process.setupSignalHandlers(); if (global.__coverage__) @@ -297,19 +298,7 @@ function setupGlobalConsole() { const originalConsole = global.console; - // Setup inspector command line API - const { addCommandLineAPI, consoleCall } = process.binding('inspector'); const Module = NativeModule.require('module'); - const { makeRequireFunction } = NativeModule.require('internal/module'); - const path = NativeModule.require('path'); - const cwd = tryGetCwd(path); - - const consoleAPIModule = new Module(''); - consoleAPIModule.paths = - Module._nodeModulePaths(cwd).concat(Module.globalPaths); - - addCommandLineAPI('require', makeRequireFunction(consoleAPIModule)); - // Setup Node.js global.console const wrappedConsole = NativeModule.require('console'); Object.defineProperty(global, 'console', { @@ -319,9 +308,19 @@ return wrappedConsole; } }); + // Setup inspector command line API + const { addCommandLineAPI, consoleCall } = process.binding('inspector'); if (!consoleCall) { return; } + const { makeRequireFunction } = NativeModule.require('internal/module'); + const path = NativeModule.require('path'); + const cwd = tryGetCwd(path); + + const consoleAPIModule = new Module(''); + consoleAPIModule.paths = + Module._nodeModulePaths(cwd).concat(Module.globalPaths); + addCommandLineAPI('require', makeRequireFunction(consoleAPIModule)); const config = {}; for (const key of Object.keys(wrappedConsole)) { if (!originalConsole.hasOwnProperty(key)) From 5db3a1b2c3d21fccb53753ed598b3e900d9892eb Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 1 Sep 2017 02:05:20 -0300 Subject: [PATCH 3/3] [squash] use individual inspectorSetup --- lib/internal/bootstrap_node.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index cd8fb3662034f3..1879e9b4d84983 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -308,11 +308,15 @@ return wrappedConsole; } }); - // Setup inspector command line API + setupInspector(originalConsole, wrappedConsole, Module); + } + + function setupInspector(originalConsole, wrappedConsole, Module) { const { addCommandLineAPI, consoleCall } = process.binding('inspector'); if (!consoleCall) { return; } + // Setup inspector command line API const { makeRequireFunction } = NativeModule.require('internal/module'); const path = NativeModule.require('path'); const cwd = tryGetCwd(path);