Skip to content

Commit

Permalink
Fix handling of sync error in @@asyncDispose (#15705)
Browse files Browse the repository at this point in the history
* Reduce recursion when disposing sync resources

* Fix handling of sync error in `@@asyncDispose`

* Make the generated helper slightly smaller
  • Loading branch information
nicolo-ribaudo authored Jun 19, 2023
1 parent 1765030 commit 0679329
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default Object.freeze({
),
dispose: helper(
"7.22.0",
'function dispose_SuppressedError(suppressed,error){return"undefined"!=typeof SuppressedError?dispose_SuppressedError=SuppressedError:(dispose_SuppressedError=function(suppressed,error){this.suppressed=suppressed,this.error=error,this.stack=(new Error).stack},dispose_SuppressedError.prototype=Object.create(Error.prototype,{constructor:{value:dispose_SuppressedError,writable:!0,configurable:!0}})),new dispose_SuppressedError(suppressed,error)}export default function _dispose(stack,error,hasError){function next(){if(0!==stack.length){var r=stack.pop();if(r.a)return Promise.resolve(r.d.call(r.v)).then(next,err);try{r.d.call(r.v)}catch(e){return err(e)}return next()}if(hasError)throw error}function err(e){return error=hasError?new dispose_SuppressedError(e,error):e,hasError=!0,next()}return next()}',
'function dispose_SuppressedError(suppressed,error){return"undefined"!=typeof SuppressedError?dispose_SuppressedError=SuppressedError:(dispose_SuppressedError=function(suppressed,error){this.suppressed=suppressed,this.error=error,this.stack=(new Error).stack},dispose_SuppressedError.prototype=Object.create(Error.prototype,{constructor:{value:dispose_SuppressedError,writable:!0,configurable:!0}})),new dispose_SuppressedError(suppressed,error)}export default function _dispose(stack,error,hasError){function next(){for(;stack.length>0;)try{var r=stack.pop(),p=r.d.call(r.v);if(r.a)return Promise.resolve(p).then(next,err)}catch(e){return err(e)}if(hasError)throw error}function err(e){return error=hasError?new dispose_SuppressedError(e,error):e,hasError=!0,next()}return next()}',
),
iterableToArrayLimit: helper(
"7.0.0-beta.0",
Expand Down
23 changes: 9 additions & 14 deletions packages/babel-helpers/src/helpers/dispose.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,16 @@ function dispose_SuppressedError(suppressed, error) {

export default function _dispose(stack, error, hasError) {
function next() {
if (stack.length === 0) {
if (hasError) throw error;
return;
while (stack.length > 0) {
try {
var r = stack.pop();
var p = r.d.call(r.v);
if (r.a) return Promise.resolve(p).then(next, err);
} catch (e) {
return err(e);
}
}

var r = stack.pop();
if (r.a) {
return Promise.resolve(r.d.call(r.v)).then(next, err);
}
try {
r.d.call(r.v);
} catch (e) {
return err(e);
}
return next();
if (hasError) throw error;
}

function err(e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ return async function () {
let disposed = false;
let beforeEnd;

const err = {};
const err1 = {};
const err2 = {};
let thrown;

try {
await using x = {
[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]() {
throw err;
throw err1;
}
};
throw err2;
} catch (e) {
thrown = e;
}

expect(thrown).toBe(err);
expect(thrown.suppressed).toBe(err1);
expect(thrown.error).toBe(err2);
}();
15 changes: 6 additions & 9 deletions packages/babel-runtime-corejs3/helpers/dispose.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ function dispose_SuppressedError(suppressed, error) {
}
function _dispose(stack, error, hasError) {
function next() {
if (0 !== stack.length) {
var r = stack.pop();
if (r.a) return _Promise.resolve(r.d.call(r.v)).then(next, err);
try {
r.d.call(r.v);
} catch (e) {
return err(e);
}
return next();
for (; stack.length > 0;) try {
var r = stack.pop(),
p = r.d.call(r.v);
if (r.a) return _Promise.resolve(p).then(next, err);
} catch (e) {
return err(e);
}
if (hasError) throw error;
}
Expand Down
15 changes: 6 additions & 9 deletions packages/babel-runtime/helpers/dispose.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ function dispose_SuppressedError(suppressed, error) {
}
function _dispose(stack, error, hasError) {
function next() {
if (0 !== stack.length) {
var r = stack.pop();
if (r.a) return Promise.resolve(r.d.call(r.v)).then(next, err);
try {
r.d.call(r.v);
} catch (e) {
return err(e);
}
return next();
for (; stack.length > 0;) try {
var r = stack.pop(),
p = r.d.call(r.v);
if (r.a) return Promise.resolve(p).then(next, err);
} catch (e) {
return err(e);
}
if (hasError) throw error;
}
Expand Down

0 comments on commit 0679329

Please sign in to comment.