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