array-buffer-view-core.js 6.4 KB

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