saambarati1@gmail.com | e455672 | 2015-07-19 16:57:44 +0000 | [diff] [blame] | 1 | "use strict"; |
| 2 | function truth() { |
| 3 | return true; |
| 4 | } |
| 5 | noInline(truth); |
| 6 | |
| 7 | function assert(cond) { |
| 8 | if (!cond) |
| 9 | throw new Error("broke assertion"); |
| 10 | } |
| 11 | noInline(assert); |
| 12 | function shouldThrowInvalidConstAssignment(f) { |
| 13 | var threw = false; |
| 14 | try { |
| 15 | f(); |
| 16 | } catch(e) { |
| 17 | if (e.name.indexOf("TypeError") !== -1 && e.message.indexOf("readonly") !== -1) |
| 18 | threw = true; |
| 19 | } |
| 20 | assert(threw); |
| 21 | } |
| 22 | noInline(shouldThrowInvalidConstAssignment); |
| 23 | |
| 24 | |
| 25 | // ========== tests below =========== |
| 26 | |
saambarati1@gmail.com | d5293a6 | 2015-07-27 23:04:43 +0000 | [diff] [blame] | 27 | const NUM_LOOPS = 1000; |
saambarati1@gmail.com | e455672 | 2015-07-19 16:57:44 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | ;(function() { |
| 31 | function foo() { |
| 32 | const x = 40; |
| 33 | const {y} = {y: 50}, [z] = [60]; |
| 34 | assert(x === 40); |
| 35 | assert(y === 50); |
| 36 | assert(z === 60); |
| 37 | } |
| 38 | function bar() { |
| 39 | const x = 40; |
| 40 | const {y} = {y: 50}, [z] = [60]; |
| 41 | function capture() { return x + y + z; } |
| 42 | assert(x === 40); |
| 43 | assert(y === 50); |
| 44 | assert(z === 60); |
| 45 | assert(capture() === 150); |
| 46 | if (truth()) { |
| 47 | const x = "x"; |
| 48 | assert(x === "x"); |
| 49 | if (truth()) { |
| 50 | let x = 100; |
| 51 | const y = 200; |
| 52 | assert(x === 100); |
| 53 | assert(y === 200); |
| 54 | |
| 55 | x = 10; |
| 56 | assert(x === 10); |
| 57 | } |
| 58 | assert(x === "x"); |
| 59 | } |
| 60 | assert(x === 40); |
| 61 | } |
| 62 | function baz() { |
| 63 | let y = 10; |
| 64 | function sideEffects() { |
| 65 | y = 20; |
| 66 | } |
| 67 | |
| 68 | const x = 10; |
| 69 | try { |
| 70 | x = sideEffects(); |
| 71 | } catch(e) {} |
| 72 | assert(y === 20); |
| 73 | assert(x === 10); |
| 74 | |
| 75 | try { |
| 76 | x = y = 30; |
| 77 | } catch(e) {} |
| 78 | assert(y === 30); |
| 79 | assert(x === 10); |
| 80 | } |
| 81 | function taz() { |
| 82 | let y = 10; |
| 83 | let z; |
| 84 | function sideEffects() { |
| 85 | y = 20; |
| 86 | return ["hello", "world"]; |
| 87 | } |
| 88 | |
| 89 | const x = 10; |
| 90 | try { |
| 91 | [z, x] = sideEffects(); |
| 92 | } catch(e) {} |
| 93 | assert(y === 20); |
| 94 | assert(x === 10); |
| 95 | assert(z === "hello"); |
| 96 | } |
| 97 | function jaz() { |
| 98 | const x = "x"; |
| 99 | function capX() { return x; } |
| 100 | assert(x === "x"); |
| 101 | assert(capX() === "x"); |
| 102 | if (truth()) { |
| 103 | let y = 40; |
| 104 | let capY = function() { return y; } |
| 105 | assert(x === "x"); |
| 106 | assert(capX() === "x"); |
| 107 | } |
| 108 | assert(x === "x"); |
| 109 | assert(capX() === "x"); |
| 110 | } |
| 111 | for (var i = 0; i < NUM_LOOPS; i++) { |
| 112 | foo(); |
| 113 | bar(); |
| 114 | baz(); |
| 115 | jaz(); |
| 116 | } |
| 117 | })(); |
| 118 | |
| 119 | |
| 120 | ;(function() { |
| 121 | function foo() { |
| 122 | const x = 40; |
| 123 | x = 30; |
| 124 | } |
| 125 | function bar() { |
| 126 | const x = 40; |
| 127 | function capX() { return x; } |
| 128 | x = 30; |
| 129 | } |
| 130 | function baz() { |
| 131 | const x = 40; |
| 132 | assert(x === 40); |
| 133 | function bad() { x = 10; } |
| 134 | bad(); |
| 135 | } |
| 136 | function jaz() { |
| 137 | const x = 40; |
| 138 | assert(x === 40); |
| 139 | function bad() { eval("x = 10"); } |
| 140 | bad(); |
| 141 | } |
| 142 | function faz() { |
| 143 | const x = 40; |
| 144 | assert(x === 40); |
| 145 | eval("x = 10"); |
| 146 | } |
| 147 | function raz() { |
| 148 | const x = 40; |
| 149 | assert(x === 40); |
| 150 | ;({x} = {x: 20}); |
| 151 | } |
| 152 | function taz() { |
| 153 | const x = 40; |
| 154 | function capX() { return x; } |
| 155 | assert(capX() === 40); |
| 156 | ;({x} = {x: 20}); |
| 157 | } |
| 158 | function paz() { |
| 159 | const x = 20; |
| 160 | const y = x = 20; |
| 161 | } |
| 162 | for (var i = 0; i < NUM_LOOPS; i++) { |
| 163 | shouldThrowInvalidConstAssignment(foo); |
| 164 | shouldThrowInvalidConstAssignment(bar); |
| 165 | shouldThrowInvalidConstAssignment(baz); |
| 166 | shouldThrowInvalidConstAssignment(jaz); |
| 167 | shouldThrowInvalidConstAssignment(faz); |
| 168 | shouldThrowInvalidConstAssignment(raz); |
| 169 | shouldThrowInvalidConstAssignment(taz); |
| 170 | shouldThrowInvalidConstAssignment(paz); |
| 171 | } |
| 172 | })(); |
| 173 | |
| 174 | |
| 175 | ;(function() { |
| 176 | function foo() { |
| 177 | const x = 40; |
| 178 | eval("x = 30;"); |
| 179 | } |
| 180 | function bar() { |
| 181 | const x = 20; |
| 182 | x += 20; |
| 183 | } |
| 184 | function baz() { |
| 185 | const x = 20; |
| 186 | x -= 20; |
| 187 | } |
| 188 | function taz() { |
| 189 | const x = 20; |
| 190 | shouldThrowInvalidConstAssignment(function() { x = 20; }); |
| 191 | shouldThrowInvalidConstAssignment(function() { x += 20; }); |
| 192 | shouldThrowInvalidConstAssignment(function() { x -= 20; }); |
| 193 | shouldThrowInvalidConstAssignment(function() { x *= 20; }); |
| 194 | shouldThrowInvalidConstAssignment(function() { x /= 20; }); |
| 195 | shouldThrowInvalidConstAssignment(function() { x >>= 20; }); |
| 196 | shouldThrowInvalidConstAssignment(function() { x <<= 20; }); |
| 197 | shouldThrowInvalidConstAssignment(function() { x ^= 20; }); |
| 198 | shouldThrowInvalidConstAssignment(function() { x++; }); |
| 199 | shouldThrowInvalidConstAssignment(function() { x--; }); |
| 200 | shouldThrowInvalidConstAssignment(function() { ++x; }); |
| 201 | shouldThrowInvalidConstAssignment(function() { --x; }); |
| 202 | } |
| 203 | function jaz() { |
| 204 | const x = 20; |
| 205 | let threw = false; |
| 206 | try { x = 20; } catch(e) { threw = true} |
| 207 | assert(threw); |
| 208 | |
| 209 | threw = false; |
| 210 | try { x += 20; } catch(e) { threw = true} |
| 211 | assert(threw); |
| 212 | |
| 213 | threw = false; |
| 214 | try { x -= 20; } catch(e) { threw = true} |
| 215 | assert(threw); |
| 216 | |
| 217 | threw = false; |
| 218 | try { x *= 20; } catch(e) { threw = true} |
| 219 | assert(threw); |
| 220 | |
| 221 | threw = false; |
| 222 | try { x /= 20; } catch(e) { threw = true} |
| 223 | assert(threw); |
| 224 | |
| 225 | threw = false; |
| 226 | try { x >>= 20; } catch(e) { threw = true} |
| 227 | assert(threw); |
| 228 | |
| 229 | threw = false; |
| 230 | try { x <<= 20; } catch(e) { threw = true} |
| 231 | assert(threw); |
| 232 | |
| 233 | threw = false; |
| 234 | try { x ^= 20; } catch(e) { threw = true} |
| 235 | assert(threw); |
| 236 | |
| 237 | threw = false; |
| 238 | try { x++; } catch(e) { threw = true} |
| 239 | assert(threw); |
| 240 | |
| 241 | |
| 242 | threw = false; |
| 243 | try { x--; } catch(e) { threw = true} |
| 244 | assert(threw); |
| 245 | |
| 246 | |
| 247 | threw = false; |
| 248 | try { ++x; } catch(e) { threw = true} |
| 249 | assert(threw); |
| 250 | |
| 251 | threw = false; |
| 252 | try { --x; } catch(e) { threw = true}; |
| 253 | assert(threw); |
| 254 | } |
| 255 | for (var i = 0; i < NUM_LOOPS; i++) { |
| 256 | shouldThrowInvalidConstAssignment(foo); |
| 257 | shouldThrowInvalidConstAssignment(bar); |
| 258 | shouldThrowInvalidConstAssignment(baz); |
| 259 | taz(); |
| 260 | jaz(); |
| 261 | } |
| 262 | })(); |