Skip to content

Commit ba17c9e

Browse files
jasnelltargos
authored andcommitted
src: refactor bootstrap to use bootstrap object
PR-URL: #20917 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Backport-PR-URL: #21172
1 parent 94dcdfb commit ba17c9e

File tree

9 files changed

+244
-197
lines changed

9 files changed

+244
-197
lines changed

lib/internal/bootstrap/node.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@
1010
// process.binding(), process._linkedBinding(), internalBinding() and
1111
// NativeModule. And then { internalBinding, NativeModule } will be passed
1212
// into this bootstrapper to bootstrap Node.js core.
13-
1413
'use strict';
1514

16-
(function bootstrapNodeJSCore(process, { internalBinding, NativeModule }) {
15+
(function bootstrapNodeJSCore(process,
16+
// bootstrapper properties... destructured to
17+
// avoid retaining a reference to the bootstrap
18+
// object.
19+
{ _setupProcessObject, _setupNextTick,
20+
_setupPromises, _chdir, _cpuUsage,
21+
_hrtime, _memoryUsage, _rawDebug,
22+
_umask, _initgroups, _setegid, _seteuid,
23+
_setgid, _setuid, _setgroups,
24+
_shouldAbortOnUncaughtToggle },
25+
{ internalBinding, NativeModule }) {
1726
const exceptionHandlerState = { captureFn: null };
1827

1928
function startup() {
@@ -36,11 +45,24 @@
3645
const _process = NativeModule.require('internal/process');
3746
_process.setupConfig(NativeModule._source);
3847
_process.setupSignalHandlers();
39-
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
48+
_process.setupUncaughtExceptionCapture(exceptionHandlerState,
49+
_shouldAbortOnUncaughtToggle);
4050
NativeModule.require('internal/process/warning').setup();
41-
NativeModule.require('internal/process/next_tick').setup();
51+
NativeModule.require('internal/process/next_tick').setup(_setupNextTick,
52+
_setupPromises);
4253
NativeModule.require('internal/process/stdio').setup();
43-
NativeModule.require('internal/process/methods').setup();
54+
NativeModule.require('internal/process/methods').setup(_chdir,
55+
_cpuUsage,
56+
_hrtime,
57+
_memoryUsage,
58+
_rawDebug,
59+
_umask,
60+
_initgroups,
61+
_setegid,
62+
_seteuid,
63+
_setgid,
64+
_setuid,
65+
_setgroups);
4466

4567
const perf = process.binding('performance');
4668
const {
@@ -55,9 +77,9 @@
5577
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END
5678
} = perf.constants;
5779

58-
_process.setup_hrtime();
59-
_process.setup_cpuUsage();
60-
_process.setupMemoryUsage();
80+
_process.setup_hrtime(_hrtime);
81+
_process.setup_cpuUsage(_cpuUsage);
82+
_process.setupMemoryUsage(_memoryUsage);
6183
_process.setupKillAndExit();
6284
if (global.__coverage__)
6385
NativeModule.require('internal/process/write-coverage').setup();
@@ -79,7 +101,7 @@
79101
}
80102

81103
_process.setupChannel();
82-
_process.setupRawDebug();
104+
_process.setupRawDebug(_rawDebug);
83105

84106
const browserGlobals = !process._noBrowserGlobals;
85107
if (browserGlobals) {
@@ -294,7 +316,7 @@
294316
}
295317

296318
function setupProcessObject() {
297-
process._setupProcessObject(pushValueToArray);
319+
_setupProcessObject(pushValueToArray);
298320

299321
function pushValueToArray() {
300322
for (var i = 0; i < arguments.length; i++)

lib/internal/process.js

+10-16
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ process.assert = deprecate(
2525
'DEP0100');
2626

2727
// Set up the process.cpuUsage() function.
28-
function setup_cpuUsage() {
29-
// Get the native function, which will be replaced with a JS version.
30-
const _cpuUsage = process.cpuUsage;
31-
28+
function setup_cpuUsage(_cpuUsage) {
3229
// Create the argument array that will be passed to the native function.
3330
const cpuValues = new Float64Array(2);
3431

@@ -92,8 +89,7 @@ function setup_cpuUsage() {
9289
// The 3 entries filled in by the original process.hrtime contains
9390
// the upper/lower 32 bits of the second part of the value,
9491
// and the remaining nanoseconds of the value.
95-
function setup_hrtime() {
96-
const _hrtime = process.hrtime;
92+
function setup_hrtime(_hrtime) {
9793
const hrValues = new Uint32Array(3);
9894

9995
process.hrtime = function hrtime(time) {
@@ -120,12 +116,11 @@ function setup_hrtime() {
120116
};
121117
}
122118

123-
function setupMemoryUsage() {
124-
const memoryUsage_ = process.memoryUsage;
119+
function setupMemoryUsage(_memoryUsage) {
125120
const memValues = new Float64Array(4);
126121

127122
process.memoryUsage = function memoryUsage() {
128-
memoryUsage_(memValues);
123+
_memoryUsage(memValues);
129124
return {
130125
rss: memValues[0],
131126
heapTotal: memValues[1],
@@ -245,18 +240,17 @@ function setupChannel() {
245240
}
246241

247242

248-
function setupRawDebug() {
249-
const rawDebug = process._rawDebug;
243+
function setupRawDebug(_rawDebug) {
250244
process._rawDebug = function() {
251-
rawDebug(util.format.apply(null, arguments));
245+
_rawDebug(util.format.apply(null, arguments));
252246
};
253247
}
254248

255249

256-
function setupUncaughtExceptionCapture(exceptionHandlerState) {
257-
// This is a typed array for faster communication with JS.
258-
const shouldAbortOnUncaughtToggle = process._shouldAbortOnUncaughtToggle;
259-
delete process._shouldAbortOnUncaughtToggle;
250+
function setupUncaughtExceptionCapture(exceptionHandlerState,
251+
shouldAbortOnUncaughtToggle) {
252+
// shouldAbortOnUncaughtToggle is a typed array for faster
253+
// communication with JS.
260254

261255
process.setUncaughtExceptionCaptureCallback = function(fn) {
262256
if (fn === null) {

lib/internal/process/methods.js

+24-40
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,49 @@
11
'use strict';
22

3-
function setupProcessMethods() {
3+
function setupProcessMethods(_chdir, _cpuUsage, _hrtime, _memoryUsage,
4+
_rawDebug, _umask, _initgroups, _setegid,
5+
_seteuid, _setgid, _setuid, _setgroups) {
46
// Non-POSIX platforms like Windows don't have certain methods.
5-
if (process.setgid !== undefined) {
6-
setupPosixMethods();
7+
if (_setgid !== undefined) {
8+
setupPosixMethods(_initgroups, _setegid, _seteuid,
9+
_setgid, _setuid, _setgroups);
710
}
811

9-
const { chdir: _chdir, umask: _umask } = process;
10-
11-
process.chdir = chdir;
12-
process.umask = umask;
13-
14-
function chdir(...args) {
12+
process.chdir = function chdir(...args) {
1513
return _chdir(...args);
16-
}
14+
};
1715

18-
function umask(...args) {
16+
process.umask = function umask(...args) {
1917
return _umask(...args);
20-
}
18+
};
2119
}
2220

23-
function setupPosixMethods() {
24-
const {
25-
initgroups: _initgroups,
26-
setegid: _setegid,
27-
seteuid: _seteuid,
28-
setgid: _setgid,
29-
setuid: _setuid,
30-
setgroups: _setgroups
31-
} = process;
32-
33-
process.initgroups = initgroups;
34-
process.setegid = setegid;
35-
process.seteuid = seteuid;
36-
process.setgid = setgid;
37-
process.setuid = setuid;
38-
process.setgroups = setgroups;
21+
function setupPosixMethods(_initgroups, _setegid, _seteuid,
22+
_setgid, _setuid, _setgroups) {
3923

40-
function initgroups(...args) {
24+
process.initgroups = function initgroups(...args) {
4125
return _initgroups(...args);
42-
}
26+
};
4327

44-
function setegid(...args) {
28+
process.setegid = function setegid(...args) {
4529
return _setegid(...args);
46-
}
30+
};
4731

48-
function seteuid(...args) {
32+
process.seteuid = function seteuid(...args) {
4933
return _seteuid(...args);
50-
}
34+
};
5135

52-
function setgid(...args) {
36+
process.setgid = function setgid(...args) {
5337
return _setgid(...args);
54-
}
38+
};
5539

56-
function setuid(...args) {
40+
process.setuid = function setuid(...args) {
5741
return _setuid(...args);
58-
}
42+
};
5943

60-
function setgroups(...args) {
44+
process.setgroups = function setgroups(...args) {
6145
return _setgroups(...args);
62-
}
46+
};
6347
}
6448

6549
exports.setup = setupProcessMethods;

lib/internal/process/next_tick.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports.setup = setupNextTick;
44

5-
function setupNextTick() {
5+
function setupNextTick(_setupNextTick, _setupPromises) {
66
const {
77
getDefaultTriggerAsyncId,
88
newAsyncId,
@@ -14,10 +14,10 @@ function setupNextTick() {
1414
emitDestroy,
1515
symbols: { async_id_symbol, trigger_async_id_symbol }
1616
} = require('internal/async_hooks');
17-
const promises = require('internal/process/promises');
17+
const emitPromiseRejectionWarnings =
18+
require('internal/process/promises').setup(_setupPromises);
1819
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
1920
const FixedQueue = require('internal/fixed_queue');
20-
const { emitPromiseRejectionWarnings } = promises;
2121

2222
// tickInfo is used so that the C++ code in src/node.cc can
2323
// have easy access to our nextTick state, and avoid unnecessary
@@ -26,7 +26,7 @@ function setupNextTick() {
2626
const [
2727
tickInfo,
2828
runMicrotasks
29-
] = process._setupNextTick(_tickCallback);
29+
] = _setupNextTick(_tickCallback);
3030

3131
// *Must* match Environment::TickInfo::Fields in src/env.h.
3232
const kHasScheduled = 0;

lib/internal/process/promises.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ const pendingUnhandledRejections = [];
77
const asyncHandledRejections = [];
88
let lastPromiseId = 0;
99

10-
module.exports = {
11-
emitPromiseRejectionWarnings
12-
};
10+
exports.setup = setupPromises;
1311

14-
process._setupPromises(unhandledRejection, handledRejection);
12+
function setupPromises(_setupPromises) {
13+
_setupPromises(unhandledRejection, handledRejection);
14+
return emitPromiseRejectionWarnings;
15+
}
1516

1617
function unhandledRejection(promise, reason) {
1718
maybeUnhandledPromises.set(promise, {

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@
308308

309309
'sources': [
310310
'src/async_wrap.cc',
311+
'src/bootstrapper.cc',
311312
'src/callback_scope.cc',
312313
'src/cares_wrap.cc',
313314
'src/connection_wrap.cc',

0 commit comments

Comments
 (0)