source: imaps-frontend/node_modules/es-abstract/2021/RawBytesToNumeric.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4var callBound = require('call-bind/callBound');
5
6var $RangeError = require('es-errors/range');
7var $SyntaxError = require('es-errors/syntax');
8var $TypeError = require('es-errors/type');
9var $BigInt = GetIntrinsic('%BigInt%', true);
10
11var hasOwnProperty = require('./HasOwnProperty');
12var IsArray = require('./IsArray');
13var IsBigIntElementType = require('./IsBigIntElementType');
14var IsUnsignedElementType = require('./IsUnsignedElementType');
15
16var bytesAsFloat32 = require('../helpers/bytesAsFloat32');
17var bytesAsFloat64 = require('../helpers/bytesAsFloat64');
18var bytesAsInteger = require('../helpers/bytesAsInteger');
19var every = require('../helpers/every');
20var isByteValue = require('../helpers/isByteValue');
21
22var $reverse = callBound('Array.prototype.reverse');
23var $slice = callBound('Array.prototype.slice');
24
25var keys = require('object-keys');
26
27// https://262.ecma-international.org/11.0/#table-the-typedarray-constructors
28var 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
45module.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};
Note: See TracBrowser for help on using the repository browser.