Skip to content

Editorial: Make better use of ValidateTypedArray #3634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Editorial: Make better use of ValidateTypedArray
  • Loading branch information
gibson042 committed Jul 1, 2025
commit 3b5e589ad8849f48819eff554886f35d5b00851c
27 changes: 9 additions & 18 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -41197,8 +41197,7 @@ <h1>%ArrayIteratorPrototype%.next ( )</h1>
1. Let _index_ be _O_.[[ArrayLikeNextIndex]].
1. Let _kind_ be _O_.[[ArrayLikeIterationKind]].
1. If _array_ has a [[TypedArrayName]] internal slot, then
1. Let _taRecord_ be MakeTypedArrayWithBufferWitnessRecord(_array_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Let _taRecord_ be ? ValidateTypedArray(_array_, ~seq-cst~).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems worse to me. Every existing use of ValidateTypedArray is passed an object which is not yet known to be a TA.

I'd be OK with introducing a new AO for this - GetInBoundsTALengthWitness or something, maybe - and then using it ValidateTypedArray and in callsites such as this. But I really don't like re-using ValidateTypedArray for this.

1. Let _len_ be TypedArrayLength(_taRecord_).
1. Else,
1. Let _len_ be ? LengthOfArrayLike(_array_).
Expand Down Expand Up @@ -41732,8 +41731,7 @@ <h1>%TypedArray%.prototype.copyWithin ( _target_, _start_ [ , _end_ ] )</h1>
1. If _count_ > 0, then
1. NOTE: The copying must be performed in a manner that preserves the bit-level encoding of the source data.
1. Let _buffer_ be _O_.[[ViewedArrayBuffer]].
1. Set _taRecord_ to MakeTypedArrayWithBufferWitnessRecord(_O_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Set _taRecord_ to ? ValidateTypedArray(_O_, ~seq-cst~).
1. Set _len_ to TypedArrayLength(_taRecord_).
1. Let _elementSize_ be TypedArrayElementSize(_O_).
1. Let _byteOffset_ be _O_.[[ByteOffset]].
Expand Down Expand Up @@ -41809,8 +41807,7 @@ <h1>%TypedArray%.prototype.fill ( _value_ [ , _start_ [ , _end_ ] ] )</h1>
1. If _relativeEnd_ = -∞, let _endIndex_ be 0.
1. Else if _relativeEnd_ &lt; 0, let _endIndex_ be max(_len_ + _relativeEnd_, 0).
1. Else, let _endIndex_ be min(_relativeEnd_, _len_).
1. Set _taRecord_ to MakeTypedArrayWithBufferWitnessRecord(_O_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Set _taRecord_ to ? ValidateTypedArray(_O_, ~seq-cst~).
1. Set _len_ to TypedArrayLength(_taRecord_).
1. Set _endIndex_ to min(_endIndex_, _len_).
1. Let _k_ be _startIndex_.
Expand Down Expand Up @@ -42194,8 +42191,7 @@ <h1>
<dd>It sets multiple values in _target_, starting at index _targetOffset_, reading the values from _source_.</dd>
</dl>
<emu-alg>
1. Let _targetRecord_ be MakeTypedArrayWithBufferWitnessRecord(_target_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_targetRecord_) is *true*, throw a *TypeError* exception.
1. Let _targetRecord_ be ? ValidateTypedArray(_target_, ~seq-cst~).
1. Let _targetLength_ be TypedArrayLength(_targetRecord_).
1. Let _src_ be ? ToObject(_source_).
1. Let _srcLength_ be ? LengthOfArrayLike(_src_).
Expand Down Expand Up @@ -42226,12 +42222,10 @@ <h1>
</dl>
<emu-alg>
1. Let _targetBuffer_ be _target_.[[ViewedArrayBuffer]].
1. Let _targetRecord_ be MakeTypedArrayWithBufferWitnessRecord(_target_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_targetRecord_) is *true*, throw a *TypeError* exception.
1. Let _targetRecord_ be ? ValidateTypedArray(_target_, ~seq-cst~).
1. Let _targetLength_ be TypedArrayLength(_targetRecord_).
1. Let _srcBuffer_ be _source_.[[ViewedArrayBuffer]].
1. Let _srcRecord_ be MakeTypedArrayWithBufferWitnessRecord(_source_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_srcRecord_) is *true*, throw a *TypeError* exception.
1. Let _srcRecord_ be ? ValidateTypedArray(_source_, ~seq-cst~).
1. Let _srcLength_ be TypedArrayLength(_srcRecord_).
1. Let _targetType_ be TypedArrayElementType(_target_).
1. Let _targetElementSize_ be TypedArrayElementSize(_target_).
Expand Down Expand Up @@ -42288,8 +42282,7 @@ <h1>%TypedArray%.prototype.slice ( _start_, _end_ )</h1>
1. Let _countBytes_ be max(_endIndex_ - _startIndex_, 0).
1. Let _A_ be ? TypedArraySpeciesCreate(_O_, « 𝔽(_countBytes_) »).
1. If _countBytes_ > 0, then
1. Set _taRecord_ to MakeTypedArrayWithBufferWitnessRecord(_O_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Set _taRecord_ to ? ValidateTypedArray(_O_, ~seq-cst~).
1. Set _endIndex_ to min(_endIndex_, TypedArrayLength(_taRecord_)).
1. Set _countBytes_ to max(_endIndex_ - _startIndex_, 0).
1. Let _srcType_ be TypedArrayElementType(_O_).
Expand Down Expand Up @@ -42745,8 +42738,7 @@ <h1>
1. Let _srcType_ be TypedArrayElementType(_srcArray_).
1. Let _srcElementSize_ be TypedArrayElementSize(_srcArray_).
1. Let _srcByteOffset_ be _srcArray_.[[ByteOffset]].
1. Let _srcRecord_ be MakeTypedArrayWithBufferWitnessRecord(_srcArray_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_srcRecord_) is *true*, throw a *TypeError* exception.
1. Let _srcRecord_ be ? ValidateTypedArray(_srcArray_, ~seq-cst~).
1. Let _elementLength_ be TypedArrayLength(_srcRecord_).
1. Let _byteLength_ be _elementSize_ × _elementLength_.
1. If _elementType_ is _srcType_, then
Expand Down Expand Up @@ -45956,9 +45948,8 @@ <h1>
<dd>This operation revalidates the index within the backing buffer for atomic operations after all argument coercions are performed in Atomics methods, as argument coercions can have arbitrary side effects, which could cause the buffer to become out of bounds. This operation does not throw when _typedArray_'s backing buffer is a SharedArrayBuffer.</dd>
</dl>
<emu-alg>
1. Let _taRecord_ be MakeTypedArrayWithBufferWitnessRecord(_typedArray_, ~unordered~).
1. NOTE: Bounds checking is not a synchronizing operation when _typedArray_'s backing buffer is a growable SharedArrayBuffer.
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Let _taRecord_ be ? ValidateTypedArray(_typedArray_, ~unordered~).
1. Assert: _byteIndexInBuffer_ ≥ _typedArray_.[[ByteOffset]].
1. If _byteIndexInBuffer_ ≥ _taRecord_.[[CachedBufferByteLength]], throw a *RangeError* exception.
1. Return ~unused~.
Expand Down