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‎

Lines changed: 2 additions & 3 deletions
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-
asyncfunctionlinker(referencingModule, specifier){
120+
asyncfunctionlinker(specifier, referencingModule){
121121
if (specifier ==='foo'){
122122
returnnewvm.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
importfoofrom'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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Module{
135135
constpromises=[];
136136
wrap.link((specifier)=>{
137137
constp=(async()=>{
138-
constm=awaitlinker(this,specifier);
138+
constm=awaitlinker(specifier,this);
139139
if(!m||!wrapMap.has(m))
140140
thrownewerrors.Error('ERR_VM_MODULE_NOT_MODULE');
141141
if(m.context!==this.context)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async function checkModuleState(){
109109

110110
{
111111
constm=newModule('import "foo"');
112-
awaitm.link(common.mustCall(async(module,specifier)=>{
112+
awaitm.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‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function simple(){
1818

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

21-
awaitbar.link(common.mustCall((module,specifier)=>{
21+
awaitbar.link(common.mustCall((specifier,module)=>{
2222
assert.strictEqual(module,bar);
2323
assert.strictEqual(specifier,'foo');
2424
returnfoo;
@@ -38,7 +38,7 @@ async function depth(){
3838
import ${parentName} from '${parentName}'
3939
export default ${parentName};
4040
`);
41-
awaitmod.link(common.mustCall((module,specifier)=>{
41+
awaitmod.link(common.mustCall((specifier,module)=>{
4242
assert.strictEqual(module,mod);
4343
assert.strictEqual(specifier,parentName);
4444
returnparentModule;
@@ -68,10 +68,10 @@ async function circular(){
6868
return foo;
6969
}
7070
`);
71-
awaitfoo.link(common.mustCall(async(fooModule,fooSpecifier)=>{
71+
awaitfoo.link(common.mustCall(async(fooSpecifier,fooModule)=>{
7272
assert.strictEqual(fooModule,foo);
7373
assert.strictEqual(fooSpecifier,'bar');
74-
awaitbar.link(common.mustCall((barModule,barSpecifier)=>{
74+
awaitbar.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
constmoduleMap=newMap();
113113
constrootModule=newModule(sourceMap.root,{url: 'vm:root'});
114-
asyncfunctionlink(referencingModule,specifier){
114+
asyncfunctionlink(specifier,referencingModule){
115115
if(moduleMap.has(specifier)){
116116
returnmoduleMap.get(specifier);
117117
}

0 commit comments

Comments
(0)