mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
src: add initial support for ESM in embedder API
This patch extends `LoadEnvironment` to support loading ES modules,
and adds the following new types:
```cpp
enum class ModuleFormat : uint8_t {
kCommonJS,
kModule,
};
// Data for specifying an entry point script for LoadEnvironment().
// This class uses an opaque layout to allow future additions without
// breaking ABI. Use the setter methods to configure the entry point.
class ModuleData {
void set_source(std::string_view source);
void set_format(ModuleFormat format);
void set_resource_name(std::string_view name);
std::string_view source() const;
ModuleFormat format() const;
std::string_view resource_name() const;
};
class StartExecutionCallbackInfoWithModule {
void set_env(Environment* env);
void set_process_object(v8::Local<v8::Object> process_object);
void set_native_require(v8::Local<v8::Function> native_require);
void set_run_module(v8::Local<v8::Function> run_module);
void set_data(void* data);
Environment* env();
v8::Local<v8::Object> process();
v8::Local<v8::Function> native_require();
v8::Local<v8::Function> run_module();
void* data();
};
```
And two new `LoadEnvironment()` overloads:
```cpp
// Run entry point with ModuleData configuration
MaybeLocal<Value> LoadEnvironment(
Environment* env,
const ModuleData* entry_point,
EmbedderPreloadCallback preload = nullptr);
// Callback-based with new StartExecutionCallbackInfoWithModule
MaybeLocal<Value> LoadEnvironment(
Environment* env,
StartExecutionCallbackWithModule cb,
EmbedderPreloadCallback preload = nullptr,
void* callback_data = nullptr);
```
PR-URL: https://github.com/nodejs/node/pull/61548
Refs: https://github.com/nodejs/node/issues/53565
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Aditi Singh <aditisingh1400@gmail.com>
This commit is contained in:
1 parent
83bcd38d35
commit
563ab699eb
10 files changed
+502
-58
No files matched your search
+138
-11
@@ -574,7 +574,7 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
|
||||
}
|
||||
|
||||
MaybeLocal<Value> LoadEnvironment(Environment* env,
|
||||
StartExecutionCallback cb,
|
||||
StartExecutionCallbackWithModule cb,
|
||||
EmbedderPreloadCallback preload) {
|
||||
env->InitializeLibuv();
|
||||
env->InitializeDiagnostics();
|
||||
@@ -586,22 +586,149 @@ MaybeLocal<Value> LoadEnvironment(Environment* env,
|
||||
return StartExecution(env, cb);
|
||||
}
|
||||
|
||||
struct StartExecutionCallbackInfoWithModule::Impl {
|
||||
Environment* env = nullptr;
|
||||
Local<Object> process_object;
|
||||
Local<Function> native_require;
|
||||
Local<Function> run_module;
|
||||
};
|
||||
|
||||
StartExecutionCallbackInfoWithModule::StartExecutionCallbackInfoWithModule()
|
||||
: impl_(std::make_unique<Impl>()) {}
|
||||
|
||||
StartExecutionCallbackInfoWithModule::~StartExecutionCallbackInfoWithModule() =
|
||||
default;
|
||||
|
||||
StartExecutionCallbackInfoWithModule::StartExecutionCallbackInfoWithModule(
|
||||
StartExecutionCallbackInfoWithModule&&) = default;
|
||||
|
||||
StartExecutionCallbackInfoWithModule&
|
||||
StartExecutionCallbackInfoWithModule::operator=(
|
||||
StartExecutionCallbackInfoWithModule&&) = default;
|
||||
|
||||
Environment* StartExecutionCallbackInfoWithModule::env() const {
|
||||
return impl_->env;
|
||||
}
|
||||
|
||||
Local<Object> StartExecutionCallbackInfoWithModule::process_object() const {
|
||||
return impl_->process_object;
|
||||
}
|
||||
|
||||
Local<Function> StartExecutionCallbackInfoWithModule::native_require() const {
|
||||
return impl_->native_require;
|
||||
}
|
||||
|
||||
Local<Function> StartExecutionCallbackInfoWithModule::run_module() const {
|
||||
return impl_->run_module;
|
||||
}
|
||||
|
||||
void StartExecutionCallbackInfoWithModule::set_env(Environment* env) {
|
||||
impl_->env = env;
|
||||
}
|
||||
|
||||
void StartExecutionCallbackInfoWithModule::set_process_object(
|
||||
Local<Object> process_object) {
|
||||
impl_->process_object = process_object;
|
||||
}
|
||||
|
||||
void StartExecutionCallbackInfoWithModule::set_native_require(
|
||||
Local<Function> native_require) {
|
||||
impl_->native_require = native_require;
|
||||
}
|
||||
|
||||
void StartExecutionCallbackInfoWithModule::set_run_module(
|
||||
Local<Function> run_module) {
|
||||
impl_->run_module = run_module;
|
||||
}
|
||||
|
||||
struct ModuleData::Impl {
|
||||
std::string_view source;
|
||||
ModuleFormat format = ModuleFormat::kCommonJS;
|
||||
std::string_view resource_name;
|
||||
};
|
||||
|
||||
ModuleData::ModuleData() : impl_(std::make_unique<Impl>()) {}
|
||||
|
||||
ModuleData::~ModuleData() = default;
|
||||
|
||||
ModuleData::ModuleData(ModuleData&&) = default;
|
||||
|
||||
ModuleData& ModuleData::operator=(ModuleData&&) = default;
|
||||
|
||||
void ModuleData::set_source(std::string_view source) {
|
||||
impl_->source = source;
|
||||
}
|
||||
|
||||
void ModuleData::set_format(ModuleFormat format) {
|
||||
impl_->format = format;
|
||||
}
|
||||
|
||||
void ModuleData::set_resource_name(std::string_view name) {
|
||||
impl_->resource_name = name;
|
||||
}
|
||||
|
||||
std::string_view ModuleData::source() const {
|
||||
return impl_->source;
|
||||
}
|
||||
|
||||
ModuleFormat ModuleData::format() const {
|
||||
return impl_->format;
|
||||
}
|
||||
|
||||
std::string_view ModuleData::resource_name() const {
|
||||
return impl_->resource_name;
|
||||
}
|
||||
|
||||
MaybeLocal<Value> LoadEnvironment(Environment* env,
|
||||
StartExecutionCallback cb,
|
||||
EmbedderPreloadCallback preload) {
|
||||
if (!cb) {
|
||||
return LoadEnvironment(
|
||||
env, StartExecutionCallbackWithModule{}, std::move(preload));
|
||||
}
|
||||
|
||||
return LoadEnvironment(
|
||||
env,
|
||||
[cb = std::move(cb)](const StartExecutionCallbackInfoWithModule& info)
|
||||
-> MaybeLocal<Value> {
|
||||
StartExecutionCallbackInfo legacy_info{
|
||||
info.process_object(), info.native_require(), info.run_module()};
|
||||
return cb(legacy_info);
|
||||
},
|
||||
std::move(preload));
|
||||
}
|
||||
|
||||
MaybeLocal<Value> LoadEnvironment(Environment* env,
|
||||
std::string_view main_script_source_utf8,
|
||||
EmbedderPreloadCallback preload) {
|
||||
ModuleData data;
|
||||
data.set_source(main_script_source_utf8);
|
||||
data.set_format(ModuleFormat::kCommonJS);
|
||||
data.set_resource_name(env->exec_path());
|
||||
return LoadEnvironment(env, &data, std::move(preload));
|
||||
}
|
||||
|
||||
MaybeLocal<Value> LoadEnvironment(Environment* env,
|
||||
const ModuleData* data,
|
||||
EmbedderPreloadCallback preload) {
|
||||
// It could be empty when it's used by SEA to load an empty script.
|
||||
CHECK_IMPLIES(main_script_source_utf8.size() > 0,
|
||||
main_script_source_utf8.data());
|
||||
CHECK_IMPLIES(data->source().size() > 0, data->source().data());
|
||||
return LoadEnvironment(
|
||||
env,
|
||||
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
|
||||
Local<Value> main_script;
|
||||
if (!ToV8Value(env->context(), main_script_source_utf8)
|
||||
.ToLocal(&main_script)) {
|
||||
return {};
|
||||
}
|
||||
return info.run_cjs->Call(
|
||||
env->context(), Null(env->isolate()), 1, &main_script);
|
||||
[data](const StartExecutionCallbackInfoWithModule& info)
|
||||
-> MaybeLocal<Value> {
|
||||
Environment* env = info.env();
|
||||
Local<Context> context = env->context();
|
||||
Isolate* isolate = env->isolate();
|
||||
Local<Value> main_script =
|
||||
ToV8Value(context, data->source()).ToLocalChecked();
|
||||
Local<Value> format =
|
||||
v8::Integer::New(isolate, static_cast<int>(data->format()));
|
||||
Local<Value> resource_name =
|
||||
ToV8Value(context, data->resource_name()).ToLocalChecked();
|
||||
Local<Value> args[] = {main_script, format, resource_name};
|
||||
return info.run_module()->Call(
|
||||
context, Null(isolate), arraysize(args), args);
|
||||
},
|
||||
std::move(preload));
|
||||
}
|
||||
|
||||
+18
-22
@@ -255,37 +255,33 @@ MaybeLocal<Value> StartExecution(Environment* env, const char* main_script_id) {
|
||||
}
|
||||
|
||||
// Convert the result returned by an intermediate main script into
|
||||
// StartExecutionCallbackInfo. Currently the result is an array containing
|
||||
// [process, requireFunction, cjsRunner]
|
||||
std::optional<StartExecutionCallbackInfo> CallbackInfoFromArray(
|
||||
Local<Context> context, Local<Value> result) {
|
||||
// StartExecutionCallbackInfoWithModule. Currently the result is an array
|
||||
// containing [process, requireFunction, runModule].
|
||||
std::optional<StartExecutionCallbackInfoWithModule> CallbackInfoFromArray(
|
||||
Environment* env, Local<Value> result) {
|
||||
CHECK(result->IsArray());
|
||||
Local<Array> args = result.As<Array>();
|
||||
CHECK_EQ(args->Length(), 3);
|
||||
Local<Value> process_obj, require_fn, runcjs_fn;
|
||||
Local<Value> process_obj, require_fn, run_module;
|
||||
Local<Context> context = env->context();
|
||||
if (!args->Get(context, 0).ToLocal(&process_obj) ||
|
||||
!args->Get(context, 1).ToLocal(&require_fn) ||
|
||||
!args->Get(context, 2).ToLocal(&runcjs_fn)) {
|
||||
!args->Get(context, 2).ToLocal(&run_module)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
CHECK(process_obj->IsObject());
|
||||
CHECK(require_fn->IsFunction());
|
||||
CHECK(runcjs_fn->IsFunction());
|
||||
// TODO(joyeecheung): some support for running ESM as an entrypoint
|
||||
// is needed. The simplest API would be to add a run_esm to
|
||||
// StartExecutionCallbackInfo which compiles, links (to builtins)
|
||||
// and evaluates a SourceTextModule.
|
||||
// TODO(joyeecheung): the env pointer should be part of
|
||||
// StartExecutionCallbackInfo, otherwise embedders are forced to use
|
||||
// lambdas to pass it into the callback, which can make the code
|
||||
// difficult to read.
|
||||
node::StartExecutionCallbackInfo info{process_obj.As<Object>(),
|
||||
require_fn.As<Function>(),
|
||||
runcjs_fn.As<Function>()};
|
||||
CHECK(run_module->IsFunction());
|
||||
StartExecutionCallbackInfoWithModule info;
|
||||
info.set_env(env);
|
||||
info.set_process_object(process_obj.As<Object>());
|
||||
info.set_native_require(require_fn.As<Function>());
|
||||
info.set_run_module(run_module.As<Function>());
|
||||
return info;
|
||||
}
|
||||
|
||||
MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
|
||||
MaybeLocal<Value> StartExecution(Environment* env,
|
||||
StartExecutionCallbackWithModule cb) {
|
||||
InternalCallbackScope callback_scope(
|
||||
env,
|
||||
Object::New(env->isolate()),
|
||||
@@ -294,7 +290,7 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
|
||||
|
||||
// Only snapshot builder or embedder applications set the
|
||||
// callback.
|
||||
if (cb != nullptr) {
|
||||
if (cb) {
|
||||
EscapableHandleScope scope(env->isolate());
|
||||
|
||||
Local<Value> result;
|
||||
@@ -308,9 +304,9 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
|
||||
}
|
||||
}
|
||||
|
||||
auto info = CallbackInfoFromArray(env->context(), result);
|
||||
auto info = CallbackInfoFromArray(env, result);
|
||||
if (!info.has_value()) {
|
||||
MaybeLocal<Value>();
|
||||
return MaybeLocal<Value>();
|
||||
}
|
||||
#if HAVE_INSPECTOR
|
||||
if (env->options()->debug_options().break_first_line) {
|
||||
|
||||
+78
@@ -769,8 +769,48 @@ struct StartExecutionCallbackInfo {
|
||||
v8::Local<v8::Function> run_cjs;
|
||||
};
|
||||
|
||||
enum class ModuleFormat : uint8_t {
|
||||
kCommonJS,
|
||||
kModule, // i.e. ES Module/SourceTextModule
|
||||
// TODO(joyeecheung): support TypeScriptModule, TypeScriptCommonJS
|
||||
};
|
||||
|
||||
// Information passed to embedder callbacks during environment startup.
|
||||
// This class is created by Node.js and passed to the embedder's callback.
|
||||
// The layout is opaque to allow future additions without breaking ABI.
|
||||
class NODE_EXTERN StartExecutionCallbackInfoWithModule {
|
||||
public:
|
||||
StartExecutionCallbackInfoWithModule();
|
||||
~StartExecutionCallbackInfoWithModule();
|
||||
|
||||
StartExecutionCallbackInfoWithModule(
|
||||
const StartExecutionCallbackInfoWithModule&) = delete;
|
||||
StartExecutionCallbackInfoWithModule& operator=(
|
||||
const StartExecutionCallbackInfoWithModule&) = delete;
|
||||
StartExecutionCallbackInfoWithModule(StartExecutionCallbackInfoWithModule&&);
|
||||
StartExecutionCallbackInfoWithModule& operator=(
|
||||
StartExecutionCallbackInfoWithModule&&);
|
||||
|
||||
Environment* env() const;
|
||||
v8::Local<v8::Object> process_object() const;
|
||||
v8::Local<v8::Function> native_require() const;
|
||||
v8::Local<v8::Function> run_module() const;
|
||||
|
||||
void set_env(Environment* env);
|
||||
void set_process_object(v8::Local<v8::Object> process_object);
|
||||
void set_native_require(v8::Local<v8::Function> native_require);
|
||||
void set_run_module(v8::Local<v8::Function> run_module);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
using StartExecutionCallback =
|
||||
std::function<v8::MaybeLocal<v8::Value>(const StartExecutionCallbackInfo&)>;
|
||||
using StartExecutionCallbackWithModule =
|
||||
std::function<v8::MaybeLocal<v8::Value>(
|
||||
const StartExecutionCallbackInfoWithModule&)>;
|
||||
using EmbedderPreloadCallback =
|
||||
std::function<void(Environment* env,
|
||||
v8::Local<v8::Value> process,
|
||||
@@ -794,10 +834,48 @@ NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
|
||||
Environment* env,
|
||||
StartExecutionCallback cb,
|
||||
EmbedderPreloadCallback preload = nullptr);
|
||||
|
||||
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
|
||||
Environment* env,
|
||||
StartExecutionCallbackWithModule cb,
|
||||
EmbedderPreloadCallback preload = nullptr);
|
||||
|
||||
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
|
||||
Environment* env,
|
||||
std::string_view main_script_source_utf8,
|
||||
EmbedderPreloadCallback preload = nullptr);
|
||||
|
||||
// Data for specifying an entry point script for LoadEnvironment().
|
||||
// This class uses an opaque layout to allow future additions without
|
||||
// breaking ABI. Use the setter methods to configure the entry point.
|
||||
class NODE_EXTERN ModuleData {
|
||||
public:
|
||||
ModuleData();
|
||||
~ModuleData();
|
||||
|
||||
ModuleData(const ModuleData&) = delete;
|
||||
ModuleData& operator=(const ModuleData&) = delete;
|
||||
ModuleData(ModuleData&&);
|
||||
ModuleData& operator=(ModuleData&&);
|
||||
|
||||
void set_source(std::string_view source);
|
||||
void set_format(ModuleFormat format);
|
||||
void set_resource_name(std::string_view name);
|
||||
|
||||
std::string_view source() const;
|
||||
ModuleFormat format() const;
|
||||
std::string_view resource_name() const;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
|
||||
Environment* env,
|
||||
const ModuleData* entry_point,
|
||||
EmbedderPreloadCallback preload = nullptr);
|
||||
|
||||
NODE_EXTERN void FreeEnvironment(Environment* env);
|
||||
|
||||
// Set a callback that is called when process.exit() is called from JS,
|
||||
|
||||
@@ -362,8 +362,8 @@ v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
|
||||
const IsolateSettings& settings = {});
|
||||
// This overload automatically picks the right 'main_script_id' if no callback
|
||||
// was provided by the embedder.
|
||||
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
|
||||
StartExecutionCallback cb = nullptr);
|
||||
v8::MaybeLocal<v8::Value> StartExecution(
|
||||
Environment* env, StartExecutionCallbackWithModule cb = nullptr);
|
||||
v8::MaybeLocal<v8::Object> GetPerContextExports(
|
||||
v8::Local<v8::Context> context, IsolateData* isolate_data = nullptr);
|
||||
void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
@@ -103,7 +103,7 @@ ExitCode NodeMainInstance::Run() {
|
||||
void NodeMainInstance::Run(ExitCode* exit_code, Environment* env) {
|
||||
if (*exit_code == ExitCode::kNoFailure) {
|
||||
if (!sea::MaybeLoadSingleExecutableApplication(env)) {
|
||||
LoadEnvironment(env, StartExecutionCallback{});
|
||||
LoadEnvironment(env, StartExecutionCallbackWithModule{});
|
||||
}
|
||||
|
||||
*exit_code =
|
||||
|
||||
@@ -731,6 +731,22 @@ void BindingData::CreatePerContextProperties(Local<Object> target,
|
||||
cached_code_type_keys.data(),
|
||||
cached_code_type_values.data(),
|
||||
cached_code_type_keys.size())));
|
||||
|
||||
LocalVector<Name> module_format_keys(isolate);
|
||||
LocalVector<Value> module_format_values(isolate);
|
||||
module_format_keys.push_back(FIXED_ONE_BYTE_STRING(isolate, "kCommonJS"));
|
||||
module_format_values.push_back(
|
||||
Integer::New(isolate, static_cast<int>(ModuleFormat::kCommonJS)));
|
||||
module_format_keys.push_back(FIXED_ONE_BYTE_STRING(isolate, "kModule"));
|
||||
module_format_values.push_back(
|
||||
Integer::New(isolate, static_cast<int>(ModuleFormat::kModule)));
|
||||
USE(target->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "moduleFormats"),
|
||||
Object::New(isolate,
|
||||
Null(isolate),
|
||||
module_format_keys.data(),
|
||||
module_format_values.data(),
|
||||
module_format_keys.size())));
|
||||
}
|
||||
|
||||
void BindingData::RegisterExternalReferences(
|
||||
|
||||
+1
-1
@@ -409,7 +409,7 @@ void Worker::Run() {
|
||||
|
||||
Debug(this, "Created message port for worker %llu", thread_id_.id);
|
||||
if (LoadEnvironment(env_.get(),
|
||||
StartExecutionCallback{},
|
||||
StartExecutionCallbackWithModule{},
|
||||
std::move(embedder_preload_))
|
||||
.IsEmpty()) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user