2 var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-native');
3 var DESCRIPTORS = require('../internals/descriptors');
4 var global = require('../internals/global');
5 var isObject = require('../internals/is-object');
6 var has = require('../internals/has');
7 var classof = require('../internals/classof');
8 var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
9 var redefine = require('../internals/redefine');
10 var defineProperty = require('../internals/object-define-property').f;
11 var getPrototypeOf = require('../internals/object-get-prototype-of');
12 var setPrototypeOf = require('../internals/object-set-prototype-of');
13 var wellKnownSymbol = require('../internals/well-known-symbol');
14 var uid = require('../internals/uid');
16 var Int8Array = global.Int8Array;
17 var Int8ArrayPrototype = Int8Array && Int8Array.prototype;
18 var Uint8ClampedArray = global.Uint8ClampedArray;
19 var Uint8ClampedArrayPrototype = Uint8ClampedArray && Uint8ClampedArray.prototype;
20 var TypedArray = Int8Array && getPrototypeOf(Int8Array);
21 var TypedArrayPrototype = Int8ArrayPrototype && getPrototypeOf(Int8ArrayPrototype);
22 var ObjectPrototype = Object.prototype;
23 var isPrototypeOf = ObjectPrototype.isPrototypeOf;
25 var TO_STRING_TAG = wellKnownSymbol('toStringTag');
26 var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
27 // Fixing native typed arrays in Opera Presto crashes the browser, see #595
28 var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
29 var TYPED_ARRAY_TAG_REQIRED = false;
32 var TypedArrayConstructorsList = {
44 var BigIntArrayConstructorsList = {
49 var isView = function isView(it) {
50 if (!isObject(it)) return false;
51 var klass = classof(it);
52 return klass === 'DataView'
53 || has(TypedArrayConstructorsList, klass)
54 || has(BigIntArrayConstructorsList, klass);
57 var isTypedArray = function (it) {
58 if (!isObject(it)) return false;
59 var klass = classof(it);
60 return has(TypedArrayConstructorsList, klass)
61 || has(BigIntArrayConstructorsList, klass);
64 var aTypedArray = function (it) {
65 if (isTypedArray(it)) return it;
66 throw TypeError('Target is not a typed array');
69 var aTypedArrayConstructor = function (C) {
71 if (isPrototypeOf.call(TypedArray, C)) return C;
72 } else for (var ARRAY in TypedArrayConstructorsList) if (has(TypedArrayConstructorsList, NAME)) {
73 var TypedArrayConstructor = global[ARRAY];
74 if (TypedArrayConstructor && (C === TypedArrayConstructor || isPrototypeOf.call(TypedArrayConstructor, C))) {
77 } throw TypeError('Target is not a typed array constructor');
80 var exportTypedArrayMethod = function (KEY, property, forced) {
81 if (!DESCRIPTORS) return;
82 if (forced) for (var ARRAY in TypedArrayConstructorsList) {
83 var TypedArrayConstructor = global[ARRAY];
84 if (TypedArrayConstructor && has(TypedArrayConstructor.prototype, KEY)) {
85 delete TypedArrayConstructor.prototype[KEY];
88 if (!TypedArrayPrototype[KEY] || forced) {
89 redefine(TypedArrayPrototype, KEY, forced ? property
90 : NATIVE_ARRAY_BUFFER_VIEWS && Int8ArrayPrototype[KEY] || property);
94 var exportTypedArrayStaticMethod = function (KEY, property, forced) {
95 var ARRAY, TypedArrayConstructor;
96 if (!DESCRIPTORS) return;
98 if (forced) for (ARRAY in TypedArrayConstructorsList) {
99 TypedArrayConstructor = global[ARRAY];
100 if (TypedArrayConstructor && has(TypedArrayConstructor, KEY)) {
101 delete TypedArrayConstructor[KEY];
104 if (!TypedArray[KEY] || forced) {
105 // V8 ~ Chrome 49-50 `%TypedArray%` methods are non-writable non-configurable
107 return redefine(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && Int8Array[KEY] || property);
108 } catch (error) { /* empty */ }
111 for (ARRAY in TypedArrayConstructorsList) {
112 TypedArrayConstructor = global[ARRAY];
113 if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
114 redefine(TypedArrayConstructor, KEY, property);
119 for (NAME in TypedArrayConstructorsList) {
120 if (!global[NAME]) NATIVE_ARRAY_BUFFER_VIEWS = false;
123 // WebKit bug - typed arrays constructors prototype is Object.prototype
124 if (!NATIVE_ARRAY_BUFFER_VIEWS || typeof TypedArray != 'function' || TypedArray === Function.prototype) {
125 // eslint-disable-next-line no-shadow
126 TypedArray = function TypedArray() {
127 throw TypeError('Incorrect invocation');
129 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
130 if (global[NAME]) setPrototypeOf(global[NAME], TypedArray);
134 if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
135 TypedArrayPrototype = TypedArray.prototype;
136 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
137 if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
141 // WebKit bug - one more object in Uint8ClampedArray prototype chain
142 if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
143 setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
146 if (DESCRIPTORS && !has(TypedArrayPrototype, TO_STRING_TAG)) {
147 TYPED_ARRAY_TAG_REQIRED = true;
148 defineProperty(TypedArrayPrototype, TO_STRING_TAG, { get: function () {
149 return isObject(this) ? this[TYPED_ARRAY_TAG] : undefined;
151 for (NAME in TypedArrayConstructorsList) if (global[NAME]) {
152 createNonEnumerableProperty(global[NAME], TYPED_ARRAY_TAG, NAME);
157 NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
158 TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQIRED && TYPED_ARRAY_TAG,
159 aTypedArray: aTypedArray,
160 aTypedArrayConstructor: aTypedArrayConstructor,
161 exportTypedArrayMethod: exportTypedArrayMethod,
162 exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
164 isTypedArray: isTypedArray,
165 TypedArray: TypedArray,
166 TypedArrayPrototype: TypedArrayPrototype