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

+80 -17
View File
@@ -15,15 +15,19 @@ const {
const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea');
const { emitExperimentalWarning } = require('internal/util');
const { emitWarningSync } = require('internal/process/warning');
const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm');
const { BuiltinModule } = require('internal/bootstrap/realm');
const { normalizeRequirableId } = BuiltinModule;
const { Module } = require('internal/modules/cjs/loader');
const { compileFunctionForCJSLoader } = internalBinding('contextify');
const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache');
const { codes: {
ERR_UNKNOWN_BUILTIN_MODULE,
} } = require('internal/errors');
const { pathToFileURL } = require('internal/url');
const { loadBuiltinModule } = require('internal/modules/helpers');
const { moduleFormats } = internalBinding('modules');
const assert = require('internal/assert');
const path = require('path');
// Don't expand process.argv[1] because in a single-executable application or an
// embedder application, the user main script isn't necessarily provided via the
// command line (e.g. it could be provided via an API or bundled into the executable).
@@ -44,12 +48,11 @@ if (isExperimentalSeaWarningNeeded()) {
// value of require.main to module.
//
// TODO(RaisinTen): Find a way to deduplicate this.
function embedderRunCjs(content) {
function embedderRunCjs(content, filename) {
// The filename of the module (used for CJS module lookup)
// is always the same as the location of the executable itself
// at the time of the loading (which means it changes depending
// on where the executable is in the file system).
const filename = process.execPath;
const customModule = new Module(filename, null);
const {
@@ -86,33 +89,93 @@ function embedderRunCjs(content) {
customModule.paths = Module._nodeModulePaths(process.execPath);
embedderRequire.main = customModule;
// This currently returns what the wrapper returns i.e. if the code
// happens to have a return statement, it returns that; Otherwise it's
// undefined.
// TODO(joyeecheung): we may want to return the customModule or put it in an
// out parameter.
return compiledWrapper(
customModule.exports, // exports
embedderRequire, // require
customModule, // module
process.execPath, // __filename
filename, // __filename
customModule.path, // __dirname
);
}
let warnedAboutBuiltins = false;
function warnNonBuiltinInSEA() {
if (isBuiltinWarningNeeded && !warnedAboutBuiltins) {
emitWarningSync(
'Currently the require() provided to the main script embedded into ' +
'single-executable applications only supports loading built-in modules.\n' +
'To load a module from disk after the single executable application is ' +
'launched, use require("module").createRequire().\n' +
'Support for bundled module loading or virtual file systems are under ' +
'discussions in https://github.com/nodejs/single-executable');
warnedAboutBuiltins = true;
}
}
function embedderRequire(id) {
const normalizedId = normalizeRequirableId(id);
if (!normalizedId) {
if (isBuiltinWarningNeeded && !warnedAboutBuiltins) {
emitWarningSync(
'Currently the require() provided to the main script embedded into ' +
'single-executable applications only supports loading built-in modules.\n' +
'To load a module from disk after the single executable application is ' +
'launched, use require("module").createRequire().\n' +
'Support for bundled module loading or virtual file systems are under ' +
'discussions in https://github.com/nodejs/single-executable');
warnedAboutBuiltins = true;
}
warnNonBuiltinInSEA();
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
}
return require(normalizedId);
}
return [process, embedderRequire, embedderRunCjs];
function embedderRunESM(content, filename) {
let resourceName;
if (path.isAbsolute(filename)) {
resourceName = pathToFileURL(filename).href;
} else {
resourceName = filename;
}
const { compileSourceTextModule } = require('internal/modules/esm/utils');
// TODO(joyeecheung): support code cache, dynamic import() and import.meta.
const wrap = compileSourceTextModule(resourceName, content);
// Cache the source map for the module if present.
if (wrap.sourceMapURL) {
maybeCacheSourceMap(resourceName, content, wrap, false, undefined, wrap.sourceMapURL);
}
const requests = wrap.getModuleRequests();
const modules = [];
for (let i = 0; i < requests.length; ++i) {
const { specifier } = requests[i];
const normalizedId = normalizeRequirableId(specifier);
if (!normalizedId) {
warnNonBuiltinInSEA();
throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier);
}
const mod = loadBuiltinModule(normalizedId);
if (!mod) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier);
}
modules.push(mod.getESMFacade());
}
wrap.link(modules);
wrap.instantiate();
wrap.evaluate(-1, false);
// TODO(joyeecheung): we may want to return the v8::Module via a vm.SourceTextModule
// when vm.SourceTextModule stablizes, or put it in an out parameter.
return wrap.getNamespace();
}
function embedderRunEntryPoint(content, format, filename) {
format ||= moduleFormats.kCommonJS;
filename ||= process.execPath;
if (format === moduleFormats.kCommonJS) {
return embedderRunCjs(content, filename);
} else if (format === moduleFormats.kModule) {
return embedderRunESM(content, filename);
}
assert.fail(`Unknown format: ${format}`);
}
return [process, embedderRequire, embedderRunEntryPoint];
+138 -11
View File
@@ -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
View File
@@ -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
View File
@@ -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,
+2 -2
View File
@@ -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);
+1 -1
View File
@@ -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 =
+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(
+1 -1
View File
@@ -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;
+161
View File
@@ -807,3 +807,164 @@ TEST_F(EnvironmentTest, EmbedderPreload) {
node::Utf8Value main_ret_str(isolate_, main_ret);
EXPECT_EQ(std::string(*main_ret_str), "preload");
}
TEST_F(EnvironmentTest, LoadEnvironmentWithESModule) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env{handle_scope, argv};
node::ModuleData entry_point;
std::string source =
"import importedProcess from 'process';\n"
"export const process = importedProcess;\n"
"const stack = new Error().stack.split('\\n');\n"
"export const frame = stack.filter(line => /at /.test(line))[0];\n";
entry_point.set_source(source);
entry_point.set_format(node::ModuleFormat::kModule);
entry_point.set_resource_name("embedded:esm.mjs");
v8::Local<v8::Value> result =
node::LoadEnvironment(*env, &entry_point).ToLocalChecked();
// The ESM entry point returns the module namespace object.
EXPECT_TRUE(result->IsObject());
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::Local<v8::String> process_str =
v8::String::NewFromUtf8Literal(isolate_, "process");
v8::Local<v8::Value> process_value =
result.As<v8::Object>()->Get(context, process_str).ToLocalChecked();
EXPECT_TRUE(process_value->IsObject());
v8::Local<v8::Value> global_process_value =
context->Global()->Get(context, process_str).ToLocalChecked();
EXPECT_TRUE(global_process_value->IsObject());
EXPECT_TRUE(process_value->StrictEquals(global_process_value));
v8::Local<v8::Value> frame_value =
result.As<v8::Object>()
->Get(context, v8::String::NewFromUtf8Literal(isolate_, "frame"))
.ToLocalChecked();
EXPECT_TRUE(frame_value->IsString());
node::Utf8Value frame_str(isolate_, frame_value);
printf("Frame: %s\n", *frame_str);
EXPECT_EQ(frame_str.ToString(), " at embedded:esm.mjs:3:15");
}
TEST_F(EnvironmentTest, LoadEnvironmentWithCallbackWithCommonJSModule) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env{handle_scope, argv};
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::MaybeLocal<v8::Value> maybe = node::LoadEnvironment(
*env,
[&](const node::StartExecutionCallbackInfoWithModule& info)
-> v8::MaybeLocal<v8::Value> {
EXPECT_NE(nullptr, info.env());
EXPECT_TRUE(info.process_object()->IsObject());
EXPECT_TRUE(info.native_require()->IsFunction());
EXPECT_TRUE(info.run_module()->IsFunction());
// require("process") === process
v8::Local<v8::String> process_str =
v8::String::NewFromUtf8Literal(isolate_, "process");
v8::Local<v8::Value> require_args[] = {process_str};
v8::Local<v8::Value> required_process =
info.native_require()
->Call(context, v8::Null(isolate_), 1, require_args)
.ToLocalChecked();
EXPECT_TRUE(required_process->IsObject());
EXPECT_TRUE(required_process->StrictEquals(info.process_object()));
// Test running a CJS entry point via run_module.
v8::Local<v8::Value> cjs_source = v8::String::NewFromUtf8Literal(
isolate_,
"globalThis.processInModule = require('process');\n"
"const stack = new Error().stack.split('\\n');\n"
"globalThis.frame = stack.filter(line => /at /.test(line))[0];\n"
"return 42;\n");
v8::Local<v8::Value> format = v8::Integer::New(
isolate_, static_cast<int32_t>(node::ModuleFormat::kCommonJS));
v8::Local<v8::Value> resource_name =
v8::String::NewFromUtf8Literal(isolate_, "/test-cjs.js");
v8::Local<v8::Value> args[] = {cjs_source, format, resource_name};
v8::Local<v8::Value> result =
info.run_module()
->Call(context, v8::Null(isolate_), 3, args)
.ToLocalChecked();
EXPECT_TRUE(result->IsUint32());
EXPECT_EQ(result.As<v8::Uint32>()->Value(), static_cast<uint32_t>(42));
v8::Local<v8::Value> process_in_module =
context->Global()
->Get(
context,
v8::String::NewFromUtf8Literal(isolate_, "processInModule"))
.ToLocalChecked();
EXPECT_TRUE(process_in_module->IsObject());
EXPECT_TRUE(process_in_module->StrictEquals(info.process_object()));
return result;
},
nullptr);
EXPECT_TRUE(!maybe.IsEmpty());
v8::Local<v8::Value> frame_value =
context->Global()
->Get(context, v8::String::NewFromUtf8Literal(isolate_, "frame"))
.ToLocalChecked();
EXPECT_TRUE(frame_value->IsString());
node::Utf8Value frame_str(isolate_, frame_value);
printf("Frame: %s\n", *frame_str);
EXPECT_EQ(frame_str.ToString(), " at /test-cjs.js:2:15");
}
TEST_F(EnvironmentTest, LoadEnvironmentWithCallbackWithESModule) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env{handle_scope, argv};
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::MaybeLocal<v8::Value> maybe = node::LoadEnvironment(
*env,
[&](const node::StartExecutionCallbackInfoWithModule& info)
-> v8::MaybeLocal<v8::Value> {
// Test running an ESM entry point via run_module.
v8::Local<v8::Value> source_str = v8::String::NewFromUtf8Literal(
isolate_,
"import importedProcess from 'process';\n"
"export const process = importedProcess;\n"
"const stack = new Error().stack.split('\\n');\n"
"export const frame = stack.filter(line => /at "
"/.test(line))[0];\n");
v8::Local<v8::Value> format = v8::Integer::New(
isolate_, static_cast<int32_t>(node::ModuleFormat::kModule));
v8::Local<v8::Value> resource_name =
v8::String::NewFromUtf8Literal(isolate_, "embedded:esm.mjs");
v8::Local<v8::Value> args[] = {source_str, format, resource_name};
return info.run_module()->Call(context, v8::Null(isolate_), 3, args);
},
nullptr);
v8::Local<v8::Value> result = maybe.ToLocalChecked();
v8::Local<v8::String> process_str =
v8::String::NewFromUtf8Literal(isolate_, "process");
// The ESM entry point returns the module namespace object.
EXPECT_TRUE(result->IsObject());
v8::Local<v8::Value> process_value =
result.As<v8::Object>()->Get(context, process_str).ToLocalChecked();
EXPECT_TRUE(process_value->IsObject());
v8::Local<v8::Value> global_process_value =
context->Global()->Get(context, process_str).ToLocalChecked();
EXPECT_TRUE(global_process_value->IsObject());
EXPECT_TRUE(process_value->StrictEquals(global_process_value));
v8::Local<v8::Value> frame_value =
result.As<v8::Object>()
->Get(context, v8::String::NewFromUtf8Literal(isolate_, "frame"))
.ToLocalChecked();
EXPECT_TRUE(frame_value->IsString());
node::Utf8Value frame_str(isolate_, frame_value);
printf("Frame: %s\n", *frame_str);
EXPECT_EQ(frame_str.ToString(), " at embedded:esm.mjs:3:15");
}
+7 -4
View File
@@ -6,9 +6,12 @@ const createdRequire = createRequire(__filename);
const { expectWarning, mustNotCall } = createdRequire(process.env.COMMON_DIRECTORY);
const builtinWarning =
`Currently the require() provided to the main script embedded into single-executable applications only supports loading built-in modules.
To load a module from disk after the single executable application is launched, use require("module").createRequire().
Support for bundled module loading or virtual file systems are under discussions in https://github.com/nodejs/single-executable`;
'Currently the require() provided to the main script embedded into ' +
'single-executable applications only supports loading built-in modules.\n' +
'To load a module from disk after the single executable application is ' +
'launched, use require("module").createRequire().\n' +
'Support for bundled module loading or virtual file systems are under ' +
'discussions in https://github.com/nodejs/single-executable';
// This additionally makes sure that no unexpected warnings are emitted.
if (!createdRequire('./sea-config.json').disableExperimentalSEAWarning) {
@@ -26,7 +29,7 @@ const { deepStrictEqual, strictEqual, throws } = require('assert');
const { dirname } = require('node:path');
// Checks that the source filename is used in the error stack trace.
strictEqual(new Error('lol').stack.split('\n')[1], ' at sea.js:29:13');
strictEqual(new Error('lol').stack.split('\n')[1], ' at sea.js:32:13');
// Should be possible to require a core module that requires using the "node:"
// scheme.