Skip to content

Commit 06f5d45

Browse files
committed
bootstrap: support more builtins in the embedded code cache
This patch: - Make NativeModuleLoader::LookupAndCompile() detect parameters based on module IDs. This allows us to compile more builtins when generating the embedded bootstrap, including - internal/per_context/* - internal/bootstrap/* - internal/main/* - Move pre_execution.js to lib/internal/process as it needs to be compiled as a regular built-in module, unlike other scripts in lib/internal/bootstrap - Move markBootstrapComplete() to the performance binding instead of making it a function-wrapper-based global to reduce number of special cases. PR-URL: nodejs#44018 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent b052bd4 commit 06f5d45

27 files changed

+156
-137
lines changed

lib/internal/bootstrap/loaders.js

+2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ class NativeModule {
326326
requireWithFallbackInDeps : nativeModuleRequire;
327327

328328
const fn = compileFunction(id);
329+
// Arguments must match the parameters specified in
330+
// NativeModuleLoader::LookupAndCompile().
329331
fn(this.exports, requireFn, this, process, internalBinding, primordials);
330332

331333
this.loaded = true;

lib/internal/bootstrap/node.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// This file is expected not to perform any asynchronous operations itself
88
// when being executed - those should be done in either
9-
// `lib/internal/bootstrap/pre_execution.js` or in main scripts. The majority
9+
// `lib/internal/process/pre_execution.js` or in main scripts. The majority
1010
// of the code here focuses on setting up the global proxy and the process
1111
// object in a synchronous manner.
1212
// As special caution is given to the performance of the startup process,
@@ -28,7 +28,7 @@
2828
// Then, depending on how the Node.js instance is launched, one of the main
2929
// scripts in `lib/internal/main` will be selected by C++ to start the actual
3030
// execution. They may run additional setups exported by
31-
// `lib/internal/bootstrap/pre_execution.js` depending on the runtime states.
31+
// `lib/internal/process/pre_execution.js` depending on the runtime states.
3232

3333
'use strict';
3434

lib/internal/main/.eslintrc.yaml

-2
This file was deleted.

lib/internal/main/check_syntax.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
const { getOptionValue } = require('internal/options');
77
const {
8-
prepareMainThreadExecution
9-
} = require('internal/bootstrap/pre_execution');
8+
prepareMainThreadExecution,
9+
markBootstrapComplete
10+
} = require('internal/process/pre_execution');
1011

1112
const {
1213
readStdin

lib/internal/bootstrap/environment.js lib/internal/main/environment.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
// that depends on run time states.
55
// It is currently only intended for preparing contexts for embedders.
66

7-
/* global markBootstrapComplete */
87
const {
9-
prepareMainThreadExecution
10-
} = require('internal/bootstrap/pre_execution');
8+
prepareMainThreadExecution,
9+
markBootstrapComplete
10+
} = require('internal/process/pre_execution');
1111

1212
prepareMainThreadExecution();
1313
markBootstrapComplete();

lib/internal/main/eval_stdin.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// Stdin is not a TTY, we will read it and execute it.
44

55
const {
6-
prepareMainThreadExecution
7-
} = require('internal/bootstrap/pre_execution');
6+
prepareMainThreadExecution,
7+
markBootstrapComplete
8+
} = require('internal/process/pre_execution');
89

910
const { getOptionValue } = require('internal/options');
1011

lib/internal/main/eval_string.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const {
88
} = primordials;
99

1010
const {
11-
prepareMainThreadExecution
12-
} = require('internal/bootstrap/pre_execution');
11+
prepareMainThreadExecution,
12+
markBootstrapComplete
13+
} = require('internal/process/pre_execution');
1314
const { evalModule, evalScript } = require('internal/process/execution');
1415
const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
1516

lib/internal/main/inspect.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// `node inspect ...` or `node debug ...`
44

55
const {
6-
prepareMainThreadExecution
7-
} = require('internal/bootstrap/pre_execution');
6+
prepareMainThreadExecution,
7+
markBootstrapComplete
8+
} = require('internal/process/pre_execution');
89

910
prepareMainThreadExecution();
1011

lib/internal/main/mksnapshot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function requireForUserSnapshot(id) {
114114
function main() {
115115
const {
116116
prepareMainThreadExecution
117-
} = require('internal/bootstrap/pre_execution');
117+
} = require('internal/process/pre_execution');
118118

119119
prepareMainThreadExecution(true, false);
120120

lib/internal/main/print_help.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ const { types } = internalBinding('options');
2020
const hasCrypto = Boolean(process.versions.openssl);
2121

2222
const {
23-
prepareMainThreadExecution
24-
} = require('internal/bootstrap/pre_execution');
23+
prepareMainThreadExecution,
24+
markBootstrapComplete
25+
} = require('internal/process/pre_execution');
2526

2627
const typeLookup = [];
2728
for (const key of ObjectKeys(types))

lib/internal/main/prof_process.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
const {
4-
prepareMainThreadExecution
5-
} = require('internal/bootstrap/pre_execution');
4+
prepareMainThreadExecution,
5+
markBootstrapComplete
6+
} = require('internal/process/pre_execution');
67

78
prepareMainThreadExecution();
89
markBootstrapComplete();

lib/internal/main/repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
// the main module is not specified and stdin is a TTY.
55

