vm: explain how to share promises between contexts w/ afterEvaluate · nodejs/node@9347ddd · GitHub
Skip to content

Commit 9347ddd

Browse files
ericrannaudtargos
authored andcommitted
vm: explain how to share promises between contexts w/ afterEvaluate
PR-URL: #59801 Fixes: #59541 Refs: https://issues.chromium.org/issues/441679231 Refs: https://groups.google.com/g/v8-dev/c/YIeRg8CUNS8/m/rEQdFuNZAAAJ Refs: https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 44ce971 commit 9347ddd

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

doc/api/vm.md

Lines changed: 63 additions & 0 deletions

test/parallel/test-vm-module-after-evaluate.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const microtaskMode = 'afterEvaluate';
1414

1515
(async () => {
1616
const mustNotCall1 = common.mustNotCall();
17+
const mustNotCall2 = common.mustNotCall();
1718
const mustCall1 = common.mustCall();
1819

1920
const inner = {};
@@ -30,4 +31,41 @@ const microtaskMode = 'afterEvaluate';
3031

3132
// Prior to the fix for Issue 59541, the next statement was never executed.
3233
mustCall1();
34+
35+
await inner.promise;
36+
37+
// This is expected: the await statement above enqueues a (thenable job) task
38+
// onto the inner context microtask queue, but it will not be checkpointed,
39+
// therefore we never make progress.
40+
mustNotCall2();
41+
})().then(common.mustNotCall());
42+
43+
(async () => {
44+
const mustNotCall1 = common.mustNotCall();
45+
const mustCall1 = common.mustCall();
46+
const mustCall2 = common.mustCall();
47+
const mustCall3 = common.mustCall();
48+
49+
const inner = {};
50+
51+
const context = vm.createContext({ inner }, { microtaskMode });
52+
53+
const module = new vm.SourceTextModule(
54+
'inner.promise = Promise.resolve();',
55+
{ context },
56+
);
57+
58+
await module.link(mustNotCall1);
59+
await module.evaluate();
60+
mustCall1();
61+
62+
setImmediate(() => {
63+
mustCall2();
64+
// This will checkpoint the inner context microtask queue, and allow the
65+
// promise from the inner context to be resolved in the outer context.
66+
module.evaluate();
67+
});
68+
69+
await inner.promise;
70+
mustCall3();
3371
})().then(common.mustCall());
Lines changed: 66 additions & 0 deletions

0 commit comments

Comments
 (0)