1// Copyright 2015 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// Flags: --harmony-default-parameters
6
7
8(function TestDefaults() {
9  function f1(x = 1) { return x }
10  assertEquals(1, f1());
11  assertEquals(1, f1(undefined));
12  assertEquals(2, f1(2));
13  assertEquals(null, f1(null));
14
15  function f2(x, y = x) { return x + y; }
16  assertEquals(8, f2(4));
17  assertEquals(8, f2(4, undefined));
18  assertEquals(6, f2(4, 2));
19
20  function f3(x = 1, y) { return x + y; }
21  assertEquals(8, f3(5, 3));
22  assertEquals(3, f3(undefined, 2));
23  assertEquals(6, f3(4, 2));
24
25  function f4(x = () => 1) { return x() }
26  assertEquals(1, f4());
27  assertEquals(1, f4(undefined));
28  assertEquals(2, f4(() => 2));
29  assertThrows(() => f4(null), TypeError);
30
31  function f5(x, y = () => x) { return x + y(); }
32  assertEquals(8, f5(4));
33  assertEquals(8, f5(4, undefined));
34  assertEquals(6, f5(4, () => 2));
35
36  function f6(x = {a: 1, m() { return 2 }}) { return x.a + x.m(); }
37  assertEquals(3, f6());
38  assertEquals(3, f6(undefined));
39  assertEquals(5, f6({a: 2, m() { return 3 }}));
40
41  var g1 = (x = 1) => { return x };
42  assertEquals(1, g1());
43  assertEquals(1, g1(undefined));
44  assertEquals(2, g1(2));
45  assertEquals(null, g1(null));
46
47  var g2 = (x, y = x) => { return x + y; };
48  assertEquals(8, g2(4));
49  assertEquals(8, g2(4, undefined));
50  assertEquals(6, g2(4, 2));
51
52  var g3 = (x = 1, y) => { return x + y; };
53  assertEquals(8, g3(5, 3));
54  assertEquals(3, g3(undefined, 2));
55  assertEquals(6, g3(4, 2));
56
57  var g4 = (x = () => 1) => { return x() };
58  assertEquals(1, g4());
59  assertEquals(1, g4(undefined));
60  assertEquals(2, g4(() => 2));
61  assertThrows(() => g4(null), TypeError);
62
63  var g5 = (x, y = () => x) => { return x + y(); };
64  assertEquals(8, g5(4));
65  assertEquals(8, g5(4, undefined));
66  assertEquals(6, g5(4, () => 2));
67
68  var g6 = (x = {a: 1, m() { return 2 }}) => { return x.a + x.m(); };
69  assertEquals(3, g6());
70  assertEquals(3, g6(undefined));
71  assertEquals(5, g6({a: 2, m() { return 3 }}));
72}());
73
74
75(function TestEvalInParameters() {
76  function f1(x = eval(0)) { return x }
77  assertEquals(0, f1());
78  function f2(x = () => eval(1)) { return x() }
79  assertEquals(1, f2());
80})();
81
82
83(function TestParameterScopingSloppy() {
84  var x = 1;
85
86  function f1(a = x) { var x = 2; return a; }
87  assertEquals(1, f1());
88  function f2(a = x) { function x() {}; return a; }
89  assertEquals(1, f2());
90  function f3(a = eval("x")) { var x; return a; }
91  assertEquals(1, f3());
92  function f31(a = eval("'use strict'; x")) { var x; return a; }
93  assertEquals(1, f31());
94  function f4(a = function() { return x }) { var x; return a(); }
95  assertEquals(1, f4());
96  function f5(a = () => x) { var x; return a(); }
97  assertEquals(1, f5());
98  function f6(a = () => eval("x")) { var x; return a(); }
99  assertEquals(1, f6());
100  function f61(a = () => { 'use strict'; return eval("x") }) { var x; return a(); }
101  assertEquals(1, f61());
102  function f62(a = () => eval("'use strict'; x")) { var x; return a(); }
103  assertEquals(1, f62());
104
105  var g1 = (a = x) => { var x = 2; return a; };
106  assertEquals(1, g1());
107  var g2 = (a = x) => { function x() {}; return a; };
108  assertEquals(1, g2());
109  var g3 = (a = eval("x")) => { var x; return a; };
110  assertEquals(1, g3());
111  var g31 = (a = eval("'use strict'; x")) => { var x; return a; };
112  assertEquals(1, g31());
113  var g4 = (a = function() { return x }) => { var x; return a(); };
114  assertEquals(1, g4());
115  var g5 = (a = () => x) => { var x; return a(); };
116  assertEquals(1, g5());
117  var g6 = (a = () => eval("x")) => { var x; return a(); };
118  assertEquals(1, g6());
119  var g61 = (a = () => { 'use strict'; return eval("x") }) => { var x; return a(); };
120  assertEquals(1, g61());
121  var g62 = (a = () => eval("'use strict'; x")) => { var x; return a(); };
122  assertEquals(1, g62());
123
124  var f11 = function f(x = f) { var f; return x; }
125  assertSame(f11, f11());
126  var f12 = function f(x = f) { function f() {}; return x; }
127  assertSame(f12, f12());
128  var f13 = function f(f = 7, x = f) { return x; }
129  assertSame(7, f13());
130
131  var o1 = {f: function(x = this) { return x; }};
132  assertSame(o1, o1.f());
133  assertSame(1, o1.f(1));
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());
142  function f2(a = x) { const x = 2; return a; }
143  assertEquals(1, f2());
144  function f3(a = x) { function x() {}; return a; }
145  assertEquals(1, f3());
146  function f4(a = eval("x")) { var x; return a; }
147  assertEquals(1, f4());
148  function f5(a = () => eval("x")) { var x; return a(); }
149  assertEquals(1, f5());
150
151  var g1 = (a = x) => { let x = 2; return a; };
152  assertEquals(1, g1());
153  var g2 = (a = x) => { const x = 2; return a; };
154  assertEquals(1, g2());
155  var g3 = (a = x) => { function x() {}; return a; };
156  assertEquals(1, g3());
157  var g4 = (a = eval("x")) => { var x; return a; };
158  assertEquals(1, g4());
159  var g5 = (a = () => eval("x")) => { var x; return a(); };
160  assertEquals(1, g5());
161
162  var f11 = function f(x = f) { let f; return x; }
163  assertSame(f11, f11());
164  var f12 = function f(x = f) { const f = 0; return x; }
165  assertSame(f12, f12());
166  var f13 = function f(x = f) { function f() {}; return x; }
167  assertSame(f13, f13());
168})();
169
170(function TestSloppyEvalScoping() {
171  var x = 1;
172
173  function f1(y = eval("var x = 2")) { with ({}) { return x; } }
174  assertEquals(1, f1());
175  function f2(y = eval("var x = 2"), z = x) { return z; }
176  assertEquals(1, f2());
177  assertEquals(1, f2(0));
178  function f3(y = eval("var x = 2"), z = eval("x")) { return z; }
179  assertEquals(1, f3());
180  assertEquals(1, f3(0));
181  function f8(y = (eval("var x = 2"), x)) { return y; }
182  assertEquals(2, f8());
183  assertEquals(0, f8(0));
184
185  function f11(z = eval("var y = 2")) { return y; }
186  assertThrows(f11, ReferenceError);
187  function f12(z = eval("var y = 2"), b = y) {}
188  assertThrows(f12, ReferenceError);
189  function f13(z = eval("var y = 2"), b = eval("y")) {}
190  assertThrows(f13, ReferenceError);
191
192  function f21(f = () => x) { eval("var x = 2"); return f() }
193  assertEquals(1, f21());
194  assertEquals(3, f21(() => 3));
195  function f22(f = () => eval("x")) { eval("var x = 2"); return f() }
196  assertEquals(1, f22());
197  assertEquals(3, f22(() => 3));
198
199  var g1 = (y = eval("var x = 2")) => { with ({}) { return x; } };
200  assertEquals(1, g1());
201  var g2 = (y = eval("var x = 2"), z = x) => { return z; };
202  assertEquals(1, g2());
203  assertEquals(1, g2(0));
204  var g3 = (y = eval("var x = 2"), z = eval("x")) => { return z; };
205  assertEquals(1, g3());
206  assertEquals(1, g3(0));
207  var g8 = (y = (eval("var x = 2"), x)) => { return y; };
208  assertEquals(2, g8());
209  assertEquals(0, g8(0));
210
211  var g11 = (z = eval("var y = 2")) => { return y; };
212  assertThrows(g11, ReferenceError);
213  var g12 = (z = eval("var y = 2"), b = y) => {};
214  assertThrows(g12, ReferenceError);
215  var g13 = (z = eval("var y = 2"), b = eval("y")) => {};
216  assertThrows(g13, ReferenceError);
217
218  var g21 = (f = () => x) => { eval("var x = 2"); return f() };
219  assertEquals(1, g21());
220  assertEquals(3, g21(() => 3));
221  var g22 = (f = () => eval("x")) => { eval("var x = 2"); return f() };
222  assertEquals(1, g22());
223  assertEquals(3, g22(() => 3));
224})();
225
226
227(function TestStrictEvalScoping() {
228  'use strict';
229  var x = 1;
230
231  function f1(y = eval("var x = 2")) { return x; }
232  assertEquals(1, f1());
233  function f2(y = eval("var x = 2"), z = x) { return z; }
234  assertEquals(1, f2());
235  assertEquals(1, f2(0));
236  function f3(y = eval("var x = 2"), z = eval("x")) { return z; }
237  assertEquals(1, f3());
238  assertEquals(1, f3(0));
239  function f8(y = (eval("var x = 2"), x)) { return y; }
240  assertEquals(1, f8());
241  assertEquals(0, f8(0));
242
243  function f11(z = eval("var y = 2")) { return y; }
244  assertThrows(f11, ReferenceError);
245  function f12(z = eval("var y = 2"), b = y) {}
246  assertThrows(f12, ReferenceError);
247  function f13(z = eval("var y = 2"), b = eval("y")) {}
248  assertThrows(f13, ReferenceError);
249
250  function f21(f = () => x) { eval("var x = 2"); return f() }
251  assertEquals(1, f21());
252  assertEquals(3, f21(() => 3));
253  function f22(f = () => eval("x")) { eval("var x = 2"); return f() }
254  assertEquals(1, f22());
255  assertEquals(3, f22(() => 3));
256})();
257
258(function TestParameterTDZSloppy() {
259  function f1(a = x, x) { return a }
260  assertThrows(() => f1(undefined, 4), ReferenceError);
261  assertEquals(4, f1(4, 5));
262  function f2(a = eval("x"), x) { return a }
263  assertThrows(() => f2(undefined, 4), ReferenceError);
264  assertEquals(4, f2(4, 5));
265  function f3(a = eval("'use strict'; x"), x) { return a }
266  assertThrows(() => f3(undefined, 4), ReferenceError);
267  assertEquals(4, f3(4, 5));
268  function f4(a = () => x, x) { return a() }
269  assertEquals(4, f4(() => 4, 5));
270  function f5(a = () => eval("x"), x) { return a() }
271  assertEquals(4, f5(() => 4, 5));
272  function f6(a = () => eval("'use strict'; x"), x) { return a() }
273  assertEquals(4, f6(() => 4, 5));
274
275  function f11(a = x, x = 2) { return a }
276  assertThrows(() => f11(), ReferenceError);
277  assertThrows(() => f11(undefined), ReferenceError);
278  assertThrows(() => f11(undefined, 4), ReferenceError);
279  assertEquals(4, f1(4, 5));
280  function f12(a = eval("x"), x = 2) { return a }
281  assertThrows(() => f12(), ReferenceError);
282  assertThrows(() => f12(undefined), ReferenceError);
283  assertThrows(() => f12(undefined, 4), ReferenceError);
284  assertEquals(4, f12(4, 5));
285  function f13(a = eval("'use strict'; x"), x = 2) { return a }
286  assertThrows(() => f13(), ReferenceError);
287  assertThrows(() => f13(undefined), ReferenceError);
288  assertThrows(() => f13(undefined, 4), ReferenceError);
289  assertEquals(4, f13(4, 5));
290
291  function f21(x = function() { return a }, ...a) { return x()[0] }
292  assertEquals(4, f21(undefined, 4));
293  function f22(x = () => a, ...a) { return x()[0] }
294  assertEquals(4, f22(undefined, 4));
295  function f23(x = () => eval("a"), ...a) { return x()[0] }
296  assertEquals(4, f23(undefined, 4));
297  function f24(x = () => {'use strict'; return eval("a") }, ...a) {
298    return x()[0]
299  }
300  assertEquals(4, f24(undefined, 4));
301  function f25(x = () => eval("'use strict'; a"), ...a) { return x()[0] }
302  assertEquals(4, f25(undefined, 4));
303
304  var g1 = (x = function() { return a }, ...a) => { return x()[0] };
305  assertEquals(4, g1(undefined, 4));
306  var g2 = (x = () => a, ...a) => { return x()[0] };
307  assertEquals(4, g2(undefined, 4));
308})();
309
310(function TestParameterTDZStrict() {
311  "use strict";
312
313  function f1(a = eval("x"), x) { return a }
314  assertThrows(() => f1(undefined, 4), ReferenceError);
315  assertEquals(4, f1(4, 5));
316  function f2(a = () => eval("x"), x) { return a() }
317  assertEquals(4, f2(() => 4, 5));
318
319  function f11(a = eval("x"), x = 2) { return a }
320  assertThrows(() => f11(), ReferenceError);
321  assertThrows(() => f11(undefined), ReferenceError);
322  assertThrows(() => f11(undefined, 4), ReferenceError);
323  assertEquals(4, f11(4, 5));
324
325  function f21(x = () => eval("a"), ...a) { return x()[0] }
326  assertEquals(4, f21(undefined, 4));
327})();
328
329(function TestArgumentsForNonSimpleParameters() {
330  function f1(x = 900) { arguments[0] = 1; return x }
331  assertEquals(9, f1(9));
332  assertEquals(900, f1());
333  function f2(x = 1001) { x = 2; return arguments[0] }
334  assertEquals(10, f2(10));
335  assertEquals(undefined, f2());
336}());
337
338
339(function TestFunctionLength() {
340   assertEquals(0, (function(x = 1) {}).length);
341   assertEquals(0, (function(x = 1, ...a) {}).length);
342   assertEquals(1, (function(x, y = 1) {}).length);
343   assertEquals(1, (function(x, y = 1, ...a) {}).length);
344   assertEquals(2, (function(x, y, z = 1) {}).length);
345   assertEquals(2, (function(x, y, z = 1, ...a) {}).length);
346   assertEquals(1, (function(x, y = 1, z) {}).length);
347   assertEquals(1, (function(x, y = 1, z, ...a) {}).length);
348   assertEquals(1, (function(x, y = 1, z, v = 2) {}).length);
349   assertEquals(1, (function(x, y = 1, z, v = 2, ...a) {}).length);
350})();
351
352(function TestDirectiveThrows() {
353  "use strict";
354
355  assertThrows(function(){ eval("function(x=1){'use strict';}") }, SyntaxError);
356  assertThrows(function(){ eval("(x=1) => {'use strict';}") }, SyntaxError);
357  assertThrows(
358    function(){ eval("(class{foo(x=1) {'use strict';}});") }, SyntaxError);
359
360  assertThrows(
361    function(){ eval("function(a, x=1){'use strict';}") }, SyntaxError);
362  assertThrows(function(){ eval("(a, x=1) => {'use strict';}") }, SyntaxError);
363  assertThrows(
364    function(){ eval("(class{foo(a, x=1) {'use strict';}});") }, SyntaxError);
365})();
366