UNPKG

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