array-of.js 5.66 KB
Newer Older
1 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 28 29 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
// 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.

// Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/source/js/src/jit-test/tests/collections



// Array.of makes real arrays.

function check(a) {
    assertEquals(Object.getPrototypeOf(a), Array.prototype);
    assertEquals(Array.isArray(a), true);
    a[9] = 9;
    assertEquals(a.length, 10);
}


check(Array.of());
check(Array.of(0));
check(Array.of(0, 1, 2));
var f = Array.of;
check(f());


// Array.of basics

var a = Array.of();

assertEquals(a.length, 0);
a = Array.of(undefined, null, 3.14, []);
assertEquals(a, [undefined, null, 3.14, []]);
a = [];
for (var i = 0; i < 1000; i++)
    a[i] = i;
assertEquals(Array.of.apply(null, a), a);


// Array.of does not leave holes

assertEquals(Array.of(undefined), [undefined]);
assertEquals(Array.of(undefined, undefined), [undefined, undefined]);
assertEquals(Array.of.apply(null, [,,undefined]), [undefined, undefined, undefined]);
assertEquals(Array.of.apply(null, Array(4)), [undefined, undefined, undefined, undefined]);


// Array.of can be transplanted to other classes.

var hits = 0;
function Bag() {
    hits++;
}
Bag.of = Array.of;

hits = 0;
var actual = Bag.of("zero", "one");
assertEquals(hits, 1);

hits = 0;
var expected = new Bag;
expected[0] = "zero";
expected[1] = "one";
expected.length = 2;
assertEquals(areSame(actual, expected), true);

hits = 0;
actual = Array.of.call(Bag, "zero", "one");
assertEquals(hits, 1);
assertEquals(areSame(actual, expected), true);

function areSame(object, array) {
    var result = object.length == array.length;
    for (var i = 0; i < object.length; i++) {
        result = result && object[i] == array[i];
    }
    return result;
}


// Array.of does not trigger prototype setters.
// (It defines elements rather than assigning to them.)

var status = "pass";
Object.defineProperty(Array.prototype, "0", {set: function(v) {status = "FAIL 1"}});
assertEquals(Array.of(1)[0], 1);
assertEquals(status, "pass");

Object.defineProperty(Bag.prototype, "0", {set: function(v) {status = "FAIL 2"}});
assertEquals(Bag.of(1)[0], 1);
assertEquals(status, "pass");


// Array.of passes the number of arguments to the constructor it calls.

var hits = 0;

function Herd(n) {
    assertEquals(arguments.length, 1);
    assertEquals(n, 5);
    hits++;
}

Herd.of = Array.of;
Herd.of("sheep", "cattle", "elephants", "whales", "seals");
assertEquals(hits, 1);


// Array.of calls a "length" setter if one is present.

var hits = 0;
var lastObj = null, lastVal = undefined;
function setter(v) {
    hits++;
    lastObj = this;
    lastVal = v;
}

// when the setter is on the new object
function Pack() {
    Object.defineProperty(this, "length", {set: setter});
}
Pack.of = Array.of;
var pack = Pack.of("wolves", "cards", "cigarettes", "lies");
assertEquals(lastObj, pack);
assertEquals(lastVal, 4);

// when the setter is on the new object's prototype
function Bevy() {}
Object.defineProperty(Bevy.prototype, "length", {set: setter});
Bevy.of = Array.of;
var bevy = Bevy.of("quail");
assertEquals(lastObj, bevy);
assertEquals(lastVal, 1);


// Array.of does a strict assignment to the new object's .length.
// The assignment is strict even if the code we're calling from is not strict.

function Empty() {}
Empty.of = Array.of;
Object.defineProperty(Empty.prototype, "length", {get: function() { return 0; }});

var nothing = new Empty;
nothing.length = 2;  // no exception; this is not a strict mode assignment

assertThrows(function() { Empty.of(); }, TypeError);


// Check superficial features of Array.of.

var desc = Object.getOwnPropertyDescriptor(Array, "of");

assertEquals(desc.configurable, true);
assertEquals(desc.enumerable, false);
assertEquals(desc.writable, true);
assertEquals(Array.of.length, 0);
assertThrows(function() { new Array.of() }, TypeError);  // not a constructor

// When the this-value passed in is not a constructor, the result is an array.
160 161 162 163 164 165 166 167 168 169 170 171 172
[
  undefined,
  null,
  false,
  "cow",
  NaN,
  67,
  Infinity,
  -Infinity,
  Math.cos, // builtin functions with no [[Construct]] slot
  Math.cos.bind(Math) // bound builtin functions with no [[Construct]] slot
].forEach(function(val) {
    assertEquals(Array.isArray(Array.of.call(val, val)), true);
173
});
174 175 176 177 178 179 180 181 182


(function testBoundConstructor() {
  var boundFn = (function() {}).bind(null);
  var instance = Array.of.call(boundFn, 1, 2, 3);
  assertEquals(instance.length, 3);
  assertEquals(instance instanceof boundFn, true);
  assertEquals(Array.isArray(instance), false);
})();
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

(function testDefinesOwnProperty() {
  // Assert that [[DefineOwnProperty]] is used in ArrayFrom, meaning a
  // setter isn't called, and a failed [[DefineOwnProperty]] will throw.
  var setterCalled = 0;
  function exotic() {
    Object.defineProperty(this,  '0', {
      get: function() { return 2; },
      set: function() { setterCalled++; }
    });
  }
  // Non-configurable properties can't be overwritten with DefineOwnProperty
  assertThrows(function () { Array.of.call(exotic, 1); }, TypeError);
  // The setter wasn't called
  assertEquals(0, setterCalled);

  // Check that array properties defined are writable, enumerable, configurable
  function ordinary() { }
  var x = Array.of.call(ordinary, 2);
  var xlength = Object.getOwnPropertyDescriptor(x, 'length');
  assertEquals(1, xlength.value);
  assertEquals(true, xlength.writable);
  assertEquals(true, xlength.enumerable);
  assertEquals(true, xlength.configurable);
  var x0 = Object.getOwnPropertyDescriptor(x, 0);
  assertEquals(2, x0.value);
  assertEquals(true, xlength.writable);
  assertEquals(true, xlength.enumerable);
  assertEquals(true, xlength.configurable);
})();