saambarati1@gmail.com | e455672 | 2015-07-19 16:57:44 +0000 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
| 3 | function truth() { |
| 4 | return true; |
| 5 | } |
| 6 | noInline(truth); |
| 7 | |
| 8 | function assert(cond) { |
| 9 | if (!cond) |
| 10 | throw new Error("broke assertion"); |
| 11 | } |
| 12 | noInline(assert); |
| 13 | |
| 14 | // Tests |
| 15 | |
| 16 | |
saambarati1@gmail.com | d5293a6 | 2015-07-27 23:04:43 +0000 | [diff] [blame] | 17 | const NUM_LOOPS = 100; |
saambarati1@gmail.com | e455672 | 2015-07-19 16:57:44 +0000 | [diff] [blame] | 18 | |
| 19 | ;(function () { |
| 20 | function foo() { |
| 21 | const x = 20; |
| 22 | const y = "y"; |
| 23 | try { |
| 24 | assert(x === 20); |
| 25 | assert(y === "y"); |
| 26 | throw "error"; |
| 27 | } catch(e) { |
| 28 | assert(x === 20); |
| 29 | } finally { |
| 30 | assert(x === 20); |
| 31 | assert(y === "y"); |
| 32 | } |
| 33 | |
| 34 | for (let i = 0; i < 1; i++) { |
| 35 | let numFinally = 0; |
| 36 | try { |
| 37 | let a = 40; |
| 38 | let capA = function() { return a; } |
| 39 | assert(capA() === 40); |
| 40 | try { |
| 41 | const b = 41; |
| 42 | const capB = function() { return b; } |
| 43 | assert(capB() === 41); |
| 44 | assert(capA() === 40); |
| 45 | try { |
| 46 | return 20; |
| 47 | } catch(e){ |
| 48 | } finally { |
| 49 | const c = 42; |
| 50 | const capC = function() { return c; } |
| 51 | assert(capC() === 42); |
| 52 | assert(capB() === 41); |
| 53 | assert(capA() === 40); |
| 54 | if (i === 0) { |
| 55 | numFinally++; |
| 56 | } |
| 57 | return 22; |
| 58 | } |
| 59 | } catch(e) { |
| 60 | } finally { |
| 61 | if (i === 0) { |
| 62 | numFinally++; |
| 63 | } |
| 64 | return 23; |
| 65 | } |
| 66 | } catch(e) { |
| 67 | } finally { |
| 68 | if (i === 0) { |
| 69 | numFinally++; |
| 70 | } |
| 71 | assert(numFinally === 3); |
| 72 | return 24; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for (var i = 0; i < NUM_LOOPS; i++) { |
| 78 | assert(foo() === 24); |
| 79 | } |
| 80 | |
| 81 | })(); |
| 82 | |
| 83 | |
| 84 | ;(function () { |
| 85 | function foo() { |
| 86 | for (let i = 0; i < 1; i++) { |
| 87 | let numFinally = 0; |
| 88 | let numErrors = 0; |
| 89 | try { |
| 90 | let a = 40; |
| 91 | let capA = function() { return a; } |
| 92 | assert(capA() === 40); |
| 93 | try { |
| 94 | const b = 41; |
| 95 | const capB = function() { return b; } |
| 96 | assert(capB() === 41); |
| 97 | assert(capA() === 40); |
| 98 | try { |
| 99 | throw "e"; |
| 100 | } catch(e) { |
| 101 | assert(i === 0); |
| 102 | assert(capB() === 41); |
| 103 | assert(capA() === 40); |
| 104 | numErrors++; |
| 105 | throw e; |
| 106 | } finally { |
| 107 | const c = 42; |
| 108 | const capC = function() { return c; } |
| 109 | const local = "local"; |
| 110 | assert(local === "local"); |
| 111 | assert(capC() === 42); |
| 112 | assert(capB() === 41); |
| 113 | assert(capA() === 40); |
| 114 | if (i === 0) { |
| 115 | numFinally++; |
| 116 | } |
| 117 | } |
| 118 | } catch(e) { |
| 119 | assert(i === 0); |
| 120 | assert(capA() === 40); |
| 121 | numErrors++; |
| 122 | const local = "local"; |
| 123 | assert(local === "local"); |
| 124 | } finally { |
| 125 | assert(capA() === 40); |
| 126 | if (i === 0) { |
| 127 | numFinally++; |
| 128 | } |
| 129 | const local = "local"; |
| 130 | assert(local === "local"); |
| 131 | return 23; |
| 132 | } |
| 133 | } catch(e) { |
| 134 | //assert(i === 0); |
| 135 | } finally { |
| 136 | if (i === 0) { |
| 137 | numFinally++; |
| 138 | } |
| 139 | |
| 140 | assert(numFinally === 3); |
| 141 | assert(numErrors === 2); |
| 142 | return 24; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | for (var i = 0; i < NUM_LOOPS; i++) { |
| 148 | assert(foo() === 24); |
| 149 | } |
| 150 | |
| 151 | })(); |
| 152 | |
| 153 | |
| 154 | var d = 100; |
| 155 | ;(function (){ |
| 156 | function foo() { |
| 157 | assert(d === 100); |
| 158 | for (let i = 0; i < 1; i++) { |
| 159 | let numFinally = 0; |
| 160 | let numErrors = 0; |
| 161 | const c = 44; |
| 162 | assert(d === 100); |
| 163 | try { |
| 164 | const d = 45; |
| 165 | if (truth()) { |
| 166 | const a = 20; |
| 167 | const capA = function() { return a; } |
| 168 | assert(capA() === 20); |
| 169 | if (truth()) { |
| 170 | const b = 21; |
| 171 | const e = 48; |
| 172 | const capB = function() { return b; } |
| 173 | assert(capB() === 21); |
| 174 | assert(d === 45); |
| 175 | try { |
| 176 | throw "e"; |
| 177 | } catch(e) { |
| 178 | assert(capA() === 20); |
| 179 | assert(a === 20); |
| 180 | numErrors++; |
| 181 | } finally { |
| 182 | assert(capA() === 20); |
| 183 | assert(e === 48); |
| 184 | numFinally++; |
| 185 | return 30; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } finally { |
| 190 | assert(c === 44); |
| 191 | assert(d === 100); |
| 192 | numFinally++; |
| 193 | assert(numFinally === 2); |
| 194 | assert(numErrors === 1); |
| 195 | return 40; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | for (var i = 0; i < NUM_LOOPS; i++) { |
| 201 | assert(foo() === 40); |
| 202 | } |
| 203 | |
| 204 | })(); |