summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor.js
blob: 1424e57d2429cb1dcb2a528e6ba877e4142c2ef6 (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
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.flatMap
description: >
  Assert behavior if this value has a custom species constructor
info: |
  Array.prototype.flatMap ( mapperFunction [ , thisArg ] )

  1. Let O be ? ToObject(this value).
  2. Let sourceLen be ? ToLength(? Get(O, "length")).
  ...
  5. Let A be ? ArraySpeciesCreate(O, 0).
  6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, 1, mapperFunction, T).
  7. Return A.

  ArraySpeciesCreate ( originalArray, length )

  3. Let isArray be ? IsArray(originalArray).
  4. If isArray is false, return ? ArrayCreate(length).
  5. Let C be ? Get(originalArray, "constructor").
  6. If IsConstructor(C) is true, then
    ...
  7. If Type(C) is Object, then
    a. Set C to ? Get(C, @@species).
    b. If C is null, set C to undefined.
  8. If C is undefined, return ? ArrayCreate(length).
  9. If IsConstructor(C) is false, throw a TypeError exception.
  10. Return ? Construct(C, « length »).

  FlattenIntoArray(target, source, sourceLen, start, depth [ , mapperFunction, thisArg ])

  3. Repeat, while sourceIndex < sourceLen
    a. Let P be ! ToString(sourceIndex).
    b. Let exists be ? HasProperty(source, P).
    c. If exists is true, then
      ...
      vi. Else,
        ...
        2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element).
features: [Array.prototype.flatMap, Symbol, Symbol.species]
includes: [propertyHelper.js]
---*/

assert.sameValue(typeof Array.prototype.flatMap, 'function');

var arr = [[42, 1], [42, 2]];
var mapperFn = function(e) { return e; };

var called = 0;
var ctorCalled = 0;
function ctor(len) {
  assert.sameValue(new.target, ctor, 'new target is defined');
  assert.sameValue(len, 0, 'first argument is always 0');
  ctorCalled++;
}

arr.constructor = {
  get [Symbol.species]() {
    called++;
    return ctor;
  }
};
var actual = arr.flatMap(mapperFn);
assert(actual instanceof ctor, 'returned value is an instance of custom ctor');
assert.sameValue(called, 1, 'got species once');
assert.sameValue(ctorCalled, 1, 'called custom ctor once');

assert.sameValue(Object.prototype.hasOwnProperty.call(actual, 'length'), false, 'it does not define an own length property');
verifyProperty(actual, '0', {
  configurable: true,
  writable: true,
  enumerable: true,
  value: 42
});
verifyProperty(actual, '1', {
  configurable: true,
  writable: true,
  enumerable: true,
  value: 1
});
verifyProperty(actual, '2', {
  configurable: true,
  writable: true,
  enumerable: true,
  value: 42
});
verifyProperty(actual, '3', {
  configurable: true,
  writable: true,
  enumerable: true,
  value: 2
});

reportCompare(0, 0);