RawBytesToNumeric.js 2.6 KB

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