1// Copyright 2009 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// Testing basic functionality of the arguments object.
28// Introduced to ensure that the fast compiler does the right thing.
29// The arguments object itself.
30assertEquals(42, function(){ return arguments;}(42)[0],
31             "return arguments value");
32assertEquals(42, function(){ return arguments;}(42)[0],
33             "arguments in plain value context");
34assertEquals(42, function(){ arguments;return 42}(37),
35             "arguments in effect context");
36assertEquals(42, function(){ if(arguments)return 42;}(),
37             "arguments in a boolean context");
38assertEquals(42, function(){ return arguments || true;}(42)[0],
39             "arguments in a short-circuit boolean context - or");
40assertEquals(true, function(){ return arguments && [true];}(42)[0],
41             "arguments in a short-circuit boolean context - and");
42assertEquals(42, function(){ arguments = 42; return 42;}(),
43             "arguments assignment");
44// Properties of the arguments object.
45assertEquals(42, function(){ return arguments[0]; }(42),
46             "args[0] value returned");
47assertEquals(42, function(){ arguments[0]; return 42}(),
48             "args[0] value ignored");
49assertEquals(42, function(){ if (arguments[0]) return 42; }(37),
50             "args[0] to boolean");
51assertEquals(42, function(){ return arguments[0] || "no"; }(42),
52             "args[0] short-circuit boolean or true");
53assertEquals(42, function(){ return arguments[0] || 42; }(0),
54             "args[0] short-circuit boolean or false");
55assertEquals(37, function(){ return arguments[0] && 37; }(42),
56             "args[0] short-circuit boolean and true");
57assertEquals(0, function(){ return arguments[0] && 42; }(0),
58             "args[0] short-circuit boolean and false");
59assertEquals(42, function(){ arguments[0] = 42; return arguments[0]; }(37),
60             "args[0] assignment");
61// Link between arguments and parameters.
62assertEquals(42, function(a) { arguments[0] = 42; return a; }(37),
63             "assign args[0]->a");
64assertEquals(42, function(a) { a = 42; return arguments[0]; }(37),
65             "assign a->args[0]");
66assertEquals(54, function(a, b) { arguments[1] = 54; return b; }(42, 37),
67             "assign args[1]->b:b");
68assertEquals(54, function(a, b) { b = 54; return arguments[1]; }(42, 47),
69             "assign b->args[1]:b");
70assertEquals(42, function(a, b) { arguments[1] = 54; return a; }(42, 37),
71             "assign args[1]->b:a");
72assertEquals(42, function(a, b) { b = 54; return arguments[0]; }(42, 47),
73             "assign b->args[1]:a");
74
75// Capture parameters in nested contexts.
76assertEquals(33,
77             function(a,b) {
78                return a + arguments[0] +
79                       function(b){ return a + b + arguments[0]; }(b); }(7,6),
80             "captured parameters");
81assertEquals(42, function(a) {
82                   arguments[0] = 42;
83                   return function(b){ return a; }();
84             }(37),
85             "capture value returned");
86assertEquals(42,
87             function(a) {
88               arguments[0] = 26;
89               return function(b){ a; return 42; }();
90             }(37),
91             "capture value ignored");
92assertEquals(42,
93             function(a) {
94               arguments[0] = 26;
95               return function(b){ if (a) return 42; }();
96              }(37),
97             "capture to boolean");
98assertEquals(42,
99             function(a) {
100               arguments[0] = 42;
101               return function(b){ return a || "no"; }();
102             }(37),
103             "capture short-circuit boolean or true");
104assertEquals(0,
105             function(a) {
106               arguments[0] = 0;
107               return function(b){ return a && 42; }();
108             }(37),
109             "capture short-circuit boolean and false");
110// Deeply nested.
111assertEquals(42,
112             function(a,b) {
113               return arguments[2] +
114                      function(){
115                        return b +
116                               function() {
117                                 return a;
118                               }();
119                      }();
120             }(7,14,21),
121             "deep nested capture");
122
123// Assignment to captured parameters.
124assertEquals(42, function(a,b) {
125                   arguments[1] = 11;
126                   return a + function(){ a = b; return a; }() + a;
127                 }(20, 37), "captured assignment");
128
129// Inside non-function scopes.
130assertEquals(42,
131             function(a) {
132               arguments[0] = 20;
133               with ({ b : 22 }) { return a + b; }
134             }(37),
135             "a in with");
136assertEquals(42,
137             function(a) {
138               with ({ b : 22 }) { return arguments[0] + b; }
139             }(20),
140             "args in with");
141assertEquals(42,
142             function(a) {
143               arguments[0] = 20;
144               with ({ b : 22 }) {
145                 return function() { return a; }() + b; }
146             }(37),
147             "captured a in with");
148assertEquals(42,
149             function(a) {
150               arguments[0] = 12;
151               with ({ b : 22 }) {
152                 return function f() {
153                          try { throw 8 } catch(e) { return e + a };
154                         }() + b;
155               }
156             }(37),
157             "in a catch in a named function captured a in with ");
158// Escaping arguments.
159function weirdargs(a,b,c) { if (!a) return arguments;
160                            return [b[2],c]; }
161var args1 = weirdargs(false, null, 40);
162var res = weirdargs(true, args1, 15);
163assertEquals(40, res[0], "return old args element");
164assertEquals(15, res[1], "return own args element");
165