array-buffer-view-core.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // Fixing native typed arrays in Opera Presto crashes the browser, see #595
  26. var NATIVE_ARRAY_BUFFER_VIEWS = NATIVE_ARRAY_BUFFER && !!setPrototypeOf && classof(global.opera) !== 'Opera';
  27. var TYPED_ARRAY_TAG_REQIRED = false;
  28. var NAME;
  29. var TypedArrayConstructorsList = {
  30. Int8Array: 1,
  31. Uint8Array: 1,
  32. Uint8ClampedArray: 1,
  33. Int16Array: 2,
  34. Uint16Array: 2,
  35. Int32Array: 4,
  36. Uint32Array: 4,
  37. Float32Array: 4,
  38. Float64Array: 8
  39. };
  40. var isView = function isView(it) {
  41. var klass = classof(it);
  42. return klass === 'DataView' || has(TypedArrayConstructorsList, klass);
  43. };
  44. var isTypedArray = function (it) {
  45. return isObject(it) && has(TypedArrayConstructorsList, classof(it));
  46. };
  47. var aTypedArray = function (it) {
  48. if (isTypedArray(it)) return it;
  49. throw TypeError('Target is not a typed array');
  50. };
  51. var aTypedArrayConstructor = function (C) {
  52. if (setPrototypeOf) {
  53. if (isPrototypeOf.call(TypedArray, C)) return C;
  54. } else for (var ARRAY in TypedArrayConstructorsList) if (has(TypedArrayConstructorsList, NAME)) {
  55. var TypedArrayConstructor = global[ARRAY];
  56. if (TypedArrayConstructor && (C === TypedArrayConstructor || isPrototypeOf.call(TypedArrayConstructor, C))) {
  57. return C;
  58. }
  59. } throw TypeError('Target is not a typed array constructor');
  60. };
  61. var exportTypedArrayMethod = function (KEY, property, forced) {
  62. if (!DESCRIPTORS) return;
  63. if (forced) for (var ARRAY in TypedArrayConstructorsList) {
  64. var TypedArrayConstructor = global[ARRAY];
  65. if (TypedArrayConstructor && has(TypedArrayConstructor.prototype, KEY)) {
  66. delete TypedArrayConstructor.prototype[KEY];
  67. }
  68. }
  69. if (!TypedArrayPrototype[KEY] || forced) {
  70. redefine(TypedArrayPrototype, KEY, forced ? property
  71. : NATIVE_ARRAY_BUFFER_VIEWS && Int8ArrayPrototype[KEY] || property);
  72. }
  73. };
  74. var exportTypedArrayStaticMethod = function (KEY, property, forced) {
  75. var ARRAY, TypedArrayConstructor;
  76. if (!DESCRIPTORS) return;
  77. if (setPrototypeOf) {
  78. if (forced) for (ARRAY in TypedArrayConstructorsList) {
  79. TypedArrayConstructor = global[ARRAY];
  80. if (TypedArrayConstructor && has(TypedArrayConstructor, KEY)) {
  81. delete TypedArrayConstructor[KEY];
  82. }
  83. }
  84. if (!TypedArray[KEY] || forced) {
  85. // V8 ~ Chrome 49-50 `%TypedArray%` methods are non-writable non-configurable
  86. try {
  87. return redefine(TypedArray, KEY, forced ? property : NATIVE_ARRAY_BUFFER_VIEWS && Int8Array[KEY] || property);
  88. } catch (error) { /* empty */ }
  89. } else return;
  90. }
  91. for (ARRAY in TypedArrayConstructorsList) {
  92. TypedArrayConstructor = global[ARRAY];
  93. if (TypedArrayConstructor && (!TypedArrayConstructor[KEY] || forced)) {
  94. redefine(TypedArrayConstructor, KEY, property);
  95. }
  96. }
  97. };
  98. for (NAME in TypedArrayConstructorsList) {
  99. if (!global[NAME]) NATIVE_ARRAY_BUFFER_VIEWS = false;
  100. }
  101. // WebKit bug - typed arrays constructors prototype is Object.prototype
  102. if (!NATIVE_ARRAY_BUFFER_VIEWS || typeof TypedArray != 'function' || TypedArray === Function.prototype) {
  103. // eslint-disable-next-line no-shadow
  104. TypedArray = function TypedArray() {
  105. throw TypeError('Incorrect invocation');
  106. };
  107. if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
  108. if (global[NAME]) setPrototypeOf(global[NAME], TypedArray);
  109. }
  110. }
  111. if (!NATIVE_ARRAY_BUFFER_VIEWS || !TypedArrayPrototype || TypedArrayPrototype === ObjectPrototype) {
  112. TypedArrayPrototype = TypedArray.prototype;
  113. if (NATIVE_ARRAY_BUFFER_VIEWS) for (NAME in TypedArrayConstructorsList) {
  114. if (global[NAME]) setPrototypeOf(global[NAME].prototype, TypedArrayPrototype);
  115. }
  116. }
  117. // WebKit bug - one more object in Uint8ClampedArray prototype chain
  118. if (NATIVE_ARRAY_BUFFER_VIEWS && getPrototypeOf(Uint8ClampedArrayPrototype) !== TypedArrayPrototype) {
  119. setPrototypeOf(Uint8ClampedArrayPrototype, TypedArrayPrototype);
  120. }
  121. if (DESCRIPTORS && !has(TypedArrayPrototype, TO_STRING_TAG)) {
  122. TYPED_ARRAY_TAG_REQIRED = true;
  123. defineProperty(TypedArrayPrototype, TO_STRING_TAG, { get: function () {
  124. return isObject(this) ? this[TYPED_ARRAY_TAG] : undefined;
  125. } });
  126. for (NAME in TypedArrayConstructorsList) if (global[NAME]) {
  127. createNonEnumerableProperty(global[NAME], TYPED_ARRAY_TAG, NAME);
  128. }
  129. }
  130. module.exports = {
  131. NATIVE_ARRAY_BUFFER_VIEWS: NATIVE_ARRAY_BUFFER_VIEWS,
  132. TYPED_ARRAY_TAG: TYPED_ARRAY_TAG_REQIRED && TYPED_ARRAY_TAG,
  133. aTypedArray: aTypedArray,
  134. aTypedArrayConstructor: aTypedArrayConstructor,
  135. exportTypedArrayMethod: exportTypedArrayMethod,
  136. exportTypedArrayStaticMethod: exportTypedArrayStaticMethod,
  137. isView: isView,
  138. isTypedArray: isTypedArray,
  139. TypedArray: TypedArray,
  140. TypedArrayPrototype: TypedArrayPrototype
  141. };