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 // Function hoisting is blocked by the conflicting x declaration | |
51 function* f21({x}) { { function x() { return 2 } } return x(); } | |
52 | |
neis
2016/08/10 09:29:29
I don't understand the comment on f21. Also the fu
Dan Ehrenberg
2016/08/10 18:29:35
Oops, added the test. I had accidentally deleted i
| |
53 assertThrows("'use strict'; function* f(x) { let x = 0; }; f({});", SyntaxErro r); | |
54 assertThrows("'use strict'; function* f({x}) { let x = 0; }; f({});", SyntaxEr ror); | |
55 assertThrows("'use strict'; function* f(x) { const x = 0; }; f({});", SyntaxEr ror); | |
56 assertThrows("'use strict'; function* f({x}) { const x = 0; }; f({});", Syntax Error); | |
57 }()); | |
neis
2016/08/10 09:29:29
No need for applying f in these four cases.
Dan Ehrenberg
2016/08/10 18:29:35
Done
| |
58 | |
59 (function TestDefaults() { | |
60 function* f1(x = 1) { return x } | |
61 assertEquals(1, f1().next().value); | |
62 assertEquals(1, f1(undefined).next().value); | |
63 assertEquals(2, f1(2).next().value); | |
64 assertEquals(null, f1(null).next().value); | |
65 | |
66 function* f2(x, y = x) { return x + y; } | |
67 assertEquals(8, f2(4).next().value); | |
68 assertEquals(8, f2(4, undefined).next().value); | |
69 assertEquals(6, f2(4, 2).next().value); | |
70 | |
71 function* f3(x = 1, y) { return x + y; } | |
72 assertEquals(8, f3(5, 3).next().value); | |
73 assertEquals(3, f3(undefined, 2).next().value); | |
74 assertEquals(6, f3(4, 2).next().value); | |
75 | |
76 function* f4(x = () => 1) { return x() } | |
77 assertEquals(1, f4().next().value); | |
78 assertEquals(1, f4(undefined).next().value); | |
79 assertEquals(2, f4(() => 2).next().value); | |
80 assertThrows(() => f4(null).next(), TypeError); | |
81 | |
82 function* f5(x, y = () => x) { return x + y(); } | |
83 assertEquals(8, f5(4).next().value); | |
84 assertEquals(8, f5(4, undefined).next().value); | |
85 assertEquals(6, f5(4, () => 2).next().value); | |
86 | |
87 function* f6(x = {a: 1, m() { return 2 }}) { return x.a + x.m(); } | |
88 assertEquals(3, f6().next().value); | |
89 assertEquals(3, f6(undefined).next().value); | |
90 assertEquals(5, f6({a: 2, m() { return 3 }}).next().value); | |
91 }()); | |
92 | |
93 | |
94 (function TestEvalInParameters() { | |
95 function* f1(x = eval(0)) { return x } | |
96 assertEquals(0, f1().next().value); | |
97 function* f2(x = () => eval(1)) { return x() } | |
98 assertEquals(1, f2().next().value); | |
99 })(); | |
100 | |
101 | |
102 (function TestParameterScopingSloppy() { | |
103 var x = 1; | |
104 | |
105 function* f1(a = x) { var x = 2; return a; } | |
106 assertEquals(1, f1().next().value); | |
107 function* f2(a = x) { function x() {}; return a; } | |
108 assertEquals(1, f2().next().value); | |
109 function* f3(a = eval("x")) { var x; return a; } | |
110 assertEquals(1, f3().next().value); | |
111 function* f31(a = eval("'use strict'; x")) { var x; return a; } | |
112 assertEquals(1, f31().next().value); | |
113 function* f4(a = function() { return x }) { var x; return a(); } | |
114 assertEquals(1, f4().next().value); | |
115 function* f5(a = () => x) { var x; return a(); } | |
116 assertEquals(1, f5().next().value); | |
117 function* f6(a = () => eval("x")) { var x; return a(); } | |
118 assertEquals(1, f6().next().value); | |
119 function* f61(a = () => { 'use strict'; return eval("x") }) { var x; return a( ); } | |
120 assertEquals(1, f61().next().value); | |
121 function* f62(a = () => eval("'use strict'; x")) { var x; return a(); } | |
122 assertEquals(1, f62().next().value); | |
123 | |
124 var f11 = function* f(x = f) { var f; return x; } | |
125 assertSame(f11, f11().next().value); | |
126 var f12 = function* f(x = f) { function f() {}; return x; } | |
127 assertSame(f12, f12().next().value); | |
128 var f13 = function* f(f = 7, x = f) { return x; } | |
129 assertSame(7, f13().next().value); | |
130 | |
131 var o1 = {f: function*(x = this) { return x; }}; | |
132 assertSame(o1, o1.f().next().value); | |
133 assertSame(1, o1.f(1).next().value); | |
134 })(); | |
135 | |
136 (function TestParameterScopingStrict() { | |
137 "use strict"; | |
138 var x = 1; | |
139 | |
140 function* f1(a = x) { let x = 2; return a; } | |
141 assertEquals(1, f1().next().value); | |
142 function* f2(a = x) { const x = 2; return a; } | |
143 assertEquals(1, f2().next().value); | |
144 function* f3(a = x) { function x() {}; return a; } | |
145 assertEquals(1, f3().next().value); | |
146 function* f4(a = eval("x")) { var x; return a; } | |
147 assertEquals(1, f4().next().value); | |
148 function* f5(a = () => eval("x")) { var x; return a(); } | |
149 assertEquals(1, f5().next().value); | |
150 | |
151 var f11 = function* f(x = f) { let f; return x; } | |
152 assertSame(f11, f11().next().value); | |
153 var f12 = function* f(x = f) { const f = 0; return x; } | |
154 assertSame(f12, f12().next().value); | |
155 var f13 = function* f(x = f) { function f() {}; return x; } | |
156 assertSame(f13, f13().next().value); | |
157 })(); | |
158 | |
159 (function TestSloppyEvalScoping() { | |
160 var x = 1; | |
161 | |
162 function* f1(y = eval("var x = 2")) { with ({}) { return x; } } | |
163 assertEquals(1, f1().next().value); | |
164 function* f2(y = eval("var x = 2"), z = x) { return z; } | |
165 assertEquals(1, f2().next().value); | |
166 assertEquals(1, f2(0).next().value); | |
167 function* f3(y = eval("var x = 2"), z = eval("x")) { return z; } | |
168 assertEquals(1, f3().next().value); | |
169 assertEquals(1, f3(0).next().value); | |
170 function* f8(y = (eval("var x = 2"), x)) { return y; } | |
171 assertEquals(2, f8().next().value); | |
172 assertEquals(0, f8(0).next().value); | |
173 | |
174 function* f11(z = eval("var y = 2")) { return y; } | |
175 assertThrows(() => f11().next(), ReferenceError); | |
176 function* f12(z = eval("var y = 2"), b = y) {} | |
177 assertThrows(() => f12().next(), ReferenceError); | |
178 function* f13(z = eval("var y = 2"), b = eval("y")) {} | |
179 assertThrows(() => f13().next(), ReferenceError); | |
180 | |
181 function* f21(f = () => x) { eval("var x = 2"); return f() } | |
182 assertEquals(1, f21().next().value); | |
183 assertEquals(3, f21(() => 3).next().value); | |
184 function* f22(f = () => eval("x")) { eval("var x = 2"); return f() } | |
185 assertEquals(1, f22().next().value); | |
186 assertEquals(3, f22(() => 3).next().value); | |
187 })(); | |
188 | |
189 | |
190 (function TestStrictEvalScoping() { | |
191 'use strict'; | |
192 var x = 1; | |
193 | |
194 function* f1(y = eval("var x = 2")) { return x; } | |
195 assertEquals(1, f1().next().value); | |
196 function* f2(y = eval("var x = 2"), z = x) { return z; } | |
197 assertEquals(1, f2().next().value); | |
198 assertEquals(1, f2(0).next().value); | |
199 function* f3(y = eval("var x = 2"), z = eval("x")) { return z; } | |
200 assertEquals(1, f3().next().value); | |
201 assertEquals(1, f3(0).next().value); | |
202 function* f8(y = (eval("var x = 2"), x)) { return y; } | |
203 assertEquals(1, f8().next().value); | |
204 assertEquals(0, f8(0).next().value); | |
205 | |
206 function* f11(z = eval("var y = 2")) { return y; } | |
207 assertThrows(() => f11().next().value, ReferenceError); | |
208 function* f12(z = eval("var y = 2"), b = y) {} | |
209 assertThrows(() => f12().next().value, ReferenceError); | |
210 function* f13(z = eval("var y = 2"), b = eval("y")) {} | |
211 assertThrows(() => f13().next().value, ReferenceError); | |
212 | |
213 function* f21(f = () => x) { eval("var x = 2"); return f() } | |
214 assertEquals(1, f21().next().value); | |
215 assertEquals(3, f21(() => 3).next().value); | |
216 function* f22(f = () => eval("x")) { eval("var x = 2"); return f() } | |
217 assertEquals(1, f22().next().value); | |
218 assertEquals(3, f22(() => 3).next().value); | |
219 })(); | |
220 | |
221 (function TestParameterTDZSloppy() { | |
222 function* f1(a = x, x) { return a } | |
223 assertThrows(() => f1(undefined, 4), ReferenceError); | |
224 assertEquals(4, f1(4, 5).next().value); | |
225 function* f2(a = eval("x"), x) { return a } | |
226 assertThrows(() => f2(undefined, 4), ReferenceError); | |
227 assertEquals(4, f2(4, 5).next().value); | |
228 function* f3(a = eval("'use strict'; x"), x) { return a } | |
229 assertThrows(() => f3(undefined, 4), ReferenceError); | |
230 assertEquals(4, f3(4, 5).next().value); | |
231 function* f4(a = () => x, x) { return a() } | |
232 assertEquals(4, f4(() => 4, 5).next().value); | |
233 function* f5(a = () => eval("x"), x) { return a() } | |
234 assertEquals(4, f5(() => 4, 5).next().value); | |
235 function* f6(a = () => eval("'use strict'; x"), x) { return a() } | |
236 assertEquals(4, f6(() => 4, 5).next().value); | |
237 | |
238 function* f11(a = x, x = 2) { return a } | |
239 assertThrows(() => f11(), ReferenceError); | |
240 assertThrows(() => f11(undefined), ReferenceError); | |
241 assertThrows(() => f11(undefined, 4), ReferenceError); | |
242 assertEquals(4, f1(4, 5).next().value); | |
243 function* f12(a = eval("x"), x = 2) { return a } | |
244 assertThrows(() => f12(), ReferenceError); | |
245 assertThrows(() => f12(undefined), ReferenceError); | |
246 assertThrows(() => f12(undefined, 4), ReferenceError); | |
247 assertEquals(4, f12(4, 5).next().value); | |
248 function* f13(a = eval("'use strict'; x"), x = 2) { return a } | |
249 assertThrows(() => f13(), ReferenceError); | |
250 assertThrows(() => f13(undefined), ReferenceError); | |
251 assertThrows(() => f13(undefined, 4), ReferenceError); | |
252 assertEquals(4, f13(4, 5).next().value); | |
253 | |
254 function* f21(x = function() { return a }, ...a) { return x()[0] } | |
255 assertEquals(4, f21(undefined, 4).next().value); | |
256 function* f22(x = () => a, ...a) { return x()[0] } | |
257 assertEquals(4, f22(undefined, 4).next().value); | |
258 function* f23(x = () => eval("a"), ...a) { return x()[0] } | |
259 assertEquals(4, f23(undefined, 4).next().value); | |
260 function* f24(x = () => {'use strict'; return eval("a") }, ...a) { | |
261 return x()[0] | |
262 } | |
263 assertEquals(4, f24(undefined, 4).next().value); | |
264 function* f25(x = () => eval("'use strict'; a"), ...a) { return x()[0] } | |
265 assertEquals(4, f25(undefined, 4).next().value); | |
266 })(); | |
267 | |
268 (function TestParameterTDZStrict() { | |
269 "use strict"; | |
270 | |
271 function* f1(a = eval("x"), x) { return a } | |
272 assertThrows(() => f1(undefined, 4), ReferenceError); | |
273 assertEquals(4, f1(4, 5).next().value); | |
274 function* f2(a = () => eval("x"), x) { return a() } | |
275 assertEquals(4, f2(() => 4, 5).next().value); | |
276 | |
277 function* f11(a = eval("x"), x = 2) { return a } | |
278 assertThrows(() => f11(), ReferenceError); | |
279 assertThrows(() => f11(undefined), ReferenceError); | |
280 assertThrows(() => f11(undefined, 4), ReferenceError); | |
281 assertEquals(4, f11(4, 5).next().value); | |
282 | |
283 function* f21(x = () => eval("a"), ...a) { return x()[0] } | |
284 assertEquals(4, f21(undefined, 4).next().value); | |
285 })(); | |
286 | |
287 (function TestArgumentsForNonSimpleParameters() { | |
288 function* f1(x = 900) { arguments[0] = 1; return x } | |
289 assertEquals(9, f1(9).next().value); | |
290 assertEquals(900, f1().next().value); | |
291 function* f2(x = 1001) { x = 2; return arguments[0] } | |
292 assertEquals(10, f2(10).next().value); | |
293 assertEquals(undefined, f2().next().value); | |
294 }()); | |
295 | |
296 | |
297 (function TestFunctionLength() { | |
298 assertEquals(0, (function*(x = 1) {}).length); | |
299 assertEquals(0, (function*(x = 1, ...a) {}).length); | |
300 assertEquals(1, (function*(x, y = 1) {}).length); | |
301 assertEquals(1, (function*(x, y = 1, ...a) {}).length); | |
302 assertEquals(2, (function*(x, y, z = 1) {}).length); | |
303 assertEquals(2, (function*(x, y, z = 1, ...a) {}).length); | |
304 assertEquals(1, (function*(x, y = 1, z) {}).length); | |
305 assertEquals(1, (function*(x, y = 1, z, ...a) {}).length); | |
306 assertEquals(1, (function*(x, y = 1, z, v = 2) {}).length); | |
307 assertEquals(1, (function*(x, y = 1, z, v = 2, ...a) {}).length); | |
308 })(); | |
309 | |
310 (function TestDirectiveThrows() { | |
311 "use strict"; | |
312 | |
313 assertThrows(function(){ eval("function*(x=1){'use strict';}") }, SyntaxError) ; | |
neis
2016/08/10 09:29:29
I think you're testing the wrong SyntaxError here
Dan Ehrenberg
2016/08/10 18:29:35
Oops, fixed in all copies.
| |
314 | |
315 assertThrows( | |
316 function(){ eval("function*(a, x=1){'use strict';}") }, SyntaxError); | |
neis
2016/08/10 09:29:29
Same here.
Maybe also test with a non-simple but
Dan Ehrenberg
2016/08/10 18:29:35
Done.
| |
317 })(); | |
OLD | NEW |