Permalink
codeql-action/node_modules/es-abstract/2018/SetValueInBuffer.js
Newer
100644
120 lines (92 sloc)
3.79 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
5
var $TypeError = GetIntrinsic('%TypeError%');
6
7
var isInteger = require('../helpers/isInteger');
8
9
var IsDetachedBuffer = require('./IsDetachedBuffer');
10
var ToInt16 = require('./ToInt16');
11
var ToInt32 = require('./ToInt32');
12
var ToInt8 = require('./ToInt8');
13
var ToUint16 = require('./ToUint16');
14
var ToUint32 = require('./ToUint32');
15
var ToUint8 = require('./ToUint8');
16
var ToUint8Clamp = require('./ToUint8Clamp');
17
18
var isArrayBuffer = require('is-array-buffer');
19
var has = require('has');
20
21
var table49 = {
22
__proto__: null,
23
Int8: 1,
24
Uint8: 1,
25
Uint8C: 1,
26
Int16: 2,
27
Uint16: 2,
28
Int32: 4,
29
Uint32: 4,
30
Float32: 4,
31
Float64: 8
32
};
33
34
var TypeToAO = {
35
__proto__: null,
36
Int8: ToInt8,
37
Uint8: ToUint8,
38
Uint8C: ToUint8Clamp,
39
Int16: ToInt16,
40
Uint16: ToUint16,
41
Int32: ToInt32,
42
Uint32: ToUint32
43
};
44
45
var defaultEndianness = require('../helpers/defaultEndianness');
46
var forEach = require('../helpers/forEach');
47
var integerToNBytes = require('../helpers/integerToNBytes');
48
var valueToFloat32Bytes = require('../helpers/valueToFloat32Bytes');
49
var valueToFloat64Bytes = require('../helpers/valueToFloat64Bytes');
50
51
// https://262.ecma-international.org/6.0/#sec-setvalueinbuffer
52
53
module.exports = function SetValueInBuffer(arrayBuffer, byteIndex, type, value) {
54
if (!isArrayBuffer(arrayBuffer)) {
55
throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer');
56
}
57
58
if (!isInteger(byteIndex)) {
59
throw new $TypeError('Assertion failed: `byteIndex` must be an integer');
60
}
61
62
if (typeof type !== 'string' || !has(table49, type)) {
63
throw new $TypeError('Assertion failed: `type` must be a Typed Array Element Type');
64
}
65
66
if (typeof value !== 'number') {
67
throw new $TypeError('Assertion failed: `value` must be a number');
68
}
69
70
if (arguments.length > 4 && typeof arguments[4] !== 'boolean') {
71
throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
72
}
73
74
if (IsDetachedBuffer(arrayBuffer)) {
75
throw new $TypeError('Assertion failed: ArrayBuffer is detached'); // step 1
76
}
77
78
// 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
79
80
if (byteIndex < 0) {
81
throw new $TypeError('Assertion failed: `byteIndex` must be non-negative'); // step 3
82
}
83
84
// 4. Assert: Type(value) is Number.
85
86
// 5. Let block be arrayBuffer’s [[ArrayBufferData]] internal slot.
87
88
// 6. Assert: block is not undefined.
89
90
var elementSize = table49[type]; // step 7
91
if (!elementSize) {
92
throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "Float32", or "Float64"');
93
}
94
95
// 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.
96
var isLittleEndian = arguments.length > 4 ? arguments[4] : defaultEndianness === 'little'; // step 8
97
98
var rawBytes;
99
if (type === 'Float32') { // step 1
100
rawBytes = valueToFloat32Bytes(value, isLittleEndian);
101
} else if (type === 'Float64') { // step 2
102
rawBytes = valueToFloat64Bytes(value, isLittleEndian);
103
} else {
104
var n = table49[type]; // step 3.a
105
106
var convOp = TypeToAO[type]; // step 3.b
107
108
var intValue = convOp(value); // step 3.c
109
110
rawBytes = integerToNBytes(intValue, n, isLittleEndian); // step 3.d, 3.e, 4
111
}
112
113
// 12. Store the individual bytes of rawBytes into block, in order, starting at block[byteIndex].
114
var arr = new Uint8Array(arrayBuffer, byteIndex, elementSize);
115
forEach(rawBytes, function (rawByte, i) {
116
arr[i] = rawByte;
117
});
118
119
// 13. Return NormalCompletion(undefined).
120
};