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
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.
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.
30 assertEquals(42, function(){ return arguments;}(42)[0],
31 "return arguments value");
32 assertEquals(42, function(){ return arguments;}(42)[0],
33 "arguments in plain value context");
34 assertEquals(42, function(){ arguments;return 42}(37),
35 "arguments in effect context");
36 assertEquals(42, function(){ if(arguments)return 42;}(),
37 "arguments in a boolean context");
38 assertEquals(42, function(){ return arguments || true;}(42)[0],
39 "arguments in a short-circuit boolean context - or");
40 assertEquals(true, function(){ return arguments && [true];}(42)[0],
41 "arguments in a short-circuit boolean context - and");
42 assertEquals(42, function(){ arguments = 42; return 42;}(),
43 "arguments assignment");
44 // Properties of the arguments object.
45 assertEquals(42, function(){ return arguments[0]; }(42),
46 "args[0] value returned");
47 assertEquals(42, function(){ arguments[0]; return 42}(),
48 "args[0] value ignored");
49 assertEquals(42, function(){ if (arguments[0]) return 42; }(37),
50 "args[0] to boolean");
51 assertEquals(42, function(){ return arguments[0] || "no"; }(42),
52 "args[0] short-circuit boolean or true");
53 assertEquals(42, function(){ return arguments[0] || 42; }(0),
54 "args[0] short-circuit boolean or false");
55 assertEquals(37, function(){ return arguments[0] && 37; }(42),
56 "args[0] short-circuit boolean and true");
57 assertEquals(0, function(){ return arguments[0] && 42; }(0),
58 "args[0] short-circuit boolean and false");
59 assertEquals(42, function(){ arguments[0] = 42; return arguments[0]; }(37),
60 "args[0] assignment");
61 // Link between arguments and parameters.
62 assertEquals(42, function(a) { arguments[0] = 42; return a; }(37),
64 assertEquals(42, function(a) { a = 42; return arguments[0]; }(37),
66 assertEquals(54, function(a, b) { arguments[1] = 54; return b; }(42, 37),
67 "assign args[1]->b:b");
68 assertEquals(54, function(a, b) { b = 54; return arguments[1]; }(42, 47),
69 "assign b->args[1]:b");
70 assertEquals(42, function(a, b) { arguments[1] = 54; return a; }(42, 37),
71 "assign args[1]->b:a");
72 assertEquals(42, function(a, b) { b = 54; return arguments[0]; }(42, 47),
73 "assign b->args[1]:a");
75 // Capture parameters in nested contexts.
78 return a + arguments[0] +
79 function(b){ return a + b + arguments[0]; }(b); }(7,6),
80 "captured parameters");
81 assertEquals(42, function(a) {
83 return function(b){ return a; }();
85 "capture value returned");
89 return function(b){ a; return 42; }();
91 "capture value ignored");
95 return function(b){ if (a) return 42; }();
97 "capture to boolean");
101 return function(b){ return a || "no"; }();
103 "capture short-circuit boolean or true");
107 return function(b){ return a && 42; }();
109 "capture short-circuit boolean and false");
113 return arguments[2] +
121 "deep nested capture");
123 // Assignment to captured parameters.
124 assertEquals(42, function(a,b) {
126 return a + function(){ a = b; return a; }() + a;
127 }(20, 37), "captured assignment");
129 // Inside non-function scopes.
133 with ({ b : 22 }) { return a + b; }
138 with ({ b : 22 }) { return arguments[0] + b; }
145 return function() { return a; }() + b; }
147 "captured a in with");
152 return function f() {
153 try { throw 8 } catch(e) { return e + a };
157 "in a catch in a named function captured a in with ");
158 // Escaping arguments.
159 function weirdargs(a,b,c) { if (!a) return arguments;
161 var args1 = weirdargs(false, null, 40);
162 var res = weirdargs(true, args1, 15);
163 assertEquals(40, res[0], "return old args element");
164 assertEquals(15, res[1], "return own args element");