source: imaps-frontend/node_modules/es-abstract/2016/ArraySetLength.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[d565449]1'use strict';
2
3var $RangeError = require('es-errors/range');
4var $TypeError = require('es-errors/type');
5
6var assign = require('object.assign');
7
8var isPropertyDescriptor = require('../helpers/records/property-descriptor');
9
10var IsArray = require('./IsArray');
11var IsDataDescriptor = require('./IsDataDescriptor');
12var OrdinaryDefineOwnProperty = require('./OrdinaryDefineOwnProperty');
13var OrdinaryGetOwnProperty = require('./OrdinaryGetOwnProperty');
14var ToNumber = require('./ToNumber');
15var ToString = require('./ToString');
16var ToUint32 = require('./ToUint32');
17
18// https://262.ecma-international.org/6.0/#sec-arraysetlength
19
20// eslint-disable-next-line max-statements, max-lines-per-function
21module.exports = function ArraySetLength(A, Desc) {
22 if (!IsArray(A)) {
23 throw new $TypeError('Assertion failed: A must be an Array');
24 }
25 if (!isPropertyDescriptor(Desc)) {
26 throw new $TypeError('Assertion failed: Desc must be a Property Descriptor');
27 }
28 if (!('[[Value]]' in Desc)) {
29 return OrdinaryDefineOwnProperty(A, 'length', Desc);
30 }
31 var newLenDesc = assign({}, Desc);
32 var newLen = ToUint32(Desc['[[Value]]']);
33 var numberLen = ToNumber(Desc['[[Value]]']);
34 if (newLen !== numberLen) {
35 throw new $RangeError('Invalid array length');
36 }
37 newLenDesc['[[Value]]'] = newLen;
38 var oldLenDesc = OrdinaryGetOwnProperty(A, 'length');
39 if (!IsDataDescriptor(oldLenDesc)) {
40 throw new $TypeError('Assertion failed: an array had a non-data descriptor on `length`');
41 }
42 var oldLen = oldLenDesc['[[Value]]'];
43 if (newLen >= oldLen) {
44 return OrdinaryDefineOwnProperty(A, 'length', newLenDesc);
45 }
46 if (!oldLenDesc['[[Writable]]']) {
47 return false;
48 }
49 var newWritable;
50 if (!('[[Writable]]' in newLenDesc) || newLenDesc['[[Writable]]']) {
51 newWritable = true;
52 } else {
53 newWritable = false;
54 newLenDesc['[[Writable]]'] = true;
55 }
56 var succeeded = OrdinaryDefineOwnProperty(A, 'length', newLenDesc);
57 if (!succeeded) {
58 return false;
59 }
60 while (newLen < oldLen) {
61 oldLen -= 1;
62 // eslint-disable-next-line no-param-reassign
63 var deleteSucceeded = delete A[ToString(oldLen)];
64 if (!deleteSucceeded) {
65 newLenDesc['[[Value]]'] = oldLen + 1;
66 if (!newWritable) {
67 newLenDesc['[[Writable]]'] = false;
68 OrdinaryDefineOwnProperty(A, 'length', newLenDesc);
69 return false;
70 }
71 }
72 }
73 if (!newWritable) {
74 return OrdinaryDefineOwnProperty(A, 'length', { '[[Writable]]': false });
75 }
76 return true;
77};
Note: See TracBrowser for help on using the repository browser.