Skip to content

Commit e7d101f

Browse files
joyeecheungruyadorno
authored andcommitted
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: #44018 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 116dccc commit e7d101f

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
@@ -4,8 +4,9 @@
44
// instead of actually running the file.
55

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

1011
const {
1112
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
@@ -572,6 +572,10 @@ function loadPreloadModules() {
572572
}
573573
}
574574

575+
function markBootstrapComplete() {
576+
internalBinding('performance').markBootstrapComplete();
577+
}
578+
575579
module.exports = {
576580
refreshRuntimeOptions,
577581
patchProcessObject,
@@ -592,5 +596,6 @@ module.exports = {
592596
setupInspectorHooks,
593597
initializeReport,
594598
initializeCJSLoader,
595-
initializeWASI
599+
initializeWASI,
600+
markBootstrapComplete
596601
};

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
@@ -445,13 +445,12 @@ MaybeLocal<Value> LoadEnvironment(
445445
native_module::NativeModuleLoader::Add(
446446
name.c_str(), UnionBytes(**main_utf16, main_utf16->length()));
447447
env->set_main_utf16(std::move(main_utf16));
448-
std::vector<Local<String>> params = {
449-
env->process_string(),
450-
env->require_string()};
448+
// Arguments must match the parameters specified in
449+
// NativeModuleLoader::LookupAndCompile().
451450
std::vector<Local<Value>> args = {
452451
env->process_object(),
453452
env->native_module_require()};
454-
return ExecuteBootstrapper(env, name.c_str(), &params, &args);
453+
return ExecuteBootstrapper(env, name.c_str(), &args);
455454
});
456455
}
457456

@@ -676,8 +675,6 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
676675

677676
Local<String> primordials_string =
678677
FIXED_ONE_BYTE_STRING(isolate, "primordials");
679-
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global");
680-
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports");
681678

682679
// Create primordials first and make it available to per-context scripts.
683680
Local<Object> primordials = Object::New(isolate);
@@ -693,12 +690,12 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
693690
nullptr};
694691

695692
for (const char** module = context_files; *module != nullptr; module++) {
696-
std::vector<Local<String>> parameters = {
697-
global_string, exports_string, primordials_string};
693+
// Arguments must match the parameters specified in
694+
// NativeModuleLoader::LookupAndCompile().
698695
Local<Value> arguments[] = {context->Global(), exports, primordials};
699696
MaybeLocal<Function> maybe_fn =
700697
native_module::NativeModuleLoader::LookupAndCompile(
701-
context, *module, &parameters, nullptr);
698+
context, *module, nullptr);
702699
Local<Function> fn;
703700
if (!maybe_fn.ToLocal(&fn)) {
704701
return Nothing<bool>();

0 commit comments

Comments
 (0)