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