SetValueInBuffer.js 3.9 KB

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