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