Skip to content

Commit f34e0f9

Browse files
bnoordhuisMylesBorins
authored andcommitted
lib: instantiate console methods eagerly
Before this commit they were instantiated lazily but that fails when the first call is under stack overflow conditions. PR-URL: #14791 Fixes: https://github.com/nodejs/help#778 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ae27cb8 commit f34e0f9

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/internal/bootstrap_node.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
setupProcessICUVersions();
2727

2828
setupGlobalVariables();
29-
if (!process._noBrowserGlobals) {
29+
const browserGlobals = !process._noBrowserGlobals;
30+
if (browserGlobals) {
3031
setupGlobalTimeouts();
3132
setupGlobalConsole();
3233
}
@@ -40,6 +41,22 @@
4041
NativeModule.require('internal/process/warning').setup();
4142
NativeModule.require('internal/process/next_tick').setup();
4243
NativeModule.require('internal/process/stdio').setup();
44+
if (browserGlobals) {
45+
// Instantiate eagerly in case the first call is under stack overflow
46+
// conditions where instantiation doesn't work.
47+
const console = global.console;
48+
console.assert;
49+
console.clear;
50+
console.count;
51+
console.countReset;
52+
console.dir;
53+
console.error;
54+
console.log;
55+
console.time;
56+
console.timeEnd;
57+
console.trace;
58+
console.warn;
59+
}
4360
_process.setupKillAndExit();
4461
_process.setupSignalHandlers();
4562
if (global.__coverage__)

test/message/stack_overflow_async.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Flags: --stack_trace_limit=3
2+
3+
'use strict';
4+
require('../common');
5+
6+
async function f() {
7+
await f();
8+
}
9+
10+
async function g() {
11+
try {
12+
await f();
13+
} catch (e) {
14+
console.log(e);
15+
}
16+
}
17+
18+
g();

test/message/stack_overflow_async.out

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RangeError: Maximum call stack size exceeded
2+
at f (*test*message*stack_overflow_async.js:*)
3+
at f (*test*message*stack_overflow_async.js:7:*)
4+
at f (*test*message*stack_overflow_async.js:7:*)

0 commit comments

Comments
 (0)