disposal-during-throw-loop.js (1741B)
1 // |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management 2 3 load(libdir + "asserts.js"); 4 5 const duringThrow = []; 6 function testDisposalInLoopWithThrow() { 7 const disposables = [ 8 { 9 val: "a", 10 [Symbol.dispose]() { 11 duringThrow.push(this.val); 12 } 13 }, 14 { 15 val: "b", 16 [Symbol.dispose]() { 17 duringThrow.push(this.val); 18 } 19 }, 20 { 21 val: "c", 22 [Symbol.dispose]() { 23 duringThrow.push(this.val); 24 } 25 } 26 ]; 27 for (using d of disposables) { 28 if (d.val === "b") { 29 throw new Error("err 1"); 30 } 31 } 32 } 33 assertThrowsInstanceOf(testDisposalInLoopWithThrow, Error); 34 assertArrayEq(duringThrow, ["a", "b"]); 35 36 const duringThrow2 = []; 37 function testDisposalInLoopWithIteratorClose() { 38 const disposables = [ 39 { 40 val: "a", 41 [Symbol.dispose]() { 42 duringThrow2.push(this.val); 43 } 44 }, 45 { 46 val: "b", 47 [Symbol.dispose]() { 48 duringThrow2.push(this.val); 49 } 50 }, 51 { 52 val: "c", 53 [Symbol.dispose]() { 54 duringThrow2.push(this.val); 55 } 56 } 57 ]; 58 const iterable = { 59 [Symbol.iterator]() { 60 let i = 0; 61 return { 62 next() { 63 if (i === disposables.length) { 64 return { done: true }; 65 } 66 return { value: disposables[i++], done: false }; 67 }, 68 return() { 69 duringThrow2.push("return"); 70 return { done: true }; 71 } 72 } 73 } 74 } 75 for (using d of iterable) { 76 if (d.val === "b") { 77 throw new Error("err 2"); 78 } 79 } 80 } 81 assertThrowsInstanceOf(testDisposalInLoopWithIteratorClose, Error); 82 assertArrayEq(duringThrow2, ["a", "b", "return"]);