arguments.js 9.29 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
// Flags: --allow-natives-syntax

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
function argc0() {
  return arguments.length;
}

function argc1(i) {
  return arguments.length;
}

function argc2(i, j) {
  return arguments.length;
}

assertEquals(0, argc0());
assertEquals(1, argc0(1));
assertEquals(2, argc0(1, 2));
assertEquals(3, argc0(1, 2, 3));
assertEquals(0, argc1());
assertEquals(1, argc1(1));
assertEquals(2, argc1(1, 2));
assertEquals(3, argc1(1, 2, 3));
assertEquals(0, argc2());
assertEquals(1, argc2(1));
assertEquals(2, argc2(1, 2));
assertEquals(3, argc2(1, 2, 3));

var index;

function argv0() {
  return arguments[index];
}

function argv1(i) {
  return arguments[index];
}

function argv2(i, j) {
  return arguments[index];
}

index = 0;
assertEquals(7, argv0(7));
assertEquals(7, argv0(7, 8));
assertEquals(7, argv0(7, 8, 9));
assertEquals(7, argv1(7));
assertEquals(7, argv1(7, 8));
assertEquals(7, argv1(7, 8, 9));
assertEquals(7, argv2(7));
assertEquals(7, argv2(7, 8));
assertEquals(7, argv2(7, 8, 9));

index = 1;
assertEquals(8, argv0(7, 8));
assertEquals(8, argv0(7, 8));
assertEquals(8, argv1(7, 8, 9));
assertEquals(8, argv1(7, 8, 9));
assertEquals(8, argv2(7, 8, 9));
assertEquals(8, argv2(7, 8, 9));

index = 2;
assertEquals(9, argv0(7, 8, 9));
assertEquals(9, argv1(7, 8, 9));
assertEquals(9, argv2(7, 8, 9));


// Test that calling a lazily compiled function with
// an unexpected number of arguments works.
function f(a) { return arguments.length; };
assertEquals(3, f(1, 2, 3));
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

function f1(x, y) {
  function g(a) {
    a[0] = "three";
    return a.length;
  }
  var l = g(arguments);
  y = 5;
  assertEquals(2, l);
  assertEquals("three", x);
  assertEquals(5, y);
}
f1(3, "five");


function f2() {
  if (arguments[0] > 0) {
    return arguments.callee(arguments[0] - 1) + arguments[0];
  }
  return 0;
}
assertEquals(55, f2(10));


function f3() {
  assertEquals(0, arguments.length);
}
f3();


function f4() {
  var arguments = 0;
  assertEquals(void 0, arguments.length);
}
f4();


function f5(x, y, z) {
  function g(a) {
    x = "two";
    y = "three";
    a[1] = "drei";
    a[2] = "fuenf";
  };

  g(arguments);
  assertEquals("two", x);
  assertEquals("drei", y);
  assertEquals("fuenf", z);
}
f5(2, 3, 5);


function f6(x, y) {
  x = "x";
  arguments[1] = "y";
  return [arguments.length, arguments[0], y, arguments[2]];
}

157 158
assertArrayEquals([0, void 0, void 0, void 0], f6());
assertArrayEquals([1, "x", void 0, void 0], f6(1));
159 160 161 162 163 164 165 166 167 168 169 170 171 172
assertArrayEquals([2, "x", "y", void 0], f6(9, 17));
assertArrayEquals([3, "x", "y", 7], f6(3, 5, 7));
assertArrayEquals([4, "x", "y", "c"], f6("a", "b", "c", "d"));


function list_args(a) {
  assertEquals("function", typeof a.callee);
  var result = [];
  result.push(a.length);
  for (i = 0; i < a.length; i++) result.push(a[i]);
  return result;
}


jgruber's avatar
jgruber committed
173
function f7(x, y) {
174 175 176 177 178 179 180
  function g(p) {
    x = p;
  }
  g(y);
  return list_args(arguments);
}

jgruber's avatar
jgruber committed
181 182 183 184
assertArrayEquals([0], f7());
assertArrayEquals([1, void 0], f7(3));
assertArrayEquals([2, 5, 5], f7(3, 5));
assertArrayEquals([3, 5, 5, 7], f7(3, 5, 7));
185 186 187 188 189 190 191

// Check out of bounds behavior.
function arg_get(x) { return arguments[x]; }
function arg_del(x) { return delete arguments[x]; }
function arg_set(x) { return (arguments[x] = 117); }
assertEquals(undefined, arg_get(0xFFFFFFFF));
assertEquals(true, arg_del(0xFFFFFFFF));
192
assertEquals(117, arg_set(0xFFFFFFFF));
193 194 195 196 197 198 199 200 201 202 203 204 205 206

(function() {
  function f(a) { return arguments; }
  var a = f(1,2,3);
  // Turn arguments into slow.
  assertTrue(%HasSloppyArgumentsElements(a));
  a[10000] = 1;
  assertTrue(%HasSloppyArgumentsElements(a));
  // Make it fast again by adding values.
  for (var i = 0; i < 1000; i++) {
    a[i] = 1.5;
  }
  assertTrue(%HasSloppyArgumentsElements(a));
})();
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

