Skip to content

Commit db0ec14

Browse files
committed
Editorial: Better align %TypedArray%.prototype.set with other mutating methods
1 parent 829319b commit db0ec14

File tree

1 file changed

+17
-89
lines changed

1 file changed

+17
-89
lines changed

spec.emu

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -287,95 +287,23 @@ contributors: Mark S. Miller, Richard Gibson
287287

288288
<emu-clause id="sec-%typedarray%.prototype.set" oldids="sec-%typedarray%.prototype.set-overloaded-offset" number="26">
289289
<h1>%TypedArray%.prototype.set ( _source_ [ , _offset_ ] )</h1>
290-
291-
<emu-clause id="sec-settypedarrayfromtypedarray" type="abstract operation" oldids="sec-%typedarray%.prototype.set-typedarray-offset">
292-
<h1>
293-
SetTypedArrayFromTypedArray (
294-
_target_: a TypedArray,
295-
_targetOffset_: a non-negative integer or +&infin;,
296-
_source_: a TypedArray,
297-
): either a normal completion containing ~unused~ or a throw completion
298-
</h1>
299-
<dl class="header">
300-
<dt>description</dt>
301-
<dd>It sets multiple values in _target_, starting at index _targetOffset_, reading the values from _source_.</dd>
302-
</dl>
303-
<emu-alg>
304-
1. Let _targetBuffer_ be _target_.[[ViewedArrayBuffer]].
305-
1. Let _targetRecord_ be MakeTypedArrayWithBufferWitnessRecord(_target_, ~seq-cst~).
306-
1. If IsTypedArrayOutOfBounds(_targetRecord_) is *true*, throw a *TypeError* exception.
307-
1. <ins>If IsImmutableBuffer(_target_.[[ViewedArrayBuffer]]) is *true*, throw a *TypeError* exception.</ins>
308-
1. Let _targetLength_ be TypedArrayLength(_targetRecord_).
309-
1. Let _srcBuffer_ be _source_.[[ViewedArrayBuffer]].
310-
1. Let _srcRecord_ be MakeTypedArrayWithBufferWitnessRecord(_source_, ~seq-cst~).
311-
1. If IsTypedArrayOutOfBounds(_srcRecord_) is *true*, throw a *TypeError* exception.
312-
1. Let _srcLength_ be TypedArrayLength(_srcRecord_).
313-
1. Let _targetType_ be TypedArrayElementType(_target_).
314-
1. Let _targetElementSize_ be TypedArrayElementSize(_target_).
315-
1. Let _targetByteOffset_ be _target_.[[ByteOffset]].
316-
1. Let _srcType_ be TypedArrayElementType(_source_).
317-
1. Let _srcElementSize_ be TypedArrayElementSize(_source_).
318-
1. Let _srcByteOffset_ be _source_.[[ByteOffset]].
319-
1. If _targetOffset_ = +∞, throw a *RangeError* exception.
320-
1. If _srcLength_ + _targetOffset_ > _targetLength_, throw a *RangeError* exception.
321-
1. If _target_.[[ContentType]] is not _source_.[[ContentType]], throw a *TypeError* exception.
322-
1. If IsSharedArrayBuffer(_srcBuffer_) is *true*, IsSharedArrayBuffer(_targetBuffer_) is *true*, and _srcBuffer_.[[ArrayBufferData]] is _targetBuffer_.[[ArrayBufferData]], let _sameSharedArrayBuffer_ be *true*; otherwise, let _sameSharedArrayBuffer_ be *false*.
323-
1. If SameValue(_srcBuffer_, _targetBuffer_) is *true* or _sameSharedArrayBuffer_ is *true*, then
324-
1. Let _srcByteLength_ be TypedArrayByteLength(_srcRecord_).
325-
1. Set _srcBuffer_ to ? CloneArrayBuffer(_srcBuffer_, _srcByteOffset_, _srcByteLength_).
326-
1. Let _srcByteIndex_ be 0.
327-
1. Else,
328-
1. Let _srcByteIndex_ be _srcByteOffset_.
329-
1. Let _targetByteIndex_ be (_targetOffset_ × _targetElementSize_) + _targetByteOffset_.
330-
1. Let _limit_ be _targetByteIndex_ + (_targetElementSize_ × _srcLength_).
331-
1. If _srcType_ is _targetType_, then
332-
1. NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
333-
1. Repeat, while _targetByteIndex_ &lt; _limit_,
334-
1. Let _value_ be GetValueFromBuffer(_srcBuffer_, _srcByteIndex_, ~uint8~, *true*, ~unordered~).
335-
1. Perform SetValueInBuffer(_targetBuffer_, _targetByteIndex_, ~uint8~, _value_, *true*, ~unordered~).
336-
1. Set _srcByteIndex_ to _srcByteIndex_ + 1.
337-
1. Set _targetByteIndex_ to _targetByteIndex_ + 1.
338-
1. Else,
339-
1. Repeat, while _targetByteIndex_ &lt; _limit_,
340-
1. Let _value_ be GetValueFromBuffer(_srcBuffer_, _srcByteIndex_, _srcType_, *true*, ~unordered~).
341-
1. Perform SetValueInBuffer(_targetBuffer_, _targetByteIndex_, _targetType_, _value_, *true*, ~unordered~).
342-
1. Set _srcByteIndex_ to _srcByteIndex_ + _srcElementSize_.
343-
1. Set _targetByteIndex_ to _targetByteIndex_ + _targetElementSize_.
344-
1. Return ~unused~.
345-
</emu-alg>
346-
</emu-clause>
347-
348-
<emu-clause id="sec-settypedarrayfromarraylike" type="abstract operation" oldids="sec-%typedarray%.prototype.set-array-offset">
349-
<h1>
350-
SetTypedArrayFromArrayLike (
351-
_target_: a TypedArray,
352-
_targetOffset_: a non-negative integer or +&infin;,
353-
_source_: an ECMAScript language value, but not a TypedArray,
354-
): either a normal completion containing ~unused~ or a throw completion
355-
</h1>
356-
<dl class="header">
357-
<dt>description</dt>
358-
<dd>It sets multiple values in _target_, starting at index _targetOffset_, reading the values from _source_.</dd>
359-
</dl>
360-
<emu-alg>
361-
1. Let _targetRecord_ be MakeTypedArrayWithBufferWitnessRecord(_target_, ~seq-cst~).
362-
1. If IsTypedArrayOutOfBounds(_targetRecord_) is *true*, throw a *TypeError* exception.
363-
1. <ins>If IsImmutableBuffer(_target_.[[ViewedArrayBuffer]]) is *true*, throw a *TypeError* exception.</ins>
364-
1. Let _targetLength_ be TypedArrayLength(_targetRecord_).
365-
1. Let _src_ be ? ToObject(_source_).
366-
1. Let _srcLength_ be ? LengthOfArrayLike(_src_).
367-
1. If _targetOffset_ = +∞, throw a *RangeError* exception.
368-
1. If _srcLength_ + _targetOffset_ > _targetLength_, throw a *RangeError* exception.
369-
1. Let _k_ be 0.
370-
1. Repeat, while _k_ &lt; _srcLength_,
371-
1. Let _Pk_ be ! ToString(𝔽(_k_)).
372-
1. Let _value_ be ? Get(_src_, _Pk_).
373-
1. Let _targetIndex_ be 𝔽(_targetOffset_ + _k_).
374-
1. Perform ? TypedArraySetElement(_target_, _targetIndex_, _value_).
375-
1. Set _k_ to _k_ + 1.
376-
1. Return ~unused~.
377-
</emu-alg>
378-
</emu-clause>
290+
<p>This method sets multiple values in this _TypedArray_, reading the values from _source_. The details differ based upon the type of _source_. The optional _offset_ value indicates the first element index in this _TypedArray_ where values are written. If omitted, it is assumed to be 0.</p>
291+
<p>It performs the following steps when called:</p>
292+
<emu-alg>
293+
1. Let _target_ be the *this* value.
294+
1. <ins>NOTE: `Let _taRecord_ be ? ValidateTypedArray(_O_, ~seq-cst~<ins>, ~write~</ins>).` here would observably change the observable calls into user code and thrown error when IsTypedArrayOutOfBounds returns *true* and _offset_ is negative. Such a change should be pursued.</ins>
295+
1. Perform ? RequireInternalSlot(_target_, [[TypedArrayName]]).
296+
1. Assert: _target_ has a [[ViewedArrayBuffer]] internal slot.
297+
1. <ins>If IsImmutableBuffer(_target_.[[ViewedArrayBuffer]]) is *true*, throw a *TypeError* exception.</ins>
298+
1. Let _targetOffset_ be ? ToIntegerOrInfinity(_offset_).
299+
1. If _targetOffset_ &lt; 0, throw a *RangeError* exception.
300+
1. If _source_ is an Object that has a [[TypedArrayName]] internal slot, then
301+
1. Perform ? SetTypedArrayFromTypedArray(_target_, _targetOffset_, _source_).
302+
1. Else,
303+
1. Perform ? SetTypedArrayFromArrayLike(_target_, _targetOffset_, _source_).
304+
1. Return *undefined*.
305+
</emu-alg>
306+
<p>This method is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
379307
</emu-clause>
380308

381309
<emu-clause id="sec-%typedarray%.prototype.sort" oldids="sec-typedarraysortcompare" number="29">

0 commit comments

Comments
 (0)