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 isView = function isView(it) {
45 var klass = classof(it);
46 return klass === 'DataView' || has(TypedArrayConstructorsList, klass);
49 var isTypedArray = function (it) {
50 return isObject(it) && has(TypedArrayConstructorsList, classof(it));
53 var aTypedArray = function (it) {
54 if (isTypedArray(it)) return it;
55 throw TypeError('Target is not a typed array');
58 var aTypedArrayConstructor = function (C) {
60 if (isPrototypeOf.call(TypedArray, C)) return C;
61 } else for (var ARRAY in TypedArrayConstructorsList) if (has(TypedArrayConstructorsList, NAME)) {
62 var TypedArrayConstructor = global[ARRAY];
63 if (TypedArrayConstructor && (C === TypedArrayConstructor || isPrototypeOf.call(TypedArrayConstructor, C))) {
66 } throw TypeError('Target is not a typed array constructor');
69 var exportTypedArrayMethod = function (KEY, property, forced) {
70 if (!DESCRIPTORS) return;
71 if (forced) for (var ARRAY in TypedArrayConstructorsList) {
72 var TypedArrayConstructor = global[ARRAY];
73 if (TypedArrayConstructor && has(TypedArrayConstructor.prototype, KEY)) {
74 delete TypedArrayConstructor.prototype[KEY];
77 if (!TypedArrayPrototype[KEY] || forced) {
78 redefine(TypedArrayPrototype, KEY, forced ? property
79 : NATIVE_ARRAY_BUFFER_VIEWS && Int8ArrayPrototype[KEY] || property);
83 var exportTypedArrayStaticMethod = function (KEY, property, forced) {
84 var ARRAY, TypedArrayConstructor;
85 if (!DESCRIPTORS) return;
87 if (forced) for (ARRAY in TypedArrayConstructorsList) {
88 TypedArrayConstructor = global[ARRAY];
89 if (TypedArrayConstructor && has(TypedArrayConstructor, KEY)) {
90 delete TypedArrayConstructor[KEY];
93 if (!TypedArray[KEY] || forced) {
94 // V8 ~ Chrome 49-50 `%TypedArray%` methods are non-writable non-configurable
96 return redefine(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && Int8Array[KEY] || property);
97 } catch (error) { /* empty */ }
100 for (ARRAY in TypedArrayConstructorsList) {
101 TypedArrayConstructor = global[ARRAY];
102 if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
103 redefine(TypedArrayConstructor, KEY, property);
108 for (NAME in TypedArrayConstructorsList) {
109 if (!global[NAME]) NATIVE_ARRAY_BUFFER_VIEWS = false;
112 // WebKit bug - typed arrays constructors prototype is Object.prototype
113 if (!NATIVE_ARRAY_BUFFER_VIEWS || typeof TypedArray != 'function' || TypedArray === Function.prototype) {
114 // eslint-disable-next-line no-shadow
115 TypedArray = function TypedArray() {
116 throw TypeError('Incorrect invocation');
118 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
119 if (global[NAME]) setPrototypeOf(global[NAME], TypedArray);
123 if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
124 TypedArrayPrototype = TypedArray.prototype;
125 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
126 if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
130 // WebKit bug - one more object in Uint8ClampedArray prototype chain
131 if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
132 setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
135 if (DESCRIPTORS && !has(TypedArrayPrototype, TO_STRING_TAG)) {
136 TYPED_ARRAY_TAG_REQIRED = true;
137 defineProperty(TypedArrayPrototype, TO_STRING_TAG, { get: function () {
138 return isObject(this) ? this[TYPED_ARRAY_TAG] : undefined;
140 for (NAME in TypedArrayConstructorsList) if (global[NAME]) {
141 createNonEnumerableProperty(global[NAME], TYPED_ARRAY_TAG, NAME);
146 NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
147 TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQIRED && TYPED_ARRAY_TAG,
148 aTypedArray: aTypedArray,
149 aTypedArrayConstructor: aTypedArrayConstructor,
150 exportTypedArrayMethod: exportTypedArrayMethod,
151 exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
153 isTypedArray: isTypedArray,
154 TypedArray: TypedArray,
155 TypedArrayPrototype: TypedArrayPrototype