Skip to content

Commit 38cd995

Browse files
committed
fixup! vm: sync-ify SourceTextModule linkage
1 parent d157629 commit 38cd995

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

doc/api/vm.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -464,18 +464,18 @@ const rootModule = new vm.SourceTextModule(`
464464
// array passed to `sourceTextModule.linkRequests(modules)` can be
465465
// empty, however.
466466
//
467-
// Note: This is a contrived example in that the linker creates a new
468-
// "foo" module every time it is called. In a full-fledged module system, a
469-
// cache would probably be used to avoid duplicated modules.
467+
// Note: This is a contrived example in that the resolveAndLinkDependencies
468+
// creates a new "foo" module every time it is called. In a full-fledged
469+
// module system, a cache would probably be used to avoid duplicated modules.
470470

471471
const moduleMap = new Map([
472472
['root', rootModule],
473473
]);
474474

475-
function linker(module) {
475+
function resolveAndLinkDependencies(module) {
476476
const requestedModules = module.moduleRequests.map((request) => {
477-
// In a full-fledged module system, the linker would resolve the
478-
// module with the module cache key `[specifier, attributes]`.
477+
// In a full-fledged module system, the resolveAndLinkDependencies would
478+
// resolve the module with the module cache key `[specifier, attributes]`.
479479
// In this example, we just use the specifier as the key.
480480
const specifier = request.specifier;
481481

@@ -488,7 +488,7 @@ function linker(module) {
488488
`, { context: referencingModule.context });
489489
moduleMap.set(specifier, linkedModule);
490490
// Resolve the dependencies of the new module as well.
491-
linker(requestedModule);
491+
resolveAndLinkDependencies(requestedModule);
492492
}
493493

494494
return requestedModule;
@@ -497,7 +497,7 @@ function linker(module) {
497497
module.linkRequests(requestedModules);
498498
}
499499

500-
linker(rootModule);
500+
resolveAndLinkDependencies(rootModule);
501501
rootModule.instantiate();
502502

503503
// Step 3
@@ -545,18 +545,18 @@ const contextifiedObject = vm.createContext({
545545
// array passed to `sourceTextModule.linkRequests(modules)` can be
546546
// empty, however.
547547
//
548-
// Note: This is a contrived example in that the linker creates a new
549-
// "foo" module every time it is called. In a full-fledged module system, a
550-
// cache would probably be used to avoid duplicated modules.
548+
// Note: This is a contrived example in that the resolveAndLinkDependencies
549+
// creates a new "foo" module every time it is called. In a full-fledged
550+
// module system, a cache would probably be used to avoid duplicated modules.
551551

552552
const moduleMap = new Map([
553553
['root', rootModule],
554554
]);
555555

556-
function linker(module) {
556+
function resolveAndLinkDependencies(module) {
557557
const requestedModules = module.moduleRequests.map((request) => {
558-
// In a full-fledged module system, the linker would resolve the
559-
// module with the module cache key `[specifier, attributes]`.
558+
// In a full-fledged module system, the resolveAndLinkDependencies would
559+
// resolve the module with the module cache key `[specifier, attributes]`.
560560
// In this example, we just use the specifier as the key.
561561
const specifier = request.specifier;
562562

@@ -569,7 +569,7 @@ const contextifiedObject = vm.createContext({
569569
`, { context: referencingModule.context });
570570
moduleMap.set(specifier, linkedModule);
571571
// Resolve the dependencies of the new module as well.
572-
linker(requestedModule);
572+
resolveAndLinkDependencies(requestedModule);
573573
}
574574

575575
return requestedModule;
@@ -578,7 +578,8 @@ const contextifiedObject = vm.createContext({
578578
module.linkRequests(requestedModules);
579579
}
580580

581-
linker(rootModule);
581+
resolveAndLinkDependencies(rootModule);
582+
rootModule.instantiate();
582583

583584
// Step 3
584585
//
@@ -824,8 +825,9 @@ const module = new vm.SourceTextModule(
824825
meta.prop = {};
825826
},
826827
});
827-
// Since module has no dependencies, the linker function will never be called.
828-
await module.link(() => {});
828+
// The module has an empty `moduleRequests` array.
829+
module.linkRequests([]);
830+
module.instantiate();
829831
await module.evaluate();
830832
831833
// Now, Object.prototype.secret will be equal to 42.
@@ -851,8 +853,9 @@ const contextifiedObject = vm.createContext({ secret: 42 });
851853
meta.prop = {};
852854
},
853855
});
854-
// Since module has no dependencies, the linker function will never be called.
855-
await module.link(() => {});
856+
// The module has an empty `moduleRequests` array.
857+
module.linkRequests([]);
858+
module.instantiate();
856859
await module.evaluate();
857860
// Now, Object.prototype.secret will be equal to 42.
858861
//
@@ -928,7 +931,8 @@ added: REPLACEME
928931
Instantiate the module with the linked requested modules.
929932
930933
This resolves the imported bindings of the module, including re-exported
931-
binding names.
934+
binding names. When there are any bindings that cannot be resolved,
935+
an error would be thrown synchronously.
932936
933937
If the requested modules include cyclic dependencies, the
934938
[`sourceTextModule.linkRequests(modules)`][] method must be called on all
@@ -953,9 +957,10 @@ The order of the module instances in the `modules` array should correspond to th
953957
954958
If the module has no dependencies, the `modules` array can be empty.
955959
956-
Composing `sourceTextModule.moduleRequests` and `sourceTextModule.link()`,
957-
this acts similar to [HostLoadImportedModule][] and [FinishLoadingImportedModule][]
958-
abstract operations in the ECMAScript specification, respectively.
960+
Users can use `sourceTextModule.moduleRequests` to implement the host-defined
961+
[HostLoadImportedModule][] abstract operation in the ECMAScript specification,
962+
and using `sourceTextModule.linkRequests()` to invoke specification defined
963+
[FinishLoadingImportedModule][], on the module with all dependencies in a batch.
959964
960965
It's up to the creator of the `SourceTextModule` to determine if the resolution
961966
of the dependencies is synchronous or asynchronous.

src/module_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void ModuleCacheKey::MemoryInfo(MemoryTracker* tracker) const {
7575
std::string ModuleCacheKey::ToString() const {
7676
std::string result = "ModuleCacheKey(\"" + specifier + "\"";
7777
if (!import_attributes.empty()) {
78-
result += " [";
78+
result += ", {";
7979
bool first = true;
8080
for (const auto& attr : import_attributes) {
8181
if (first) {
@@ -85,7 +85,7 @@ std::string ModuleCacheKey::ToString() const {
8585
}
8686
result += attr.first + ": " + attr.second;
8787
}
88-
result += "]";
88+
result += "}";
8989
}
9090
result += ")";
9191
return result;

0 commit comments

Comments
 (0)