UNPKG

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