| 1 |
// |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management |
| 2 |
|
| 3 |
load(libdir + "asserts.js"); |
| 4 |
|
| 5 |
{ |
| 6 |
const values = []; |
| 7 |
const errorsToThrow = [new Error("test1"), new Error("test2")]; |
| 8 |
async function* gen() { |
| 9 |
await using d = { |
| 10 |
value: "d", |
| 11 |
[Symbol.asyncDispose]() { |
| 12 |
values.push(this.value); |
| 13 |
} |
| 14 |
} |
| 15 |
yield await Promise.resolve("a"); |
| 16 |
yield await Promise.resolve("b"); |
| 17 |
await using c = { |
| 18 |
value: "c", |
| 19 |
[Symbol.asyncDispose]() { |
| 20 |
values.push(this.value); |
| 21 |
throw errorsToThrow[0]; // This error will suppress the error thrown below. |
| 22 |
} |
| 23 |
} |
| 24 |
throw errorsToThrow[1]; // This error will be suppressed during disposal. |
| 25 |
} |
| 26 |
|
| 27 |
async function testDisposeWithThrowAndPendingException() { |
| 28 |
let x = gen(); |
| 29 |
values.push((await x.next()).value); |
| 30 |
values.push((await x.next()).value); |
| 31 |
await x.next(); |
| 32 |
} |
| 33 |
let e = null; |
| 34 |
testDisposeWithThrowAndPendingException().catch((err) => { e = err; }); |
| 35 |
drainJobQueue(); |
| 36 |
assertSuppressionChain(() => { throw e; }, errorsToThrow); |
| 37 |
assertArrayEq(values, ["a", "b", "c", "d"]); |
| 38 |
} |
| 39 |
|
| 40 |
{ |
| 41 |
const values = []; |
| 42 |
const errorsToThrow = [new Error("test1"), new Error("test2")]; |
| 43 |
async function* gen() { |
| 44 |
await using c = { |
| 45 |
value: "c", |
| 46 |
[Symbol.asyncDispose]() { |
| 47 |
values.push(this.value); |
| 48 |
throw errorsToThrow[0]; |
| 49 |
} |
| 50 |
} |
| 51 |
yield await Promise.resolve("a"); |
| 52 |
yield await Promise.resolve("b"); |
| 53 |
return; |
| 54 |
} |
| 55 |
async function testDisposeWithThrowAndForcedThrowInAsyncGenerator() { |
| 56 |
let x = gen(); |
| 57 |
values.push((await x.next()).value); |
| 58 |
await x.throw(errorsToThrow[1]); |
| 59 |
} |
| 60 |
let e = null; |
| 61 |
testDisposeWithThrowAndForcedThrowInAsyncGenerator().catch((err) => { e = err; }); |
| 62 |
drainJobQueue(); |
| 63 |
assertSuppressionChain(() => { throw e; }, errorsToThrow); |
| 64 |
assertArrayEq(values, ["a", "c"]); |
| 65 |
} |