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