| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 |
|
|---|
| 5 | var hasOwnProperty = require('./HasOwnProperty');
|
|---|
| 6 | var ToBigInt64 = require('./ToBigInt64');
|
|---|
| 7 | var ToBigUint64 = require('./ToBigUint64');
|
|---|
| 8 | var ToInt16 = require('./ToInt16');
|
|---|
| 9 | var ToInt32 = require('./ToInt32');
|
|---|
| 10 | var ToInt8 = require('./ToInt8');
|
|---|
| 11 | var ToUint16 = require('./ToUint16');
|
|---|
| 12 | var ToUint32 = require('./ToUint32');
|
|---|
| 13 | var ToUint8 = require('./ToUint8');
|
|---|
| 14 | var ToUint8Clamp = require('./ToUint8Clamp');
|
|---|
| 15 |
|
|---|
| 16 | var valueToFloat32Bytes = require('../helpers/valueToFloat32Bytes');
|
|---|
| 17 | var valueToFloat64Bytes = require('../helpers/valueToFloat64Bytes');
|
|---|
| 18 | var integerToNBytes = require('../helpers/integerToNBytes');
|
|---|
| 19 |
|
|---|
| 20 | var tableTAO = require('./tables/typed-array-objects');
|
|---|
| 21 |
|
|---|
| 22 | // https://262.ecma-international.org/11.0/#table-the-typedarray-constructors
|
|---|
| 23 | var TypeToAO = {
|
|---|
| 24 | __proto__: null,
|
|---|
| 25 | $Int8: ToInt8,
|
|---|
| 26 | $Uint8: ToUint8,
|
|---|
| 27 | $Uint8C: ToUint8Clamp,
|
|---|
| 28 | $Int16: ToInt16,
|
|---|
| 29 | $Uint16: ToUint16,
|
|---|
| 30 | $Int32: ToInt32,
|
|---|
| 31 | $Uint32: ToUint32,
|
|---|
| 32 | $BigInt64: ToBigInt64,
|
|---|
| 33 | $BigUint64: ToBigUint64
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | // https://262.ecma-international.org/11.0/#sec-numerictorawbytes
|
|---|
| 37 |
|
|---|
| 38 | module.exports = function NumericToRawBytes(type, value, isLittleEndian) {
|
|---|
| 39 | if (typeof type !== 'string' || !hasOwnProperty(tableTAO.size, '$' + type)) {
|
|---|
| 40 | throw new $TypeError('Assertion failed: `type` must be a TypedArray element type');
|
|---|
| 41 | }
|
|---|
| 42 | if (typeof value !== 'number' && typeof value !== 'bigint') {
|
|---|
| 43 | throw new $TypeError('Assertion failed: `value` must be a Number or a BigInt');
|
|---|
| 44 | }
|
|---|
| 45 | if (typeof isLittleEndian !== 'boolean') {
|
|---|
| 46 | throw new $TypeError('Assertion failed: `isLittleEndian` must be a Boolean');
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (type === 'Float32') { // step 1
|
|---|
| 50 | return valueToFloat32Bytes(value, isLittleEndian);
|
|---|
| 51 | } else if (type === 'Float64') { // step 2
|
|---|
| 52 | return valueToFloat64Bytes(value, isLittleEndian);
|
|---|
| 53 | } // step 3
|
|---|
| 54 |
|
|---|
| 55 | var n = tableTAO.size['$' + type]; // step 3.a
|
|---|
| 56 |
|
|---|
| 57 | var convOp = TypeToAO['$' + type]; // step 3.b
|
|---|
| 58 |
|
|---|
| 59 | var intValue = convOp(value); // step 3.c
|
|---|
| 60 |
|
|---|
| 61 | return integerToNBytes(intValue, n, isLittleEndian); // step 3.d, 3.e, 4
|
|---|
| 62 | };
|
|---|