Skip to content

Commit 97aab2e

Browse files
committed
fixup! vm: add support for import assertions in dynamic imports
1 parent f3b27e9 commit 97aab2e

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

doc/api/vm.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ added: v0.3.1
5656
changes:
5757
- version: REPLACEME
5858
pr-url: https://github.com/nodejs/node/pull/40249
59-
description: Added support of import assertions to the
59+
description: Added support for import assertions to the
6060
`importModuleDynamically` parameter.
6161
- version: v10.6.0
6262
pr-url: https://github.com/nodejs/node/pull/20300
@@ -96,7 +96,8 @@ changes:
9696
* `specifier` {string} specifier passed to `import()`
9797
* `script` {vm.Script}
9898
* `importAssertions` {Object} The `"assert"` value passed to the
99-
`optionExpression` optional parameter.
99+
`optionExpression` optional parameter, or an empty object if no value was
100+
provided.
100101
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
101102
recommended in order to take advantage of error tracking, and to avoid
102103
issues with namespaces that contain `then` function exports.
@@ -652,7 +653,7 @@ defined in the ECMAScript specification.
652653
changes:
653654
- version: REPLACEME
654655
pr-url: https://github.com/nodejs/node/pull/40249
655-
description: Added support of import assertions to the
656+
description: Added support for import assertions to the
656657
`importModuleDynamically` parameter.
657658
-->
658659

@@ -681,7 +682,8 @@ changes:
681682
* `specifier` {string} specifier passed to `import()`
682683
* `module` {vm.Module}
683684
* `importAssertions` {Object} The `"assert"` value passed to the
684-
`optionExpression` optional parameter.
685+
`optionExpression` optional parameter, or an empty object if no value was
686+
provided.
685687
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
686688
recommended in order to take advantage of error tracking, and to avoid
687689
issues with namespaces that contain `then` function exports.
@@ -869,7 +871,7 @@ added: v10.10.0
869871
changes:
870872
- version: REPLACEME
871873
pr-url: https://github.com/nodejs/node/pull/40249
872-
description: Added support of import assertions to the
874+
description: Added support for import assertions to the
873875
`importModuleDynamically` parameter.
874876
- version: v15.9.0
875877
pr-url: https://github.com/nodejs/node/pull/35431
@@ -913,7 +915,8 @@ changes:
913915
* `specifier` {string} specifier passed to `import()`
914916
* `function` {Function}
915917
* `importAssertions` {Object} The `"assert"` value passed to the
916-
`optionExpression` optional parameter.
918+
`optionExpression` optional parameter, or an empty object if no value was
919+
provided.
917920
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
918921
recommended in order to take advantage of error tracking, and to avoid
919922
issues with namespaces that contain `then` function exports.
@@ -1091,7 +1094,7 @@ added: v0.3.1
10911094
changes:
10921095
- version: REPLACEME
10931096
pr-url: https://github.com/nodejs/node/pull/40249
1094-
description: Added support of import assertions to the
1097+
description: Added support for import assertions to the
10951098
`importModuleDynamically` parameter.
10961099
- version: v6.3.0
10971100
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1139,7 +1142,8 @@ changes:
11391142
* `specifier` {string} specifier passed to `import()`
11401143
* `script` {vm.Script}
11411144
* `importAssertions` {Object} The `"assert"` value passed to the
1142-
`optionExpression` optional parameter.
1145+
`optionExpression` optional parameter, or an empty object if no value was
1146+
provided.
11431147
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
11441148
recommended in order to take advantage of error tracking, and to avoid
11451149
issues with namespaces that contain `then` function exports.
@@ -1174,7 +1178,7 @@ added: v0.3.1
11741178
changes:
11751179
- version: REPLACEME
11761180
pr-url: https://github.com/nodejs/node/pull/40249
1177-
description: Added support of import assertions to the
1181+
description: Added support for import assertions to the
11781182
`importModuleDynamically` parameter.
11791183
- version: v14.6.0
11801184
pr-url: https://github.com/nodejs/node/pull/34023
@@ -1243,7 +1247,8 @@ changes:
12431247
* `specifier` {string} specifier passed to `import()`
12441248
* `script` {vm.Script}
12451249
* `importAssertions` {Object} The `"assert"` value passed to the
1246-
`optionExpression` optional parameter.
1250+
`optionExpression` optional parameter, or an empty object if no value was
1251+
provided.
12471252
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
12481253
recommended in order to take advantage of error tracking, and to avoid
12491254
issues with namespaces that contain `then` function exports.
@@ -1282,7 +1287,7 @@ added: v0.3.1
12821287
changes:
12831288
- version: REPLACEME
12841289
pr-url: https://github.com/nodejs/node/pull/40249
1285-
description: Added suppoort of import assertions to the
1290+
description: Added support for import assertions to the
12861291
`importModuleDynamically` parameter.
12871292
- version: v6.3.0
12881293
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1328,7 +1333,8 @@ changes:
13281333
* `specifier` {string} specifier passed to `import()`
13291334
* `script` {vm.Script}
13301335
* `importAssertions` {Object} The `"assert"` value passed to the
1331-
`optionExpression` optional parameter.
1336+
`optionExpression` optional parameter, or an empty object if no value was
1337+
provided.
13321338
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
13331339
recommended in order to take advantage of error tracking, and to avoid
13341340
issues with namespaces that contain `then` function exports.

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

src/module_wrap.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,20 @@ static MaybeLocal<Promise> ImportModuleDynamically(
602602
UNREACHABLE();
603603
}
604604

605+
Local<Object> assertions =
606+
Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0);
607+
for (int i = 0; i < import_assertions->Length(); i += 2) {
608+
assertions
609+
->Set(env->context(),
610+
Local<String>::Cast(import_assertions->Get(env->context(), i)),
611+
Local<Value>::Cast(import_assertions->Get(env->context(), i + 1)))
612+
.ToChecked();
613+
}
614+
605615
Local<Value> import_args[] = {
606616
object,
607617
Local<Value>(specifier),
618+
assertions,
608619
};
609620

610621
Local<Value> result;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
// Flags: --experimental-vm-modules
3+
// Flags: --experimental-vm-modules --harmony-import-assertions
44

55
const common = require('../common');
66

@@ -56,6 +56,20 @@ async function test() {
5656
assert.strictEqual(foo.namespace, await globalThis.fooResult);
5757
delete globalThis.fooResult;
5858
}
59+
60+
{
61+
const s = new Script('import("foo", { assert: { key: "value" } })', {
62+
importModuleDynamically: common.mustCall((specifier, wrap, assertion) => {
63+
assert.strictEqual(specifier, 'foo');
64+
assert.strictEqual(wrap, s);
65+
assert.deepStrictEqual(assertion, { __proto__: null, key: 'value' });
66+
return foo;
67+
}),
68+
});
69+
70+
const result = s.runInThisContext();
71+
assert.strictEqual(foo.namespace, await result);
72+
}
5973
}
6074

6175
async function testInvalid() {

0 commit comments

Comments
 (0)