2 var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-native');
3 var DESCRIPTORS = require('../internals/descriptors');
4 var global = require('../internals/global');
5 var isCallable = require('../internals/is-callable');
6 var isObject = require('../internals/is-object');
7 var hasOwn = require('../internals/has-own-property');
8 var classof = require('../internals/classof');
9 var tryToString = require('../internals/try-to-string');
10 var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
11 var redefine = require('../internals/redefine');
12 var defineProperty = require('../internals/object-define-property').f;
13 var isPrototypeOf = require('../internals/object-is-prototype-of');
14 var getPrototypeOf = require('../internals/object-get-prototype-of');
15 var setPrototypeOf = require('../internals/object-set-prototype-of');
16 var wellKnownSymbol = require('../internals/well-known-symbol');
17 var uid = require('../internals/uid');
19 var Int8Array = global.Int8Array;
20 var Int8ArrayPrototype = Int8Array && Int8Array.prototype;
21 var Uint8ClampedArray = global.Uint8ClampedArray;
22 var Uint8ClampedArrayPrototype = Uint8ClampedArray && Uint8ClampedArray.prototype;
23 var TypedArray = Int8Array && getPrototypeOf(Int8Array);
24 var TypedArrayPrototype = Int8ArrayPrototype && getPrototypeOf(Int8ArrayPrototype);
25 var ObjectPrototype = Object.prototype;
26 var TypeError = global.TypeError;
28 var TO_STRING_TAG = wellKnownSymbol('toStringTag');
29 var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
30 var TYPED_ARRAY_CONSTRUCTOR = uid('TYPED_ARRAY_CONSTRUCTOR');
31 // Fixing native typed arrays in Opera Presto crashes the browser, see #595
32 var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
33 var TYPED_ARRAY_TAG_REQIRED = false;
34 var NAME, Constructor, Prototype;
36 var TypedArrayConstructorsList = {
48 var BigIntArrayConstructorsList = {
53 var isView = function isView(it) {
54 if (!isObject(it)) return false;
55 var klass = classof(it);
56 return klass === 'DataView'
57 || hasOwn(TypedArrayConstructorsList, klass)
58 || hasOwn(BigIntArrayConstructorsList, klass);
61 var isTypedArray = function (it) {
62 if (!isObject(it)) return false;
63 var klass = classof(it);
64 return hasOwn(TypedArrayConstructorsList, klass)
65 || hasOwn(BigIntArrayConstructorsList, klass);
68 var aTypedArray = function (it) {
69 if (isTypedArray(it)) return it;
70 throw TypeError('Target is not a typed array');
73 var aTypedArrayConstructor = function (C) {
74 if (isCallable(C) && (!setPrototypeOf || isPrototypeOf(TypedArray, C))) return C;
75 throw TypeError(tryToString(C) + ' is not a typed array constructor');
78 var exportTypedArrayMethod = function (KEY, property, forced) {
79 if (!DESCRIPTORS) return;
80 if (forced) for (var ARRAY in TypedArrayConstructorsList) {
81 var TypedArrayConstructor = global[ARRAY];
82 if (TypedArrayConstructor && hasOwn(TypedArrayConstructor.prototype, KEY)) try {
83 delete TypedArrayConstructor.prototype[KEY];
84 } catch (error) { /* empty */ }
86 if (!TypedArrayPrototype[KEY] || forced) {
87 redefine(TypedArrayPrototype, KEY, forced ? property
88 : NATIVE_ARRAY_BUFFER_VIEWS && Int8ArrayPrototype[KEY] || property);
92 var exportTypedArrayStaticMethod = function (KEY, property, forced) {
93 var ARRAY, TypedArrayConstructor;
94 if (!DESCRIPTORS) return;
96 if (forced) for (ARRAY in TypedArrayConstructorsList) {
97 TypedArrayConstructor = global[ARRAY];
98 if (TypedArrayConstructor && hasOwn(TypedArrayConstructor, KEY)) try {
99 delete TypedArrayConstructor[KEY];
100 } catch (error) { /* empty */ }
102 if (!TypedArray[KEY] || forced) {
103 // V8 ~ Chrome 49-50 `%TypedArray%` methods are non-writable non-configurable
105 return redefine(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && TypedArray[KEY] || property);
106 } catch (error) { /* empty */ }
109 for (ARRAY in TypedArrayConstructorsList) {
110 TypedArrayConstructor = global[ARRAY];
111 if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
112 redefine(TypedArrayConstructor, KEY, property);
117 for (NAME in TypedArrayConstructorsList) {
118 Constructor = global[NAME];
119 Prototype = Constructor && Constructor.prototype;
120 if (Prototype) createNonEnumerableProperty(Prototype, TYPED_ARRAY_CONSTRUCTOR, Constructor);
121 else NATIVE_ARRAY_BUFFER_VIEWS = false;
124 for (NAME in BigIntArrayConstructorsList) {
125 Constructor = global[NAME];
126 Prototype = Constructor && Constructor.prototype;
127 if (Prototype) createNonEnumerableProperty(Prototype, TYPED_ARRAY_CONSTRUCTOR, Constructor);
130 // WebKit bug - typed arrays constructors prototype is Object.prototype
131 if (!NATIVE_ARRAY_BUFFER_VIEWS || !isCallable(TypedArray) || TypedArray === Function.prototype) {
132 // eslint-disable-next-line no-shadow -- safe
133 TypedArray = function TypedArray() {
134 throw TypeError('Incorrect invocation');
136 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
137 if (global[NAME]) setPrototypeOf(global[NAME], TypedArray);
141 if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
142 TypedArrayPrototype = TypedArray.prototype;
143 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
144 if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
148 // WebKit bug - one more object in Uint8ClampedArray prototype chain
149 if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
150 setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
153 if (DESCRIPTORS && !hasOwn(TypedArrayPrototype, TO_STRING_TAG)) {
154 TYPED_ARRAY_TAG_REQIRED = true;
155 defineProperty(TypedArrayPrototype, TO_STRING_TAG, { get: function () {
156 return isObject(this) ? this[TYPED_ARRAY_TAG] : undefined;
158 for (NAME in TypedArrayConstructorsList) if (global[NAME]) {
159 createNonEnumerableProperty(global[NAME], TYPED_ARRAY_TAG, NAME);
164 NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
165 TYPED_ARRAY_CONSTRUCTOR: TYPED_ARRAY_CONSTRUCTOR,
166 TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQIRED && TYPED_ARRAY_TAG,
167 aTypedArray: aTypedArray,
168 aTypedArrayConstructor: aTypedArrayConstructor,
169 exportTypedArrayMethod: exportTypedArrayMethod,
170 exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
172 isTypedArray: isTypedArray,
173 TypedArray: TypedArray,
174 TypedArrayPrototype: TypedArrayPrototype