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