Skip to content

Commit 57f29b7

Browse files
joyeecheungtargos
authored andcommitted
bootstrap: use different scripts to setup different configurations
This patch splits the handling of `isMainThread` and `ownsProcessState` from conditionals in `lib/internal/bootstrap/node.js` into different scripts under `lib/internal/bootstrap/switches/`, and call them accordingly from C++ after `node.js` is run. This: - Creates a common denominator of the main thread and the worker thread bootstrap that can be snapshotted and shared by both. - Makes it possible to override the configurations on-the-fly. PR-URL: #30862 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 1c17f42 commit 57f29b7

14 files changed

Lines changed: 568 additions & 545 deletions

lib/internal/bootstrap/node.js

Lines changed: 18 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// This file is invoked by `node::RunBootstrapping()` in `src/node.cc`, and is
44
// responsible for setting up node.js core before executing main scripts
55
// under `lib/internal/main/`.
6-
// This file is currently run to bootstrap both the main thread and the worker
7-
// threads. Some setups are conditional, controlled with isMainThread and
8-
// ownsProcessState.
6+
//
97
// This file is expected not to perform any asynchronous operations itself
108
// when being executed - those should be done in either
119
// `lib/internal/bootstrap/pre_execution.js` or in main scripts. The majority
@@ -22,16 +20,21 @@
2220
// module loaders, including `process.binding()`, `process._linkedBinding()`,
2321
// `internalBinding()` and `NativeModule`.
2422
//
25-
// After this file is run, one of the main scripts under `lib/internal/main/`
26-
// will be selected by C++ to start the actual execution. The main scripts may
27-
// run additional setups exported by `lib/internal/bootstrap/pre_execution.js`,
28-
// depending on the execution mode.
23+
// This file is run to bootstrap both the main thread and the worker threads.
24+
// After this file is run, certain properties are setup according to the
25+
// configuration of the Node.js instance using the files in
26+
// `lib/internal/bootstrap/switches/`.
27+
//
28+
// Then, depending on how the Node.js instance is launched, one of the main
29+
// scripts in `lib/internal/main` will be selected by C++ to start the actual
30+
// execution. They may run additional setups exported by
31+
// `lib/internal/bootstrap/pre_execution.js` depending on the runtime states.
2932

3033
'use strict';
3134

3235
// This file is compiled as if it's wrapped in a function with arguments
3336
// passed by node::RunBootstrapping()
34-
/* global process, require, internalBinding, isMainThread, ownsProcessState */
37+
/* global process, require, internalBinding */
3538

3639
setupPrepareStackTrace();
3740

@@ -54,48 +57,12 @@ setupBuffer();
5457
process.domain = null;
5558
process._exiting = false;
5659

57-
// Bootstrappers for all threads, including worker threads and main thread
58-
const perThreadSetup = require('internal/process/per_thread');
59-
// Bootstrappers for the main thread only
60-
let mainThreadSetup;
61-
// Bootstrappers for the worker threads only
62-
let workerThreadSetup;
63-
if (ownsProcessState) {
64-
mainThreadSetup = require(
65-
'internal/process/main_thread_only'
66-
);
67-
} else {
68-
workerThreadSetup = require(
69-
'internal/process/worker_thread_only'
70-
);
71-
}
72-
7360
// process.config is serialized config.gypi
7461
process.config = JSONParse(internalBinding('native_module').config);
7562

63+
// Bootstrappers for all threads, including worker threads and main thread
64+
const perThreadSetup = require('internal/process/per_thread');
7665
const rawMethods = internalBinding('process_methods');
77-
// Set up methods and events on the process object for the main thread
78-
if (isMainThread) {
79-
process.abort = rawMethods.abort;
80-
const wrapped = mainThreadSetup.wrapProcessMethods(rawMethods);
81-
process.umask = wrapped.umask;
82-
process.chdir = wrapped.chdir;
83-
process.cwd = wrapped.cwd;
84-
85-
// TODO(joyeecheung): deprecate and remove these underscore methods
86-
process._debugProcess = rawMethods._debugProcess;
87-
process._debugEnd = rawMethods._debugEnd;
88-
process._startProfilerIdleNotifier =
89-
rawMethods._startProfilerIdleNotifier;
90-
process._stopProfilerIdleNotifier = rawMethods._stopProfilerIdleNotifier;
91-
} else {
92-
const wrapped = workerThreadSetup.wrapProcessMethods(rawMethods);
93-
94-
process.abort = workerThreadSetup.unavailable('process.abort()');
95-
process.chdir = workerThreadSetup.unavailable('process.chdir()');
96-
process.umask = wrapped.umask;
97-
process.cwd = rawMethods.cwd;
98-
}
9966

10067
// Set up methods on the process object for all threads
10168
{
@@ -119,6 +86,11 @@ if (isMainThread) {
11986
process.memoryUsage = wrapped.memoryUsage;
12087
process.kill = wrapped.kill;
12188