suppressed-error-handling-loop.js (2879B)
1 // |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management 2 3 load(libdir + "asserts.js"); 4 5 { 6 const disposed = []; 7 const errorsToThrow = [new Error("test1"), new Error("test2")]; 8 function testDisposedWithThrowInOrdinaryLoop() { 9 const disposables = [ 10 { 11 [Symbol.dispose]() { 12 disposed.push(1); 13 throw errorsToThrow[0]; 14 } 15 } 16 ]; 17 for (let i = 0; i < 5; i++) { 18 using x = disposables[i]; 19 throw errorsToThrow[1]; 20 } 21 } 22 assertSuppressionChain(testDisposedWithThrowInOrdinaryLoop, errorsToThrow); 23 assertArrayEq(disposed, [1]); 24 } 25 26 { 27 const disposed = []; 28 const errorsToThrow = [new Error("test1"), new Error("test2")]; 29 function testDisposedWithThrowInLoopRequiringIteratorClose() { 30 const disposables = [ 31 { 32 [Symbol.dispose]() { 33 disposed.push(1); 34 throw errorsToThrow[0]; 35 } 36 } 37 ] 38 for (const d of disposables) { 39 using x = d; 40 throw errorsToThrow[1]; 41 } 42 } 43 assertSuppressionChain(testDisposedWithThrowInLoopRequiringIteratorClose, errorsToThrow); 44 assertArrayEq(disposed, [1]); 45 } 46 47 { 48 const values = []; 49 const errorsToThrow = [new Error("test1"), new Error("test2")]; 50 function testDisposedWithThrowInLoopWithCustomIterable() { 51 const disposables = [ 52 { 53 val: "a", 54 [Symbol.dispose]() { 55 values.push(this.val); 56 } 57 }, 58 { 59 val: "b", 60 [Symbol.dispose]() { 61 values.push(this.val); 62 throw errorsToThrow[0]; 63 } 64 }, 65 ]; 66 const iterable = { 67 [Symbol.iterator]() { 68 let i = 0; 69 return { 70 next() { 71 if (i === disposables.length) { 72 return { done: true }; 73 } 74 return { value: disposables[i++], done: false }; 75 }, 76 return() { 77 values.push("return"); 78 return { done: true }; 79 } 80 } 81 } 82 } 83 for (using d of iterable) { 84 if (d.val === "b") { 85 throw errorsToThrow[1]; 86 } 87 } 88 } 89 assertSuppressionChain(testDisposedWithThrowInLoopWithCustomIterable, errorsToThrow); 90 assertArrayEq(values, ["a", "b", "return"]); 91 } 92 93 { 94 const disposed = []; 95 const errorsToThrow = [new Error("test1"), new Error("test2"), new Error("test3"), new Error("test4")]; 96 function testDisposeWithThrowInForOfLoop() { 97 const d1 = { 98 [Symbol.dispose]() { 99 disposed.push(1); 100 throw errorsToThrow[0]; 101 } 102 } 103 const d2 = { 104 [Symbol.dispose]() { 105 disposed.push(2); 106 throw errorsToThrow[1]; 107 } 108 } 109 110 for (using d of [d1, d2]) { 111 throw errorsToThrow[2]; 112 } 113 } 114 assertSuppressionChain(testDisposeWithThrowInForOfLoop, [errorsToThrow[0], errorsToThrow[2]]); 115 assertArrayEq(disposed, [1]); 116 }