OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 (function TestDefaultBeforeInitializingYield() { |
| 6 var y = 0; |
| 7 var z = 0; |
| 8 function* f1(x = (y = 1)) { z = 1 }; |
| 9 assertEquals(0, y); |
| 10 assertEquals(0, z); |
| 11 var gen = f1(); |
| 12 assertEquals(1, y); |
| 13 assertEquals(0, z); |
| 14 gen.next(); |
| 15 assertEquals(1, y); |
| 16 assertEquals(1, z); |
| 17 })(); |
| 18 |
| 19 (function TestShadowingOfParameters() { |
| 20 function* f1({x}) { var x = 2; return x } |
| 21 assertEquals(2, f1({x: 1}).next().value); |
| 22 function* f2({x}) { { var x = 2; } return x; } |
| 23 assertEquals(2, f2({x: 1}).next().value); |
| 24 function* f3({x}) { var y = x; var x = 2; return y; } |
| 25 assertEquals(1, f3({x: 1}).next().value); |
| 26 function* f4({x}) { { var y = x; var x = 2; } return y; } |
| 27 assertEquals(1, f4({x: 1}).next().value); |
| 28 function* f5({x}, g = () => x) { var x = 2; return g(); } |
| 29 assertEquals(1, f5({x: 1}).next().value); |
| 30 function* f6({x}, g = () => x) { { var x = 2; } return g(); } |
| 31 assertEquals(1, f6({x: 1}).next().value); |
| 32 function* f7({x}) { var g = () => x; var x = 2; return g(); } |
| 33 assertEquals(2, f7({x: 1}).next().value); |
| 34 function* f8({x}) { { var g = () => x; var x = 2; } return g(); } |
| 35 assertEquals(2, f8({x: 1}).next().value); |
| 36 function* f9({x}, g = () => eval("x")) { var x = 2; return g(); } |
| 37 assertEquals(1, f9({x: 1}).next().value); |
| 38 |
| 39 function* f10({x}, y) { var y; return y } |
| 40 assertEquals(2, f10({x: 6}, 2).next().value); |
| 41 function* f11({x}, y) { var z = y; var y = 2; return z; } |
| 42 assertEquals(1, f11({x: 6}, 1).next().value); |
| 43 function* f12(y, g = () => y) { var y = 2; return g(); } |
| 44 assertEquals(1, f12(1).next().value); |
| 45 function* f13({x}, y, [z], v) { var x, y, z; return x*y*z*v } |
| 46 assertEquals(210, f13({x: 2}, 3, [5], 7).next().value); |
| 47 |
| 48 function* f20({x}) { function x() { return 2 }; return x(); } |
| 49 assertEquals(2, f20({x: 1}).next().value); |
| 50 // Annex B 3.3 function hoisting is blocked by the conflicting x declaration |
| 51 function* f21({x}) { { function x() { return 2 } } return x; } |
| 52 assertEquals(1, f21({x: 1}).next().value); |
| 53 |
| 54 assertThrows("'use strict'; function* f(x) { let x = 0; }", SyntaxError); |
| 55 assertThrows("'use strict'; function* f({x}) { let x = 0; }", SyntaxError); |
| 56 assertThrows("'use strict'; function* f(x) { const x = 0; }", SyntaxError); |
| 57 assertThrows("'use strict'; function* f({x}) { const x = 0; }", SyntaxError); |
| 58 }()); |
| 59 |
| 60 (function TestDefaults() { |
| 61 function* f1(x = 1) { return x } |
| 62 assertEquals(1, f1().next().value); |
| 63 assertEquals(1, f1(undefined).next().value); |
| 64 assertEquals(2, f1(2).next().value); |
| 65 assertEquals(null, f1(null).next().value); |
| 66 |
| 67 function* f2(x, y = x) { return x + y; } |
| 68 assertEquals(8, f2(4).next().value); |
| 69 assertEquals(8, f2(4, undefined).next().value); |
| 70 assertEquals(6, f2(4, 2).next().value); |
| 71 |
| 72 function* f3(x = 1, y) { return x + y; } |
| 73 assertEquals(8, f3(5, 3).next().value); |
| 74 assertEquals(3, f3(undefined, 2).next().value); |
| 75 assertEquals(6, f3(4, 2).next().value); |
| 76 |
| 77 function* f4(x = () => 1) { return x() } |
| 78 assertEquals(1, f4().next().value); |
| 79 assertEquals(1, f4(undefined).next().value); |
| 80 assertEquals(2, f4(() => 2).next().value); |
| 81 assertThrows(() => f4(null).next(), TypeError); |
| 82 |
| 83 function* f5(x, y = () => x) { return x + y(); } |
| 84 assertEquals(8, f5(4).next().value); |
| 85 assertEquals(8, f5(4, undefined).next().value); |
| 86 assertEquals(6, f5(4, () => 2).next().value); |
| 87 |
| 88 function* f6(x = {a: 1, m() { return 2 }}) { return x.a + x.m(); } |
| 89 assertEquals(3, f6().next().value); |
| 90 assertEquals(3, f6(undefined).next().value); |
| 91 assertEquals(5, f6({a: 2, m() { return 3 }}).next().value); |
| 92 }()); |
| 93 |
| 94 |
| 95 (function TestEvalInParameters() { |
| 96 function* f1(x = eval(0)) { return x } |
| 97 assertEquals(0, f1().next().value); |
| 98 function* f2(x = () => eval(1)) { return x() } |
| 99 assertEquals(1, f2().next().value); |
| 100 })(); |
| 101 |
| 102 |
| 103 (function TestParameterScopingSloppy() { |
| 104 var x = 1; |
| 105 |
| 106 function* f1(a = x) { var x = 2; return a; } |
| 107 assertEquals(1, f1().next().value); |
| 108 function* f2(a = x) { function x() {}; return a; } |
| 109 assertEquals(1, f2().next().value); |
| 110 function* f3(a = eval("x")) { var x; return a; } |
| 111 assertEquals(1, f3().next().value); |
| 112 function* f31(a = eval("'use strict'; x")) { var x; return a; } |
| 113 assertEquals(1, f31().next().value); |
| 114 function* f4(a = function() { return x }) { var x; return a(); } |
| 115 assertEquals(1, f4().next().value); |
| 116 function* f5(a = () => x) { var x; return a(); } |
| 117 assertEquals(1, f5().next().value); |
| 118 function* f6(a = () => eval("x")) { var x; return a(); } |
| 119 assertEquals(1, f6().next().value); |
| 120 function* f61(a = () => { 'use strict'; return eval("x") }) { var x; return a(
); } |
| 121 assertEquals(1, f61().next().value); |
| 122 function* f62(a = () => eval("'use strict'; x")) { var x; return a(); } |
| 123 assertEquals(1, f62().next().value); |
| 124 |
| 125 var f11 = function* f(x = f) { var f; return x; } |
| 126 assertSame(f11, f11().next().value); |
| 127 var f12 = function* f(x = f) { function f() {}; return x; } |
| 128 assertSame(f12, f12().next().value); |
| 129 var f13 = function* f(f = 7, x = f) { return x; } |
| 130 assertSame(7, f13().next().value); |
| 131 |
| 132 var o1 = {f: function*(x = this) { return x; }}; |
| 133 assertSame(o1, o1.f().next().value); |
| 134 assertSame(1, o1.f(1).next().value); |
| 135 })(); |
| 136 |
| 137 (function TestParameterScopingStrict() { |
| 138 "use strict"; |
| 139 var x = 1; |
| 140 |
| 141 function* f1(a = x) { let x = 2; return a; } |
| 142 assertEquals(1, f1().next().value); |
| 143 function* f2(a = x) { const x = 2; return a; } |
| 144 assertEquals(1, f2().next().value); |
| 145 function* f3(a = x) { function x() {}; return a; } |
| 146 assertEquals(1, f3().next().value); |
| 147 function* f4(a = eval("x")) { var x; return a; } |
| 148 assertEquals(1, f4().next().value); |
| 149 function* f5(a = () => eval("x")) { var x; return a(); } |
| 150 assertEquals(1, f5().next().value); |
| 151 |
| 152 var f11 = function* f(x = f) { let f; return x; } |
| 153 assertSame(f11, f11().next().value); |
| 154 var f12 = function* f(x = f) { const f = 0; return x; } |
| 155 assertSame(f12, f12().next().value); |
| 156 var f13 = function* f(x = f) { function f() {}; return x; } |
| 157 assertSame(f13, f13().next().value); |
| 158 })(); |
| 159 |
| 160 (function TestSloppyEvalScoping() { |
| 161 var x = 1; |
| 162 |
| 163 function* f1(y = eval("var x = 2")) { with ({}) { return x; } } |
| 164 assertEquals(1, f1().next().value); |
| 165 function* f2(y = eval("var x = 2"), z = x) { return z; } |
| 166 assertEquals(1, f2().next().value); |
| 167 assertEquals(1, f2(0).next().value); |
| 168 function* f3(y = eval("var x = 2"), z = eval("x")) { return z; } |
| 169 assertEquals(1, f3().next().value); |
| 170 assertEquals(1, f3(0).next().value); |
| 171 function* f8(y = (eval("var x = 2"), x)) { return y; } |
| 172 assertEquals(2, f8().next().value); |
| 173 assertEquals(0, f8(0).next().value); |
| 174 |
| 175 function* f11(z = eval("var y = 2")) { return y; } |
| 176 assertThrows(() => f11().next(), ReferenceError); |
| 177 function* f12(z = eval("var y = 2"), b = y) {} |
| 178 assertThrows(() => f12().next(), ReferenceError); |
| 179 function* f13(z = eval("var y = 2"), b = eval("y")) {} |
| 180 assertThrows(() => f13().next(), ReferenceError); |
| 181 |
| 182 function* f21(f = () => x) { eval("var x = 2"); return f() } |
| 183 assertEquals(1, f21().next().value); |
| 184 assertEquals(3, f21(() => 3).next().value); |
| 185 function* f22(f = () => eval("x")) { eval("var x = 2"); return f() } |
| 186 assertEquals(1, f22().next().value); |
| 187 assertEquals(3, f22(() => 3).next().value); |
| 188 })(); |
| 189 |
| 190 |
| 191 (function TestStrictEvalScoping() { |
| 192 'use strict'; |
| 193 var x = 1; |
| 194 |
| 195 function* f1(y = eval("var x = 2")) { return x; } |
| 196 assertEquals(1, f1().next().value); |
| 197 function* f2(y = eval("var x = 2"), z = x) { return z; } |
| 198 assertEquals(1, f2().next().value); |
| 199 assertEquals(1, f2(0).next().value); |
| 200 function* f3(y = eval("var x = 2"), z = eval("x")) { return z; } |
| 201 assertEquals(1, f3().next().value); |
| 202 assertEquals(1, f3(0).next().value); |
| 203 function* f8(y = (eval("var x = 2"), x)) { return y; } |
| 204 assertEquals(1, f8().next().value); |
| 205 assertEquals(0, f8(0).next().value); |
| 206 |
| 207 function* f11(z = eval("var y = 2")) { return y; } |
| 208 assertThrows(() => f11().next().value, ReferenceError); |
| 209 function* f12(z = eval("var y = 2"), b = y) {} |
| 210 assertThrows(() => f12().next().value, ReferenceError); |
| 211 function* f13(z = eval("var y = 2"), b = eval("y")) {} |
| 212 assertThrows(() => f13().next().value, ReferenceError); |
| 213 |
| 214 function* f21(f = () => x) { eval("var x = 2"); return f() } |
| 215 assertEquals(1, f21().next().value); |
| 216 assertEquals(3, f21(() => 3).next().value); |
| 217 function* f22(f = () => eval("x")) { eval("var x = 2"); return f() } |
| 218 assertEquals(1, f22().next().value); |
| 219 assertEquals(3, f22(() => 3).next().value); |
| 220 })(); |
| 221 |
| 222 (function TestParameterTDZSloppy() { |
| 223 function* f1(a = x, x) { return a } |
| 224 assertThrows(() => f1(undefined, 4), ReferenceError); |
| 225 assertEquals(4, f1(4, 5).next().value); |
| 226 function* f2(a = eval("x"), x) { return a } |
| 227 assertThrows(() => f2(undefined, 4), ReferenceError); |
| 228 assertEquals(4, f2(4, 5).next().value); |
| 229 function* f3(a = eval("'use strict'; x"), x) { return a } |
| 230 assertThrows(() => f3(undefined, 4), ReferenceError); |
| 231 assertEquals(4, f3(4, 5).next().value); |
| 232 function* f4(a = () => x, x) { return a() } |
| 233 assertEquals(4, f4(() => 4, 5).next().value); |
| 234 function* f5(a = () => eval("x"), x) { return a() } |
| 235 assertEquals(4, f5(() => 4, 5).next().value); |
| 236 function* f6(a = () => eval("'use strict'; x"), x) { return a() } |
| 237 assertEquals(4, f6(() => 4, 5).next().value); |
| 238 |
| 239 function* f11(a = x, x = 2) { return a } |
| 240 assertThrows(() => f11(), ReferenceError); |
| 241 assertThrows(() => f11(undefined), ReferenceError); |
| 242 assertThrows(() => f11(undefined, 4), ReferenceError); |
| 243 assertEquals(4, f1(4, 5).next().value); |
| 244 function* f12(a = eval("x"), x = 2) { return a } |
| 245 assertThrows(() => f12(), ReferenceError); |
| 246 assertThrows(() => f12(undefined), ReferenceError); |
| 247 assertThrows(() => f12(undefined, 4), ReferenceError); |
| 248 assertEquals(4, f12(4, 5).next().value); |
| 249 function* f13(a = eval("'use strict'; x"), x = 2) { return a } |
| 250 assertThrows(() => f13(), ReferenceError); |
| 251 assertThrows(() => f13(undefined), ReferenceError); |
| 252 assertThrows(() => f13(undefined, 4), ReferenceError); |
| 253 assertEquals(4, f13(4, 5).next().value); |
| 254 |
| 255 function* f21(x = function() { return a }, ...a) { return x()[0] } |
| 256 assertEquals(4, f21(undefined, 4).next().value); |
| 257 function* f22(x = () => a, ...a) { return x()[0] } |
| 258 assertEquals(4, f22(undefined, 4).next().value); |
| 259 function* f23(x = () => eval("a"), ...a) { return x()[0] } |
| 260 assertEquals(4, f23(undefined, 4).next().value); |
| 261 function* f24(x = () => {'use strict'; return eval("a") }, ...a) { |
| 262 return x()[0] |
| 263 } |
| 264 assertEquals(4, f24(undefined, 4).next().value); |
| 265 function* f25(x = () => eval("'use strict'; a"), ...a) { return x()[0] } |
| 266 assertEquals(4, f25(undefined, 4).next().value); |
| 267 })(); |
| 268 |
| 269 (function TestParameterTDZStrict() { |
| 270 "use strict"; |
| 271 |
| 272 function* f1(a = eval("x"), x) { return a } |
| 273 assertThrows(() => f1(undefined, 4), ReferenceError); |
| 274 assertEquals(4, f1(4, 5).next().value); |
| 275 function* f2(a = () => eval("x"), x) { return a() } |
| 276 assertEquals(4, f2(() => 4, 5).next().value); |
| 277 |
| 278 function* f11(a = eval("x"), x = 2) { return a } |
| 279 assertThrows(() => f11(), ReferenceError); |
| 280 assertThrows(() => f11(undefined), ReferenceError); |
| 281 assertThrows(() => f11(undefined, 4), ReferenceError); |
| 282 assertEquals(4, f11(4, 5).next().value); |
| 283 |
| 284 function* f21(x = () => eval("a"), ...a) { return x()[0] } |
| 285 assertEquals(4, f21(undefined, 4).next().value); |
| 286 })(); |
| 287 |
| 288 (function TestArgumentsForNonSimpleParameters() { |
| 289 function* f1(x = 900) { arguments[0] = 1; return x } |
| 290 assertEquals(9, f1(9).next().value); |
| 291 assertEquals(900, f1().next().value); |
| 292 function* f2(x = 1001) { x = 2; return arguments[0] } |
| 293 assertEquals(10, f2(10).next().value); |
| 294 assertEquals(undefined, f2().next().value); |
| 295 }()); |
| 296 |
| 297 |
| 298 (function TestFunctionLength() { |
| 299 assertEquals(0, (function*(x = 1) {}).length); |
| 300 assertEquals(0, (function*(x = 1, ...a) {}).length); |
| 301 assertEquals(1, (function*(x, y = 1) {}).length); |
| 302 assertEquals(1, (function*(x, y = 1, ...a) {}).length); |
| 303 assertEquals(2, (function*(x, y, z = 1) {}).length); |
| 304 assertEquals(2, (function*(x, y, z = 1, ...a) {}).length); |
| 305 assertEquals(1, (function*(x, y = 1, z) {}).length); |
| 306 assertEquals(1, (function*(x, y = 1, z, ...a) {}).length); |
| 307 assertEquals(1, (function*(x, y = 1, z, v = 2) {}).length); |
| 308 assertEquals(1, (function*(x, y = 1, z, v = 2, ...a) {}).length); |
| 309 })(); |
| 310 |
| 311 (function TestDirectiveThrows() { |
| 312 "use strict"; |
| 313 |
| 314 assertThrows("(function*(x=1){'use strict';})", SyntaxError); |
| 315 assertThrows("(function*(a, x=1){'use strict';})", SyntaxError); |
| 316 assertThrows("(function*({x}){'use strict';})", SyntaxError); |
| 317 })(); |
OLD | NEW |