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

Implement deterministic collapse of 'await' in 'await using' #58929

Merged
merged 2 commits into from
Jun 25, 2024
Merged
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
17 changes: 13 additions & 4 deletions src/compiler/factory/emitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,10 @@ export const addDisposableResourceHelper: UnscopedEmitHelper = {
};

/**
* The `s` variable represents two boolean flags from the `DisposeResources` algorithm:
* - `needsAwait` (`1`) — Indicates that an `await using` for a `null` or `undefined` resource was encountered.
* - `hasAwaited` (`2`) — Indicates that the algorithm has performed an Await.
*
* @internal
*/
export const disposeResourcesHelper: UnscopedEmitHelper = {
Expand All @@ -1430,17 +1434,22 @@ export const disposeResourcesHelper: UnscopedEmitHelper = {
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
46 changes: 46 additions & 0 deletions src/testRunner/unittests/evaluation/usingDeclarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1810,4 +1810,50 @@ describe("unittests:: evaluation:: usingDeclarations", () => {
"exit",
]);
});

it("deterministic collapse of Await", async () => {
const { main, output } = evaluator.evaluateTypeScript(
`
export const output: any[] = [];

let asyncId = 0;
function increment() { asyncId++; }

export async function main() {
// increment asyncId at the top of each turn of the microtask queue
let pending = Promise.resolve();
for (let i = 0; i < 10; i++) {
pending = pending.then(increment);
}

{
using sync1 = { [Symbol.dispose]() { output.push(asyncId); } }; // asyncId: 2
await using async1 = null, async2 = null;
using sync2 = { [Symbol.dispose]() { output.push(asyncId); } }; // asyncId: 1
await using async3 = null, async4 = null;
output.push(asyncId); // asyncId: 0
}

output.push(asyncId); // asyncId: Ideally, 2, but ends up being 4 due to delays imposed by 'await'

await pending; // wait for the remaining 'increment' frames to complete.
}

`,
{ target: ts.ScriptTarget.ES2018 },
);

await main();

assert.deepEqual(output, [
0,
1,
2,

// This really should be 2, but our transpile introduces an extra `await` by necessity to observe the
// result of __disposeResources. The process of adopting the result ends up taking two turns of the
// microtask queue.
4,
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (env.stack.length) {
var rec = env.stack.pop();
while (r = env.stack.pop()) {
try {
var result = rec.dispose && rec.dispose.call(rec.value);
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
Expand Down
Loading