RawBytesToNumeric.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $RangeError = require('es-errors/range');
  5. var $SyntaxError = require('es-errors/syntax');
  6. var $TypeError = require('es-errors/type');
  7. var $BigInt = GetIntrinsic('%BigInt%', true);
  8. var hasOwnProperty = require('./HasOwnProperty');
  9. var IsArray = require('./IsArray');
  10. var IsBigIntElementType = require('./IsBigIntElementType');
  11. var IsUnsignedElementType = require('./IsUnsignedElementType');
  12. var bytesAsFloat32 = require('../helpers/bytesAsFloat32');
  13. var bytesAsFloat64 = require('../helpers/bytesAsFloat64');
  14. var bytesAsInteger = require('../helpers/bytesAsInteger');
  15. var every = require('../helpers/every');
  16. var isByteValue = require('../helpers/isByteValue');
  17. var $reverse = callBound('Array.prototype.reverse');
  18. var $slice = callBound('Array.prototype.slice');
  19. var keys = require('object-keys');
  20. // https://262.ecma-international.org/11.0/#table-the-typedarray-constructors
  21. var TypeToSizes = {
  22. __proto__: null,
  23. Int8: 1,
  24. Uint8: 1,
  25. Uint8C: 1,
  26. Int16: 2,
  27. Uint16: 2,
  28. Int32: 4,
  29. Uint32: 4,
  30. BigInt64: 8,
  31. BigUint64: 8,
  32. Float32: 4,
  33. Float64: 8
  34. };
  35. // https://262.ecma-international.org/11.0/#sec-rawbytestonumeric
  36. module.exports = function RawBytesToNumeric(type, rawBytes, isLittleEndian) {
  37. if (!hasOwnProperty(TypeToSizes, type)) {
  38. throw new $TypeError('Assertion failed: `type` must be a TypedArray element type: ' + keys(TypeToSizes));
  39. }
  40. if (!IsArray(rawBytes) || !every(rawBytes, isByteValue)) {
  41. throw new $TypeError('Assertion failed: `rawBytes` must be an Array of bytes');
  42. }
  43. if (typeof isLittleEndian !== 'boolean') {
  44. throw new $TypeError('Assertion failed: `isLittleEndian` must be a Boolean');
  45. }
  46. var elementSize = TypeToSizes[type]; // step 1
  47. if (rawBytes.length !== elementSize) {
  48. // this assertion is not in the spec, but it'd be an editorial error if it were ever violated
  49. throw new $RangeError('Assertion failed: `rawBytes` must have a length of ' + elementSize + ' for type ' + type);
  50. }
  51. var isBigInt = IsBigIntElementType(type);
  52. if (isBigInt && !$BigInt) {
  53. throw new $SyntaxError('this environment does not support BigInts');
  54. }
  55. // eslint-disable-next-line no-param-reassign
  56. rawBytes = $slice(rawBytes, 0, elementSize);
  57. if (!isLittleEndian) {
  58. $reverse(rawBytes); // step 2
  59. }
  60. if (type === 'Float32') { // step 3
  61. return bytesAsFloat32(rawBytes);
  62. }
  63. if (type === 'Float64') { // step 4
  64. return bytesAsFloat64(rawBytes);
  65. }
  66. return bytesAsInteger(rawBytes, elementSize, IsUnsignedElementType(type), isBigInt);
  67. };