source: imaps-frontend/node_modules/es-abstract/2018/SetValueInBuffer.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: 3.8 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = require('es-errors/type');
6var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
7
8var isInteger = require('../helpers/isInteger');
9
10var IsDetachedBuffer = require('./IsDetachedBuffer');
11var ToInt16 = require('./ToInt16');
12var ToInt32 = require('./ToInt32');
13var ToInt8 = require('./ToInt8');
14var ToUint16 = require('./ToUint16');
15var ToUint32 = require('./ToUint32');
16var ToUint8 = require('./ToUint8');
17var ToUint8Clamp = require('./ToUint8Clamp');
18
19var isArrayBuffer = require('is-array-buffer');
20var hasOwn = require('hasown');
21
22var tableTAO = require('./tables/typed-array-objects');
23
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};
34
35var defaultEndianness = require('../helpers/defaultEndianness');
36var forEach = require('../helpers/forEach');
37var integerToNBytes = require('../helpers/integerToNBytes');
38var valueToFloat32Bytes = require('../helpers/valueToFloat32Bytes');
39var valueToFloat64Bytes = require('../helpers/valueToFloat64Bytes');
40
41// https://262.ecma-international.org/6.0/#sec-setvalueinbuffer
42
43module.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};
Note: See TracBrowser for help on using the repository browser.