[whatwg/webidl] Fix `BufferSource` algorithms for shared and resizable buffers (PR #1529)

Previously, many `BufferSource` algorithms were using internal slots such as `[[ByteOffset]]` and `[[ByteLength]]` directly. However, with the addition of `SharedArrayBuffer` (#353, #1311) and `[AllowResizable]` (#982), this comes with extra caveats:
* To get the byte length of a `SharedArrayBuffer`, we need to use [`ArrayBufferByteLength`](https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybufferbytelength) to match [`SharedArrayBuffer.prototype.byteLength`](https://tc39.es/ecma262/multipage/structured-data.html#sec-get-sharedarraybuffer.prototype.bytelength).
* To get the byte length of a ["length-tracking" typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#behavior_when_viewing_a_resizable_buffer), we need to use [`TypedArrayByteLength`](https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-typedarraybytelength). Similarly, for a length-tracking `DataView`, we need to use [`GetViewByteLength`](https://tc39.es/ecma262/multipage/structured-data.html#sec-getviewbytelength).
* To get the byte offset of a typed array or `DataView`, we first need to check if the view is still inbounds using [`IsArrayBufferViewOutOfBounds`](https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-isarraybufferviewoutofbounds).

Transferring an `ArrayBuffer` must also take into account whether it should remain resizable or not. Fortunately, we can use the new [`ArrayBufferCopyAndDetach`](https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybuffercopyanddetach) operation for that.
* I added an optional `preserveResizability` parameter to "transfer an `ArrayBuffer`", so specifications can choose whether to preserve resizability or not. By default, this is not preserved. (Most specs aren't using `[AllowResizable]` yet anyway.)
* I removed the `realm` parameter of that algorithm, since `ArrayBufferCopyAndDetach` doesn't accept such a parameter. [According to WebDex](https://dontcallmedom.github.io/webdex/t.html#transfer%40%40ArrayBuffer%40dfn), no specs were actually using that parameter anyway.

Open questions:
* [ ] When dealing with views backed by a `SharedArrayBuffer`, what memory ordering should we use? ECMAScript seems to use `SEQ-CST` pretty much everywhere (e.g. [`%TypedArray%.prototype.byteLength`](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-get-%typedarray%.prototype.bytelength)), but ["get a copy of the bytes"](https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy) uses `UNORDERED` for `GetValueFromBuffer`.

Fixes #1312.
Fixes #1385.

- [ ] At least two implementers are interested (and none opposed):
   * …
   * …
- [ ] [Tests](https://github.com/web-platform-tests/wpt) are written and can be reviewed and commented upon at:
   * … <!-- If these tests are tentative, link a PR to make them non-tentative. -->
- [ ] [Implementation bugs](https://github.com/whatwg/meta/blob/main/MAINTAINERS.md#handling-pull-requests) are filed:
   * Chromium: …
   * Gecko: …
   * WebKit: …
   * Deno: …
   * Node.js: …
   * webidl2.js: …
   * widlparser: …
- [ ] [MDN issue](https://github.com/whatwg/meta/blob/main/MAINTAINERS.md#handling-pull-requests) is filed: …
- [ ] The top of this comment includes a [clear commit message](https://github.com/whatwg/meta/blob/main/COMMITTING.md) to use. <!-- If you created this PR from a single commit, Github copied its message. Otherwise, you need to add a commit message yourself. -->

(See [WHATWG Working Mode: Changes](https://whatwg.org/working-mode#changes) for more details.)


<!--
    This comment and the below content is programmatically generated.
    You may add a comma-separated list of anchors you'd like a
    direct link to below (e.g. #idl-serializers, #idl-sequence):

    Don't remove this comment or modify anything below this line.
    If you don't want a preview generated for this pull request,
    just replace the whole of this comment's content by "no preview"
    and remove what's below.
-->
***
<a href="https://whatpr.org/webidl/1529.html" title="Last updated on Oct 8, 2025, 9:35 PM UTC (4a52a2f)">Preview</a> | <a href="https://whatpr.org/webidl/1529/a79e69e...4a52a2f.html" title="Last updated on Oct 8, 2025, 9:35 PM UTC (4a52a2f)">Diff</a>
You can view, comment on, or merge this pull request online at:

  https://github.com/whatwg/webidl/pull/1529

-- Commit Summary --

  * Fix buffer source's byte length
  * Add byte offset to ArrayBufferView
  * Fix writing bytes into an ArrayBufferView backed by a SharedArrayBuffer
  * Simplify writing into an ArrayBufferView
  * Move assert before conversion
  * Fix detached check
  * Use ArrayBufferCopyAndDetach to transfer an ArrayBuffer
  * Rework "get a copy of the bytes held"
  * Use IsArrayBufferViewOutOfBounds instead
  * Use SEQ-CST ordering to match ECMAScript

-- File Changes --

    M index.bs (85)

-- Patch Links --

https://github.com/whatwg/webidl/pull/1529.patch
https://github.com/whatwg/webidl/pull/1529.diff

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/webidl/pull/1529
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/webidl/pull/1529@github.com>

Received on Wednesday, 8 October 2025 21:35:29 UTC