UNPKG

10.2 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var global = require('../internals/global');
4var call = require('../internals/function-call');
5var DESCRIPTORS = require('../internals/descriptors');
6var TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS = require('../internals/typed-array-constructors-require-wrappers');
7var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
8var ArrayBufferModule = require('../internals/array-buffer');
9var anInstance = require('../internals/an-instance');
10var createPropertyDescriptor = require('../internals/create-property-descriptor');
11var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
12var isIntegralNumber = require('../internals/is-integral-number');
13var toLength = require('../internals/to-length');
14var toIndex = require('../internals/to-index');
15var toOffset = require('../internals/to-offset');
16var toUint8Clamped = require('../internals/to-uint8-clamped');
17var toPropertyKey = require('../internals/to-property-key');
18var hasOwn = require('../internals/has-own-property');
19var classof = require('../internals/classof');
20var isObject = require('../internals/is-object');
21var isSymbol = require('../internals/is-symbol');
22var create = require('../internals/object-create');
23var isPrototypeOf = require('../internals/object-is-prototype-of');
24var setPrototypeOf = require('../internals/object-set-prototype-of');
25var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
26var typedArrayFrom = require('../internals/typed-array-from');
27var forEach = require('../internals/array-iteration').forEach;
28var setSpecies = require('../internals/set-species');
29var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
30var definePropertyModule = require('../internals/object-define-property');
31var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
32var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');
33var InternalStateModule = require('../internals/internal-state');
34var inheritIfRequired = require('../internals/inherit-if-required');
35
36var getInternalState = InternalStateModule.get;
37var setInternalState = InternalStateModule.set;
38var enforceInternalState = InternalStateModule.enforce;
39var nativeDefineProperty = definePropertyModule.f;
40var nativeGetOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
41var RangeError = global.RangeError;
42var ArrayBuffer = ArrayBufferModule.ArrayBuffer;
43var ArrayBufferPrototype = ArrayBuffer.prototype;
44var DataView = ArrayBufferModule.DataView;
45var NATIVE_ARRAY_BUFFER_VIEWS = ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS;
46var TYPED_ARRAY_TAG = ArrayBufferViewCore.TYPED_ARRAY_TAG;
47var TypedArray = ArrayBufferViewCore.TypedArray;
48var TypedArrayPrototype = ArrayBufferViewCore.TypedArrayPrototype;
49var isTypedArray = ArrayBufferViewCore.isTypedArray;
50var BYTES_PER_ELEMENT = 'BYTES_PER_ELEMENT';
51var WRONG_LENGTH = 'Wrong length';
52
53var addGetter = function (it, key) {
54 defineBuiltInAccessor(it, key, {
55 configurable: true,
56 get: function () {
57 return getInternalState(this)[key];
58 }
59 });
60};
61
62var isArrayBuffer = function (it) {
63 var klass;
64 return isPrototypeOf(ArrayBufferPrototype, it) || (klass = classof(it)) === 'ArrayBuffer' || klass === 'SharedArrayBuffer';
65};
66
67var isTypedArrayIndex = function (target, key) {
68 return isTypedArray(target)
69 && !isSymbol(key)
70 && key in target
71 && isIntegralNumber(+key)
72 && key >= 0;
73};
74
75var 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
82var 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 // TODO: add validation descriptor w/o calling accessors
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
99if (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 () { /* empty */ };