Skip to content

Commit e6a4fb6

Browse files
joyeecheungtargos
authored andcommitted
process: split execution into main scripts
This patch splits the execution mode selection from the environment setup in `lib/internal/bootstrap/node.js`, and split the entry point of different execution mode into main scripts under `lib/internal/main`: - `check_syntax.js`: used when `-c`/`--check` which only checks the syntax of the input instead of executing it. - `eval_stdin.js`: used when `-e` is passed without value and stdin is not a TTY (e.g. something is piped). - `eval_string`: used when `-e` is passed along with a string argument - `inspect.js`: for `node inspect`/`node debug` - `print_bash_completion.js`: for `--completion-bash` - `print_help.js`: for `--help` - `prof_process.js`: for `--prof-process` - `repl.js`: for the REPL - `run_main_module.js`: used when a main module is passed - `run_third_party_main.js`: for the legacy `_third_party_main.js` support - `worker_thread.js`: for workers This makes the entry points easier to navigate and paves the way for customized v8 snapshots (that do not need to deserialize execution mode setup) and better embedder APIs. As an example, after this patch, for the most common case where Node.js executes a user module as an entry point, it essentially goes through: - `lib/internal/per_context.js` to setup the v8 Context (which is also run when setting up contexts for the `vm` module) - `lib/internal/bootstrap/loaders.js` to set up internal binding and builtin module loaders (that are separate from the loaders accessible in the user land). - `lib/internal/bootstrap/node.js`: to set up the rest of the environment, including various globals and the process object - `lib/internal/main/run_main_module.js`: which is selected from C++ to prepare execution of the user module. This patch also removes `NativeModuleLoader::CompileAndCall` and exposes `NativeModuleLoader::LookupAndCompile` directly so that we can handle syntax errors and runtime errors of bootstrap scripts differently. PR-URL: #25667 Reviewed-By: Anna Henningsen <anna@addaleax.net> Backport-PR-URL: #26036
1 parent d342707 commit e6a4fb6

37 files changed

+869
-735
lines changed

lib/internal/bootstrap/cache.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,32 @@ const { hasTracing, hasInspector } = process.binding('config');
1313

1414
// Modules with source code compiled in js2c that
1515
// cannot be compiled with the code cache.
16-
const cannotUseCache = [
16+
const cannotBeRequired = [
1717
'sys', // Deprecated.
1818
'internal/v8_prof_polyfill',
1919
'internal/v8_prof_processor',
2020

2121
'internal/per_context',
2222

2323
'internal/test/binding',
24-
// TODO(joyeecheung): update the C++ side so that
25-
// the code cache is also used when compiling these two files.
24+
2625
'internal/bootstrap/loaders',
2726
'internal/bootstrap/node'
2827
];
2928

3029
// Skip modules that cannot be required when they are not
3130
// built into the binary.
3231
if (!hasInspector) {
33-
cannotUseCache.push(
32+
cannotBeRequired.push(
3433
'inspector',
3534
'internal/util/inspector',
3635
);
3736
}
3837
if (!hasTracing) {
39-
cannotUseCache.push('trace_events');
38+
cannotBeRequired.push('trace_events');
4039
}
4140
if (!process.versions.openssl) {
42-
cannotUseCache.push(
41+
cannotBeRequired.push(
4342
'crypto',
4443
'https',
4544
'http2',
@@ -67,11 +66,11 @@ if (!process.versions.openssl) {
6766

6867
const cachableBuiltins = [];
6968
for (const id of NativeModule.map.keys()) {
70-
if (id.startsWith('internal/deps') ||
69+
if (id.startsWith('internal/deps') || id.startsWith('internal/main') ||
7170
id.startsWith('v8/') || id.startsWith('node-inspect/')) {
72-
cannotUseCache.push(id);
71+
cannotBeRequired.push(id);
7372
}
74-
if (!cannotUseCache.includes(id)) {
73+
if (!cannotBeRequired.includes(id)) {
7574
cachableBuiltins.push(id);
7675
}
7776
}
@@ -80,5 +79,5 @@ module.exports = {
8079
cachableBuiltins,
8180
getCodeCache,
8281
compileFunction,
83-
cannotUseCache
82+
cannotBeRequired
8483
};

lib/internal/bootstrap/loaders.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ internalBinding('module_wrap').callbackMap = new WeakMap();
160160

161161
// Think of this as module.exports in this file even though it is not
162162
// written in CommonJS style.
163-
const loaderExports = { internalBinding, NativeModule };
163+
const loaderExports = {
164+
internalBinding,
165+
NativeModule,
166+
require: nativeModuleRequire
167+
};
168+
164169
const loaderId = 'internal/bootstrap/loaders';
165170

166171
// Set up NativeModule.
@@ -194,7 +199,7 @@ for (var i = 0; i < moduleIds.length; ++i) {
194199
NativeModule.map.set(id, mod);
195200
}
196201

197-
NativeModule.require = function(id) {
202+
function nativeModuleRequire(id) {
198203
if (id === loaderId) {
199204
return loaderExports;
200205
}
@@ -218,8 +223,9 @@ NativeModule.require = function(id) {
218223
moduleLoadList.push(`NativeModule ${id}`);
219224
mod.compile();
220225
return mod.exports;
221-
};
226+
}
222227

228+
NativeModule.require = nativeModuleRequire;
223229
NativeModule.exists = function(id) {
224230
return NativeModule.map.has(id);
225231
};

0 commit comments

Comments
 (0)