[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $ArrayPrototype = GetIntrinsic('%Array.prototype%');
|
---|
| 6 | var $RangeError = require('es-errors/range');
|
---|
| 7 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 8 | var $TypeError = require('es-errors/type');
|
---|
[79a0317] | 9 | var isInteger = require('math-intrinsics/isInteger');
|
---|
| 10 | var MAX_ARRAY_LENGTH = require('math-intrinsics/constants/maxArrayLength');
|
---|
| 11 | var $setProto = require('set-proto');
|
---|
[d565449] | 12 |
|
---|
| 13 | // https://262.ecma-international.org/12.0/#sec-arraycreate
|
---|
| 14 |
|
---|
| 15 | module.exports = function ArrayCreate(length) {
|
---|
| 16 | if (!isInteger(length) || length < 0) {
|
---|
| 17 | throw new $TypeError('Assertion failed: `length` must be an integer Number >= 0');
|
---|
| 18 | }
|
---|
| 19 | if (length > MAX_ARRAY_LENGTH) {
|
---|
| 20 | throw new $RangeError('length is greater than (2**32 - 1)');
|
---|
| 21 | }
|
---|
| 22 | var proto = arguments.length > 1 ? arguments[1] : $ArrayPrototype;
|
---|
| 23 | var A = []; // steps 3, 5
|
---|
| 24 | if (proto !== $ArrayPrototype) { // step 4
|
---|
| 25 | if (!$setProto) {
|
---|
| 26 | throw new $SyntaxError('ArrayCreate: a `proto` argument that is not `Array.prototype` is not supported in an environment that does not support setting the [[Prototype]]');
|
---|
| 27 | }
|
---|
| 28 | $setProto(A, proto);
|
---|
| 29 | }
|
---|
| 30 | if (length !== 0) { // bypasses the need for step 6
|
---|
| 31 | A.length = length;
|
---|
| 32 | }
|
---|
| 33 | /* step 6, the above as a shortcut for the below
|
---|
| 34 | OrdinaryDefineOwnProperty(A, 'length', {
|
---|
| 35 | '[[Configurable]]': false,
|
---|
| 36 | '[[Enumerable]]': false,
|
---|
| 37 | '[[Value]]': length,
|
---|
| 38 | '[[Writable]]': true
|
---|
| 39 | });
|
---|
| 40 | */
|
---|
| 41 | return A;
|
---|
| 42 | };
|
---|