array-buffer.js 10 KB

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