// |reftest| shell-option(--enable-arraybuffer-immutable) skip-if(!this.hasOwnProperty('Atomics')||!ArrayBuffer.prototype.sliceToImmutable||!xulRuntime.shell) -- Atomics,immutable-arraybuffer is not enabled unconditionally, requires shell-options
// Copyright (C) 2025 Richard Gibson. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-atomics.add
description: >
  Throws a TypeError exception when the backing buffer is immutable
info: |
  Atomics.add ( typedArray, index, value )
  1. Let add be a new read-modify-write modification function...
  2. Return ? AtomicReadModifyWrite(typedArray, index, value, add).

  AtomicReadModifyWrite ( typedArray, index, value, op )
  1. Let byteIndexInBuffer be ? ValidateAtomicAccessOnIntegerTypedArray(typedArray, index, false, ~write~).

  ValidateAtomicAccessOnIntegerTypedArray ( typedArray, requestIndex [ , waitable [ , accessMode ] ] )
  1. If waitable is not present, set waitable to false.
  2. If accessMode is not present, set accessMode to ~read~.
  3. Let taRecord be ? ValidateIntegerTypedArray(typedArray, waitable, accessMode).

  ValidateIntegerTypedArray ( typedArray, waitable [ , accessMode ] )
  1. If accessMode is not present, set accessMode to ~read~.
  2. Let taRecord be ? ValidateTypedArray(typedArray, unordered, accessMode).

  ValidateTypedArray ( O, order [ , accessMode ] )
  ...
  4. If accessMode is ~write~ and IsImmutableBuffer(O.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
features: [Atomics, immutable-arraybuffer]
includes: [compareArray.js, testTypedArray.js]
---*/

testWithAllTypedArrayConstructors(function(TA, makeCtorArg) {
  var calls = [];
  var index = {
    valueOf() {
      calls.push("index.valueOf");
      return 0;
    }
  };
  var value = {
    valueOf() {
      calls.push("value.valueOf");
      return 1;
    }
  };

  var ta = new TA(makeCtorArg(8));
  assert.throws(TypeError, function() {
    Atomics.add(ta, index, value);
  });
  assert.compareArray(calls, []);
}, null, ["immutable"]);

reportCompare(0, 0);