66
const {
7-
prepareMainThreadExecution
8-
} = require('internal/bootstrap/pre_execution');
7+
prepareMainThreadExecution,
8+
markBootstrapComplete
9+
} = require('internal/process/pre_execution');
910

1011
const esmLoader = require('internal/process/esm_loader');
1112
const {

lib/internal/main/run_main_module.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
const {
4-
prepareMainThreadExecution
5-
} = require('internal/bootstrap/pre_execution');
4+
prepareMainThreadExecution,
5+
markBootstrapComplete
6+
} = require('internal/process/pre_execution');
67

78
prepareMainThreadExecution(true);
89

lib/internal/main/test_runner.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const {
1212
} = primordials;
1313
const {
1414
prepareMainThreadExecution,
15-
} = require('internal/bootstrap/pre_execution');
15+
markBootstrapComplete
16+
} = require('internal/process/pre_execution');
1617
const { spawn } = require('child_process');
1718
const { readdirSync, statSync } = require('fs');
1819
const console = require('internal/console/global');

lib/internal/main/worker_thread.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ const {
3030
initializeReport,
3131
initializeSourceMapsHandlers,
3232
loadPreloadModules,
33-
setupTraceCategoryState
34-
} = require('internal/bootstrap/pre_execution');
33+
setupTraceCategoryState,
34+
markBootstrapComplete
35+
} = require('internal/process/pre_execution');
3536

3637
const {
3738
threadId,

lib/internal/bootstrap/pre_execution.js lib/internal/process/pre_execution.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ function loadPreloadModules() {
596596
}
597597
}
598598

599+
function markBootstrapComplete() {
600+
internalBinding('performance').markBootstrapComplete();
601+
}
602+
599603
module.exports = {
600604
refreshRuntimeOptions,
601605
patchProcessObject,
@@ -616,5 +620,6 @@ module.exports = {
616620
setupInspectorHooks,
617621
initializeReport,
618622
initializeCJSLoader,
619-
initializeWASI
623+
initializeWASI,
624+
markBootstrapComplete
620625
};

lib/internal/v8/startup_snapshot.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212
setSerializeCallback,
1313
setDeserializeCallback,
1414
setDeserializeMainFunction: _setDeserializeMainFunction,
15-
markBootstrapComplete
1615
} = internalBinding('mksnapshot');
1716

1817
function isBuildingSnapshot() {
@@ -87,8 +86,9 @@ function setDeserializeMainFunction(callback, data) {
8786

8887
_setDeserializeMainFunction(function deserializeMain() {
8988
const {
90-
prepareMainThreadExecution
91-
} = require('internal/bootstrap/pre_execution');
89+
prepareMainThreadExecution,
90+
markBootstrapComplete
91+
} = require('internal/process/pre_execution');
9292

9393
// This should be in sync with run_main_module.js until we make that
9494
// a built-in main function.

lib/internal/wasm_web_api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function lazyUndici() {
1515

1616
// This is essentially an implementation of a v8::WasmStreamingCallback, except
1717
// that it is implemented in JavaScript because the fetch() implementation is
18-
// difficult to use from C++. See lib/internal/bootstrap/pre_execution.js and
18+
// difficult to use from C++. See lib/internal/process/pre_execution.js and
1919
// src/node_wasm_web_api.cc that interact with this function.
2020
function wasmStreamingCallback(streamState, source) {
2121
(async () => {

src/api/environment.cc

+6-9
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,12 @@ MaybeLocal<Value> LoadEnvironment(
452452
native_module::NativeModuleLoader::Add(
453453
name.c_str(), UnionBytes(**main_utf16, main_utf16->length()));
454454
env->set_main_utf16(std::move(main_utf16));
455-
std::vector<Local<String>> params = {
456-
env->process_string(),
457-
env->require_string()};
455+
// Arguments must match the parameters specified in
456+
// NativeModuleLoader::LookupAndCompile().
458457
std::vector<Local<Value>> args = {
459458
env->process_object(),
460459
env->native_module_require()};
461-
return ExecuteBootstrapper(env, name.c_str(), &params, &args);
460+
return ExecuteBootstrapper(env, name.c_str(), &args);
462461
});
463462
}
464463

@@ -683,8 +682,6 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
683682

684683
Local<String> primordials_string =
685684
FIXED_ONE_BYTE_STRING(isolate, "primordials");
686-
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global");
687-
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports");
688685

689686
// Create primordials first and make it available to per-context scripts.
690687
Local<Object> primordials = Object::New(isolate);
@@ -700,12 +697,12 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
700697
nullptr};
701698

702699
for (const char** module = context_files; *module != nullptr; module++) {
703-
std::vector<Local<String>> parameters = {
704-
global_string, exports_string, primordials_string};
700+
// Arguments must match the parameters specified in
701+
// NativeModuleLoader::LookupAndCompile().
705702
Local<Value> arguments[] = {context->Global(), exports, primordials};
706703
MaybeLocal<Function> maybe_fn =
707704
native_module::NativeModuleLoader::LookupAndCompile(
708-
context, *module, &parameters, nullptr);
705+
context, *module, nullptr);
709706
Local<Function> fn;
710707
if (!maybe_fn.ToLocal(&fn)) {
711708
return Nothing<bool>();

0 commit comments

Comments
 (0)