| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e2b6bdc commit 389a24b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -604,23 +604,13 @@ void ModuleWrap::GetModuleRequests(const FunctionCallbackInfo<Value>& args) { | |||
| 604 | 604 | ||
| 605 | 605 | // moduleWrap.link(moduleWraps) | |
| 606 | 606 | void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) { | |
| 607 | - Realm* realm = Realm::GetCurrent(args); | ||
| 608 | 607 | Isolate* isolate = args.GetIsolate(); | |
| 609 | 608 | ||
| 610 | 609 | ModuleWrap* dependent; | |
| 611 | 610 | ASSIGN_OR_RETURN_UNWRAP(&dependent, args.This()); | |
| 612 | 611 | ||
| 613 | 612 | CHECK_EQ(args.Length(), 1); | |
| 614 | 613 | ||
| 615 | - Local<Data> linked_requests = | ||
| 616 | - args.This()->GetInternalField(kLinkedRequestsSlot); | ||
| 617 | - if (linked_requests->IsValue() && | ||
| 618 | - !linked_requests.As<Value>()->IsUndefined()) { | ||
| 619 | - // If the module is already linked, we should not link it again. | ||
| 620 | - THROW_ERR_VM_MODULE_LINK_FAILURE(realm->env(), "module is already linked"); | ||
| 621 | - return; | ||
| 622 | - } | ||
| 623 | - | ||
| 624 | 614 | Local<FixedArray> requests = | |
| 625 | 615 | dependent->module_.Get(isolate)->GetModuleRequests(); | |
| 626 | 616 | Local<Array> modules = args[0].As<Array>(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,82 @@ | |||
| 1 | + // Flags: --experimental-vm-modules | ||
| 2 | + | ||
| 3 | + 'use strict'; | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + const vm = require('vm'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + | ||
| 9 | + // This test verifies that a module can be returned multiple | ||
| 10 | + // times in the linker function in `module.link(linker)`. | ||
| 11 | + // `module.link(linker)` should handle the race condition of | ||
| 12 | + // `module.status` when the linker function is asynchronous. | ||
| 13 | + | ||
| 14 | + // Regression of https://github.com/nodejs/node/issues/59480 | ||
| 15 | + | ||
| 16 | + const sources = { | ||
| 17 | + './index.js': ` | ||
| 18 | + import foo from "./foo.js"; | ||
| 19 | + import shared from "./shared.js"; | ||
| 20 | + export default { | ||
| 21 | + foo, | ||
| 22 | + shared | ||
| 23 | + }; | ||
| 24 | + `, | ||
| 25 | + './foo.js': ` | ||
| 26 | + import shared from "./shared.js"; | ||
| 27 | + export default { | ||
| 28 | + name: "foo" | ||
| 29 | + }; | ||
| 30 | + `, | ||
| 31 | + './shared.js': ` | ||
| 32 | + export default { | ||
| 33 | + name: "shared", | ||
| 34 | + }; | ||
| 35 | + `, | ||
| 36 | + }; | ||
| 37 | + | ||
| 38 | + const moduleCache = new Map(); | ||
| 39 | + | ||
| 40 | + function getModuleInstance(identifier) { | ||
| 41 | + let module = moduleCache.get(identifier); | ||
| 42 | + | ||
| 43 | + if (!module) { | ||
| 44 | + module = new vm.SourceTextModule(sources[identifier], { | ||
| 45 | + identifier, | ||
| 46 | + }); | ||
| 47 | + moduleCache.set(identifier, module); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + return module; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + async function esmImport(identifier) { | ||
| 54 | + const module = getModuleInstance(identifier); | ||
| 55 | + const requests = []; | ||
| 56 | + | ||
| 57 | + await module.link(async (specifier, referrer) => { | ||
| 58 | + requests.push([specifier, referrer.identifier]); | ||
| 59 | + // Use `Promise.resolve` to defer a tick to create a race condition on | ||
| 60 | + // `module.status` when a module is being imported several times. | ||
| 61 | + return Promise.resolve(getModuleInstance(specifier)); | ||
| 62 | + }); | ||
| 63 | + | ||
| 64 | + await module.evaluate(); | ||
| 65 | + return [module.namespace, requests]; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + async function test() { | ||
| 69 | + const { 0: mod, 1: requests } = await esmImport('./index.js'); | ||
| 70 | + assert.strictEqual(mod.default.foo.name, 'foo'); | ||
| 71 | + assert.strictEqual(mod.default.shared.name, 'shared'); | ||
| 72 | + | ||
| 73 | + // Assert that there is no duplicated requests. | ||
| 74 | + assert.deepStrictEqual(requests, [ | ||
| 75 | + // [specifier, referrer] | ||
| 76 | + ['./foo.js', './index.js'], | ||
| 77 | + ['./shared.js', './index.js'], | ||
| 78 | + ['./shared.js', './foo.js'], | ||
| 79 | + ]); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + test().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments