Skip to content

Commit 91e224b

Browse files
committed
vm: add import assertion support
1 parent b5f5c46 commit 91e224b

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

doc/api/vm.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@ Record][]s in the ECMAScript specification.
541541
import foo from 'foo';
542542
// ^^^^^ the module specifier
543543
```
544+
* `extra` {Object}
545+
* `assert` {Object} The data from the assertion:
546+
<!-- eslint-skip -->
547+
```js
548+
import foo from 'foo' assert { name: 'value' };
549+
// ^^^^^^^^^^^^^^^^^ the assertion
550+
```
551+
Per ECMA-262, hosts are expected to ignore assertions that they do not
552+
support, as opposed to, for example, triggering an error if an supported
553+
assertion is present.
544554

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

lib/internal/vm/module.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,19 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
279279
Utf8Value specifier_utf8(env->isolate(), specifier);
280280
std::string specifier_std(*specifier_utf8, specifier_utf8.length());
281281

282+
Local<Object> assertions = Object::New(isolate);
283+
Local<FixedArray> raw_assertions = module_request->GetImportAssertions();
284+
for (int i = 0; i < raw_assertions->Length(); i += 3) {
285+
assertions
286+
->Set(env->context(),
287+
Local<String>::Cast(raw_assertions->Get(env->context(), i)),
288+
Local<Value>::Cast(raw_assertions->Get(env->context(), i + 1)))
289+
.ToChecked();
290+
}
291+
282292
Local<Value> argv[] = {
283-
specifier
293+
specifier,
294+
assertions,
284295
};
285296

286297
MaybeLocal<Value> maybe_resolve_return_value =

test/parallel/test-vm-module-link.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

@@ -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)