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:
Joyee Cheung authored and Antoine du Hamel committed 2026-02-22 23:16:14 +01:00
1 parent 83bcd38d35
commit 563ab699eb
10 files changed
+502 -58

No files matched your search

+16
View File
@@ -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(