1 | 'use strict';
|
---|
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');
|
---|
15 |
|
---|
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;
|
---|
24 |
|
---|
25 | var TO_STRING_TAG = wellKnownSymbol('toStringTag');
|
---|
26 | var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
|
---|
27 | var TYPED_ARRAY_CONSTRUCTOR = uid('TYPED_ARRAY_CONSTRUCTOR');
|
---|
28 | // Fixing native typed arrays in Opera Presto crashes the browser, see #595
|
---|
29 | var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
|
---|
30 | var TYPED_ARRAY_TAG_REQIRED = false;
|
---|
31 | var NAME, Constructor, Prototype;
|
---|
32 |
|
---|
33 | var TypedArrayConstructorsList = {
|
---|
34 | Int8Array: 1,
|
---|
35 | Uint8Array: 1,
|
---|
36 | Uint8ClampedArray: 1,
|
---|
37 | Int16Array: 2,
|
---|
38 | Uint16Array: 2,
|
---|
39 | Int32Array: 4,
|
---|
40 | Uint32Array: 4,
|
---|
41 | Float32Array: 4,
|
---|
42 | Float64Array: 8
|
---|
43 | };
|
---|
44 |
|
---|
45 | var BigIntArrayConstructorsList = {
|
---|
46 | BigInt64Array: 8,
|
---|
47 | BigUint64Array: 8
|
---|
48 | };
|
---|
49 |
|
---|
50 | var isView = function isView(it) {
|
---|
51 | if (!isObject(it)) return false;
|
---|
52 | var klass = classof(it);
|
---|
53 | return klass === 'DataView'
|
---|
54 | || has(TypedArrayConstructorsList, klass)
|
---|
55 | || has(BigIntArrayConstructorsList, klass);
|
---|
56 | };
|
---|
57 |
|
---|
58 | var isTypedArray = function (it) {
|
---|
59 | if (!isObject(it)) return false;
|
---|
60 | var klass = classof(it);
|
---|
61 | return has(TypedArrayConstructorsList, klass)
|
---|
62 | || has(BigIntArrayConstructorsList, klass);
|
---|
63 | };
|
---|
64 |
|
---|
65 | var aTypedArray = function (it) {
|
---|
66 | if (isTypedArray(it)) return it;
|
---|
67 | throw TypeError('Target is not a typed array');
|
---|
68 | };
|
---|
69 |
|
---|
70 | var aTypedArrayConstructor = function (C) {
|
---|
71 | if (setPrototypeOf && !isPrototypeOf.call(TypedArray, C)) {
|
---|
72 | throw TypeError('Target is not a typed array constructor');
|
---|
73 | } return C;
|
---|
74 | };
|
---|
75 |
|
---|
76 | var exportTypedArrayMethod = function (KEY, property, forced) {
|
---|
77 | if (!DESCRIPTORS) return;
|
---|
78 | if (forced) for (var ARRAY in TypedArrayConstructorsList) {
|
---|
79 | var TypedArrayConstructor = global[ARRAY];
|
---|
80 | if (TypedArrayConstructor && has(TypedArrayConstructor.prototype, KEY)) try {
|
---|
81 | delete TypedArrayConstructor.prototype[KEY];
|
---|
82 | } catch (error) { /* empty */ }
|
---|
83 | }
|
---|
84 | if (!TypedArrayPrototype[KEY] || forced) {
|
---|
85 | redefine(TypedArrayPrototype, KEY, forced ? property
|
---|
86 | : NATIVE_ARRAY_BUFFER_VIEWS && Int8ArrayPrototype[KEY] || property);
|
---|
87 | }
|
---|
88 | };
|
---|
89 |
|
---|
90 | var exportTypedArrayStaticMethod = function (KEY, property, forced) {
|
---|
91 | var ARRAY, TypedArrayConstructor;
|
---|
92 | if (!DESCRIPTORS) return;
|
---|
93 | if (setPrototypeOf) {
|
---|
94 | if (forced) for (ARRAY in TypedArrayConstructorsList) {
|
---|
95 | TypedArrayConstructor = global[ARRAY];
|
---|
96 | if (TypedArrayConstructor && has(TypedArrayConstructor, KEY)) try {
|
---|
97 | delete TypedArrayConstructor[KEY];
|
---|
98 | } catch (error) { /* empty */ }
|
---|
99 | }
|
---|
100 | if (!TypedArray[KEY] || forced) {
|
---|
101 | // V8 ~ Chrome 49-50 `%TypedArray%` methods are non-writable non-configurable
|
---|
102 | try {
|
---|
103 | return redefine(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && TypedArray[KEY] || property);
|
---|
104 | } catch (error) { /* empty */ }
|
---|
105 | } else return;
|
---|
106 | }
|
---|
107 | for (ARRAY in TypedArrayConstructorsList) {
|
---|
108 | TypedArrayConstructor = global[ARRAY];
|
---|
109 | if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
|
---|
110 | redefine(TypedArrayConstructor, KEY, property);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | };
|
---|
114 |
|
---|
115 | for (NAME in TypedArrayConstructorsList) {
|
---|
116 | Constructor = global[NAME];
|
---|
117 | Prototype = Constructor && Constructor.prototype;
|
---|
118 | if (Prototype) createNonEnumerableProperty(Prototype, TYPED_ARRAY_CONSTRUCTOR, Constructor);
|
---|
119 | else NATIVE_ARRAY_BUFFER_VIEWS = false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | for (NAME in BigIntArrayConstructorsList) {
|
---|
123 | Constructor = global[NAME];
|
---|
124 | Prototype = Constructor && Constructor.prototype;
|
---|
125 | if (Prototype) createNonEnumerableProperty(Prototype, TYPED_ARRAY_CONSTRUCTOR, Constructor);
|
---|
126 | }
|
---|
127 |
|
---|
128 | // WebKit bug - typed arrays constructors prototype is Object.prototype
|
---|
129 | if (!NATIVE_ARRAY_BUFFER_VIEWS || typeof TypedArray != 'function' || TypedArray === Function.prototype) {
|
---|
130 | // eslint-disable-next-line no-shadow -- safe
|
---|
131 | TypedArray = function TypedArray() {
|
---|
132 | throw TypeError('Incorrect invocation');
|
---|
133 | };
|
---|
134 | if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
|
---|
135 | if (global[NAME]) setPrototypeOf(global[NAME], TypedArray);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
|
---|
140 | TypedArrayPrototype = TypedArray.prototype;
|
---|
141 | if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
|
---|
142 | if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | // WebKit bug - one more object in Uint8ClampedArray prototype chain
|
---|
147 | if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
|
---|
148 | setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (DESCRIPTORS && !has(TypedArrayPrototype, TO_STRING_TAG)) {
|
---|
152 | TYPED_ARRAY_TAG_REQIRED = true;
|
---|
153 | defineProperty(TypedArrayPrototype, TO_STRING_TAG, { get: function () {
|
---|
154 | return isObject(this) ? this[TYPED_ARRAY_TAG] : undefined;
|
---|
155 | } });
|
---|
156 | for (NAME in TypedArrayConstructorsList) if (global[NAME]) {
|
---|
157 | createNonEnumerableProperty(global[NAME], TYPED_ARRAY_TAG, NAME);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | module.exports = {
|
---|
162 | NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
|
---|
163 | TYPED_ARRAY_CONSTRUCTOR: TYPED_ARRAY_CONSTRUCTOR,
|
---|
164 | TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQIRED && TYPED_ARRAY_TAG,
|
---|
165 | aTypedArray: aTypedArray,
|
---|
166 | aTypedArrayConstructor: aTypedArrayConstructor,
|
---|
167 | exportTypedArrayMethod: exportTypedArrayMethod,
|
---|
168 | exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
|
---|
169 | isView: isView,
|
---|
170 | isTypedArray: isTypedArray,
|
---|
171 | TypedArray: TypedArray,
|
---|
172 | TypedArrayPrototype: TypedArrayPrototype
|
---|
173 | };
|
---|