UNPKG

7.51 kBJavaScriptView Raw
1'use strict';
2var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-basic-detection');
3var DESCRIPTORS = require('../internals/descriptors');
4var global = require('../internals/global');
5var isCallable = require('../internals/is-callable');
6var isObject = require('../internals/is-object');
7var hasOwn = require('../internals/has-own-property');
8var classof = require('../internals/classof');
9var tryToString = require('../internals/try-to-string');
10var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
11var defineBuiltIn = require('../internals/define-built-in');
12var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
13var isPrototypeOf = require('../internals/object-is-prototype-of');
14var getPrototypeOf = require('../internals/object-get-prototype-of');
15var setPrototypeOf = require('../internals/object-set-prototype-of');
16var wellKnownSymbol = require('../internals/well-known-symbol');
17var uid = require('../internals/uid');
18var InternalStateModule = require('../internals/internal-state');
19
20var enforceInternalState = InternalStateModule.enforce;
21var getInternalState = InternalStateModule.get;
22var Int8Array = global.Int8Array;
23var Int8ArrayPrototype = Int8Array && Int8Array.prototype;
24var Uint8ClampedArray = global.Uint8ClampedArray;
25var Uint8ClampedArrayPrototype = Uint8ClampedArray && Uint8ClampedArray.prototype;
26var TypedArray = Int8Array && getPrototypeOf(Int8Array);
27var TypedArrayPrototype = Int8ArrayPrototype && getPrototypeOf(Int8ArrayPrototype);
28var ObjectPrototype = Object.prototype;
29var TypeError = global.TypeError;
30
31var TO_STRING_TAG = wellKnownSymbol('toStringTag');
32var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
33var TYPED_ARRAY_CONSTRUCTOR = 'TypedArrayConstructor';
34// Fixing native typed arrays in Opera Presto crashes the browser, see #595
35var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
36var TYPED_ARRAY_TAG_REQUIRED = false;
37var NAME, Constructor, Prototype;
38
39var TypedArrayConstructorsList = {
40 Int8Array: 1,
41 Uint8Array: 1,
42 Uint8ClampedArray: 1,
43 Int16Array: 2,
44 Uint16Array: 2,
45 Int32Array: 4,
46 Uint32Array: 4,
47 Float32Array: 4,
48 Float64Array: 8
49};
50
51var BigIntArrayConstructorsList = {
52 BigInt64Array: 8,
53 BigUint64Array: 8
54};
55
56var isView = function isView(it) {
57 if (!isObject(it)) return false;
58 var klass = classof(it);
59 return klass === 'DataView'
60 || hasOwn(TypedArrayConstructorsList, klass)
61 || hasOwn(BigIntArrayConstructorsList, klass);
62};
63
64var getTypedArrayConstructor = function (it) {
65 var proto = getPrototypeOf(it);
66 if (!isObject(proto)) return;
67 var state = getInternalState(proto);
68 return (state && hasOwn(state, TYPED_ARRAY_CONSTRUCTOR)) ? state[TYPED_ARRAY_CONSTRUCTOR] : getTypedArrayConstructor(proto);
69};
70
71var isTypedArray = function (it) {
72 if (!isObject(it)) return false;
73 var klass = classof(it);
74 return hasOwn(TypedArrayConstructorsList, klass)
75 || hasOwn(BigIntArrayConstructorsList, klass);
76};
77
78var aTypedArray = function (it) {
79 if (isTypedArray(it)) return it;
80 throw new TypeError('Target is not a typed array');
81};
82
83var aTypedArrayConstructor = function (C) {
84 if (isCallable(C) && (!setPrototypeOf || isPrototypeOf(TypedArray, C))) return C;
85 throw new TypeError(tryToString(C) + ' is not a typed array constructor');
86};
87
88var exportTypedArrayMethod = function (KEY, property, forced, options) {
89 if (!DESCRIPTORS) return;
90 if (forced) for (var ARRAY in TypedArrayConstructorsList) {
91 var TypedArrayConstructor = global[ARRAY];
92 if (TypedArrayConstructor && hasOwn(TypedArrayConstructor.prototype, KEY)) try {
93 delete TypedArrayConstructor.prototype[KEY];
94 } catch (error) {
95 // old WebKit bug - some methods are non-configurable
96 try {
97 TypedArrayConstructor.prototype[KEY] = property;
98 } catch (error2) { /* empty */ }
99 }
100 }
101 if (!TypedArrayPrototype[KEY] || forced) {
102 defineBuiltIn(TypedArrayPrototype, KEY, forced ? property
103 : NATIVE_ARRAY_BUFFER_VIEWS && Int8ArrayPrototype[KEY] || property, options);
104 }
105};
106
107var exportTypedArrayStaticMethod = function (KEY, property, forced) {
108 var ARRAY, TypedArrayConstructor;
109 if (!DESCRIPTORS) return;
110 if (setPrototypeOf) {
111 if (forced) for (ARRAY in TypedArrayConstructorsList) {
112 TypedArrayConstructor = global[ARRAY];
113 if (TypedArrayConstructor && hasOwn(TypedArrayConstructor, KEY)) try {
114 delete TypedArrayConstructor[KEY];
115 } catch (error) { /* empty */ }
116 }
117 if (!TypedArray[KEY] || forced) {
118 // V8 ~ Chrome 49-50 `%TypedArray%` methods are non-writable non-configurable
119 try {
120 return defineBuiltIn(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && TypedArray[KEY] || property);
121 } catch (error) { /* empty */ }
122 } else return;
123 }
124 for (ARRAY in TypedArrayConstructorsList) {
125 TypedArrayConstructor = global[ARRAY];
126 if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
127 defineBuiltIn(TypedArrayConstructor, KEY, property);
128 }
129 }
130};
131
132for (NAME in TypedArrayConstructorsList) {
133 Constructor = global[NAME];
134 Prototype = Constructor && Constructor.prototype;
135 if (Prototype) enforceInternalState(Prototype)[TYPED_ARRAY_CONSTRUCTOR] = Constructor;
136 else NATIVE_ARRAY_BUFFER_VIEWS = false;
137}
138
139for (NAME in BigIntArrayConstructorsList) {
140 Constructor = global[NAME];
141 Prototype = Constructor && Constructor.prototype;
142 if (Prototype) enforceInternalState(Prototype)[TYPED_ARRAY_CONSTRUCTOR] = Constructor;
143}
144
145// WebKit bug - typed arrays constructors prototype is Object.prototype
146if (!NATIVE_ARRAY_BUFFER_VIEWS || !isCallable(TypedArray) || TypedArray === Function.prototype) {
147 // eslint-disable-next-line no-shadow -- safe
148 TypedArray = function TypedArray() {
149 throw new TypeError('Incorrect invocation');
150 };
151 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
152 if (global[NAME]) setPrototypeOf(global[NAME], TypedArray);
153 }
154}
155
156if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
157 TypedArrayPrototype = TypedArray.prototype;
158 if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
159 if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
160 }
161}
162
163// WebKit bug - one more object in Uint8ClampedArray prototype chain
164if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
165 setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
166}
167
168if (DESCRIPTORS && !hasOwn(TypedArrayPrototype, TO_STRING_TAG)) {
169 TYPED_ARRAY_TAG_REQUIRED = true;
170 defineBuiltInAccessor(TypedArrayPrototype, TO_STRING_TAG, {
171 configurable: true,
172 get: function () {
173 return isObject(this) ? this[TYPED_ARRAY_TAG] : undefined;
174 }
175 });
176 for (NAME in TypedArrayConstructorsList) if (global[NAME]) {
177 createNonEnumerableProperty(global[NAME], TYPED_ARRAY_TAG, NAME);
178 }
179}
180
181module.exports = {
182 NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
183 TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQUIRED && TYPED_ARRAY_TAG,
184 aTypedArray: aTypedArray,
185 aTypedArrayConstructor: aTypedArrayConstructor,
186 exportTypedArrayMethod: exportTypedArrayMethod,
187 exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
188 getTypedArrayConstructor: getTypedArrayConstructor,
189 isView: isView,
190 isTypedArray: isTypedArray,
191 TypedArray: TypedArray,
192 TypedArrayPrototype: TypedArrayPrototype
193};