source: frontend/node_modules/es-abstract/2025/NumericToRawBytes.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[9af201e]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var hasOwnProperty = require('./HasOwnProperty');
6var ToBigInt64 = require('./ToBigInt64');
7var ToBigUint64 = require('./ToBigUint64');
8var ToInt16 = require('./ToInt16');
9var ToInt32 = require('./ToInt32');
10var ToInt8 = require('./ToInt8');
11var ToUint16 = require('./ToUint16');
12var ToUint32 = require('./ToUint32');
13var ToUint8 = require('./ToUint8');
14var ToUint8Clamp = require('./ToUint8Clamp');
15
16var valueToFloat16Bytes = require('../helpers/valueToFloat16Bytes');
17var valueToFloat32Bytes = require('../helpers/valueToFloat32Bytes');
18var valueToFloat64Bytes = require('../helpers/valueToFloat64Bytes');
19var integerToNBytes = require('../helpers/integerToNBytes');
20
21var tableTAO = require('./tables/typed-array-objects');
22
23// https://262.ecma-international.org/15.0/#table-the-typedarray-constructors
24var TypeToAO = {
25 __proto__: null,
26 $INT8: ToInt8,
27 $UINT8: ToUint8,
28 $UINT8C: ToUint8Clamp,
29 $INT16: ToInt16,
30 $UINT16: ToUint16,
31 $INT32: ToInt32,
32 $UINT32: ToUint32,
33 $BIGINT64: ToBigInt64,
34 $BIGUINT64: ToBigUint64
35};
36
37// https://262.ecma-international.org/16.0/#sec-numerictorawbytes
38
39module.exports = function NumericToRawBytes(type, value, isLittleEndian) {
40 if (typeof type !== 'string' || !hasOwnProperty(tableTAO.size, '$' + type)) {
41 throw new $TypeError('Assertion failed: `type` must be a TypedArray element type');
42 }
43 if (typeof value !== 'number' && typeof value !== 'bigint') {
44 throw new $TypeError('Assertion failed: `value` must be a Number or a BigInt');
45 }
46 if (typeof isLittleEndian !== 'boolean') {
47 throw new $TypeError('Assertion failed: `isLittleEndian` must be a Boolean');
48 }
49
50 if (type === 'FLOAT16') { // step 1
51 return valueToFloat16Bytes(value, isLittleEndian);
52 } else if (type === 'FLOAT32') { // step 2
53 return valueToFloat32Bytes(value, isLittleEndian);
54 } else if (type === 'FLOAT64') { // step 3
55 return valueToFloat64Bytes(value, isLittleEndian);
56 } // step 4
57
58 var n = tableTAO.size['$' + type]; // step 4.a
59
60 var convOp = TypeToAO['$' + type]; // step 4.b
61
62 var intValue = convOp(value); // step 4.c
63
64 return integerToNBytes(intValue, n, isLittleEndian); // step 4.d, 4.e, 5
65};
Note: See TracBrowser for help on using the repository browser.