Skip to content

Commit d6b0b14

Browse files
committed
vm: add import assertion support
1 parent 3bee6d8 commit d6b0b14

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

doc/api/vm.md

+10
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,16 @@ The identifier of the current module, as set in the constructor.
617617
import foo from 'foo';
618618
// ^^^^^ the module specifier
619619
```
620+
* `extra` {Object}
621+
* `assert` {Object} The data from the assertion:
622+
<!-- eslint-skip -->
623+
```js
624+
import foo from 'foo' assert { name: 'value' };
625+
// ^^^^^^^^^^^^^^^^^ the assertion
626+
```
627+
Per ECMA-262, hosts are expected to ignore assertions that they do not
628+
support, as opposed to, for example, triggering an error if an
629+
unsupported assertion is present.
620630

621631
* `referencingModule` {vm.Module} The `Module` object `link()` is called on.
622632
* Returns: {vm.Module|Promise}

lib/internal/vm/module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ class SourceTextModule extends Module {
308308
this[kLink] = async (linker) => {
309309
this.#statusOverride = 'linking';
310310

311-
const promises = this[kWrap].link(async (identifier) => {
312-
const module = await linker(identifier, this);
311+
const promises = this[kWrap].link(async (identifier, assert) => {
312+
const module = await linker(identifier, this, { assert });
313313
if (module[kWrap] === undefined) {
314314
throw new ERR_VM_MODULE_NOT_MODULE();
315315
}

src/module_wrap.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,20 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
286286
Utf8Value specifier_utf8(env->isolate(), specifier);
287287
std::string specifier_std(*specifier_utf8, specifier_utf8.length());
288288

289+
Local<FixedArray> raw_assertions = module_request->GetImportAssertions();
290+
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+
Local<String>::Cast(raw_assertions->Get(env->context(), i)),
296+
Local<Value>::Cast(raw_assertions->Get(env->context(), i + 1)))
297+
.ToChecked();
298+
}
299+
289300
Local<Value> argv[] = {
290-
specifier
301+
specifier,
302+
assertions,
291303
};
292304

293305
MaybeLocal<Value> maybe_resolve_return_value =

test/parallel/test-vm-module-link.js

+15-1
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

@@ -124,12 +124,26 @@ async function circular2() {
124124
await rootModule.evaluate();
125125
}
126126

127+
async function asserts() {
128+
const m = new SourceTextModule(`
129+
import "foo" assert { n1: 'v1', n2: 'v2' };
130+
`, { identifier: 'm' });
131+
await m.link((s, r, p) => {
132+
assert.strictEqual(s, 'foo');
133+
assert.strictEqual(r.identifier, 'm');
134+
assert.strictEqual(p.assert.n1, 'v1');
135+
assert.strictEqual(p.assert.n2, 'v2');
136+
return new SourceTextModule('');
137+
});
138+
}
139+
127140
const finished = common.mustCall();
128141

129142
(async function main() {
130143
await simple();
131144
await depth();
132145
await circular();
133146
await circular2();
147+
await asserts();
134148
finished();
135149
})().then(common.mustCall());

0 commit comments

Comments
 (0)