From e4e9948e7f7f77f2b51fb62d2e5a97d6c99c915d Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:00:13 +0200 Subject: [PATCH 01/10] Fix buffer source's byte length --- index.bs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/index.bs b/index.bs index fad8407f..67bb471e 100644 --- a/index.bs +++ b/index.bs @@ -9322,9 +9322,24 @@ a reference to the same object that the IDL value represents. 1. Let |jsBufferSource| be the result of [=converted to a JavaScript value|converting=] |bufferSource| to a JavaScript value. - 1. If |jsBufferSource| has a \[[ViewedArrayBuffer]] internal slot, then return - |jsBufferSource|.\[[ByteLength]]. - 1. Return |jsBufferSource|.\[[ArrayBufferByteLength]]. + 1. If |jsBufferSource| has a \[[ViewedArrayBuffer]] [=/internal slot=], then: + 1. If |jsBufferSource| has a \[[TypedArrayName]] [=/internal slot=]: + 1. Assert: |jsBufferSource| is a [=typed array type=] instance. + 1. Let |taRecord| be [$MakeTypedArrayWithBufferWitnessRecord$](|jsBufferSource|, + Unordered). + 1. Return [$TypedArrayByteLength$](|taRecord|). + 1. Otherwise: + 1. Assert: |jsBufferSource| is a {{DataView}}. + 1. Let |viewRecord| be [$MakeDataViewWithBufferWitnessRecord$](|jsBufferSource|, + Unordered). + 1. If [$IsViewOutOfBounds$](|viewRecord|) is true, return 0. + 1. Return [$GetViewByteLength$](|viewRecord|). + 1. Otherwise, if [$IsSharedArrayBuffer$](|jsBufferSource|) is true, then: + 1. Assert: |jsBufferSource| is a {{SharedArrayBuffer}}. + 1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Unordered). + 1. Otherwise: + 1. Assert: |jsBufferSource| is an {{ArrayBuffer}}. + 1. Return |jsBufferSource|.\[[ArrayBufferByteLength]]. From 447a26c6e3330e447509911fd70f4336d1d9f556 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:13:20 +0200 Subject: [PATCH 02/10] Add byte offset to ArrayBufferView --- index.bs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/index.bs b/index.bs index 67bb471e..ac9940a3 100644 --- a/index.bs +++ b/index.bs @@ -9298,15 +9298,13 @@ a reference to the same object that the IDL value represents. |bufferSource| to a JavaScript value. 1. Let |jsArrayBuffer| be |jsBufferSource|. 1. Let |offset| be 0. - 1. Let |length| be 0. 1. If |jsBufferSource| has a \[[ViewedArrayBuffer]] [=/internal slot=], then: 1. Set |jsArrayBuffer| to |jsBufferSource|.\[[ViewedArrayBuffer]]. - 1. Set |offset| to |jsBufferSource|.\[[ByteOffset]]. - 1. Set |length| to |jsBufferSource|.\[[ByteLength]]. + 1. Set |offset| to |jsBufferSource|'s [=ArrayBufferView/byte offset=]. 1. Otherwise: 1. Assert: |jsBufferSource| is an {{ArrayBuffer}} or {{SharedArrayBuffer}} object. - 1. Set |length| to |jsBufferSource|.\[[ArrayBufferByteLength]]. + 1. Let |length| be |jsBufferSource|'s [=BufferSource/byte length=]. 1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is true, then return the empty [=byte sequence=]. 1. Let |bytes| be a new [=byte sequence=] of [=byte sequence/length=] equal to |length|. @@ -9316,6 +9314,23 @@ a reference to the same object that the IDL value represents. 1. Return |bytes|. +
+ The byte offset of an {{ArrayBufferView}} + |view| is the value returned by the following steps: + + 1. Let |jsView| be the result of [=converted to a JavaScript value|converting=] |view| to + a JavaScript value. + 1. If |jsView| has a \[[TypedArrayName]] [=/internal slot=]: + 1. Assert: |jsView| is a [=typed array type=] instance. + 1. Let |taRecord| be [$MakeTypedArrayWithBufferWitnessRecord$](|jsView|, Unordered). + 1. If [$IsTypedArrayOutOfBounds$](|taRecord|) is true, throw a {{TypeError}} exception. + 1. Otherwise: + 1. Assert: |jsView| is a {{DataView}}. + 1. Let |viewRecord| be [$MakeDataViewWithBufferWitnessRecord$](|jsView|, Unordered). + 1. If [$IsViewOutOfBounds$](|viewRecord|) is true, throw a {{TypeError}} exception. + 1. Return |jsView|.\[[ByteOffset]]. +
+
The byte length of a [=buffer source type=] instance |bufferSource| is the value returned by the following steps: From b8d7401c52826484838625fbabf93232ed583364 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:20:24 +0200 Subject: [PATCH 03/10] Fix writing bytes into an ArrayBufferView backed by a SharedArrayBuffer --- index.bs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.bs b/index.bs index ac9940a3..04f422ab 100644 --- a/index.bs +++ b/index.bs @@ -9398,8 +9398,7 @@ a reference to the same object that the IDL value represents. |startingOffset|. 1. Assert: if |view| is not a {{DataView}}, then |bytes|'s [=byte sequence/length=] [=modulo=] the [=element size=] of |view|'s type is 0. - 1. Let |arrayBuffer| be the result of [=converted to an IDL value|converting=] - |jsView|.\[[ViewedArrayBuffer]] to an IDL value of type {{ArrayBuffer}}. + 1. Let |arrayBuffer| be |view|'s [=underlying buffer=]. 1. [=ArrayBuffer/Write=] |bytes| into |arrayBuffer| with [=ArrayBuffer/write/startingOffset=] set to |jsView|.\[[ByteOffset]] + |startingOffset|. From 037e6ff0f282d683b984f783eee10651a6ad84c0 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:26:43 +0200 Subject: [PATCH 04/10] Simplify writing into an ArrayBufferView --- index.bs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/index.bs b/index.bs index 04f422ab..1c03b9cd 100644 --- a/index.bs +++ b/index.bs @@ -9380,7 +9380,7 @@ a reference to the same object that the IDL value represents. 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] |arrayBuffer| to a JavaScript value. - 1. Assert: |bytes|'s [=byte sequence/length=] ≤ |jsArrayBuffer|.\[[ArrayBufferByteLength]] + 1. Assert: |bytes|'s [=byte sequence/length=] ≤ |arrayBuffer|'s [=byte length=] − |startingOffset|. 1. For |i| in [=the range=] |startingOffset| to |startingOffset| + |bytes|'s [=byte sequence/length=] − 1, inclusive, perform [$SetValueInBuffer$](|jsArrayBuffer|, @@ -9392,15 +9392,12 @@ a reference to the same object that the IDL value represents. {{ArrayBufferView}} |view|, optionally given a |startingOffset| (default 0): - 1. Let |jsView| be the result of [=converted to a JavaScript value|converting=] |view| to - a JavaScript value. - 1. Assert: |bytes|'s [=byte sequence/length=] ≤ |jsView|.\[[ByteLength]] − + 1. Assert: |bytes|'s [=byte sequence/length=] ≤ |view|'s [=byte length=] − |startingOffset|. 1. Assert: if |view| is not a {{DataView}}, then |bytes|'s [=byte sequence/length=] [=modulo=] the [=element size=] of |view|'s type is 0. - 1. Let |arrayBuffer| be |view|'s [=underlying buffer=]. - 1. [=ArrayBuffer/Write=] |bytes| into |arrayBuffer| with - [=ArrayBuffer/write/startingOffset=] set to |jsView|.\[[ByteOffset]] + + 1. [=ArrayBuffer/Write=] |bytes| into |view|'s [=underlying buffer=] with + [=ArrayBuffer/write/startingOffset=] set to |view|'s [=ArrayBufferView/byte offset=] + |startingOffset|.
From ba8a3fa41cd232d29d462e8e86714487e2b59e86 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:28:04 +0200 Subject: [PATCH 05/10] Move assert before conversion --- index.bs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.bs b/index.bs index 1c03b9cd..d71c20ba 100644 --- a/index.bs +++ b/index.bs @@ -9378,10 +9378,10 @@ a reference to the same object that the IDL value represents. |startingOffset| (default 0): - 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] - |arrayBuffer| to a JavaScript value. 1. Assert: |bytes|'s [=byte sequence/length=] ≤ |arrayBuffer|'s [=byte length=] − |startingOffset|. + 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] + |arrayBuffer| to a JavaScript value. 1. For |i| in [=the range=] |startingOffset| to |startingOffset| + |bytes|'s [=byte sequence/length=] − 1, inclusive, perform [$SetValueInBuffer$](|jsArrayBuffer|, |i|, Uint8, |bytes|[|i| - |startingOffset|], true, Unordered). From 9866f0932f2c43cb7d9ad510903d14a20854aec9 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:32:17 +0200 Subject: [PATCH 06/10] Fix detached check --- index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.bs b/index.bs index d71c20ba..0d12aefa 100644 --- a/index.bs +++ b/index.bs @@ -9461,7 +9461,7 @@ a reference to the same object that the IDL value represents. 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] |arrayBuffer| to a JavaScript value. - 1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is false, then [=JavaScript/throw=] a + 1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is true, then [=JavaScript/throw=] a {{TypeError}}. 1. Let |arrayBufferData| be |jsArrayBuffer|.\[[ArrayBufferData]]. 1. Let |arrayBufferByteLength| be |jsArrayBuffer|.\[[ArrayBufferByteLength]]. From 3140a47be54fe2195683e0e3a1e22b53f7b01a3f Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:49:20 +0200 Subject: [PATCH 07/10] Use ArrayBufferCopyAndDetach to transfer an ArrayBuffer --- index.bs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/index.bs b/index.bs index 0d12aefa..730c03f2 100644 --- a/index.bs +++ b/index.bs @@ -9455,22 +9455,20 @@ a reference to the same object that the IDL value represents. 1. Return true. -
+
To transfer an {{ArrayBuffer}} |arrayBuffer|, optionally - given a [=realm=] |targetRealm|: + given |preserveResizability| (default false): 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] |arrayBuffer| to a JavaScript value. 1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is true, then [=JavaScript/throw=] a {{TypeError}}. - 1. Let |arrayBufferData| be |jsArrayBuffer|.\[[ArrayBufferData]]. - 1. Let |arrayBufferByteLength| be |jsArrayBuffer|.\[[ArrayBufferByteLength]]. - 1. Perform [=?=] [$DetachArrayBuffer$](|jsArrayBuffer|). - 1. If |targetRealm| is not given, let |targetRealm| be the [=current realm=]. - 1. Let |jsTransferred| be [=?=] - [$AllocateArrayBuffer$](|targetRealm|.\[[Intrinsics]].\[[{{%ArrayBuffer%}}]], 0). - 1. Set |jsTransferred|.\[[ArrayBufferData]] to |arrayBufferData|. - 1. Set |jsTransferred|.\[[ArrayBufferByteLength]] to |arrayBufferByteLength|. + 1. If |preserveResizability| is true, then: + 1. Let |jsTransferred| be [=?=] [$ArrayBufferCopyAndDetach$](|jsArrayBuffer|, undefined, + Preserve-Resizability). + 1. Otherwise, + 1. Let |jsTransferred| be [=?=] [$ArrayBufferCopyAndDetach$](|jsArrayBuffer|, undefined, + Fixed-Length). 1. Return the result of [=converted to an IDL value|converting=] |jsTransferred| to an IDL value of type {{ArrayBuffer}}. From fabdef2608e90096b5cdc43f4a653c7a1645b824 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 22:59:29 +0200 Subject: [PATCH 08/10] Rework "get a copy of the bytes held" --- index.bs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/index.bs b/index.bs index 730c03f2..21ca9194 100644 --- a/index.bs +++ b/index.bs @@ -9294,17 +9294,15 @@ a reference to the same object that the IDL value represents. To get a copy of the bytes held by the buffer source given a [=buffer source type=] instance |bufferSource|: - 1. Let |jsBufferSource| be the result of [=converted to a JavaScript value|converting=] - |bufferSource| to a JavaScript value. - 1. Let |jsArrayBuffer| be |jsBufferSource|. + 1. Let |arrayBuffer| be |bufferSource|. 1. Let |offset| be 0. - 1. If |jsBufferSource| has a \[[ViewedArrayBuffer]] [=/internal slot=], then: - 1. Set |jsArrayBuffer| to |jsBufferSource|.\[[ViewedArrayBuffer]]. - 1. Set |offset| to |jsBufferSource|'s [=ArrayBufferView/byte offset=]. - 1. Otherwise: - 1. Assert: |jsBufferSource| is an {{ArrayBuffer}} or - {{SharedArrayBuffer}} object. - 1. Let |length| be |jsBufferSource|'s [=BufferSource/byte length=]. + 1. Let |length| be |bufferSource|'s [=BufferSource/byte length=]. + 1. If |bufferSource| is a [=buffer view type=] instance, then: + 1. Set |arrayBuffer| to |bufferSource|'s [=underlying buffer=]. + 1. Set |offset| to |bufferSource|'s [=ArrayBufferView/byte offset=]. + 1. Assert: |arrayBuffer| is an {{ArrayBuffer}} or {{SharedArrayBuffer}} object. + 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] + |arrayBuffer| to a JavaScript value. 1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is true, then return the empty [=byte sequence=]. 1. Let |bytes| be a new [=byte sequence=] of [=byte sequence/length=] equal to |length|. From e2308a0de448cb9854361bceb7870767b50a0428 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 23:21:47 +0200 Subject: [PATCH 09/10] Use IsArrayBufferViewOutOfBounds instead --- index.bs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/index.bs b/index.bs index 21ca9194..9223e2f4 100644 --- a/index.bs +++ b/index.bs @@ -9318,14 +9318,7 @@ a reference to the same object that the IDL value represents. 1. Let |jsView| be the result of [=converted to a JavaScript value|converting=] |view| to a JavaScript value. - 1. If |jsView| has a \[[TypedArrayName]] [=/internal slot=]: - 1. Assert: |jsView| is a [=typed array type=] instance. - 1. Let |taRecord| be [$MakeTypedArrayWithBufferWitnessRecord$](|jsView|, Unordered). - 1. If [$IsTypedArrayOutOfBounds$](|taRecord|) is true, throw a {{TypeError}} exception. - 1. Otherwise: - 1. Assert: |jsView| is a {{DataView}}. - 1. Let |viewRecord| be [$MakeDataViewWithBufferWitnessRecord$](|jsView|, Unordered). - 1. If [$IsViewOutOfBounds$](|viewRecord|) is true, throw a {{TypeError}} exception. + 1. If [$IsArrayBufferViewOutOfBounds$](|jsView|) is true, throw a {{TypeError}} exception. 1. Return |jsView|.\[[ByteOffset]].
From 4a52a2f5dd62f23b6b97bc859f2bbe958e984efd Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Wed, 8 Oct 2025 23:32:52 +0200 Subject: [PATCH 10/10] Use SEQ-CST ordering to match ECMAScript --- index.bs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.bs b/index.bs index 9223e2f4..a95cb0b2 100644 --- a/index.bs +++ b/index.bs @@ -9332,17 +9332,17 @@ a reference to the same object that the IDL value represents. 1. If |jsBufferSource| has a \[[TypedArrayName]] [=/internal slot=]: 1. Assert: |jsBufferSource| is a [=typed array type=] instance. 1. Let |taRecord| be [$MakeTypedArrayWithBufferWitnessRecord$](|jsBufferSource|, - Unordered). + Seq-Cst). 1. Return [$TypedArrayByteLength$](|taRecord|). 1. Otherwise: 1. Assert: |jsBufferSource| is a {{DataView}}. 1. Let |viewRecord| be [$MakeDataViewWithBufferWitnessRecord$](|jsBufferSource|, - Unordered). + Seq-Cst). 1. If [$IsViewOutOfBounds$](|viewRecord|) is true, return 0. 1. Return [$GetViewByteLength$](|viewRecord|). 1. Otherwise, if [$IsSharedArrayBuffer$](|jsBufferSource|) is true, then: 1. Assert: |jsBufferSource| is a {{SharedArrayBuffer}}. - 1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Unordered). + 1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst). 1. Otherwise: 1. Assert: |jsBufferSource| is an {{ArrayBuffer}}. 1. Return |jsBufferSource|.\[[ArrayBufferByteLength]].