array-buffer-view-core.js 6.8 KB

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