Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ function setupUserModules(forceDefaultLoader = false) {
} = require('internal/modules/helpers');
assert(!hasStartedUserCJSExecution());
assert(!hasStartedUserESMExecution());
if (getEmbedderOptions().hasEmbedderPreload) {
runEmbedderPreload();
}
// Do not enable preload modules if custom loaders are disabled.
// For example, loader workers are responsible for doing this themselves.
// And preload modules are not supported in ShadowRealm as well.
Expand Down Expand Up @@ -725,6 +728,10 @@ function initializeFrozenIntrinsics() {
}
}

function runEmbedderPreload() {
internalBinding('mksnapshot').runEmbedderPreload(process, require);
}

function loadPreloadModules() {
// For user code, we preload modules if `-r` is passed
const preloadModules = getOptionValue('--require');
Expand Down
18 changes: 12 additions & 6 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,25 +538,31 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
#endif
}

MaybeLocal<Value> LoadEnvironment(
Environment* env,
StartExecutionCallback cb) {
MaybeLocal<Value> LoadEnvironment(Environment* env,
StartExecutionCallback cb,
EmbedderPreloadCallback preload) {
env->InitializeLibuv();
env->InitializeDiagnostics();
if (preload) {
env->set_embedder_preload(std::move(preload));
}

return StartExecution(env, cb);
}

MaybeLocal<Value> LoadEnvironment(Environment* env,
std::string_view main_script_source_utf8) {
std::string_view main_script_source_utf8,
EmbedderPreloadCallback preload) {
CHECK_NOT_NULL(main_script_source_utf8.data());
return LoadEnvironment(
env, [&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
env,
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
Local<Value> main_script =
ToV8Value(env->context(), main_script_source_utf8).ToLocalChecked();
return info.run_cjs->Call(
env->context(), Null(env->isolate()), 1, &main_script);
});
},
std::move(preload));
}

Environment* GetCurrentEnvironment(Local<Context> context) {
Expand Down
8 changes: 8 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ inline builtins::BuiltinLoader* Environment::builtin_loader() {
return &builtin_loader_;
}

inline const EmbedderPreloadCallback& Environment::embedder_preload() const {
return embedder_preload_;
}

inline void Environment::set_embedder_preload(EmbedderPreloadCallback fn) {
embedder_preload_ = std::move(fn);
}

inline double Environment::new_async_id() {
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
Expand Down
4 changes: 4 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,9 @@ class Environment : public MemoryRetainer {

#endif // HAVE_INSPECTOR

inline const EmbedderPreloadCallback& embedder_preload() const;
Comment thread
zcbenz marked this conversation as resolved.
inline void set_embedder_preload(EmbedderPreloadCallback fn);

inline void set_process_exit_handler(
std::function<void(Environment*, ExitCode)>&& handler);

Expand Down Expand Up @@ -1204,6 +1207,7 @@ class Environment : public MemoryRetainer {
std::unique_ptr<PrincipalRealm> principal_realm_ = nullptr;

builtins::BuiltinLoader builtin_loader_;
EmbedderPreloadCallback embedder_preload_;

// Used by allocate_managed_buffer() and release_managed_buffer() to keep
// track of the BackingStore for a given pointer.
Expand Down
25 changes: 23 additions & 2 deletions src/node.h