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