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