From da1af3d3fc1b6c780070167527ef1964fcfc81f6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 12 Aug 2017 15:49:41 +0200 Subject: [PATCH] lib: instantiate console methods eagerly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit they were instantiated lazily but that fails when the first call is under stack overflow conditions. PR-URL: https://github.com/nodejs/node/pull/14791 Fixes: https://github.com/nodejs/help#778 Reviewed-By: Colin Ihrig Reviewed-By: Timothy Gu Reviewed-By: Refael Ackermann Reviewed-By: Tobias Nießen Reviewed-By: James M Snell --- lib/internal/bootstrap_node.js | 19 ++++++++++++++++++- test/message/stack_overflow_async.js | 18 ++++++++++++++++++ test/message/stack_overflow_async.out | 4 ++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/message/stack_overflow_async.js create mode 100644 test/message/stack_overflow_async.out diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 492e3a2119..310db50960 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -26,7 +26,8 @@ setupProcessICUVersions(); setupGlobalVariables(); - if (!process._noBrowserGlobals) { + const browserGlobals = !process._noBrowserGlobals; + if (browserGlobals) { setupGlobalTimeouts(); setupGlobalConsole(); } @@ -40,6 +41,22 @@ 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.setupSignalHandlers(); if (global.__coverage__) diff --git a/test/message/stack_overflow_async.js b/test/message/stack_overflow_async.js new file mode 100644 index 0000000000..9aefbf9557 --- /dev/null +++ b/test/message/stack_overflow_async.js @@ -0,0 +1,18 @@ +// Flags: --stack_trace_limit=3 + +'use strict'; +require('../common'); + +async function f() { + await f(); +} + +async function g() { + try { + await f(); + } catch (e) { + console.log(e); + } +} + +g(); diff --git a/test/message/stack_overflow_async.out b/test/message/stack_overflow_async.out new file mode 100644 index 0000000000..4028c7642a --- /dev/null +++ b/test/message/stack_overflow_async.out @@ -0,0 +1,4 @@ +RangeError: Maximum call stack size exceeded + at f (*test*message*stack_overflow_async.js:*) + at f (*test*message*stack_overflow_async.js:7:*) + at f (*test*message*stack_overflow_async.js:7:*)