[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $Array = GetIntrinsic('%Array%');
|
---|
| 6 | var $species = GetIntrinsic('%Symbol.species%', true);
|
---|
| 7 | var $TypeError = require('es-errors/type');
|
---|
| 8 |
|
---|
| 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/6.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 | var len = length === 0 ? 0 : length;
|
---|
| 23 | var C;
|
---|
| 24 | var isArray = IsArray(originalArray);
|
---|
| 25 | if (isArray) {
|
---|
| 26 | C = Get(originalArray, 'constructor');
|
---|
| 27 | // TODO: figure out how to make a cross-realm normal Array, a same-realm Array
|
---|
| 28 | // if (IsConstructor(C)) {
|
---|
| 29 | // if C is another realm's Array, C = undefined
|
---|
| 30 | // Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(Array))) === null ?
|
---|
| 31 | // }
|
---|
| 32 | if ($species && Type(C) === 'Object') {
|
---|
| 33 | C = Get(C, $species);
|
---|
| 34 | if (C === null) {
|
---|
| 35 | C = void 0;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 | if (typeof C === 'undefined') {
|
---|
| 40 | return $Array(len);
|
---|
| 41 | }
|
---|
| 42 | if (!IsConstructor(C)) {
|
---|
| 43 | throw new $TypeError('C must be a constructor');
|
---|
| 44 | }
|
---|
| 45 | return new C(len); // Construct(C, len);
|
---|
| 46 | };
|
---|
| 47 |
|
---|