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');
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';
52 var fromList = function (C, list) {
53 aTypedArrayConstructor(C);
55 var length = list.length;
56 var result = new C(length);
57 while (length > index) result[index] = list[index++];
61 var addGetter = function (it, key) {
62 nativeDefineProperty(it, key, { get: function () {
63 return getInternalState(this)[key];
67 var isArrayBuffer = function (it) {
69 return isPrototypeOf(ArrayBufferPrototype, it) || (klass = classof(it)) == 'ArrayBuffer' || klass == 'SharedArrayBuffer';
72 var isTypedArrayIndex = function (target, key) {
73 return isTypedArray(target)
76 && isIntegralNumber(+key)
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);
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)
99 target[key] = descriptor.value;
101 } return nativeDefineProperty(target, key, descriptor);
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');
114 $({ target: 'Object', stat: true, forced: !NATIVE_ARRAY_BUFFER_VIEWS }, {
115 getOwnPropertyDescriptor: wrappedGetOwnPropertyDescriptor,
116 defineProperty: wrappedDefineProperty
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;
129 var getter = function (that, index) {
130 var data = getInternalState(that);
131 return data.view[GETTER](index * BYTES + data.byteOffset, true);
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);
140 var addElement = function (that, index) {
141 nativeDefineProperty(that, index, {
143 return getter(this, index);
145 set: function (value) {
146 return setter(this, index, value);
152 if (!NATIVE_ARRAY_BUFFER_VIEWS) {
153 TypedArrayConstructor = wrapper(function (that, data, offset, $length) {
154 anInstance(that, TypedArrayConstructorPrototype);
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)) {
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);
171 byteLength = toLength($length) * BYTES;
172 if (byteLength + byteOffset > $len) throw RangeError(WRONG_LENGTH);
174 length = byteLength / BYTES;
175 } else if (isTypedArray(data)) {
176 return fromList(TypedArrayConstructor, data);
178 return call(typedArrayFrom, TypedArrayConstructor, data);
180 setInternalState(that, {
182 byteOffset: byteOffset,
183 byteLength: byteLength,
185 view: new DataView(buffer)
187 while (index < length) addElement(that, index++);
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);
207 if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
208 forEach(getOwnPropertyNames(NativeTypedArrayConstructor), function (key) {
209 if (!(key in TypedArrayConstructor)) {
210 createNonEnumerableProperty(TypedArrayConstructor, key, NativeTypedArrayConstructor[key]);
213 TypedArrayConstructor.prototype = TypedArrayConstructorPrototype;
216 if (TypedArrayConstructorPrototype.constructor !== TypedArrayConstructor) {
217 createNonEnumerableProperty(TypedArrayConstructorPrototype, 'constructor', TypedArrayConstructor);
220 createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_CONSTRUCTOR, TypedArrayConstructor);
222 if (TYPED_ARRAY_TAG) {
223 createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
226 exported[CONSTRUCTOR_NAME] = TypedArrayConstructor;
229 global: true, forced: TypedArrayConstructor != NativeTypedArrayConstructor, sham: !NATIVE_ARRAY_BUFFER_VIEWS
232 if (!(BYTES_PER_ELEMENT in TypedArrayConstructor)) {
233 createNonEnumerableProperty(TypedArrayConstructor, BYTES_PER_ELEMENT, BYTES);
236 if (!(BYTES_PER_ELEMENT in TypedArrayConstructorPrototype)) {
237 createNonEnumerableProperty(TypedArrayConstructorPrototype, BYTES_PER_ELEMENT, BYTES);
240 setSpecies(CONSTRUCTOR_NAME);
242 } else module.exports = function () { /* empty */ };