massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / internals / typed-array-constructor.js
1 'use strict';
2 var $ = require('../internals/export');
3 var global = require('../internals/global');
4 var call = require('../internals/function-call');
5 var DESCRIPTORS = require('../internals/descriptors');
6 var TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS = require('../internals/typed-array-constructors-require-wrappers');
7 var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
8 var ArrayBufferModule = require('../internals/array-buffer');
9 var anInstance = require('../internals/an-instance');
10 var createPropertyDescriptor = require('../internals/create-property-descriptor');
11 var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
12 var isIntegralNumber = require('../internals/is-integral-number');
13 var toLength = require('../internals/to-length');
14 var toIndex = require('../internals/to-index');
15 var toOffset = require('../internals/to-offset');
16 var toPropertyKey = require('../internals/to-property-key');
17 var hasOwn = require('../internals/has-own-property');
18 var classof = require('../internals/classof');
19 var isObject = require('../internals/is-object');
20 var isSymbol = require('../internals/is-symbol');
21 var create = require('../internals/object-create');
22 var isPrototypeOf = require('../internals/object-is-prototype-of');
23 var setPrototypeOf = require('../internals/object-set-prototype-of');
24 var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
25 var typedArrayFrom = require('../internals/typed-array-from');
26 var forEach = require('../internals/array-iteration').forEach;
27 var setSpecies = require('../internals/set-species');
28 var definePropertyModule = require('../internals/object-define-property');
29 var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
30 var InternalStateModule = require('../internals/internal-state');
31 var inheritIfRequired = require('../internals/inherit-if-required');
32
33 var getInternalState = InternalStateModule.get;
34 var setInternalState = InternalStateModule.set;
35 var nativeDefineProperty = definePropertyModule.f;
36 var nativeGetOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
37 var round = Math.round;
38 var RangeError = global.RangeError;
39 var ArrayBuffer = ArrayBufferModule.ArrayBuffer;
40 var ArrayBufferPrototype = ArrayBuffer.prototype;
41 var DataView = ArrayBufferModule.DataView;
42 var NATIVE_ARRAY_BUFFER_VIEWS = ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS;
43 var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
44 var TYPED_ARRAY_TAG = ArrayBufferViewCore.TYPED_ARRAY_TAG;
45 var TypedArray = ArrayBufferViewCore.TypedArray;
46 var TypedArrayPrototype = ArrayBufferViewCore.TypedArrayPrototype;
47 var aTypedArrayConstructor = ArrayBufferViewCore.aTypedArrayConstructor;
48 var isTypedArray = ArrayBufferViewCore.isTypedArray;
49 var BYTES_PER_ELEMENT = 'BYTES_PER_ELEMENT';
50 var WRONG_LENGTH = 'Wrong length';
51
52 var fromList = function (C, list) {
53   aTypedArrayConstructor(C);
54   var index = 0;
55   var length = list.length;
56   var result = new C(length);
57   while (length > index) result[index] = list[index++];
58   return result;
59 };
60
61 var addGetter = function (it, key) {
62   nativeDefineProperty(it, key, { get: function () {
63     return getInternalState(this)[key];
64   } });
65 };
66
67 var isArrayBuffer = function (it) {
68   var klass;
69   return isPrototypeOf(ArrayBufferPrototype, it) || (klass = classof(it)) == 'ArrayBuffer' || klass == 'SharedArrayBuffer';
70 };
71
72 var isTypedArrayIndex = function (target, key) {
73   return isTypedArray(target)
74     && !isSymbol(key)
75     && key in target
76     && isIntegralNumber(+key)
77     && key >= 0;
78 };
79
80 var wrappedGetOwnPropertyDescriptor = function getOwnPropertyDescriptor(target, key) {
81   key = toPropertyKey(key);
82   return isTypedArrayIndex(target, key)
83     ? createPropertyDescriptor(2, target[key])
84     : nativeGetOwnPropertyDescriptor(target, key);
85 };
86
87 var wrappedDefineProperty = function defineProperty(target, key, descriptor) {
88   key = toPropertyKey(key);
89   if (isTypedArrayIndex(target, key)
90     && isObject(descriptor)
91     && hasOwn(descriptor, 'value')
92     && !hasOwn(descriptor, 'get')
93     && !hasOwn(descriptor, 'set')
94     // TODO: add validation descriptor w/o calling accessors
95     && !descriptor.configurable
96     && (!hasOwn(descriptor, 'writable') || descriptor.writable)
97     && (!hasOwn(descriptor, 'enumerable') || descriptor.enumerable)
98   ) {
99     target[key] = descriptor.value;
100     return target;
101   } return nativeDefineProperty(target, key, descriptor);
102 };
103
104 if (DESCRIPTORS) {
105   if (!NATIVE_ARRAY_BUFFER_VIEWS) {
106     getOwnPropertyDescriptorModule.f = wrappedGetOwnPropertyDescriptor;
107     definePropertyModule.f = wrappedDefineProperty;
108     addGetter(TypedArrayPrototype, 'buffer');
109     addGetter(TypedArrayPrototype, 'byteOffset');
110     addGetter(TypedArrayPrototype, 'byteLength');
111     addGetter(TypedArrayPrototype, 'length');
112   }
113
114   $({ target: 'Object', stat: true, forced: !NATIVE_ARRAY_BUFFER_VIEWS }, {
115     getOwnPropertyDescriptor: wrappedGetOwnPropertyDescriptor,
116     defineProperty: wrappedDefineProperty
117   });
118
119   module.exports = function (TYPE, wrapper, CLAMPED) {
120     var BYTES = TYPE.match(/\d+$/)[0] / 8;
121     var CONSTRUCTOR_NAME = TYPE + (CLAMPED ? 'Clamped' : '') + 'Array';
122     var GETTER = 'get' + TYPE;
123     var SETTER = 'set' + TYPE;
124     var NativeTypedArrayConstructor = global[CONSTRUCTOR_NAME];
125     var TypedArrayConstructor = NativeTypedArrayConstructor;
126     var TypedArrayConstructorPrototype = TypedArrayConstructor && TypedArrayConstructor.prototype;
127     var exported = {};
128
129     var getter = function (that, index) {
130       var data = getInternalState(that);
131       return data.view[GETTER](index * BYTES + data.byteOffset, true);
132     };
133
134     var setter = function (that, index, value) {
135       var data = getInternalState(that);
136       if (CLAMPED) value = (value = round(value)) < 0 ? 0 : value > 0xFF ? 0xFF : value & 0xFF;
137       data.view[SETTER](index * BYTES + data.byteOffset, value, true);
138     };
139
140     var addElement = function (that, index) {
141       nativeDefineProperty(that, index, {
142         get: function () {
143           return getter(this, index);
144         },
145         set: function (value) {
146           return setter(this, index, value);
147         },
148         enumerable: true
149       });
150     };
151
152     if (!NATIVE_ARRAY_BUFFER_VIEWS) {
153       TypedArrayConstructor = wrapper(function (that, data, offset, $length) {
154         anInstance(that, TypedArrayConstructorPrototype);
155         var index = 0;
156         var byteOffset = 0;
157         var buffer, byteLength, length;
158         if (!isObject(data)) {
159           length = toIndex(data);
160           byteLength = length * BYTES;
161           buffer = new ArrayBuffer(byteLength);
162         } else if (isArrayBuffer(data)) {
163           buffer = data;
164           byteOffset = toOffset(offset, BYTES);
165           var $len = data.byteLength;
166           if ($length === undefined) {
167             if ($len % BYTES) throw RangeError(WRONG_LENGTH);
168             byteLength = $len - byteOffset;
169             if (byteLength < 0) throw RangeError(WRONG_LENGTH);
170           } else {
171             byteLength = toLength($length) * BYTES;
172             if (byteLength + byteOffset > $len) throw RangeError(WRONG_LENGTH);
173           }
174           length = byteLength / BYTES;
175         } else if (isTypedArray(data)) {
176           return fromList(TypedArrayConstructor, data);
177         } else {
178           return call(typedArrayFrom, TypedArrayConstructor, data);
179         }
180         setInternalState(that, {
181           buffer: buffer,
182           byteOffset: byteOffset,
183           byteLength: byteLength,
184           length: length,
185           view: new DataView(buffer)
186         });
187         while (index < length) addElement(that, index++);
188       });
189
190       if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
191       TypedArrayConstructorPrototype = TypedArrayConstructor.prototype = create(TypedArrayPrototype);
192     } else if (TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS) {
193       TypedArrayConstructor = wrapper(function (dummy, data, typedArrayOffset, $length) {
194         anInstance(dummy, TypedArrayConstructorPrototype);
195         return inheritIfRequired(function () {
196           if (!isObject(data)) return new NativeTypedArrayConstructor(toIndex(data));
197           if (isArrayBuffer(data)) return $length !== undefined
198             ? new NativeTypedArrayConstructor(data, toOffset(typedArrayOffset, BYTES), $length)
199             : typedArrayOffset !== undefined
200               ? new NativeTypedArrayConstructor(data, toOffset(typedArrayOffset, BYTES))
201               : new NativeTypedArrayConstructor(data);
202           if (isTypedArray(data)) return fromList(TypedArrayConstructor, data);
203           return call(typedArrayFrom, TypedArrayConstructor, data);
204         }(), dummy, TypedArrayConstructor);
205       });
206
207       if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
208       forEach(getOwnPropertyNames(NativeTypedArrayConstructor), function (key) {
209         if (!(key in TypedArrayConstructor)) {
210           createNonEnumerableProperty(TypedArrayConstructor, key, NativeTypedArrayConstructor[key]);
211         }
212       });
213       TypedArrayConstructor.prototype = TypedArrayConstructorPrototype;
214     }
215
216     if (TypedArrayConstructorPrototype.constructor !== TypedArrayConstructor) {
217       createNonEnumerableProperty(TypedArrayConstructorPrototype, 'constructor', TypedArrayConstructor);
218     }
219
220     createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_CONSTRUCTOR, TypedArrayConstructor);
221
222     if (TYPED_ARRAY_TAG) {
223       createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
224     }
225
226     exported[CONSTRUCTOR_NAME] = TypedArrayConstructor;
227
228     $({
229       global: true, forced: TypedArrayConstructor != NativeTypedArrayConstructor, sham: !NATIVE_ARRAY_BUFFER_VIEWS
230     }, exported);
231
232     if (!(BYTES_PER_ELEMENT in TypedArrayConstructor)) {
233       createNonEnumerableProperty(TypedArrayConstructor, BYTES_PER_ELEMENT, BYTES);
234     }
235
236     if (!(BYTES_PER_ELEMENT in TypedArrayConstructorPrototype)) {
237       createNonEnumerableProperty(TypedArrayConstructorPrototype, BYTES_PER_ELEMENT, BYTES);
238     }
239
240     setSpecies(CONSTRUCTOR_NAME);
241   };
242 } else module.exports = function () { /* empty */ };