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');
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';
46 var fromList = function (C, list) {
48 var length = list.length;
49 var result = new (aTypedArrayConstructor(C))(length);
50 while (length > index) result[index] = list[index++];
54 var addGetter = function (it, key) {
55 nativeDefineProperty(it, key, { get: function () {
56 return getInternalState(this)[key];
60 var isArrayBuffer = function (it) {
62 return it instanceof ArrayBuffer || (klass = classof(it)) == 'ArrayBuffer' || klass == 'SharedArrayBuffer';
65 var isTypedArrayIndex = function (target, key) {
66 return isTypedArray(target)
67 && typeof key != 'symbol'
69 && String(+key) == String(key);
72 var wrappedGetOwnPropertyDescriptor = function getOwnPropertyDescriptor(target, key) {
73 return isTypedArrayIndex(target, key = toPrimitive(key, true))
74 ? createPropertyDescriptor(2, target[key])
75 : nativeGetOwnPropertyDescriptor(target, key);
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)
89 target[key] = descriptor.value;
91 } return nativeDefineProperty(target, key, descriptor);
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');
104 $({ target: 'Object', stat: true, forced: !NATIVE_ARRAY_BUFFER_VIEWS }, {
105 getOwnPropertyDescriptor: wrappedGetOwnPropertyDescriptor,
106 defineProperty: wrappedDefineProperty
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;
119 var getter = function (that, index) {
120 var data = getInternalState(that);
121 return data.view[GETTER](index * BYTES + data.byteOffset, true);
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);
130 var addElement = function (that, index) {
131 nativeDefineProperty(that, index, {
133 return getter(this, index);
135 set: function (value) {
136 return setter(this, index, value);
142 if (!NATIVE_ARRAY_BUFFER_VIEWS) {
143 TypedArrayConstructor = wrapper(function (that, data, offset, $length) {
144 anInstance(that, TypedArrayConstructor, CONSTRUCTOR_NAME);
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)) {
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);
161 byteLength = toLength($length) * BYTES;
162 if (byteLength + byteOffset > $len) throw RangeError(WRONG_LENGTH);
164 length = byteLength / BYTES;
165 } else if (isTypedArray(data)) {
166 return fromList(TypedArrayConstructor, data);
168 return typedArrayFrom.call(TypedArrayConstructor, data);
170 setInternalState(that, {
172 byteOffset: byteOffset,
173 byteLength: byteLength,
175 view: new DataView(buffer)
177 while (index < length) addElement(that, index++);
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);
197 if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
198 forEach(getOwnPropertyNames(NativeTypedArrayConstructor), function (key) {
199 if (!(key in TypedArrayConstructor)) {
200 createNonEnumerableProperty(TypedArrayConstructor, key, NativeTypedArrayConstructor[key]);
203 TypedArrayConstructor.prototype = TypedArrayConstructorPrototype;
206 if (TypedArrayConstructorPrototype.constructor !== TypedArrayConstructor) {
207 createNonEnumerableProperty(TypedArrayConstructorPrototype, 'constructor', TypedArrayConstructor);
210 if (TYPED_ARRAY_TAG) {
211 createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
214 exported[CONSTRUCTOR_NAME] = TypedArrayConstructor;
217 global: true, forced: TypedArrayConstructor != NativeTypedArrayConstructor, sham: !NATIVE_ARRAY_BUFFER_VIEWS
220 if (!(BYTES_PER_ELEMENT in TypedArrayConstructor)) {
221 createNonEnumerableProperty(TypedArrayConstructor, BYTES_PER_ELEMENT, BYTES);
224 if (!(BYTES_PER_ELEMENT in TypedArrayConstructorPrototype)) {
225 createNonEnumerableProperty(TypedArrayConstructorPrototype, BYTES_PER_ELEMENT, BYTES);
228 setSpecies(CONSTRUCTOR_NAME);
230 } else module.exports = function () { /* empty */ };