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 30 function argc0() { 31 return arguments.length; 32 } 33 34 function argc1(i) { 35 return arguments.length; 36 } 37 38 function argc2(i, j) { 39 return arguments.length; 40 } 41 42 assertEquals(0, argc0()); 43 assertEquals(1, argc0(1)); 44 assertEquals(2, argc0(1, 2)); 45 assertEquals(3, argc0(1, 2, 3)); 46 assertEquals(0, argc1()); 47 assertEquals(1, argc1(1)); 48 assertEquals(2, argc1(1, 2)); 49 assertEquals(3, argc1(1, 2, 3)); 50 assertEquals(0, argc2()); 51 assertEquals(1, argc2(1)); 52 assertEquals(2, argc2(1, 2)); 53 assertEquals(3, argc2(1, 2, 3)); 54 55 var index; 56 57 function argv0() { 58 return arguments[index]; 59 } 60 61 function argv1(i) { 62 return arguments[index]; 63 } 64 65 function argv2(i, j) { 66 return arguments[index]; 67 } 68 69 index = 0; 70 assertEquals(7, argv0(7)); 71 assertEquals(7, argv0(7, 8)); 72 assertEquals(7, argv0(7, 8, 9)); 73 assertEquals(7, argv1(7)); 74 assertEquals(7, argv1(7, 8)); 75 assertEquals(7, argv1(7, 8, 9)); 76 assertEquals(7, argv2(7)); 77 assertEquals(7, argv2(7, 8)); 78 assertEquals(7, argv2(7, 8, 9)); 79 80 index = 1; 81 assertEquals(8, argv0(7, 8)); 82 assertEquals(8, argv0(7, 8)); 83 assertEquals(8, argv1(7, 8, 9)); 84 assertEquals(8, argv1(7, 8, 9)); 85 assertEquals(8, argv2(7, 8, 9)); 86 assertEquals(8, argv2(7, 8, 9)); 87 88 index = 2; 89 assertEquals(9, argv0(7, 8, 9)); 90 assertEquals(9, argv1(7, 8, 9)); 91 assertEquals(9, argv2(7, 8, 9)); 92 93 94 // Test that calling a lazily compiled function with 95 // an unexpected number of arguments works. 96 function f(a) { return arguments.length; }; 97 assertEquals(3, f(1, 2, 3)); 98 99 function 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 } 110 f1(3, "five"); 111 112 113 function f2() { 114 if (arguments[0] > 0) { 115 return arguments.callee(arguments[0] - 1) + arguments[0]; 116 } 117 return 0; 118 } 119 assertEquals(55, f2(10)); 120 121 122 function f3() { 123 assertEquals(0, arguments.length); 124 } 125 f3(); 126 127 128 function f4() { 129 var arguments = 0; 130 assertEquals(void 0, arguments.length); 131 } 132 f4(); 133 134 135 function 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 } 148 f5(2, 3, 5); 149 150 151 function f6(x, y) { 152 x = "x"; 153 arguments[1] = "y"; 154 return [arguments.length, arguments[0], y, arguments[2]]; 155 } 156 157 assertArrayEquals([0, void 0, void 0, void 0], f6()); 158 assertArrayEquals([1, "x", void 0, void 0], f6(1)); 159 assertArrayEquals([2, "x", "y", void 0], f6(9, 17)); 160 assertArrayEquals([3, "x", "y", 7], f6(3, 5, 7)); 161 assertArrayEquals([4, "x", "y", "c"], f6("a", "b", "c", "d")); 162 163 164 function 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 173 function f1(x, y) { 174 function g(p) { 175 x = p; 176 } 177 g(y); 178 return list_args(arguments); 179 } 180 181 assertArrayEquals([0], f1()); 182 assertArrayEquals([1, void 0], f1(3)); 183 assertArrayEquals([2, 5, 5], f1(3, 5)); 184 assertArrayEquals([3, 5, 5, 7], f1(3, 5, 7)); 185 186 // Check out of bounds behavior. 187 function arg_get(x) { return arguments[x]; } 188 function arg_del(x) { return delete arguments[x]; } 189 function arg_set(x) { return (arguments[x] = 117); } 190 assertEquals(undefined, arg_get(0xFFFFFFFF)); 191 assertEquals(true, arg_del(0xFFFFFFFF)); 192 assertEquals(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 208 (function testDeleteArguments() { 209 function f() { return arguments }; 210 var args = f(1, 2); 211 assertEquals(1, args[0]); 212 assertEquals(2, args[1]); 213 assertEquals(2, args.length); 214 215 delete args[0]; 216 assertEquals(undefined, args[0]); 217 assertEquals(2, args[1]); 218 assertEquals(2, args.length); 219 220 delete args[1]; 221 assertEquals(undefined, args[0]); 222 assertEquals(undefined, args[1]); 223 assertEquals(2, args.length); 224 })(); 225 226 (function testDeleteFastSloppyArguments() { 227 function f(a) { return arguments }; 228 var args = f(1, 2); 229 assertEquals(1, args[0]); 230 assertEquals(2, args[1]); 231 assertEquals(2, args.length); 232 233 delete args[0]; 234 assertEquals(undefined, args[0]); 235 assertEquals(2, args[1]); 236 assertEquals(2, args.length); 237 238 delete args[1]; 239 assertEquals(undefined, args[0]); 240 assertEquals(undefined, args[1]); 241 assertEquals(2, args.length); 242 })(); 243 244 (function testDeleteSlowSloppyArguments() { 245 var key = 10000; 246 function f(a) { 247 arguments[key] = key; 248 return arguments 249 }; 250 var args = f(1, 2); 251 assertEquals(1, args[0]); 252 assertEquals(2, args[1]); 253 assertEquals(key, args[key]); 254 assertEquals(2, args.length); 255 256 delete args[0]; 257 assertEquals(undefined, args[0]); 258 assertEquals(2, args[1]); 259 assertEquals(key, args[key]); 260 assertEquals(2, args.length); 261 262 delete args[1]; 263 assertEquals(undefined, args[0]); 264 assertEquals(undefined, args[1]); 265 assertEquals(key, args[key]); 266 assertEquals(2, args.length); 267 268 delete args[key]; 269 assertEquals(undefined, args[0]); 270 assertEquals(undefined, args[1]); 271 assertEquals(undefined, args[key]); 272 assertEquals(2, args.length); 273 })(); 274