1// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --allow-natives-syntax
29
30function argc0() {
31  return arguments.length;
32}
33
34function argc1(i) {
35  return arguments.length;
36}
37
38function argc2(i, j) {
39  return arguments.length;
40}
41
42assertEquals(0, argc0());
43assertEquals(1, argc0(1));
44assertEquals(2, argc0(1, 2));
45assertEquals(3, argc0(1, 2, 3));
46assertEquals(0, argc1());
47assertEquals(1, argc1(1));
48assertEquals(2, argc1(1, 2));
49assertEquals(3, argc1(1, 2, 3));
50assertEquals(0, argc2());
51assertEquals(1, argc2(1));
52assertEquals(2, argc2(1, 2));
53assertEquals(3, argc2(1, 2, 3));
54
55var index;
56
57function argv0() {
58  return arguments[index];
59}
60
61function argv1(i) {
62  return arguments[index];
63}
64
65function argv2(i, j) {
66  return arguments[index];
67}
68
69index = 0;
70assertEquals(7, argv0(7));
71assertEquals(7, argv0(7, 8));
72assertEquals(7, argv0(7, 8, 9));
73assertEquals(7, argv1(7));
74assertEquals(7, argv1(7, 8));
75assertEquals(7, argv1(7, 8, 9));
76assertEquals(7, argv2(7));
77assertEquals(7, argv2(7, 8));
78assertEquals(7, argv2(7, 8, 9));
79
80index = 1;
81assertEquals(8, argv0(7, 8));
82assertEquals(8, argv0(7, 8));
83assertEquals(8, argv1(7, 8, 9));
84assertEquals(8, argv1(7, 8, 9));
85assertEquals(8, argv2(7, 8, 9));
86assertEquals(8, argv2(7, 8, 9));
87
88index = 2;
89assertEquals(9, argv0(7, 8, 9));
90assertEquals(9, argv1(7, 8, 9));
91assertEquals(9, argv2(7, 8, 9));
92
93
94// Test that calling a lazily compiled function with
95// an unexpected number of arguments works.
96function f(a) { return arguments.length; };
97assertEquals(3, f(1, 2, 3));
98
99function f1(x, y) {
100  function g(a) {
101    a[0] = "three";
102    return a.length;
103  }
104  var l = g(arguments);
105  y = 5;
106  assertEquals(2, l);
107  assertEquals("three", x);
108  assertEquals(5, y);
109}
110f1(3, "five");
111
112
113function f2() {
114  if (arguments[0] > 0) {
115    return arguments.callee(arguments[0] - 1) + arguments[0];
116  }
117  return 0;
118}
119assertEquals(55, f2(10));
120
121
122function f3() {
123  assertEquals(0, arguments.length);
124}
125f3();
126
127
128function f4() {
129  var arguments = 0;
130  assertEquals(void 0, arguments.length);
131}
132f4();
133
134
135function f5(x, y, z) {
136  function g(a) {
137    x = "two";
138    y = "three";
139    a[1] = "drei";
140    a[2] = "fuenf";
141  };
142
143  g(arguments);
144  assertEquals("two", x);
145  assertEquals("drei", y);
146  assertEquals("fuenf", z);
147}
148f5(2, 3, 5);
149
150
151function f6(x, y) {
152  x = "x";
153  arguments[1] = "y";
154  return [arguments.length, arguments[0], y, arguments[2]];
155}
156
157assertArrayEquals([0, void 0, void 0, void 0], f6());
158assertArrayEquals([1, "x", void 0, void 0], f6(1));
159assertArrayEquals([2, "x", "y", void 0], f6(9, 17));
160assertArrayEquals([3, "x", "y", 7], f6(3, 5, 7));
161assertArrayEquals([4, "x", "y", "c"], f6("a", "b", "c", "d"));
162
163
164function list_args(a) {
165  assertEquals("function", typeof a.callee);
166  var result = [];
167  result.push(a.length);
168  for (i = 0; i < a.length; i++) result.push(a[i]);
169  return result;
170}
171
172
173function f1(x, y) {
174  function g(p) {
175    x = p;
176  }
177  g(y);
178  return list_args(arguments);
179}
180
181assertArrayEquals([0], f1());
182assertArrayEquals([1, void 0], f1(3));
183assertArrayEquals([2, 5, 5], f1(3, 5));
184assertArrayEquals([3, 5, 5, 7], f1(3, 5, 7));
185
186// Check out of bounds behavior.
187function arg_get(x) { return arguments[x]; }
188function arg_del(x) { return delete arguments[x]; }
189function arg_set(x) { return (arguments[x] = 117); }
190assertEquals(undefined, arg_get(0xFFFFFFFF));
191assertEquals(true, arg_del(0xFFFFFFFF));
192assertEquals(117, arg_set(0xFFFFFFFF));
193
194(function() {
195  function f(a) { return arguments; }
196  var a = f(1,2,3);
197  // Turn arguments into slow.
198  assertTrue(%HasSloppyArgumentsElements(a));
199  a[10000] = 1;
200  assertTrue(%HasSloppyArgumentsElements(a));
201  // Make it fast again by adding values.
202  for (var i = 0; i < 1000; i++) {
203    a[i] = 1.5;
204  }
205  assertTrue(%HasSloppyArgumentsElements(a));
206})();
207