source: imaps-frontend/node_modules/es-abstract/2022/SetValueInBuffer.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.1 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $SyntaxError = require('es-errors/syntax');
6var $TypeError = require('es-errors/type');
7var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
8
9var isInteger = require('../helpers/isInteger');
10
11var IsBigIntElementType = require('./IsBigIntElementType');
12var IsDetachedBuffer = require('./IsDetachedBuffer');
13var NumericToRawBytes = require('./NumericToRawBytes');
14
15var isArrayBuffer = require('is-array-buffer');
16var isSharedArrayBuffer = require('is-shared-array-buffer');
17var hasOwn = require('hasown');
18
19var tableTAO = require('./tables/typed-array-objects');
20
21var defaultEndianness = require('../helpers/defaultEndianness');
22var forEach = require('../helpers/forEach');
23
24// https://262.ecma-international.org/12.0/#sec-setvalueinbuffer
25
26/* eslint max-params: 0 */
27
28module.exports = function SetValueInBuffer(arrayBuffer, byteIndex, type, value, isTypedArray, order) {
29 var isSAB = isSharedArrayBuffer(arrayBuffer);
30 if (!isArrayBuffer(arrayBuffer) && !isSAB) {
31 throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer');
32 }
33
34 if (!isInteger(byteIndex) || byteIndex < 0) {
35 throw new $TypeError('Assertion failed: `byteIndex` must be a non-negative integer');
36 }
37
38 if (typeof type !== 'string' || !hasOwn(tableTAO.size, '$' + type)) {
39 throw new $TypeError('Assertion failed: `type` must be a Typed Array Element Type');
40 }
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
46 if (typeof isTypedArray !== 'boolean') {
47 throw new $TypeError('Assertion failed: `isTypedArray` must be a boolean');
48 }
49 if (order !== 'SeqCst' && order !== 'Unordered' && order !== 'Init') {
50 throw new $TypeError('Assertion failed: `order` must be `"SeqCst"`, `"Unordered"`, or `"Init"`');
51 }
52
53 if (arguments.length > 6 && typeof arguments[6] !== 'boolean') {
54 throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
55 }
56
57 if (IsDetachedBuffer(arrayBuffer)) {
58 throw new $TypeError('Assertion failed: ArrayBuffer is detached'); // step 1
59 }
60
61 // 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
62
63 if (IsBigIntElementType(type) ? typeof value !== 'bigint' : typeof value !== 'number') { // step 3
64 throw new $TypeError('Assertion failed: `value` must be a BigInt if type is BigInt64 or BigUint64, otherwise a Number');
65 }
66
67 // 4. Let block be arrayBuffer’s [[ArrayBufferData]] internal slot.
68
69 var elementSize = tableTAO.size['$' + type]; // step 5
70
71 // 6. 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.
72 var isLittleEndian = arguments.length > 6 ? arguments[6] : defaultEndianness === 'little'; // step 6
73
74 var rawBytes = NumericToRawBytes(type, value, isLittleEndian); // step 7
75
76 if (isSAB) { // step 8
77 /*
78 Let execution be the [[CandidateExecution]] field of the surrounding agent's Agent Record.
79 Let eventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is AgentSignifier().
80 If isTypedArray is true and IsNoTearConfiguration(type, order) is true, let noTear be true; otherwise let noTear be false.
81 Append WriteSharedMemory { [[Order]]: order, [[NoTear]]: noTear, [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize, [[Payload]]: rawBytes } to eventList.
82 */
83 throw new $SyntaxError('SharedArrayBuffer is not supported by this implementation');
84 } else {
85 // 9. Store the individual bytes of rawBytes into block, in order, starting at block[byteIndex].
86 var arr = new $Uint8Array(arrayBuffer, byteIndex, elementSize);
87 forEach(rawBytes, function (rawByte, i) {
88 arr[i] = rawByte;
89 });
90 }
91
92 // 10. Return NormalCompletion(undefined).
93};
Note: See TracBrowser for help on using the repository browser.