From 4a40227ba734be98a71a91e765b258540fd57d88 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 19 Jun 2024 15:00:26 -0400 Subject: [PATCH] Implement deterministic collapse of 'await' in 'await using' --- tslib.es6.js | 13 +++++++++---- tslib.es6.mjs | 15 ++++++++++----- tslib.js | 13 +++++++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/tslib.es6.js b/tslib.es6.js index b212c25..82db058 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -327,17 +327,22 @@ export function __disposeResources(env) { 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(); diff --git a/tslib.es6.mjs b/tslib.es6.mjs index 0df91b1..f4cd5db 100644 --- a/tslib.es6.mjs +++ b/tslib.es6.mjs @@ -326,17 +326,22 @@ export function __disposeResources(env) { 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); + fail(e); } } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); if (env.hasError) throw env.error; } return next(); diff --git a/tslib.js b/tslib.js index 0f7c846..e74ecaa 100644 --- a/tslib.js +++ b/tslib.js @@ -374,17 +374,22 @@ var __disposeResources; 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();