array-buffer-view-core.js 7.3 KB

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