vm: add support for import assertions in dynamic imports · nodejs/node@4116b6c · GitHub
Skip to content

Commit 4116b6c

Browse files
aduh95targos
authored andcommitted
vm: add support for import assertions in dynamic imports
PR-URL: #40249 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 2d409ed commit 4116b6c

5 files changed

Lines changed: 90 additions & 12 deletions

File tree

doc/api/vm.md

Lines changed: 46 additions & 0 deletions

lib/internal/process/esm_loader.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
const {
4+
ObjectCreate,
5+
} = primordials;
6+
37
const {
48
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
59
} = require('internal/errors').codes;
@@ -22,13 +26,14 @@ exports.initializeImportMetaObject = function(wrap, meta) {
2226
}
2327
};
2428

25-
exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
29+
exports.importModuleDynamicallyCallback =
30+
async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
2631
const { callbackMap } = internalBinding('module_wrap');
2732
if (callbackMap.has(wrap)) {
2833
const { importModuleDynamically } = callbackMap.get(wrap);
2934
if (importModuleDynamically !== undefined) {
3035
return importModuleDynamically(
31-
specifier, getModuleFromWrap(wrap) || wrap);
36+
specifier, getModuleFromWrap(wrap) || wrap, assertions);
3237
}
3338
}
3439
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
@@ -69,6 +74,7 @@ async function initializeLoader() {
6974
const exports = await internalEsmLoader.import(
7075
customLoaders,
7176
pathToFileURL(cwd).href,
77+
ObjectCreate(null),
7278
);
7379

7480
// Hooks must then be added to external/public loader

lib/vm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ function compileFunction(code, params, options = {}) {
378378
const wrapped = importModuleDynamicallyWrap(importModuleDynamically);
379379
const func = result.function;
380380
callbackMap.set(result.cacheKey, {
381-
importModuleDynamically: (s, _k) => wrapped(s, func),
381+
importModuleDynamically: (s, _k, i) => wrapped(s, func, i),
382382
});
383383
}
384384

src/module_wrap.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
253253
args.GetReturnValue().Set(that);
254254
}
255255

256+
static Local<Object> createImportAssertionContainer(Environment* env,
257+
Isolate* isolate, Local<FixedArray> raw_assertions) {
258+
Local<Object> assertions =
259+
Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0);
260+
for (int i = 0; i < raw_assertions->Length(); i += 3) {
261+
assertions
262+
->Set(env->context(),
263+
raw_assertions->Get(env->context(), i).As<String>(),
264+
raw_assertions->Get(env->context(), i + 1).As<Value>())
265+
.ToChecked();
266+
}
267+
268+
return assertions;
269+
}
270+
256271
void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
257272
Environment* env = Environment::GetCurrent(args);
258273
Isolate* isolate = args.GetIsolate();
@@ -288,14 +303,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
288303

289304
Local<FixedArray> raw_assertions = module_request->GetImportAssertions();
290305
Local<Object> assertions =
291-
Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0);
292-
for (int i = 0; i < raw_assertions->Length(); i += 3) {
293-
assertions
294-
->Set(env->context(),
295-
raw_assertions->Get(env->context(), i).As<String>(),
296-
raw_assertions->Get(env->context(), i + 1).As<Value>())
297-
.ToChecked();
298-
}
306+
createImportAssertionContainer(env, isolate, raw_assertions);
299307

300308
Local<Value> argv[] = {
301309
specifier,
@@ -602,9 +610,13 @@ static MaybeLocal<Promise> ImportModuleDynamically(
602610
UNREACHABLE();
603611
}
604612

613+
Local<Object> assertions =
614+
createImportAssertionContainer(env, isolate, import_assertions);
615+
605616
Local<Value> import_args[] = {
606617
object,
607618
Local<Value>(specifier),
619+
assertions,
608620
};
609621

610622
Local<Value> result;

test/parallel/test-vm-module-dynamic-import.js

Lines changed: 15 additions & 1 deletion

0 commit comments

Comments
 (0)