| 1 | 'use strict';
|
|---|
| 2 | var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-basic-detection');
|
|---|
| 3 | var DESCRIPTORS = require('../internals/descriptors');
|
|---|
| 4 | var globalThis = require('../internals/global-this');
|
|---|
| 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 defineBuiltIn = require('../internals/define-built-in');
|
|---|
| 12 | var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
|
|---|
| 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');
|
|---|
| 18 | var InternalStateModule = require('../internals/internal-state');
|
|---|
| 19 |
|
|---|
| 20 | var enforceInternalState = InternalStateModule.enforce;
|
|---|
| 21 | var getInternalState = InternalStateModule.get;
|
|---|
| 22 | var Int8Array = globalThis.Int8Array;
|
|---|
| 23 | var Int8ArrayPrototype = Int8Array && Int8Array.prototype;
|
|---|
| 24 | var Uint8ClampedArray = globalThis.Uint8ClampedArray;
|
|---|
| 25 | var Uint8ClampedArrayPrototype = Uint8ClampedArray && Uint8ClampedArray.prototype;
|
|---|
| 26 | var TypedArray = Int8Array && getPrototypeOf(Int8Array);
|
|---|
| 27 | var TypedArrayPrototype = Int8ArrayPrototype && getPrototypeOf(Int8ArrayPrototype);
|
|---|
| 28 | var ObjectPrototype = Object.prototype;
|
|---|
| 29 | var TypeError = globalThis.TypeError;
|
|---|
| 30 |
|
|---|
| 31 | var TO_STRING_TAG = wellKnownSymbol('toStringTag');
|
|---|
| 32 | var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
|
|---|
| 33 | var TYPED_ARRAY_CONSTRUCTOR = 'TypedArrayConstructor';
|
|---|
| 34 | // Fixing native typed arrays in Opera Presto crashes the browser, see #595
|
|---|
| 35 | var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(globalThis.opera) !== 'Opera';
|
|---|
| 36 | var TYPED_ARRAY_TAG_REQUIRED = false;
|
|---|
| 37 | var NAME, Constructor, Prototype;
|
|---|
| 38 |
|
|---|
| 39 | var 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 |
|
|---|
| 51 | var BigIntArrayConstructorsList = {
|
|---|
| 52 | BigInt64Array: 8,
|
|---|
| 53 | BigUint64Array: 8
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | var 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 |
|
|---|
| 64 | var 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 |
|
|---|
| 71 | var 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 |
|
|---|
| 78 | var aTypedArray = function (it) {
|
|---|
| 79 | if (isTypedArray(it)) return it;
|
|---|
| 80 | throw new TypeError('Target is not a typed array');
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | var 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 |
|
|---|
| 88 | var exportTypedArrayMethod = function (KEY, property, forced, options) {
|
|---|
| 89 | if (!DESCRIPTORS) return;
|
|---|
| 90 | if (forced) for (var ARRAY in TypedArrayConstructorsList) {
|
|---|
| 91 | var TypedArrayConstructor = globalThis[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 |
|
|---|
| 107 | var 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 = globalThis[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 = globalThis[ARRAY];
|
|---|
| 126 | if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
|
|---|
| 127 | defineBuiltIn(TypedArrayConstructor, KEY, property);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | for (NAME in TypedArrayConstructorsList) {
|
|---|
| 133 | Constructor = globalThis[NAME];
|
|---|
| 134 | Prototype = Constructor && Constructor.prototype;
|
|---|
| 135 | if (Prototype) enforceInternalState(Prototype)[TYPED_ARRAY_CONSTRUCTOR] = Constructor;
|
|---|
| 136 | else NATIVE_ARRAY_BUFFER_VIEWS = false;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | for (NAME in BigIntArrayConstructorsList) {
|
|---|
| 140 | Constructor = globalThis[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
|
|---|
| 146 | if (!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 (globalThis[NAME]) setPrototypeOf(globalThis[NAME], TypedArray);
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
|
|---|
| 157 | TypedArrayPrototype = TypedArray.prototype;
|
|---|
| 158 | if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
|
|---|
| 159 | if (globalThis[NAME]) setPrototypeOf(globalThis[NAME].prototype, TypedArrayPrototype);
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | // WebKit bug - one more object in Uint8ClampedArray prototype chain
|
|---|
| 164 | if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
|
|---|
| 165 | setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | if (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 (globalThis[NAME]) {
|
|---|
| 177 | createNonEnumerableProperty(globalThis[NAME].prototype, TYPED_ARRAY_TAG, NAME);
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | module.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 | };
|
|---|