0af672b4abc54bf7d3389d0c3350e6247e612ce43ee3c9fa5b4ccfad78872302d02cf3f70e8e162e53367374eab36d68b84da824b6f8795403243c13b9349a 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var global = require('../internals/global');
  4. var call = require('../internals/function-call');
  5. var DESCRIPTORS = require('../internals/descriptors');
  6. var TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS = require('../internals/typed-array-constructors-require-wrappers');
  7. var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
  8. var ArrayBufferModule = require('../internals/array-buffer');
  9. var anInstance = require('../internals/an-instance');
  10. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  11. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  12. var isIntegralNumber = require('../internals/is-integral-number');
  13. var toLength = require('../internals/to-length');
  14. var toIndex = require('../internals/to-index');
  15. var toOffset = require('../internals/to-offset');
  16. var toUint8Clamped = require('../internals/to-uint8-clamped');
  17. var toPropertyKey = require('../internals/to-property-key');
  18. var hasOwn = require('../internals/has-own-property');
  19. var classof = require('../internals/classof');
  20. var isObject = require('../internals/is-object');
  21. var isSymbol = require('../internals/is-symbol');
  22. var create = require('../internals/object-create');
  23. var isPrototypeOf = require('../internals/object-is-prototype-of');
  24. var setPrototypeOf = require('../internals/object-set-prototype-of');
  25. var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
  26. var typedArrayFrom = require('../internals/typed-array-from');
  27. var forEach = require('../internals/array-iteration').forEach;
  28. var setSpecies = require('../internals/set-species');
  29. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  30. var definePropertyModule = require('../internals/object-define-property');
  31. var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
  32. var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');
  33. var InternalStateModule = require('../internals/internal-state');
  34. var inheritIfRequired = require('../internals/inherit-if-required');
  35. var getInternalState = InternalStateModule.get;
  36. var setInternalState = InternalStateModule.set;
  37. var enforceInternalState = InternalStateModule.enforce;
  38. var nativeDefineProperty = definePropertyModule.f;
  39. var nativeGetOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
  40. var RangeError = global.RangeError;
  41. var ArrayBuffer = ArrayBufferModule.ArrayBuffer;
  42. var ArrayBufferPrototype = ArrayBuffer.prototype;
  43. var DataView = ArrayBufferModule.DataView;
  44. var NATIVE_ARRAY_BUFFER_VIEWS = ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS;
  45. var TYPED_ARRAY_TAG = ArrayBufferViewCore.TYPED_ARRAY_TAG;
  46. var TypedArray = ArrayBufferViewCore.TypedArray;
  47. var TypedArrayPrototype = ArrayBufferViewCore.TypedArrayPrototype;
  48. var isTypedArray = ArrayBufferViewCore.isTypedArray;
  49. var BYTES_PER_ELEMENT = 'BYTES_PER_ELEMENT';
  50. var WRONG_LENGTH = 'Wrong length';
  51. var addGetter = function (it, key) {
  52. defineBuiltInAccessor(it, key, {
  53. configurable: true,
  54. get: function () {
  55. return getInternalState(this)[key];
  56. }
  57. });
  58. };
  59. var isArrayBuffer = function (it) {
  60. var klass;
  61. return isPrototypeOf(ArrayBufferPrototype, it) || (klass = classof(it)) === 'ArrayBuffer' || klass === 'SharedArrayBuffer';
  62. };
  63. var isTypedArrayIndex = function (target, key) {
  64. return isTypedArray(target)
  65. && !isSymbol(key)
  66. && key in target
  67. && isIntegralNumber(+key)
  68. && key >= 0;
  69. };
  70. var wrappedGetOwnPropertyDescriptor = function getOwnPropertyDescriptor(target, key) {
  71. key = toPropertyKey(key);
  72. return isTypedArrayIndex(target, key)
  73. ? createPropertyDescriptor(2, target[key])
  74. : nativeGetOwnPropertyDescriptor(target, key);
  75. };
  76. var wrappedDefineProperty = function defineProperty(target, key, descriptor) {
  77. key = toPropertyKey(key);
  78. if (isTypedArrayIndex(target, key)
  79. && isObject(descriptor)
  80. && hasOwn(descriptor, 'value')
  81. && !hasOwn(descriptor, 'get')
  82. && !hasOwn(descriptor, 'set')
  83. // TODO: add validation descriptor w/o calling accessors
  84. && !descriptor.configurable
  85. && (!hasOwn(descriptor, 'writable') || descriptor.writable)
  86. && (!hasOwn(descriptor, 'enumerable') || descriptor.enumerable)
  87. ) {
  88. target[key] = descriptor.value;
  89. return target;
  90. } return nativeDefineProperty(target, key, descriptor);
  91. };
  92. if (DESCRIPTORS) {
  93. if (!NATIVE_ARRAY_BUFFER_VIEWS) {
  94. getOwnPropertyDescriptorModule.f = wrappedGetOwnPropertyDescriptor;
  95. definePropertyModule.f = wrappedDefineProperty;
  96. addGetter(TypedArrayPrototype, 'buffer');
  97. addGetter(TypedArrayPrototype, 'byteOffset');
  98. addGetter(TypedArrayPrototype, 'byteLength');
  99. addGetter(TypedArrayPrototype, 'length');
  100. }
  101. $({ target: 'Object', stat: true, forced: !NATIVE_ARRAY_BUFFER_VIEWS }, {
  102. getOwnPropertyDescriptor: wrappedGetOwnPropertyDescriptor,
  103. defineProperty: wrappedDefineProperty
  104. });
  105. module.exports = function (TYPE, wrapper, CLAMPED) {
  106. var BYTES = TYPE.match(/\d+/)[0] / 8;
  107. var CONSTRUCTOR_NAME = TYPE + (CLAMPED ? 'Clamped' : '') + 'Array';
  108. var GETTER = 'get' + TYPE;
  109. var SETTER = 'set' + TYPE;
  110. var NativeTypedArrayConstructor = global[CONSTRUCTOR_NAME];
  111. var TypedArrayConstructor = NativeTypedArrayConstructor;
  112. var TypedArrayConstructorPrototype = TypedArrayConstructor && TypedArrayConstructor.prototype;
  113. var exported = {};
  114. var getter = function (that, index) {
  115. var data = getInternalState(that);
  116. return data.view[GETTER](index * BYTES + data.byteOffset, true);
  117. };
  118. var setter = function (that, index, value) {
  119. var data = getInternalState(that);
  120. data.view[SETTER](index * BYTES + data.byteOffset, CLAMPED ? toUint8Clamped(value) : value, true);
  121. };
  122. var addElement = function (that, index) {
  123. nativeDefineProperty(that, index, {
  124. get: function () {
  125. return getter(this, index);
  126. },
  127. set: function (value) {
  128. return setter(this, index, value);
  129. },
  130. enumerable: true
  131. });
  132. };
  133. if (!NATIVE_ARRAY_BUFFER_VIEWS) {
  134. TypedArrayConstructor = wrapper(function (that, data, offset, $length) {
  135. anInstance(that, TypedArrayConstructorPrototype);
  136. var index = 0;
  137. var byteOffset = 0;
  138. var buffer, byteLength, length;
  139. if (!isObject(data)) {
  140. length = toIndex(data);
  141. byteLength = length * BYTES;
  142. buffer = new ArrayBuffer(byteLength);
  143. } else if (isArrayBuffer(data)) {
  144. buffer = data;
  145. byteOffset = toOffset(offset, BYTES);
  146. var $len = data.byteLength;
  147. if ($length === undefined) {
  148. if ($len % BYTES) throw new RangeError(WRONG_LENGTH);
  149. byteLength = $len - byteOffset;
  150. if (byteLength < 0) throw new RangeError(WRONG_LENGTH);
  151. } else {
  152. byteLength = toLength($length) * BYTES;
  153. if (byteLength + byteOffset > $len) throw new RangeError(WRONG_LENGTH);
  154. }
  155. length = byteLength / BYTES;
  156. } else if (isTypedArray(data)) {
  157. return arrayFromConstructorAndList(TypedArrayConstructor, data);
  158. } else {
  159. return call(typedArrayFrom, TypedArrayConstructor, data);
  160. }
  161. setInternalState(that, {
  162. buffer: buffer,
  163. byteOffset: byteOffset,
  164. byteLength: byteLength,
  165. length: length,
  166. view: new DataView(buffer)
  167. });
  168. while (index < length) addElement(that, index++);
  169. });
  170. if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
  171. TypedArrayConstructorPrototype = TypedArrayConstructor.prototype = create(TypedArrayPrototype);
  172. } else if (TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS) {
  173. TypedArrayConstructor = wrapper(function (dummy, data, typedArrayOffset, $length) {
  174. anInstance(dummy, TypedArrayConstructorPrototype);
  175. return inheritIfRequired(function () {
  176. if (!isObject(data)) return new NativeTypedArrayConstructor(toIndex(data));
  177. if (isArrayBuffer(data)) return $length !== undefined
  178. ? new NativeTypedArrayConstructor(data, toOffset(typedArrayOffset, BYTES), $length)
  179. : typedArrayOffset !== undefined
  180. ? new NativeTypedArrayConstructor(data, toOffset(typedArrayOffset, BYTES))
  181. : new NativeTypedArrayConstructor(data);
  182. if (isTypedArray(data)) return arrayFromConstructorAndList(TypedArrayConstructor, data);
  183. return call(typedArrayFrom, TypedArrayConstructor, data);
  184. }(), dummy, TypedArrayConstructor);
  185. });
  186. if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
  187. forEach(getOwnPropertyNames(NativeTypedArrayConstructor), function (key) {
  188. if (!(key in TypedArrayConstructor)) {
  189. createNonEnumerableProperty(TypedArrayConstructor, key, NativeTypedArrayConstructor[key]);
  190. }
  191. });
  192. TypedArrayConstructor.prototype = TypedArrayConstructorPrototype;
  193. }
  194. if (TypedArrayConstructorPrototype.constructor !== TypedArrayConstructor) {
  195. createNonEnumerableProperty(TypedArrayConstructorPrototype, 'constructor', TypedArrayConstructor);
  196. }
  197. enforceInternalState(TypedArrayConstructorPrototype).TypedArrayConstructor = TypedArrayConstructor;
  198. if (TYPED_ARRAY_TAG) {
  199. createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
  200. }
  201. var FORCED = TypedArrayConstructor !== NativeTypedArrayConstructor;
  202. exported[CONSTRUCTOR_NAME] = TypedArrayConstructor;
  203. $({ global: true, constructor: true, forced: FORCED, sham: !NATIVE_ARRAY_BUFFER_VIEWS }, exported);
  204. if (!(BYTES_PER_ELEMENT in TypedArrayConstructor)) {
  205. createNonEnumerableProperty(TypedArrayConstructor, BYTES_PER_ELEMENT, BYTES);
  206. }
  207. if (!(BYTES_PER_ELEMENT in TypedArrayConstructorPrototype)) {
  208. createNonEnumerableProperty(TypedArrayConstructorPrototype, BYTES_PER_ELEMENT, BYTES);
  209. }
  210. setSpecies(CONSTRUCTOR_NAME);
  211. };
  212. } else module.exports = function () { /* empty */ };