SetValueInBuffer.js 3.6 KB

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