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/15.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/15.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. };