| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 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 = GetIntrinsic('%TypeError%'); |
| 8 | |
| 9 | var Get = require('./Get'); |
| 10 | var IsArray = require('./IsArray'); |
| 11 | var IsConstructor = require('./IsConstructor'); |
| 12 | var IsInteger = require('./IsInteger'); |
| 13 | var Type = require('./Type'); |
| 14 | |
| 15 | // https://ecma-international.org/ecma-262/6.0/#sec-arrayspeciescreate |
| 16 | |
| 17 | module.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 | |