1 | 'use strict';
|
2 | var DESCRIPTORS = require('../internals/descriptors');
|
3 | var global = require('../internals/global');
|
4 | var isObject = require('../internals/is-object');
|
5 | var has = require('../internals/has');
|
6 | var classof = require('../internals/classof');
|
7 | var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
|
8 | var redefine = require('../internals/redefine');
|
9 | var defineProperty = require('../internals/object-define-property').f;
|
10 | var getPrototypeOf = require('../internals/object-get-prototype-of');
|
11 | var setPrototypeOf = require('../internals/object-set-prototype-of');
|
12 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
13 | var uid = require('../internals/uid');
|
14 |
|
15 | var DataView = global.DataView;
|
16 | var DataViewPrototype = DataView && DataView.prototype;
|
17 | var Int8Array = global.Int8Array;
|
18 | var Int8ArrayPrototype = Int8Array && Int8Array.prototype;
|
19 | var Uint8ClampedArray = global.Uint8ClampedArray;
|
20 | var Uint8ClampedArrayPrototype = Uint8ClampedArray && Uint8ClampedArray.prototype;
|
21 | var TypedArray = Int8Array && getPrototypeOf(Int8Array);
|
22 | var TypedArrayPrototype = Int8ArrayPrototype && getPrototypeOf(Int8ArrayPrototype);
|
23 | var ObjectPrototype = Object.prototype;
|
24 | var isPrototypeOf = ObjectPrototype.isPrototypeOf;
|
25 |
|
26 | var TO_STRING_TAG = wellKnownSymbol('toStringTag');
|
27 | var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
|
28 | var NATIVE_ARRAY_BUFFER = !!(global.ArrayBuffer && DataView);
|
29 |
|
30 | var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
|
31 | var TYPED_ARRAY_TAG_REQIRED = false;
|
32 | var NAME;
|
33 |
|
34 | var 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 |
|
46 | var isView = function isView(it) {
|
47 | var klass = classof(it);
|
48 | return klass === 'DataView' || has(TypedArrayConstructorsList, klass);
|
49 | };
|
50 |
|
51 | var isTypedArray = function (it) {
|
52 | return isObject(it) && has(TypedArrayConstructorsList, classof(it));
|
53 | };
|
54 |
|
55 | var aTypedArray = function (it) {
|
56 | if (isTypedArray(it)) return it;
|
57 | throw TypeError('Target is not a typed array');
|
58 | };
|
59 |
|
60 | var 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 |
|
71 | var 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 |
|
85 | var 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 |
|
97 | try {
|
98 | return redefine(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && Int8Array[KEY] || property);
|
99 | } catch (error) { }
|
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 |
|
110 | for (NAME in TypedArrayConstructorsList) {
|
111 | if (!global[NAME]) NATIVE_ARRAY_BUFFER_VIEWS = false;
|
112 | }
|
113 |
|
114 |
|
115 | if (!NATIVE_ARRAY_BUFFER_VIEWS || typeof TypedArray != 'function' || TypedArray === Function.prototype) {
|
116 |
|
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 |
|
125 | if (!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 |
|
133 | if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
|
134 | setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
|
135 | }
|
136 |
|
137 | if (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 |
|
148 | if (NATIVE_ARRAY_BUFFER && setPrototypeOf && getPrototypeOf(DataViewPrototype) !== ObjectPrototype) {
|
149 | setPrototypeOf(DataViewPrototype, ObjectPrototype);
|
150 | }
|
151 |
|
152 | module.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 | };
|