1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $species = GetIntrinsic('%Symbol.species%', true);
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 |
|
---|
8 | var ArrayCreate = require('./ArrayCreate');
|
---|
9 | var Get = require('./Get');
|
---|
10 | var IsArray = require('./IsArray');
|
---|
11 | var IsConstructor = require('./IsConstructor');
|
---|
12 | var Type = require('./Type');
|
---|
13 |
|
---|
14 | var isInteger = require('../helpers/isInteger');
|
---|
15 |
|
---|
16 | // https://262.ecma-international.org/12.0/#sec-arrayspeciescreate
|
---|
17 |
|
---|
18 | module.exports = function ArraySpeciesCreate(originalArray, length) {
|
---|
19 | if (!isInteger(length) || length < 0) {
|
---|
20 | throw new $TypeError('Assertion failed: length must be an integer >= 0');
|
---|
21 | }
|
---|
22 |
|
---|
23 | var isArray = IsArray(originalArray);
|
---|
24 | if (!isArray) {
|
---|
25 | return ArrayCreate(length);
|
---|
26 | }
|
---|
27 |
|
---|
28 | var C = Get(originalArray, 'constructor');
|
---|
29 | // TODO: figure out how to make a cross-realm normal Array, a same-realm Array
|
---|
30 | // if (IsConstructor(C)) {
|
---|
31 | // if C is another realm's Array, C = undefined
|
---|
32 | // Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(Array))) === null ?
|
---|
33 | // }
|
---|
34 | if ($species && Type(C) === 'Object') {
|
---|
35 | C = Get(C, $species);
|
---|
36 | if (C === null) {
|
---|
37 | C = void 0;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (typeof C === 'undefined') {
|
---|
42 | return ArrayCreate(length);
|
---|
43 | }
|
---|
44 | if (!IsConstructor(C)) {
|
---|
45 | throw new $TypeError('C must be a constructor');
|
---|
46 | }
|
---|
47 | return new C(length); // Construct(C, length);
|
---|
48 | };
|
---|
49 |
|
---|