Skip to content

Commit a4b420a

Browse files
authored
Normative: Account for TypedArraySpeciesCreate species lookup (#47)
Fixes #46
1 parent bad00ba commit a4b420a

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed

spec.emu

+231
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,81 @@ contributors: Mark S. Miller, Richard Gibson
176176
<emu-clause id="sec-typedarray-objects" number="2">
177177
<h1>TypedArray Objects</h1>
178178

179+
<emu-clause id="sec-properties-of-the-%typedarray%-intrinsic-object" number="2">
180+
<h1>Properties of the %TypedArray% Intrinsic Object</h1>
181+
<p>The %TypedArray% intrinsic object:</p>
182+
<ul>
183+
<li>has a [[Prototype]] internal slot whose value is %Function.prototype%.</li>
184+
<li>has a *"name"* property whose value is *"TypedArray"*.</li>
185+
<li>has the following properties:</li>
186+
</ul>
187+
188+
<emu-clause id="sec-%typedarray%.from">
189+
<h1>%TypedArray%.from ( _source_ [ , _mapper_ [ , _thisArg_ ] ] )</h1>
190+
<p>This method performs the following steps when called:</p>
191+
<emu-alg>
192+
1. Let _C_ be the *this* value.
193+
1. If IsConstructor(_C_) is *false*, throw a *TypeError* exception.
194+
1. If _mapper_ is *undefined*, then
195+
1. Let _mapping_ be *false*.
196+
1. Else,
197+
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
198+
1. Let _mapping_ be *true*.
199+
1. Let _usingIterator_ be ? GetMethod(_source_, %Symbol.iterator%).
200+
1. If _usingIterator_ is not *undefined*, then
201+
1. Let _values_ be ? IteratorToList(? GetIteratorFromMethod(_source_, _usingIterator_)).
202+
1. Let _len_ be the number of elements in _values_.
203+
1. Let _targetObj_ be ? TypedArrayCreateFromConstructor(_C_, « 𝔽(_len_) »<ins>, ~write~</ins>).
204+
1. Let _k_ be 0.
205+
1. Repeat, while _k_ &lt; _len_,
206+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
207+
1. Let _kValue_ be the first element of _values_.
208+
1. Remove the first element from _values_.
209+
1. If _mapping_ is *true*, then
210+
1. Let _mappedValue_ be ? Call(_mapper_, _thisArg_, « _kValue_, 𝔽(_k_) »).
211+
1. Else,
212+
1. Let _mappedValue_ be _kValue_.
213+
1. Perform ? Set(_targetObj_, _Pk_, _mappedValue_, *true*).
214+
1. Set _k_ to _k_ + 1.
215+
1. Assert: _values_ is now an empty List.
216+
1. Return _targetObj_.
217+
1. NOTE: _source_ is not an iterable object, so assume it is already an array-like object.
218+
1. Let _arrayLike_ be ! ToObject(_source_).
219+
1. Let _len_ be ? LengthOfArrayLike(_arrayLike_).
220+
1. Let _targetObj_ be ? TypedArrayCreateFromConstructor(_C_, « 𝔽(_len_) »<ins>, ~write~</ins>).
221+
1. Let _k_ be 0.
222+
1. Repeat, while _k_ &lt; _len_,
223+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
224+
1. Let _kValue_ be ? Get(_arrayLike_, _Pk_).
225+
1. If _mapping_ is *true*, then
226+
1. Let _mappedValue_ be ? Call(_mapper_, _thisArg_, « _kValue_, 𝔽(_k_) »).
227+
1. Else,
228+
1. Let _mappedValue_ be _kValue_.
229+
1. Perform ? Set(_targetObj_, _Pk_, _mappedValue_, *true*).
230+
1. Set _k_ to _k_ + 1.
231+
1. Return _targetObj_.
232+
</emu-alg>
233+
</emu-clause>
234+
235+
<emu-clause id="sec-%typedarray%.of">
236+
<h1>%TypedArray%.of ( ..._items_ )</h1>
237+
<p>This method performs the following steps when called:</p>
238+
<emu-alg>
239+
1. Let _len_ be the number of elements in _items_.
240+
1. Let _C_ be the *this* value.
241+
1. If IsConstructor(_C_) is *false*, throw a *TypeError* exception.
242+
1. Let _newObj_ be ? TypedArrayCreateFromConstructor(_C_, « 𝔽(_len_) »<ins>, ~write~</ins>).
243+
1. Let _k_ be 0.
244+
1. Repeat, while _k_ &lt; _len_,
245+
1. Let _kValue_ be _items_[_k_].
246+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
247+
1. Perform ? Set(_newObj_, _Pk_, _kValue_, *true*).
248+
1. Set _k_ to _k_ + 1.
249+
1. Return _newObj_.
250+
</emu-alg>
251+
</emu-clause>
252+
</emu-clause>
253+
179254
<emu-clause id="sec-properties-of-the-%typedarrayprototype%-object" number="3">
180255
<h1>Properties of the %TypedArray% Prototype Object</h1>
181256

@@ -262,6 +337,60 @@ contributors: Mark S. Miller, Richard Gibson
262337
</emu-alg>
263338
</emu-clause>
264339

340+
<emu-clause id="sec-%typedarray%.prototype.filter" number="10">
341+
<h1>%TypedArray%.prototype.filter ( _callback_ [ , _thisArg_ ] )</h1>
342+
<p>The interpretation and use of the arguments of this method are the same as for `Array.prototype.filter` as defined in <emu-xref href="#sec-array.prototype.filter"></emu-xref>.</p>
343+
<p>This method performs the following steps when called:</p>
344+
<emu-alg>
345+
1. Let _O_ be the *this* value.
346+
1. Let _taRecord_ be ? ValidateTypedArray(_O_, ~seq-cst~).
347+
1. Let _len_ be TypedArrayLength(_taRecord_).
348+
1. If IsCallable(_callback_) is *false*, throw a *TypeError* exception.
349+
1. Let _kept_ be a new empty List.
350+
1. Let _captured_ be 0.
351+
1. Let _k_ be 0.
352+
1. Repeat, while _k_ &lt; _len_,
353+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
354+
1. Let _kValue_ be ! Get(_O_, _Pk_).
355+
1. Let _selected_ be ToBoolean(? Call(_callback_, _thisArg_, « _kValue_, 𝔽(_k_), _O_ »)).
356+
1. If _selected_ is *true*, then
357+
1. Append _kValue_ to _kept_.
358+
1. Set _captured_ to _captured_ + 1.
359+
1. Set _k_ to _k_ + 1.
360+
1. Let _A_ be ? TypedArraySpeciesCreate(_O_, « 𝔽(_captured_) »<ins>, ~write~</ins>).
361+
1. <ins>Assert: IsImmutableBuffer(_A_.[[ViewedArrayBuffer]]) is *false*.</ins>
362+
1. Let _n_ be 0.
363+
1. For each element _e_ of _kept_, do
364+
1. Perform ! Set(_A_, ! ToString(𝔽(_n_)), _e_, *true*).
365+
1. Set _n_ to _n_ + 1.
366+
1. Return _A_.
367+
</emu-alg>
368+
<p>This method is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
369+
</emu-clause>
370+
371+
<emu-clause id="sec-%typedarray%.prototype.map" number="22">
372+
<h1>%TypedArray%.prototype.map ( _callback_ [ , _thisArg_ ] )</h1>
373+
<p>The interpretation and use of the arguments of this method are the same as for `Array.prototype.map` as defined in <emu-xref href="#sec-array.prototype.map"></emu-xref>.</p>
374+
<p>This method performs the following steps when called:</p>
375+
<emu-alg>
376+
1. Let _O_ be the *this* value.
377+
1. Let _taRecord_ be ? ValidateTypedArray(_O_, ~seq-cst~).
378+
1. Let _len_ be TypedArrayLength(_taRecord_).
379+
1. If IsCallable(_callback_) is *false*, throw a *TypeError* exception.
380+
1. Let _A_ be ? TypedArraySpeciesCreate(_O_, « 𝔽(_len_) »<ins>, ~write~</ins>).
381+
1. <ins>Assert: IsImmutableBuffer(_A_.[[ViewedArrayBuffer]]) is *false*.</ins>
382+
1. Let _k_ be 0.
383+
1. Repeat, while _k_ &lt; _len_,
384+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
385+
1. Let _kValue_ be ! Get(_O_, _Pk_).
386+
1. Let _mappedValue_ be ? Call(_callback_, _thisArg_, « _kValue_, 𝔽(_k_), _O_ »).
387+
1. Perform ? Set(_A_, _Pk_, _mappedValue_, *true*).
388+
1. Set _k_ to _k_ + 1.
389+
1. Return _A_.
390+
</emu-alg>
391+
<p>This method is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
392+
</emu-clause>
393+
265394
<emu-clause id="sec-%typedarray%.prototype.reverse" number="25">
266395
<h1>%TypedArray%.prototype.reverse ( )</h1>
267396
<p>The interpretation and use of the arguments of this method are the same as for `Array.prototype.reverse` as defined in <emu-xref href="#sec-array.prototype.reverse"></emu-xref>.</p>
@@ -307,6 +436,60 @@ contributors: Mark S. Miller, Richard Gibson
307436
<p>This method is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
308437
</emu-clause>
309438

439+
<emu-clause id="sec-%typedarray%.prototype.slice" number="27">
440+
<h1>%TypedArray%.prototype.slice ( _start_, _end_ )</h1>
441+
<p>The interpretation and use of the arguments of this method are the same as for `Array.prototype.slice` as defined in <emu-xref href="#sec-array.prototype.slice"></emu-xref>.</p>
442+
<p>This method performs the following steps when called:</p>
443+
<emu-alg>
444+
1. Let _O_ be the *this* value.
445+
1. Let _taRecord_ be ? ValidateTypedArray(_O_, ~seq-cst~).
446+
1. Let _srcArrayLength_ be TypedArrayLength(_taRecord_).
447+
1. Let _relativeStart_ be ? ToIntegerOrInfinity(_start_).
448+
1. If _relativeStart_ = -∞, let _startIndex_ be 0.
449+
1. Else if _relativeStart_ &lt; 0, let _startIndex_ be max(_srcArrayLength_ + _relativeStart_, 0).
450+
1. Else, let _startIndex_ be min(_relativeStart_, _srcArrayLength_).
451+
1. If _end_ is *undefined*, let _relativeEnd_ be _srcArrayLength_; else let _relativeEnd_ be ? ToIntegerOrInfinity(_end_).
452+
1. If _relativeEnd_ = -∞, let _endIndex_ be 0.
453+
1. Else if _relativeEnd_ &lt; 0, let _endIndex_ be max(_srcArrayLength_ + _relativeEnd_, 0).
454+
1. Else, let _endIndex_ be min(_relativeEnd_, _srcArrayLength_).
455+
1. Let _countBytes_ be max(_endIndex_ - _startIndex_, 0).
456+
1. Let _A_ be ? TypedArraySpeciesCreate(_O_, « 𝔽(_countBytes_) »<ins>, ~write~</ins>).
457+
1. <ins>Assert: IsImmutableBuffer(_A_.[[ViewedArrayBuffer]]) is *false*.</ins>
458+
1. If _countBytes_ > 0, then
459+
1. Set _taRecord_ to MakeTypedArrayWithBufferWitnessRecord(_O_, ~seq-cst~).
460+
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
461+
1. Set _endIndex_ to min(_endIndex_, TypedArrayLength(_taRecord_)).
462+
1. Set _countBytes_ to max(_endIndex_ - _startIndex_, 0).
463+
1. Let _srcType_ be TypedArrayElementType(_O_).
464+
1. Let _targetType_ be TypedArrayElementType(_A_).
465+
1. If _srcType_ is _targetType_, then
466+
1. NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
467+
1. Let _srcBuffer_ be _O_.[[ViewedArrayBuffer]].
468+
1. Let _targetBuffer_ be _A_.[[ViewedArrayBuffer]].
469+
1. Let _elementSize_ be TypedArrayElementSize(_O_).
470+
1. Let _srcByteOffset_ be _O_.[[ByteOffset]].
471+
1. Let _srcByteIndex_ be (_startIndex_ × _elementSize_) + _srcByteOffset_.
472+
1. Let _targetByteIndex_ be _A_.[[ByteOffset]].
473+
1. Let _endByteIndex_ be _targetByteIndex_ + (_countBytes_ × _elementSize_).
474+
1. Repeat, while _targetByteIndex_ &lt; _endByteIndex_,
475+
1. Let _value_ be GetValueFromBuffer(_srcBuffer_, _srcByteIndex_, ~uint8~, *true*, ~unordered~).
476+
1. Perform SetValueInBuffer(_targetBuffer_, _targetByteIndex_, ~uint8~, _value_, *true*, ~unordered~).
477+
1. Set _srcByteIndex_ to _srcByteIndex_ + 1.
478+
1. Set _targetByteIndex_ to _targetByteIndex_ + 1.
479+
1. Else,
480+
1. Let _n_ be 0.
481+
1. Let _k_ be _startIndex_.
482+
1. Repeat, while _k_ &lt; _endIndex_,
483+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
484+
1. Let _kValue_ be ! Get(_O_, _Pk_).
485+
1. Perform ! Set(_A_, ! ToString(𝔽(_n_)), _kValue_, *true*).
486+
1. Set _k_ to _k_ + 1.
487+
1. Set _n_ to _n_ + 1.
488+
1. Return _A_.
489+
</emu-alg>
490+
<p>This method is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
491+
</emu-clause>
492+
310493
<emu-clause id="sec-%typedarray%.prototype.sort" oldids="sec-typedarraysortcompare" number="29">
311494
<h1>%TypedArray%.prototype.sort ( _comparator_ )</h1>
312495
<p>This is a distinct method that, except as described below, implements the same requirements as those of `Array.prototype.sort` as defined in <emu-xref href="#sec-array.prototype.sort"></emu-xref>. The implementation of this method may be optimized with the knowledge that the *this* value is an object that has a fixed length and whose integer-indexed properties are not sparse.</p>
@@ -336,6 +519,54 @@ contributors: Mark S. Miller, Richard Gibson
336519
<emu-clause id="sec-abstract-operations-for-typedarray-objects" number="4">
337520
<h1>Abstract Operations for TypedArray Objects</h1>
338521

522+
<emu-clause id="typedarray-species-create" type="abstract operation" number="1">
523+
<h1>
524+
TypedArraySpeciesCreate (
525+
_exemplar_: a TypedArray,
526+
_argumentList_: a List of ECMAScript language values,
527+
<ins>optional _accessMode_: ~read~ or ~write~,</ins>
528+
): either a normal completion containing a TypedArray or a throw completion
529+
</h1>
530+
<dl class="header">
531+
<dt>description</dt>
532+
<dd>It is used to specify the creation of a new TypedArray using a constructor function that is derived from _exemplar_. Unlike ArraySpeciesCreate, which can create non-Array objects through the use of %Symbol.species%, this operation enforces that the constructor function creates an actual TypedArray.</dd>
533+
</dl>
534+
<emu-alg>
535+
1. <ins>If _accessMode_ is not present, set _accessMode_ to ~read~.</ins>
536+
1. Let _defaultConstructor_ be the intrinsic object associated with the constructor name _exemplar_.[[TypedArrayName]] in <emu-xref href="#table-the-typedarray-constructors"></emu-xref>.
537+
1. Let _constructor_ be ? SpeciesConstructor(_exemplar_, _defaultConstructor_).
538+
1. Let _result_ be ? TypedArrayCreateFromConstructor(_constructor_, _argumentList_<ins>, _accessMode_</ins>).
539+
1. <del>Assert: _result_ has [[TypedArrayName]] and [[ContentType]] internal slots.</del>
540+
1. <ins>Assert: _result_ has all of the internal slots of a <var>TypedArray</var> instance (<emu-xref href="#sec-properties-of-typedarray-instances"></emu-xref>).</ins>
541+
1. If _result_.[[ContentType]] is not _exemplar_.[[ContentType]], throw a *TypeError* exception.
542+
1. Return _result_.
543+
</emu-alg>
544+
</emu-clause>
545+
546+
<emu-clause id="sec-typedarraycreatefromconstructor" oldids="typedarray-create" type="abstract operation">
547+
<h1>
548+
TypedArrayCreateFromConstructor (
549+
_constructor_: a constructor,
550+
_argumentList_: a List of ECMAScript language values,
551+
<ins>optional _accessMode_: ~read~ or ~write~,</ins>
552+
): either a normal completion containing a TypedArray or a throw completion
553+
</h1>
554+
<dl class="header">
555+
<dt>description</dt>
556+
<dd>It is used to specify the creation of a new TypedArray using a constructor function.</dd>
557+
</dl>
558+
<emu-alg>
559+
1. <ins>If _accessMode_ is not present, set _accessMode_ to ~read~.</ins>
560+
1. Let _newTypedArray_ be ? Construct(_constructor_, _argumentList_).
561+
1. Let _taRecord_ be ? ValidateTypedArray(_newTypedArray_, ~seq-cst~<ins>, _accessMode_</ins>).
562+
1. If the number of elements in _argumentList_ is 1 and _argumentList_[0] is a Number, then
563+
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
564+
1. Let _length_ be TypedArrayLength(_taRecord_).
565+
1. If _length_ &lt; ℝ(_argumentList_[0]), throw a *TypeError* exception.
566+
1. Return _newTypedArray_.
567+
</emu-alg>
568+
</emu-clause>
569+
339570
<emu-clause id="sec-validatetypedarray" type="abstract operation" number="4">
340571
<h1>
341572
ValidateTypedArray (

0 commit comments

Comments
 (0)