Skip to content

Commit d1b579c

Browse files
joyeecheungantsmartian
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: nodejs#25667 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 76687de commit d1b579c

37 files changed

+854
-739
lines changed

lib/internal/bootstrap/cache.js

Lines changed: 9 additions & 11 deletions
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,10 @@ if (!process.versions.openssl) {
6766

6867
const cachableBuiltins = [];
6968
for (const id of NativeModule.map.keys()) {
70-
if (id.startsWith('internal/deps') ||
71-
id.startsWith('v8/') || id.startsWith('node-inspect/')) {
72-
cannotUseCache.push(id);
69+
if (id.startsWith('internal/deps') || id.startsWith('internal/main')) {
70+
cannotBeRequired.push(id);
7371
}
74-
if (!cannotUseCache.includes(id)) {
72+
if (!cannotBeRequired.includes(id)) {
7573
cachableBuiltins.push(id);
7674
}
7775
}
@@ -80,5 +78,5 @@ module.exports = {
8078
cachableBuiltins,
8179
getCodeCache,
8280
compileFunction,
83-
cannotUseCache
81+
cannotBeRequired
8482
};

lib/internal/bootstrap/loaders.js

Lines changed: 9 additions & 3 deletions
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
const config = internalBinding('config');
166171

@@ -195,7 +200,7 @@ for (var i = 0; i < moduleIds.length; ++i) {
195200
NativeModule.map.set(id, mod);
196201
}
197202

198-
NativeModule.require = function(id) {
203+
function nativeModuleRequire(id) {
199204
if (id === loaderId) {
200205
return loaderExports;
201206
}
@@ -219,8 +224,9 @@ NativeModule.require = function(id) {
219224
moduleLoadList.push(`NativeModule ${id}`);
220225
mod.compile();
221226
return mod.exports;
222-
};
227+
}
223228

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

0 commit comments

Comments
 (0)