Node-Red configuration
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

array-buffer.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 'use strict';
  2. var globalThis = require('../internals/global-this');
  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 fround = require('../internals/math-fround');
  16. var IEEE754 = require('../internals/ieee754');
  17. var getPrototypeOf = require('../internals/object-get-prototype-of');
  18. var setPrototypeOf = require('../internals/object-set-prototype-of');
  19. var arrayFill = require('../internals/array-fill');
  20. var arraySlice = require('../internals/array-slice');
  21. var inheritIfRequired = require('../internals/inherit-if-required');
  22. var copyConstructorProperties = require('../internals/copy-constructor-properties');
  23. var setToStringTag = require('../internals/set-to-string-tag');
  24. var InternalStateModule = require('../internals/internal-state');
  25. var PROPER_FUNCTION_NAME = FunctionName.PROPER;
  26. var CONFIGURABLE_FUNCTION_NAME = FunctionName.CONFIGURABLE;
  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 getInternalArrayBufferState = InternalStateModule.getterFor(ARRAY_BUFFER);
  33. var getInternalDataViewState = InternalStateModule.getterFor(DATA_VIEW);
  34. var setInternalState = InternalStateModule.set;
  35. var NativeArrayBuffer = globalThis[ARRAY_BUFFER];
  36. var $ArrayBuffer = NativeArrayBuffer;
  37. var ArrayBufferPrototype = $ArrayBuffer && $ArrayBuffer[PROTOTYPE];
  38. var $DataView = globalThis[DATA_VIEW];
  39. var DataViewPrototype = $DataView && $DataView[PROTOTYPE];
  40. var ObjectPrototype = Object.prototype;
  41. var Array = globalThis.Array;
  42. var RangeError = globalThis.RangeError;
  43. var fill = uncurryThis(arrayFill);
  44. var reverse = uncurryThis([].reverse);
  45. var packIEEE754 = IEEE754.pack;
  46. var unpackIEEE754 = IEEE754.unpack;
  47. var packInt8 = function (number) {
  48. return [number & 0xFF];
  49. };
  50. var packInt16 = function (number) {
  51. return [number & 0xFF, number >> 8 & 0xFF];
  52. };
  53. var packInt32 = function (number) {
  54. return [number & 0xFF, number >> 8 & 0xFF, number >> 16 & 0xFF, number >> 24 & 0xFF];
  55. };
  56. var unpackInt32 = function (buffer) {
  57. return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
  58. };
  59. var packFloat32 = function (number) {
  60. return packIEEE754(fround(number), 23, 4);
  61. };
  62. var packFloat64 = function (number) {
  63. return packIEEE754(number, 52, 8);
  64. };
  65. var addGetter = function (Constructor, key, getInternalState) {
  66. defineBuiltInAccessor(Constructor[PROTOTYPE], key, {
  67. configurable: true,
  68. get: function () {
  69. return getInternalState(this)[key];
  70. }
  71. });
  72. };
  73. var get = function (view, count, index, isLittleEndian) {
  74. var store = getInternalDataViewState(view);
  75. var intIndex = toIndex(index);
  76. var boolIsLittleEndian = !!isLittleEndian;
  77. if (intIndex + count > store.byteLength) throw new RangeError(WRONG_INDEX);
  78. var bytes = store.bytes;
  79. var start = intIndex + store.byteOffset;
  80. var pack = arraySlice(bytes, start, start + count);
  81. return boolIsLittleEndian ? pack : reverse(pack);
  82. };
  83. var set = function (view, count, index, conversion, value, isLittleEndian) {
  84. var store = getInternalDataViewState(view);
  85. var intIndex = toIndex(index);
  86. var pack = conversion(+value);
  87. var boolIsLittleEndian = !!isLittleEndian;
  88. if (intIndex + count > store.byteLength) throw new RangeError(WRONG_INDEX);
  89. var bytes = store.bytes;
  90. var start = intIndex + store.byteOffset;
  91. for (var i = 0; i < count; i++) bytes[start + i] = pack[boolIsLittleEndian ? i : count - i - 1];
  92. };
  93. if (!NATIVE_ARRAY_BUFFER) {
  94. $ArrayBuffer = function ArrayBuffer(length) {
  95. anInstance(this, ArrayBufferPrototype);
  96. var byteLength = toIndex(length);
  97. setInternalState(this, {
  98. type: ARRAY_BUFFER,
  99. bytes: fill(Array(byteLength), 0),
  100. byteLength: byteLength
  101. });
  102. if (!DESCRIPTORS) {
  103. this.byteLength = byteLength;
  104. this.detached = false;
  105. }
  106. };
  107. ArrayBufferPrototype = $ArrayBuffer[PROTOTYPE];
  108. $DataView = function DataView(buffer, byteOffset, byteLength) {
  109. anInstance(this, DataViewPrototype);
  110. anInstance(buffer, ArrayBufferPrototype);
  111. var bufferState = getInternalArrayBufferState(buffer);
  112. var bufferLength = bufferState.byteLength;
  113. var offset = toIntegerOrInfinity(byteOffset);
  114. if (offset < 0 || offset > bufferLength) throw new RangeError('Wrong offset');
  115. byteLength = byteLength === undefined ? bufferLength - offset : toLength(byteLength);
  116. if (offset + byteLength > bufferLength) throw new RangeError(WRONG_LENGTH);
  117. setInternalState(this, {
  118. type: DATA_VIEW,
  119. buffer: buffer,
  120. byteLength: byteLength,
  121. byteOffset: offset,
  122. bytes: bufferState.bytes
  123. });
  124. if (!DESCRIPTORS) {
  125. this.buffer = buffer;
  126. this.byteLength = byteLength;
  127. this.byteOffset = offset;
  128. }
  129. };
  130. DataViewPrototype = $DataView[PROTOTYPE];
  131. if (DESCRIPTORS) {
  132. addGetter($ArrayBuffer, 'byteLength', getInternalArrayBufferState);
  133. addGetter($DataView, 'buffer', getInternalDataViewState);
  134. addGetter($DataView, 'byteLength', getInternalDataViewState);
  135. addGetter($DataView, 'byteOffset', getInternalDataViewState);
  136. }
  137. defineBuiltIns(DataViewPrototype, {
  138. getInt8: function getInt8(byteOffset) {
  139. return get(this, 1, byteOffset)[0] << 24 >> 24;
  140. },
  141. getUint8: function getUint8(byteOffset) {
  142. return get(this, 1, byteOffset)[0];
  143. },
  144. getInt16: function getInt16(byteOffset /* , littleEndian */) {
  145. var bytes = get(this, 2, byteOffset, arguments.length > 1 ? arguments[1] : false);
  146. return (bytes[1] << 8 | bytes[0]) << 16 >> 16;
  147. },
  148. getUint16: function getUint16(byteOffset /* , littleEndian */) {
  149. var bytes = get(this, 2, byteOffset, arguments.length > 1 ? arguments[1] : false);
  150. return bytes[1] << 8 | bytes[0];
  151. },
  152. getInt32: function getInt32(byteOffset /* , littleEndian */) {
  153. return unpackInt32(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : false));
  154. },
  155. getUint32: function getUint32(byteOffset /* , littleEndian */) {
  156. return unpackInt32(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : false)) >>> 0;
  157. },
  158. getFloat32: function getFloat32(byteOffset /* , littleEndian */) {
  159. return unpackIEEE754(get(this, 4, byteOffset, arguments.length > 1 ? arguments[1] : false), 23);
  160. },
  161. getFloat64: function getFloat64(byteOffset /* , littleEndian */) {
  162. return unpackIEEE754(get(this, 8, byteOffset, arguments.length > 1 ? arguments[1] : false), 52);
  163. },
  164. setInt8: function setInt8(byteOffset, value) {
  165. set(this, 1, byteOffset, packInt8, value);
  166. },
  167. setUint8: function setUint8(byteOffset, value) {
  168. set(this, 1, byteOffset, packInt8, value);
  169. },
  170. setInt16: function setInt16(byteOffset, value /* , littleEndian */) {
  171. set(this, 2, byteOffset, packInt16, value, arguments.length > 2 ? arguments[2] : false);
  172. },
  173. setUint16: function setUint16(byteOffset, value /* , littleEndian */) {
  174. set(this, 2, byteOffset, packInt16, value, arguments.length > 2 ? arguments[2] : false);
  175. },
  176. setInt32: function setInt32(byteOffset, value /* , littleEndian */) {
  177. set(this, 4, byteOffset, packInt32, value, arguments.length > 2 ? arguments[2] : false);
  178. },
  179. setUint32: function setUint32(byteOffset, value /* , littleEndian */) {
  180. set(this, 4, byteOffset, packInt32, value, arguments.length > 2 ? arguments[2] : false);
  181. },
  182. setFloat32: function setFloat32(byteOffset, value /* , littleEndian */) {
  183. set(this, 4, byteOffset, packFloat32, value, arguments.length > 2 ? arguments[2] : false);
  184. },
  185. setFloat64: function setFloat64(byteOffset, value /* , littleEndian */) {
  186. set(this, 8, byteOffset, packFloat64, value, arguments.length > 2 ? arguments[2] : false);
  187. }
  188. });
  189. } else {
  190. var INCORRECT_ARRAY_BUFFER_NAME = PROPER_FUNCTION_NAME && NativeArrayBuffer.name !== ARRAY_BUFFER;
  191. /* eslint-disable no-new, sonar/inconsistent-function-call -- required for testing */
  192. if (!fails(function () {
  193. NativeArrayBuffer(1);
  194. }) || !fails(function () {
  195. new NativeArrayBuffer(-1);
  196. }) || fails(function () {
  197. new NativeArrayBuffer();
  198. new NativeArrayBuffer(1.5);
  199. new NativeArrayBuffer(NaN);
  200. return NativeArrayBuffer.length !== 1 || INCORRECT_ARRAY_BUFFER_NAME && !CONFIGURABLE_FUNCTION_NAME;
  201. })) {
  202. /* eslint-enable no-new, sonar/inconsistent-function-call -- required for testing */
  203. $ArrayBuffer = function ArrayBuffer(length) {
  204. anInstance(this, ArrayBufferPrototype);
  205. return inheritIfRequired(new NativeArrayBuffer(toIndex(length)), this, $ArrayBuffer);
  206. };
  207. $ArrayBuffer[PROTOTYPE] = ArrayBufferPrototype;
  208. ArrayBufferPrototype.constructor = $ArrayBuffer;
  209. copyConstructorProperties($ArrayBuffer, NativeArrayBuffer);
  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. };