blob: bbfeaf7aba743b196e8c25f45b2665166f4f8f9d [file] [log] [blame]
Tim van der Lippefdbd42e2020-04-07 14:14:361'use strict';
2
Tim van der Lippe2c891972021-07-29 15:22:503var GetIntrinsic = require('get-intrinsic');
Tim van der Lippefdbd42e2020-04-07 14:14:364
5var $ArrayPrototype = GetIntrinsic('%Array.prototype%');
Nikolay Vitkovd76576c2024-12-02 14:10:156var $RangeError = require('es-errors/range');
7var $SyntaxError = require('es-errors/syntax');
8var $TypeError = require('es-errors/type');
Tim van der Lippefdbd42e2020-04-07 14:14:369
Nikolay Vitkovd76576c2024-12-02 14:10:1510var isInteger = require('../helpers/isInteger');
11
12var hasProto = require('has-proto')();
Tim van der Lippefdbd42e2020-04-07 14:14:3613
14var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1;
15
16var $setProto = GetIntrinsic('%Object.setPrototypeOf%', true) || (
Nikolay Vitkovd76576c2024-12-02 14:10:1517 hasProto
18 ? function (O, proto) {
Tim van der Lippefdbd42e2020-04-07 14:14:3619 O.__proto__ = proto; // eslint-disable-line no-proto, no-param-reassign
20 return O;
21 }
Nikolay Vitkovd76576c2024-12-02 14:10:1522 : null
Tim van der Lippefdbd42e2020-04-07 14:14:3623);
24
Nikolay Vitkovd76576c2024-12-02 14:10:1525// https://262.ecma-international.org/6.0/#sec-arraycreate
Tim van der Lippefdbd42e2020-04-07 14:14:3626
27module.exports = function ArrayCreate(length) {
Nikolay Vitkovd76576c2024-12-02 14:10:1528 if (!isInteger(length) || length < 0) {
Tim van der Lippefdbd42e2020-04-07 14:14:3629 throw new $TypeError('Assertion failed: `length` must be an integer Number >= 0');
30 }
31 if (length > MAX_ARRAY_LENGTH) {
32 throw new $RangeError('length is greater than (2**32 - 1)');
33 }
34 var proto = arguments.length > 1 ? arguments[1] : $ArrayPrototype;
35 var A = []; // steps 5 - 7, and 9
36 if (proto !== $ArrayPrototype) { // step 8
37 if (!$setProto) {
38 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]]');
39 }
40 $setProto(A, proto);
41 }
42 if (length !== 0) { // bypasses the need for step 2
43 A.length = length;
44 }
45 /* step 10, the above as a shortcut for the below
Nikolay Vitkovd76576c2024-12-02 14:10:1546 OrdinaryDefineOwnProperty(A, 'length', {
47 '[[Configurable]]': false,
48 '[[Enumerable]]': false,
49 '[[Value]]': length,
50 '[[Writable]]': true
51 });
52 */
Tim van der Lippefdbd42e2020-04-07 14:14:3653 return A;
54};