arguments-iterator.js 6.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax


// Note in general that "arguments.foo" and "var o = arguments; o.foo"
// are treated differently by full-codegen, and so both cases need to be
// tested.

function TestDirectArgumentsIteratorProperty() {
  assertTrue(arguments.hasOwnProperty(Symbol.iterator));
  assertFalse(arguments.propertyIsEnumerable(Symbol.iterator));
  var descriptor = Object.getOwnPropertyDescriptor(arguments, Symbol.iterator);
  assertTrue(descriptor.writable);
  assertFalse(descriptor.enumerable);
  assertTrue(descriptor.configurable);
19 20
  assertEquals(descriptor.value, [][Symbol.iterator]);
  assertEquals(arguments[Symbol.iterator], [][Symbol.iterator]);
21 22 23 24 25 26 27 28
}
TestDirectArgumentsIteratorProperty();


function TestIndirectArgumentsIteratorProperty() {
  var o = arguments;
  assertTrue(o.hasOwnProperty(Symbol.iterator));
  assertFalse(o.propertyIsEnumerable(Symbol.iterator));
29
  assertEquals(o[Symbol.iterator], [][Symbol.iterator]);
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 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 157 158 159 160 161 162
}
TestIndirectArgumentsIteratorProperty();


function assertIteratorResult(value, done, result) {
  assertEquals({value: value, done: done}, result);
}


function TestDirectValues1(a, b, c) {
  var iterator = arguments[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());
  assertIteratorResult(c, false, iterator.next());
  assertIteratorResult(undefined, true, iterator.next());
}
TestDirectValues1(1, 2, 3);


function TestIndirectValues1(a, b, c) {
  var args = arguments;
  var iterator = args[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());
  assertIteratorResult(c, false, iterator.next());
  assertIteratorResult(undefined, true, iterator.next());
}
TestIndirectValues1(1, 2, 3);


function TestDirectValues2(a, b, c) {
  var iterator = arguments[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());
  assertIteratorResult(c, false, iterator.next());
  assertIteratorResult(undefined, true, iterator.next());

  arguments[3] = 4;
  arguments.length = 4;
  assertIteratorResult(undefined, true, iterator.next());
}
TestDirectValues2(1, 2, 3);


function TestIndirectValues2(a, b, c) {
  var args = arguments;
  var iterator = args[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());
  assertIteratorResult(c, false, iterator.next());
  assertIteratorResult(undefined, true, iterator.next());

  arguments[3] = 4;
  arguments.length = 4;
  assertIteratorResult(undefined, true, iterator.next());
}
TestIndirectValues2(1, 2, 3);


function TestDirectValues3(a, b, c) {
  var iterator = arguments[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());

  arguments.length = 2;
  assertIteratorResult(undefined, true, iterator.next());
}
TestDirectValues3(1, 2, 3);


function TestIndirectValues3(a, b, c) {
  var args = arguments;
  var iterator = args[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());

  arguments.length = 2;
  assertIteratorResult(undefined, true, iterator.next());
}
TestIndirectValues3(1, 2, 3);


function TestDirectValues4(a, b, c) {
  var iterator = arguments[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());
  assertIteratorResult(c, false, iterator.next());

  arguments.length = 4;
  assertIteratorResult(undefined, false, iterator.next());
  assertIteratorResult(undefined, true, iterator.next());
}
TestDirectValues4(1, 2, 3);


function TestIndirectValues4(a, b, c) {
  var args = arguments;
  var iterator = args[Symbol.iterator]();
  assertIteratorResult(a, false, iterator.next());
  assertIteratorResult(b, false, iterator.next());
  assertIteratorResult(c, false, iterator.next());

  arguments.length = 4;
  assertIteratorResult(undefined, false, iterator.next());
  assertIteratorResult(undefined, true, iterator.next());
}
TestIndirectValues4(1, 2, 3);


function TestForOf() {
  var i = 0;
  for (var value of arguments) {
    assertEquals(arguments[i++], value);
  }

  assertEquals(arguments.length, i);
}
TestForOf(1, 2, 3, 4, 5);


function TestAssignmentToIterator() {
  var i = 0;
  arguments[Symbol.iterator] = [].entries;
  for (var entry of arguments) {
    assertEquals([i, arguments[i]], entry);
    i++;
  }

  assertEquals(arguments.length, i);
}
TestAssignmentToIterator(1, 2, 3, 4, 5);


163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
// Regression test for crbug.com/521484.
function TestAssignmentToIterator2() {
  var i = 0;
  arguments.__defineGetter__('callee', function(){});
  arguments.__defineGetter__('length', function(){ return 1 });
  arguments[Symbol.iterator] = [].entries;
  for (var entry of arguments) {
    assertEquals([i, arguments[i]], entry);
    i++;
  }

  assertEquals(arguments.length, i);
}
TestAssignmentToIterator2(1, 2, 3, 4, 5);

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
function TestArgumentsMutation() {
  var i = 0;
  for (var x of arguments) {
    assertEquals(arguments[i], x);
    arguments[i+1] *= 2;
    i++;
  }

  assertEquals(arguments.length, i);
}
TestArgumentsMutation(1, 2, 3, 4, 5);


function TestSloppyArgumentsAliasing(a0, a1, a2, a3, a4) {
  var i = 0;
  for (var x of arguments) {
    assertEquals(arguments[i], x);
    a0 = a1; a1 = a2; a3 = a4;
    i++;
  }

  assertEquals(arguments.length, i);
}
TestSloppyArgumentsAliasing(1, 2, 3, 4, 5);


function TestStrictArgumentsAliasing(a0, a1, a2, a3, a4) {
  "use strict";
  var i = 0;
  for (var x of arguments) {
    a0 = a1; a1 = a2; a3 = a4;
    assertEquals(arguments[i], x);
    i++;
  }

  assertEquals(arguments.length, i);
}
TestStrictArgumentsAliasing(1, 2, 3, 4, 5);


function TestArgumentsAsProto() {
  "use strict";

  var o = {__proto__:arguments};
222
  assertSame([][Symbol.iterator], o[Symbol.iterator]);
223 224 225
  // Make o dict-mode.
  %OptimizeObjectForAddingMultipleProperties(o, 0);
  assertFalse(o.hasOwnProperty(Symbol.iterator));
226
  assertSame([][Symbol.iterator], o[Symbol.iterator]);
227 228 229
  o[Symbol.iterator] = 10;
  assertTrue(o.hasOwnProperty(Symbol.iterator));
  assertEquals(10, o[Symbol.iterator]);
230
  assertSame([][Symbol.iterator], arguments[Symbol.iterator]);
231 232 233

  // Frozen o.
  o = Object.freeze({__proto__:arguments});
234
  assertSame([][Symbol.iterator], o[Symbol.iterator]);
235
  assertFalse(o.hasOwnProperty(Symbol.iterator));
236
  assertSame([][Symbol.iterator], o[Symbol.iterator]);
237
  assertThrows(function () { o[Symbol.iterator] = 10 });
238
  assertFalse(o.hasOwnProperty(Symbol.iterator));
239 240
  assertEquals([][Symbol.iterator], o[Symbol.iterator]);
  assertSame([][Symbol.iterator], arguments[Symbol.iterator]);
241 242
}
TestArgumentsAsProto();