(function testDeleteArguments() {
  function f() { return arguments };
  var args = f(1, 2);
  assertEquals(1, args[0]);
  assertEquals(2, args[1]);
  assertEquals(2, args.length);

  delete args[0];
  assertEquals(undefined, args[0]);
  assertEquals(2, args[1]);
  assertEquals(2, args.length);

  delete args[1];
  assertEquals(undefined, args[0]);
  assertEquals(undefined, args[1]);
  assertEquals(2, args.length);
})();

(function testDeleteFastSloppyArguments() {
  function f(a) { return arguments };
  var args = f(1, 2);
  assertEquals(1, args[0]);
  assertEquals(2, args[1]);
  assertEquals(2, args.length);

  delete args[0];
  assertEquals(undefined, args[0]);
  assertEquals(2, args[1]);
  assertEquals(2, args.length);

  delete args[1];
  assertEquals(undefined, args[0]);
  assertEquals(undefined, args[1]);
  assertEquals(2, args.length);
})();

(function testDeleteSlowSloppyArguments() {
  var key = 10000;
  function f(a) {
    arguments[key] = key;
    return arguments
  };
  var args = f(1, 2);
251
  %HeapObjectVerify(args);
252 253 254 255 256 257
  assertEquals(1, args[0]);
  assertEquals(2, args[1]);
  assertEquals(key, args[key]);
  assertEquals(2, args.length);

  delete args[0];
258
  %HeapObjectVerify(args);
259 260 261 262 263 264
  assertEquals(undefined, args[0]);
  assertEquals(2, args[1]);
  assertEquals(key, args[key]);
  assertEquals(2, args.length);

  delete args[1];
265
  %HeapObjectVerify(args);
266 267 268 269 270 271
  assertEquals(undefined, args[0]);
  assertEquals(undefined, args[1]);
  assertEquals(key, args[key]);
  assertEquals(2, args.length);

  delete args[key];
272
  %HeapObjectVerify(args);
273 274 275 276 277
  assertEquals(undefined, args[0]);
  assertEquals(undefined, args[1]);
  assertEquals(undefined, args[key]);
  assertEquals(2, args.length);
})();
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
(function testDeleteSlowSloppyArguments2() {
  function f(a) {
    return arguments
  };
  var args = f(1, 2);
  %HeapObjectVerify(args);
  assertEquals(1, args[0]);
  assertEquals(2, args[1]);
  assertEquals(2, args.length);

  delete args[1];
  %HeapObjectVerify(args);
  assertEquals(1, args[0]);
  assertEquals(undefined, args[1]);
  assertEquals(undefined, args[2]);
  assertEquals(2, args.length);

  delete args[0];
  %HeapObjectVerify(args);
  assertEquals(undefined, args[0]);
  assertEquals(undefined, args[1]);
  assertEquals(undefined, args[2]);
  assertEquals(2, args.length);
})();

(function testSloppyArgumentProperties() {
  function f(a, b) { return arguments }
  let args = f(1, 2, 3, 4);
  %HeapObjectVerify(args);
  assertEquals(4, args.length);
  args.foo = "foo";
  %HeapObjectVerify(args);
  assertEquals("foo", args.foo);
  assertEquals(4, args.length);

  delete args.foo;
  %HeapObjectVerify(args);
  assertEquals(undefined, args.foo);
  assertEquals(4, args.length);
})();


(function testSloppyArgumentsLengthMapChange() {
  function f(a) { return arguments };
  let args1 = f(1);
  let args2 = f(1,2);
  assertTrue(%HaveSameMap(args1, args2));
  args2.length = 12;
  assertTrue(%HaveSameMap(args1, args2));
  args2.length = "aa"
  assertTrue(%HaveSameMap(args1, args2));

  let args3 = f(1);
  let args4 = f(1,2);
  // Creating holes causes map transitions.
  assertTrue(%HaveSameMap(args1, args3));
  assertTrue(%HaveSameMap(args1, args4));
  delete args3[0];
  assertFalse(%HaveSameMap(args1, args3));
  delete args4[1];
  assertFalse(%HaveSameMap(args1, args4));
})();

342 343 344 345 346 347 348 349 350 351 352 353 354
(function testSloppyArgumentsLengthMapChange() {
  function f(a) { return arguments };
  let args1 = f(1);
  let args2 = f(1,2);
  assertTrue(%HaveSameMap(args1, args2));
  // Changing the length type doesn't causes a map transition.
  args2.length = 12;
  assertTrue(%HaveSameMap(args1, args2));
  args2.length = 12.0;
  assertTrue(%HaveSameMap(args1, args2));
  args2.length = "aa"
  assertTrue(%HaveSameMap(args1, args2));
})();
355 356 357 358 359 360 361 362 363 364 365 366 367 368


(function testArgumentsVerification() {
  (function f2(a,a) {
    %HeapObjectVerify(arguments);
  })(1,2);

  function f7(a,a,a,a,a,a,a) {
    %HeapObjectVerify(arguments);
  };
  f7(1,2,3,4,5,6);
  f7(1,2,3,4,5,6,7);
  f7(1,2,3,4,5,6,7,8);
})();
369 370 371 372 373 374 375 376 377 378 379

(function testArgumentsHole() {
  function f(a) {
    arguments[3] = 1;
    return arguments[2];
  };

  assertEquals(undefined, f(1));
  assertEquals(undefined, f(1));
  assertEquals(undefined, f(1));
})();