Skip to content

Commit 02afdbc

Browse files
devsnekMylesBorins
authored andcommitted
vm: flip Module#link's signature
The specifier parameter is deemed to be more essential than referencingModule. Flipping the parameter order allows developers to write simple linker functions that only take in a specifier. PR-URL: #18471 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent de3231c commit 02afdbc

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

doc/api/vm.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 });
117117
// "foo" module every time it is called. In a full-fledged module system, a
118118
// cache would probably be used to avoid duplicated modules.
119119

120-
async function linker(referencingModule, specifier) {
120+
async function linker(specifier, referencingModule) {
121121
if (specifier === 'foo') {
122122
return new vm.Module(`
123123
// The "secret" variable refers to the global variable we added to
@@ -319,14 +319,13 @@ can only be called once per module.
319319

320320
Two parameters will be passed to the `linker` function:
321321

322-
- `referencingModule` The `Module` object `link()` is called on.
323322
- `specifier` The specifier of the requested module:
324-
325323
<!-- eslint-skip -->
326324
```js
327325
import foo from 'foo';
328326
// ^^^^^ the module specifier
329327
```
328+
- `referencingModule` The `Module` object `link()` is called on.
330329

331330
The function is expected to return a `Module` object or a `Promise` that
332331
eventually resolves to a `Module` object. The returned `Module` must satisfy the

lib/internal/vm/Module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Module {
135135
const promises = [];
136136
wrap.link((specifier) => {
137137
const p = (async () => {
138-
const m = await linker(this, specifier);
138+
const m = await linker(specifier, this);
139139
if (!m || !wrapMap.has(m))
140140
throw new errors.Error('ERR_VM_MODULE_NOT_MODULE');
141141
if (m.context !== this.context)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async function checkModuleState() {
109109

110110
{
111111
const m = new Module('import "foo";');
112-
await m.link(common.mustCall(async (module, specifier) => {
112+
await m.link(common.mustCall(async (specifier, module) => {
113113
assert.strictEqual(module, m);
114114
assert.strictEqual(specifier, 'foo');
115115
assert.strictEqual(m.linkingStatus, 'linking');

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function simple() {
1818

1919
assert.deepStrictEqual(bar.dependencySpecifiers, ['foo']);
2020

21-
await bar.link(common.mustCall((module, specifier) => {
21+
await bar.link(common.mustCall((specifier, module) => {
2222
assert.strictEqual(module, bar);
2323
assert.strictEqual(specifier, 'foo');
2424
return foo;
@@ -38,7 +38,7 @@ async function depth() {
3838
import ${parentName} from '${parentName}';
3939
export default ${parentName};
4040
`);
41-
await mod.link(common.mustCall((module, specifier) => {
41+
await mod.link(common.mustCall((specifier, module) => {
4242
assert.strictEqual(module, mod);
4343
assert.strictEqual(specifier, parentName);
4444
return parentModule;
@@ -68,10 +68,10 @@ async function circular() {
6868
return foo;
6969
}
7070
`);
71-
await foo.link(common.mustCall(async (fooModule, fooSpecifier) => {
71+
await foo.link(common.mustCall(async (fooSpecifier, fooModule) => {
7272
assert.strictEqual(fooModule, foo);
7373
assert.strictEqual(fooSpecifier, 'bar');
74-
await bar.link(common.mustCall((barModule, barSpecifier) => {
74+
await bar.link(common.mustCall((barSpecifier, barModule) => {
7575
assert.strictEqual(barModule, bar);
7676
assert.strictEqual(barSpecifier, 'foo');
7777
assert.strictEqual(foo.linkingStatus, 'linking');
@@ -111,7 +111,7 @@ async function circular2() {
111111
};
112112
const moduleMap = new Map();
113113
const rootModule = new Module(sourceMap.root, { url: 'vm:root' });
114-
async function link(referencingModule, specifier) {
114+
async function link(specifier, referencingModule) {
115115
if (moduleMap.has(specifier)) {
116116
return moduleMap.get(specifier);
117117
}

0 commit comments

Comments
 (0)