Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: use import attributes instead of import assertions #50141

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v10.6.0
pr-url: https://github.com/nodejs/node/pull/20300
Expand Down Expand Up @@ -101,7 +101,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"with"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -615,6 +615,14 @@ The identifier of the current module, as set in the constructor.

### `module.link(linker)`

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/50141
description: The option `extra.assert` is renamed to `extra.attributes`. The
former name is still provided for backward compatibility.
-->

* `linker` {Function}
* `specifier` {string} The specifier of the requested module:
```mjs
Expand All @@ -625,15 +633,14 @@ The identifier of the current module, as set in the constructor.
* `referencingModule` {vm.Module} The `Module` object `link()` is called on.

* `extra` {Object}
* `assert` {Object} The data from the assertion:
<!-- eslint-skip -->
```js
import foo from 'foo' assert { name: 'value' };
// ^^^^^^^^^^^^^^^^^ the assertion
* `attributes` {Object} The data from the attribute:
```mjs
import foo from 'foo' with { name: 'value' };
// ^^^^^^^^^^^^^^^^^ the attribute
```
Per ECMA-262, hosts are expected to ignore assertions that they do not
support, as opposed to, for example, triggering an error if an
unsupported assertion is present.
Per ECMA-262, hosts are expected to trigger an error if an
unsupported attribute is present.
* `assert` {Object} Alias for `extra.attributes`.

* Returns: {vm.Module|Promise}
* Returns: {Promise}
Expand Down Expand Up @@ -732,7 +739,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
-->

Expand Down Expand Up @@ -762,7 +769,7 @@ changes:
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
* `specifier` {string} specifier passed to `import()`
* `module` {vm.Module}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"with"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -977,7 +984,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v15.9.0
pr-url: https://github.com/nodejs/node/pull/35431
Expand Down Expand Up @@ -1021,7 +1028,7 @@ changes:
considered stable.
* `specifier` {string} specifier passed to `import()`
* `function` {Function}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"with"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -1207,7 +1214,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v6.3.0
pr-url: https://github.com/nodejs/node/pull/6635
Expand Down Expand Up @@ -1245,7 +1252,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"with"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -1285,7 +1292,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v14.6.0
pr-url: https://github.com/nodejs/node/pull/34023
Expand Down Expand Up @@ -1344,7 +1351,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"with"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -1388,7 +1395,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v6.3.0
pr-url: https://github.com/nodejs/node/pull/6635
Expand Down Expand Up @@ -1424,7 +1431,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"with"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ class SourceTextModule extends Module {
this[kLink] = async (linker) => {
this.#statusOverride = 'linking';

const promises = this[kWrap].link(async (identifier, assert) => {
const module = await linker(identifier, this, { assert });
const promises = this[kWrap].link(async (identifier, attributes) => {
const module = await linker(identifier, this, { attributes, assert: attributes });
if (module[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-vm-module-link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

// Flags: --experimental-vm-modules
// Flags: --experimental-vm-modules --harmony-import-attributes

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

Expand Down Expand Up @@ -126,12 +126,14 @@ async function circular2() {

async function asserts() {
const m = new SourceTextModule(`
import "foo" assert { n1: 'v1', n2: 'v2' };
import "foo" with { n1: 'v1', n2: 'v2' };
`, { identifier: 'm' });
await m.link((s, r, p) => {
assert.strictEqual(s, 'foo');
assert.strictEqual(r.identifier, 'm');
assert.strictEqual(p.attributes.n1, 'v1');
assert.strictEqual(p.assert.n1, 'v1');
assert.strictEqual(p.attributes.n2, 'v2');
assert.strictEqual(p.assert.n2, 'v2');
return new SourceTextModule('');
});
Expand Down