1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $TypeError = require('es-errors/type');
|
---|
6 | var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
|
---|
7 |
|
---|
8 | var isInteger = require('../helpers/isInteger');
|
---|
9 |
|
---|
10 | var IsDetachedBuffer = require('./IsDetachedBuffer');
|
---|
11 | var ToInt16 = require('./ToInt16');
|
---|
12 | var ToInt32 = require('./ToInt32');
|
---|
13 | var ToInt8 = require('./ToInt8');
|
---|
14 | var ToUint16 = require('./ToUint16');
|
---|
15 | var ToUint32 = require('./ToUint32');
|
---|
16 | var ToUint8 = require('./ToUint8');
|
---|
17 | var ToUint8Clamp = require('./ToUint8Clamp');
|
---|
18 |
|
---|
19 | var isArrayBuffer = require('is-array-buffer');
|
---|
20 | var hasOwn = require('hasown');
|
---|
21 |
|
---|
22 | var tableTAO = require('./tables/typed-array-objects');
|
---|
23 |
|
---|
24 | var 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 | };
|
---|
34 |
|
---|
35 | var defaultEndianness = require('../helpers/defaultEndianness');
|
---|
36 | var forEach = require('../helpers/forEach');
|
---|
37 | var integerToNBytes = require('../helpers/integerToNBytes');
|
---|
38 | var valueToFloat32Bytes = require('../helpers/valueToFloat32Bytes');
|
---|
39 | var valueToFloat64Bytes = require('../helpers/valueToFloat64Bytes');
|
---|
40 |
|
---|
41 | // https://262.ecma-international.org/6.0/#sec-setvalueinbuffer
|
---|
42 |
|
---|
43 | module.exports = function SetValueInBuffer(arrayBuffer, byteIndex, type, value) {
|
---|
44 | if (!isArrayBuffer(arrayBuffer)) {
|
---|
45 | throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer');
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (!isInteger(byteIndex)) {
|
---|
49 | throw new $TypeError('Assertion failed: `byteIndex` must be an integer');
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (typeof type !== 'string' || !hasOwn(tableTAO.size, '$' + type)) {
|
---|
53 | throw new $TypeError('Assertion failed: `type` must be a Typed Array Element Type');
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (typeof value !== 'number') {
|
---|
57 | throw new $TypeError('Assertion failed: `value` must be a number');
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (arguments.length > 4 && typeof arguments[4] !== 'boolean') {
|
---|
61 | throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (IsDetachedBuffer(arrayBuffer)) {
|
---|
65 | throw new $TypeError('Assertion failed: ArrayBuffer is detached'); // step 1
|
---|
66 | }
|
---|
67 |
|
---|
68 | // 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
|
---|
69 |
|
---|
70 | if (byteIndex < 0) {
|
---|
71 | throw new $TypeError('Assertion failed: `byteIndex` must be non-negative'); // step 3
|
---|
72 | }
|
---|
73 |
|
---|
74 | // 4. Assert: Type(value) is Number.
|
---|
75 |
|
---|
76 | // 5. Let block be arrayBuffer’s [[ArrayBufferData]] internal slot.
|
---|
77 |
|
---|
78 | // 6. Assert: block is not undefined.
|
---|
79 |
|
---|
80 | var elementSize = tableTAO.size['$' + type]; // step 7
|
---|
81 | if (!elementSize) {
|
---|
82 | throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "Float32", or "Float64"');
|
---|
83 | }
|
---|
84 |
|
---|
85 | // 8. If isLittleEndian is not present, set isLittleEndian to either true or false. The choice is implementation dependent and should be the alternative that is most efficient for the implementation. An implementation must use the same value each time this step is executed and the same value must be used for the corresponding step in the GetValueFromBuffer abstract operation.
|
---|
86 | var isLittleEndian = arguments.length > 4 ? arguments[4] : defaultEndianness === 'little'; // step 8
|
---|
87 |
|
---|
88 | var rawBytes;
|
---|
89 | if (type === 'Float32') { // step 1
|
---|
90 | rawBytes = valueToFloat32Bytes(value, isLittleEndian);
|
---|
91 | } else if (type === 'Float64') { // step 2
|
---|
92 | rawBytes = valueToFloat64Bytes(value, isLittleEndian);
|
---|
93 | } else {
|
---|
94 | var n = elementSize; // step 3.a
|
---|
95 |
|
---|
96 | var convOp = TypeToAO[type]; // step 3.b
|
---|
97 |
|
---|
98 | var intValue = convOp(value); // step 3.c
|
---|
99 |
|
---|
100 | rawBytes = integerToNBytes(intValue, n, isLittleEndian); // step 3.d, 3.e, 4
|
---|
101 | }
|
---|
102 |
|
---|
103 | // 12. Store the individual bytes of rawBytes into block, in order, starting at block[byteIndex].
|
---|
104 | var arr = new $Uint8Array(arrayBuffer, byteIndex, elementSize);
|
---|
105 | forEach(rawBytes, function (rawByte, i) {
|
---|
106 | arr[i] = rawByte;
|
---|
107 | });
|
---|
108 |
|
---|
109 | // 13. Return NormalCompletion(undefined).
|
---|
110 | };
|
---|