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: fix link with invalid graph #37597

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 25 additions & 18 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ const kNoError = Symbol('kNoError');
class SourceTextModule extends Module {
#error = kNoError;
#statusOverride;
#waitingLinkPromises;

constructor(sourceText, options = {}) {
validateString(sourceText, 'sourceText');
Expand Down Expand Up @@ -308,31 +309,37 @@ class SourceTextModule extends Module {
this[kLink] = async (linker) => {
this.#statusOverride = 'linking';

const promises = this[kWrap].link(async (identifier) => {
const module = await linker(identifier, this);
if (module[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
if (module.context !== this.context) {
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
}
if (module.status === 'errored') {
throw new ERR_VM_MODULE_LINKING_ERRORED();
}
if (module.status === 'unlinked') {
await module[kLink](linker);
}
return module[kWrap];
});

try {
const promises = this[kWrap].link(async (identifier) => {
const module = await linker(identifier, this);
if (module[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
if (module.context !== this.context) {
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
}
if (module.status === 'errored') {
throw new ERR_VM_MODULE_LINKING_ERRORED();
}
if (module.status === 'unlinked') {
await module[kLink](linker);
}
if (module.status === 'linking') {
await module.#waitingLinkPromises;
}
return module[kWrap];
});

if (promises !== undefined) {
await PromiseAll(promises);
assert(this.#waitingLinkPromises === undefined);
this.#waitingLinkPromises = PromiseAll(promises);
}
await this.#waitingLinkPromises;
} catch (e) {
this.#error = e;
throw e;
} finally {
this.#waitingLinkPromises = undefined;
this.#statusOverride = undefined;
}
};
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-vm-module-link-37426.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Flags: --experimental-vm-modules

'use strict';

const common = require('../common');
const { SourceTextModule, SyntheticModule } = require('vm');

const tm = new SourceTextModule('import "a"');

tm
.link(async () => {
const tm2 = new SourceTextModule('import "b"');
tm2.link(async () => {
const sm = new SyntheticModule([], () => {});
await sm.link(() => {});
await sm.evaluate();
return sm;
}).then(() => tm2.evaluate());
return tm2;
})
.then(() => tm.evaluate())
.then(common.mustCall())
.catch(common.mustNotCall());