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 28function argc0() { 29 return arguments.length; 30} 31 32function argc1(i) { 33 return arguments.length; 34} 35 36function argc2(i, j) { 37 return arguments.length; 38} 39 40assertEquals(0, argc0()); 41assertEquals(1, argc0(1)); 42assertEquals(2, argc0(1, 2)); 43assertEquals(3, argc0(1, 2, 3)); 44assertEquals(0, argc1()); 45assertEquals(1, argc1(1)); 46assertEquals(2, argc1(1, 2)); 47assertEquals(3, argc1(1, 2, 3)); 48assertEquals(0, argc2()); 49assertEquals(1, argc2(1)); 50assertEquals(2, argc2(1, 2)); 51assertEquals(3, argc2(1, 2, 3)); 52 53var index; 54 55function argv0() { 56 return arguments[index]; 57} 58 59function argv1(i) { 60 return arguments[index]; 61} 62 63function argv2(i, j) { 64 return arguments[index]; 65} 66 67index = 0; 68assertEquals(7, argv0(7)); 69assertEquals(7, argv0(7, 8)); 70assertEquals(7, argv0(7, 8, 9)); 71assertEquals(7, argv1(7)); 72assertEquals(7, argv1(7, 8)); 73assertEquals(7, argv1(7, 8, 9)); 74assertEquals(7, argv2(7)); 75assertEquals(7, argv2(7, 8)); 76assertEquals(7, argv2(7, 8, 9)); 77 78index = 1; 79assertEquals(8, argv0(7, 8)); 80assertEquals(8, argv0(7, 8)); 81assertEquals(8, argv1(7, 8, 9)); 82assertEquals(8, argv1(7, 8, 9)); 83assertEquals(8, argv2(7, 8, 9)); 84assertEquals(8, argv2(7, 8, 9)); 85 86index = 2; 87assertEquals(9, argv0(7, 8, 9)); 88assertEquals(9, argv1(7, 8, 9)); 89assertEquals(9, argv2(7, 8, 9)); 90 91 92// Test that calling a lazily compiled function with 93// an unexpected number of arguments works. 94function f(a) { return arguments.length; }; 95assertEquals(3, f(1, 2, 3)); 96 97function f1(x, y) { 98 function g(a) { 99 a[0] = "three"; 100 return a.length; 101 } 102 var l = g(arguments); 103 y = 5; 104 assertEquals(2, l); 105 assertEquals("three", x); 106 assertEquals(5, y); 107} 108f1(3, "five"); 109 110 111function f2() { 112 if (arguments[0] > 0) { 113 return arguments.callee(arguments[0] - 1) + arguments[0]; 114 } 115 return 0; 116} 117assertEquals(55, f2(10)); 118 119 120function f3() { 121 assertEquals(0, arguments.length); 122} 123f3(); 124 125 126function f4() { 127 var arguments = 0; 128 assertEquals(void 0, arguments.length); 129} 130f4(); 131 132 133function f5(x, y, z) { 134 function g(a) { 135 x = "two"; 136 y = "three"; 137 a[1] = "drei"; 138 a[2] = "fuenf"; 139 }; 140 141 g(arguments); 142 assertEquals("two", x); 143 assertEquals("drei", y); 144 assertEquals("fuenf", z); 145} 146f5(2, 3, 5); 147 148 149function f6(x, y) { 150 x = "x"; 151 arguments[1] = "y"; 152 return [arguments.length, arguments[0], y, arguments[2]]; 153} 154 155assertArrayEquals([0, void 0, void 0, void 0], f6()); 156assertArrayEquals([1, "x", void 0, void 0], f6(1)); 157assertArrayEquals([2, "x", "y", void 0], f6(9, 17)); 158assertArrayEquals([3, "x", "y", 7], f6(3, 5, 7)); 159assertArrayEquals([4, "x", "y", "c"], f6("a", "b", "c", "d")); 160 161 162function list_args(a) { 163 assertEquals("function", typeof a.callee); 164 var result = []; 165 result.push(a.length); 166 for (i = 0; i < a.length; i++) result.push(a[i]); 167 return result; 168} 169 170 171function f1(x, y) { 172 function g(p) { 173 x = p; 174 } 175 g(y); 176 return list_args(arguments); 177} 178 179assertArrayEquals([0], f1()); 180assertArrayEquals([1, void 0], f1(3)); 181assertArrayEquals([2, 5, 5], f1(3, 5)); 182assertArrayEquals([3, 5, 5, 7], f1(3, 5, 7)); 183 184// Check out of bounds behavior. 185function arg_get(x) { return arguments[x]; } 186function arg_del(x) { return delete arguments[x]; } 187function arg_set(x) { return (arguments[x] = 117); } 188assertEquals(undefined, arg_get(0xFFFFFFFF)); 189assertEquals(true, arg_del(0xFFFFFFFF)); 190assertEquals(117, arg_set(0xFFFFFFFF));