vm: allow dynamic import with a referrer realm

A referrer can be a Script Record, a Cyclic Module Record, or a Realm
Record as defined in https://tc39.es/ecma262/#sec-HostLoadImportedModule.

Add support for dynamic import calls with a realm as the referrer and
allow specifying an `importModuleDynamically` callback in
`vm.createContext`.

PR-URL: https://github.com/nodejs/node/pull/50360
Refs: https://github.com/nodejs/node/issues/49726
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Chengzhong Wu authored and GitHub committed 2023-11-01 06:05:13 +00:00
1 parent ffb326c583
commit 86afefafda
9 files changed
+159 -22

No files matched your search

+27 -1
View File
@@ -288,6 +288,19 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
.IsNothing()) {
return BaseObjectPtr<ContextifyContext>();
}
// Assign host_defined_options_id to the global object so that in the
// callback of ImportModuleDynamically, we can get the
// host_defined_options_id from the v8::Context without accessing the
// wrapper object.
if (new_context_global
->SetPrivate(v8_context,
env->host_defined_option_symbol(),
options->host_defined_options_id)
.IsNothing()) {
return BaseObjectPtr<ContextifyContext>();
}
env->AssignToContext(v8_context, nullptr, info);
if (!env->contextify_wrapper_template()
@@ -308,6 +321,16 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
.IsNothing()) {
return BaseObjectPtr<ContextifyContext>();
}
// Assign host_defined_options_id to the sandbox object so that module
// callbacks like importModuleDynamically can be registered once back to the
// JS land.
if (sandbox_obj
->SetPrivate(v8_context,
env->host_defined_option_symbol(),
options->host_defined_options_id)
.IsNothing()) {
return BaseObjectPtr<ContextifyContext>();
}
return result;
}
@@ -344,7 +367,7 @@ void ContextifyContext::RegisterExternalReferences(
void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_EQ(args.Length(), 6);
CHECK_EQ(args.Length(), 7);
CHECK(args[0]->IsObject());
Local<Object> sandbox = args[0].As<Object>();
@@ -375,6 +398,9 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
MicrotaskQueue::New(env->isolate(), MicrotasksPolicy::kExplicit);
}
CHECK(args[6]->IsSymbol());
options.host_defined_options_id = args[6].As<Symbol>();
TryCatchScope try_catch(env);
BaseObjectPtr<ContextifyContext> context_ptr =
ContextifyContext::New(env, sandbox, &options);