summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/es6/array-fill-receiver.js
blob: 21d7a2ab035c407e4b6701480d362f058eeb9862 (plain)
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
// Copyright 2018 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

// Ensure `Array.prototype.fill` functions correctly for numerous elements
// kinds.

// If no arguments are provided, call Array.p.fill without any arguments,
// otherwise the test is allowed to specify what value to use to better control
// ElementsKind transitions. From and to is provided by the harness.
function callAndAssertFill(object, test_value, harness_value, from, to) {
  let value = arguments.length > 2 ? test_value : harness_value;

  Array.prototype.fill.call(object, value, from, to);

  %HeapObjectVerify(object);
  assertArrayHasValueInRange(object, value, from, to);
}

function assertArrayHasValueInRange(obj, value, from, to) {
  for (let i = from; i < to; ++i) {
    assertEquals(value, obj[i]);
  }
}

// Tests are executed multiple times. Creating arrays using literal notation
// will create COW-Arrays, which will propagate the most general ElementsKind
// back to their allocation site.
// pristineArray will always return a 🐄-Array with the ElementsKind we actually
// want.
let i = 0;
function pristineArray(str) {
  return eval(str + "//" + (i++));
}

let tests = {
  ARRAY_PACKED_ELEMENTS(value, from, to) {
    let array = pristineArray(
        `["Some string", {}, /foobar/, "Another string", {}]`);
    assertTrue(%HasObjectElements(array));
    assertFalse(%HasHoleyElements(array));

    callAndAssertFill(array, "42", ...arguments);
  },

  ARRAY_HOLEY_ELEMENTS(value, from, to) {
    let array = pristineArray(`["Some string", , {}, , "Another string"]`);
    assertTrue(%HasObjectElements(array));
    assertTrue(%HasHoleyElements(array));

    callAndAssertFill(array, "42", ...arguments);
  },

  ARRAY_PACKED_SMI_ELEMENTS(value, from, to) {
    let array = pristineArray(`[0, -42, 5555, 23, 6]`);
    assertTrue(%HasSmiElements(array));
    assertFalse(%HasHoleyElements(array));

    callAndAssertFill(array, 42, ...arguments);
  },

  ARRAY_HOLEY_SMI_ELEMENTS(value, from, to) {
    let array = pristineArray(`[0, , 5555, , 6]`);
    assertTrue(%HasSmiElements(array));
    assertTrue(%HasHoleyElements(array));

    callAndAssertFill(array, 42, ...arguments);
  },

  ARRAY_PACKED_DOUBLE_ELEMENTS(value, from, to) {
    let array = pristineArray(`[3.14, 7.00001, NaN, -25.3333, 1.0]`);
    assertTrue(%HasDoubleElements(array));
    assertFalse(%HasHoleyElements(array));

    callAndAssertFill(array, 42.42, ...arguments);
  },

  ARRAY_HOLEY_DOUBLE_ELEMENTS(value, from, to) {
    let array = pristineArray(`[3.14, , , , 1.0]`);
    assertTrue(%HasDoubleElements(array));
    assertTrue(%HasHoleyElements(array));

    callAndAssertFill(array, 42.42, ...arguments);
  },

  ARRAY_DICTIONARY_ELEMENTS(value, from, to) {
    let array = pristineArray(`[0, , 2, 3, 4]`);
    Object.defineProperty(array, 1, { get() { return this.foo; },
                                      set(val) { this.foo = val; }});
    assertTrue(%HasDictionaryElements(array));

    callAndAssertFill(array, "42", ...arguments);
  }

  // TODO(szuend): Add additional tests receivers other than arrays
  //               (Objects, TypedArrays, etc.).
};

function RunTest(test) {
  test();
  test(undefined);
  test(undefined, 1);
  test(undefined, 1, 4);
}

function RunTests(tests) {
  Object.keys(tests).forEach(test => RunTest(tests[test]));
}

RunTests(tests);

Array.prototype.__proto__ = {
  __proto__: Array.prototype.__proto__
};

RunTests(tests);