array-buffer.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. 'use strict';
  2. var global = require('../internals/global');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var DESCRIPTORS = require('../internals/descriptors');
  5. var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-native');
  6. var FunctionName = require('../internals/function-name');
  7. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  8. var redefineAll = require('../internals/redefine-all');
  9. var fails = require('../internals/fails');
  10. var anInstance = require('../internals/an-instance');
  11. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  12. var toLength = require('../internals/to-length');
  13. var toIndex = require('../internals/to-index');
  14. var IEEE754 = require('../internals/ieee754');
  15. var getPrototypeOf = require('../internals/object-get-prototype-of');
  16. var setPrototypeOf = require('../internals/object-set-prototype-of');
  17. var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
  18. var defineProperty = require('../internals/object-define-property').f;
  19. var arrayFill = require('../internals/array-fill');
  20. var arraySlice = require('../internals/array-slice-simple');
  21. var setToStringTag = require('../internals/set-to-string-tag');
  22. var InternalStateModule = require('../internals/internal-state');
  23. var PROPER_FUNCTION_NAME = FunctionName.PROPER;
  24. var CONFIGURABLE_FUNCTION_NAME = FunctionName.CONFIGURABLE;
  25. var getInternalState = InternalStateModule.get;
  26. var setInternalState = InternalStateModule.set;
  27. var ARRAY_BUFFER = 'ArrayBuffer';
  28. var DATA_VIEW = 'DataView';
  29. var PROTOTYPE = 'prototype';
  30. var WRONG_LENGTH = 'Wrong length';
  31. var WRONG_INDEX = 'Wrong index';
  32. var NativeArrayBuffer = global[ARRAY_BUFFER];
  33. var $ArrayBuffer = NativeArrayBuffer;
  34. var ArrayBufferPrototype = $ArrayBuffer && $ArrayBuffer[PROTOTYPE];
  35. var $DataView = global[DATA_VIEW];
  36. var DataViewPrototype = $DataView && $DataView[PROTOTYPE];
  37. var ObjectPrototype = Object.prototype;
  38. var Array = global.Array;
  39. var RangeError = global.RangeError;
  40. var fill = uncurryThis(arrayFill);
  41. var reverse = uncurryThis([].reverse);
  42. var packIEEE754 = IEEE754.pack;
  43. var unpackIEEE754 = IEEE754.unpack;
  44. var packInt8 = function (number) {
  45. return [number & 0xFF];
  46. };
  47. var packInt16 = function (number) {
  48. return [number & 0xFF, number >> 8 & 0xFF];
  49. };
  50. var packInt32 = function (number) {
  51. return [number & 0xFF, number >> 8 & 0xFF, number >> 16 & 0xFF, number >> 24 & 0xFF];
  52. };
  53. var unpackInt32 = function (buffer) {
  54. return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
  55. };
  56. var packFloat32 = function (number) {
  57. return packIEEE754(number, 23, 4);
  58. };
  59. var packFloat64 = function (number) {
  60. return packIEEE754(number, 52, 8);
  61. };
  62. var addGetter = function (Constructor, key) {
  63. defineProperty(Constructor[PROTOTYPE], key, { get: function () { return getInternalState(this)[key]; } });
  64. };
  65. var get = function (view, count, index, isLittleEndian) {
  66. var intIndex = toIndex(index);
  67. var store = getInternalState(view);
  68. if (intIndex + count > store.byteLength) throw RangeError(WRONG_INDEX);
  69. var bytes = getInternalState(store.buffer).bytes;
  70. var start = intIndex + store.byteOffset;
  71. var pack = arraySlice(bytes, start, start + count);
  72. return isLittleEndian ? pack : reverse(pack);
  73. };
  74. var set = function (view, count, index, conversion, value, isLittleEndian) {
  75. var intIndex = toIndex(index);
  76. var store = getInternalState(view);
  77. if (intIndex + count > store.byteLength) throw RangeError(WRONG_INDEX);
  78. var bytes = getInternalState(store.buffer).bytes;
  79. var start = intIndex + store.byteOffset;
  80. var pack = conversion(+value);
  81. for (var i = 0; i < count; i++) bytes[start + i] = pack[isLittleEndian ? i : count - i - 1];
  82. };
  83. if (!NATIVE_ARRAY_BUFFER) {
  84. $ArrayBuffer = function ArrayBuffer(length) {
  85. anInstance(this, ArrayBufferPrototype);
  86. var byteLength = toIndex(length);
  87. setInternalState(this, {
  88. bytes: fill(Array(byteLength), 0),
  89. byteLength: byteLength
  90. });
  91. if (!DESCRIPTORS) this.byteLength = byteLength;
  92. };
  93. ArrayBufferPrototype = $ArrayBuffer[PROTOTYPE];
  94. $DataView = function DataView(buffer, byteOffset, byteLength) {
  95. anInstance(this, DataViewPrototype);
  96. anInstance(buffer, ArrayBufferPrototype);
  97. var bufferLength = getInternalState(buffer).byteLength;
  98. var offset = toIntegerOrInfinity(byteOffset);
  99. if (offset < 0 || offset > bufferLength) throw RangeError('Wrong offset');
  100. byteLength = byteLength === undefined ? bufferLength - offset : toLength(byteLength);
  101. if (offset + byteLength > bufferLength) throw RangeError(WRONG_LENGTH);
  102. setInternalState(this, {
  103. buffer: buffer,
  104. byteLength: byteLength,
  105. byteOffset: offset
  106. });
  107. if (!DESCRIPTORS) {
  108. this.buffer = buffer;
  109. this.byteLength = byteLength;
  110. this.byteOffset = offset;
  111. }
  112. };
  113. DataViewPrototype = $DataView[PROTOTYPE];
  114. if (DESCRIPTORS) {
  115. addGetter($ArrayBuffer, 'byteLength');
  116. addGetter($DataView, 'buffer');
  117. addGetter($DataView, 'byteLength');
  118. addGetter($DataView, 'byteOffset');
  119. }
  120. redefineAll(DataViewPrototype, {
  121. getInt8: function getInt8(byteOffset) {
  122. return get(this, 1, byteOffset)[0] << 24 >> 24;
  123. },
  124. getUint8: function getUint8(byteOffset) {
  125. return get(this, 1, byteOffset)[0];
  126. },
  127. getInt16: function getInt16(byteOffset /* , littleEndian */) {
  128. var bytes = get(this, 2, byteOffset, arguments.length > 1 ? arguments[1] : undefined);
  129. return (bytes[1] << 8 | bytes[0]) << 16 >> 16;
  130. },
  131. getUint16: function getUint16(byteOffset /* , littleEndian */) {
  132. var bytes = get(this, 2, byteOffset, arguments.length > 1 ? arguments[1] : undefined);
  133. return bytes[1] << 8 | bytes[0];
  134. },
  135. getInt32: function getInt32(byteOffset /* , littleEndian */) {
  136. return unpackInt32(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : undefined));
  137. },
  138. getUint32: function getUint32(byteOffset /* , littleEndian */) {
  139. return unpackInt32(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : undefined)) >>> 0;
  140. },
  141. getFloat32: function getFloat32(byteOffset /* , littleEndian */) {
  142. return unpackIEEE754(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : undefined), 23);
  143. },
  144. getFloat64: function getFloat64(byteOffset /* , littleEndian */) {
  145. return unpackIEEE754(get(this, 8, byteOffset, arguments.length > 1 ? arguments[1] : undefined), 52);
  146. },
  147. setInt8: function setInt8(byteOffset, value) {
  148. set(this, 1, byteOffset, packInt8, value);
  149. },
  150. setUint8: function setUint8(byteOffset, value) {
  151. set(this, 1, byteOffset, packInt8, value);
  152. },
  153. setInt16: function setInt16(byteOffset, value /* , littleEndian */) {
  154. set(this, 2, byteOffset, packInt16, value, arguments.length > 2 ? arguments[2] : undefined);
  155. },
  156. setUint16: function setUint16(byteOffset, value /* , littleEndian */) {
  157. set(this, 2, byteOffset, packInt16, value, arguments.length > 2 ? arguments[2] : undefined);
  158. },
  159. setInt32: function setInt32(byteOffset, value /* , littleEndian */) {
  160. set(this, 4, byteOffset, packInt32, value, arguments.length > 2 ? arguments[2] : undefined);
  161. },
  162. setUint32: function setUint32(byteOffset, value /* , littleEndian */) {
  163. set(this, 4, byteOffset, packInt32, value, arguments.length > 2 ? arguments[2] : undefined);
  164. },
  165. setFloat32: function setFloat32(byteOffset, value /* , littleEndian */) {
  166. set(this, 4, byteOffset, packFloat32, value, arguments.length > 2 ? arguments[2] : undefined);
  167. },
  168. setFloat64: function setFloat64(byteOffset, value /* , littleEndian */) {
  169. set(this, 8, byteOffset, packFloat64, value, arguments.length > 2 ? arguments[2] : undefined);
  170. }
  171. });
  172. } else {
  173. var INCORRECT_ARRAY_BUFFER_NAME = PROPER_FUNCTION_NAME && NativeArrayBuffer.name !== ARRAY_BUFFER;
  174. /* eslint-disable no-new -- required for testing */
  175. if (!fails(function () {
  176. NativeArrayBuffer(1);
  177. }) || !fails(function () {
  178. new NativeArrayBuffer(-1);
  179. }) || fails(function () {
  180. new NativeArrayBuffer();
  181. new NativeArrayBuffer(1.5);
  182. new NativeArrayBuffer(NaN);
  183. return INCORRECT_ARRAY_BUFFER_NAME && !CONFIGURABLE_FUNCTION_NAME;
  184. })) {
  185. /* eslint-enable no-new -- required for testing */
  186. $ArrayBuffer = function ArrayBuffer(length) {
  187. anInstance(this, ArrayBufferPrototype);
  188. return new NativeArrayBuffer(toIndex(length));
  189. };
  190. $ArrayBuffer[PROTOTYPE] = ArrayBufferPrototype;
  191. for (var keys = getOwnPropertyNames(NativeArrayBuffer), j = 0, key; keys.length > j;) {
  192. if (!((key = keys[j++]) in $ArrayBuffer)) {
  193. createNonEnumerableProperty($ArrayBuffer, key, NativeArrayBuffer[key]);
  194. }
  195. }
  196. ArrayBufferPrototype.constructor = $ArrayBuffer;
  197. } else if (INCORRECT_ARRAY_BUFFER_NAME && CONFIGURABLE_FUNCTION_NAME) {
  198. createNonEnumerableProperty(NativeArrayBuffer, 'name', ARRAY_BUFFER);
  199. }
  200. // WebKit bug - the same parent prototype for typed arrays and data view
  201. if (setPrototypeOf && getPrototypeOf(DataViewPrototype) !== ObjectPrototype) {
  202. setPrototypeOf(DataViewPrototype, ObjectPrototype);
  203. }
  204. // iOS Safari 7.x bug
  205. var testView = new $DataView(new $ArrayBuffer(2));
  206. var $setInt8 = uncurryThis(DataViewPrototype.setInt8);
  207. testView.setInt8(0, 2147483648);
  208. testView.setInt8(1, 2147483649);
  209. if (testView.getInt8(0) || !testView.getInt8(1)) redefineAll(DataViewPrototype, {
  210. setInt8: function setInt8(byteOffset, value) {
  211. $setInt8(this, byteOffset, value << 24 >> 24);
  212. },
  213. setUint8: function setUint8(byteOffset, value) {
  214. $setInt8(this, byteOffset, value << 24 >> 24);
  215. }
  216. }, { unsafe: true });
  217. }
  218. setToStringTag($ArrayBuffer, ARRAY_BUFFER);
  219. setToStringTag($DataView, DATA_VIEW);
  220. module.exports = {
  221. ArrayBuffer: $ArrayBuffer,
  222. DataView: $DataView
  223. };