1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15var local = 0;
16
17switch(0) { /* This switch forces a pre-scanner run. */
18default:
19
20  function f(a = (5, local = 6),
21             b = ((5 + function(a = 6) { return a }() * 3)),
22             c,
23             d = true ? 1 : 2)
24  {
25    return "" + a + ", " + b + ", " + c + ", " + d;
26  }
27
28  assert(f() === "6, 23, undefined, 1");
29  assert(local === 6);
30
31  var obj = {
32    f: function(a = [10,,20],
33                b,
34                c = Math.cos(0),
35                d)
36    {
37      return "" + a + ", " + b + ", " + c + ", " + d;
38    }
39  };
40
41  assert(obj.f() === "10,,20, undefined, 1, undefined");
42
43  function g(a, b = (local = 7)) { }
44
45  local = 0;
46  g();
47  assert(local === 7);
48
49  local = 0;
50  g(0);
51  assert(local === 7);
52
53  local = 0;
54  g(0, undefined);
55  assert(local === 7);
56
57  local = 0;
58  g(0, null);
59  assert(local === 0);
60
61  local = 0;
62  g(0, false);
63  assert(local === 0);
64  break;
65}
66
67function CheckSyntaxError(str)
68{
69  try {
70    eval(str);
71    assert(false);
72  } catch (e) {
73    assert(e instanceof SyntaxError);
74  }
75}
76
77CheckSyntaxError('function x(a += 5) {}');
78CheckSyntaxError('function x(a =, b) {}');
79CheckSyntaxError('function x(a = (b) {}');
80CheckSyntaxError('function x(a, a = 5) {}');
81CheckSyntaxError('function x(a = 5, a) {}');
82
83// Pre-scanner tests.
84var str = "a = 5, b, c = function() { for (var a = 0; a < 4; a++) ; return a; } ()"
85
86var f = new Function (str, str);
87f();
88
89var f = new Function (str, "return (a + c) * (b == undefined ? 1 : 0)");
90assert (f() == 9);
91
92function duplicatedArg (a = c, b = d, c) {
93  assert(a === 1);
94  assert(b === 2);
95  assert(c === 3);
96}
97
98duplicatedArg(1, 2, 3);
99