23.2 TypedArray Objects
A TypedArray presents an array-like view of an underlying binary data buffer (
|
Element Type | Element Size | Conversion Operation | Description |
---|---|---|---|---|
Int8Array %Int8Array% |
|
1 |
|
8-bit two's complement signed |
Uint8Array %Uint8Array% |
|
1 |
|
8-bit unsigned |
Uint8ClampedArray %Uint8ClampedArray% |
|
1 |
|
8-bit unsigned |
Int16Array %Int16Array% |
|
2 |
|
16-bit two's complement signed |
Uint16Array %Uint16Array% |
|
2 |
|
16-bit unsigned |
Int32Array %Int32Array% |
|
4 |
|
32-bit two's complement signed |
Uint32Array %Uint32Array% |
|
4 |
|
32-bit unsigned |
BigInt64Array %BigInt64Array% |
|
8 |
|
64-bit two's complement signed |
BigUint64Array %BigUint64Array% |
|
8 |
|
64-bit unsigned |
Float32Array %Float32Array% |
|
4 | 32-bit IEEE floating point | |
Float64Array %Float64Array% |
|
8 | 64-bit IEEE floating point |
In the definitions below, references to TypedArray should be replaced with the appropriate
23.2.1 The %TypedArray% Intrinsic Object
The %TypedArray% intrinsic object:
- is a
constructor function object that all of the TypedArrayconstructor objects inherit from. - along with its corresponding prototype object, provides common properties that are inherited by all TypedArray
constructors and their instances. - does not have a global name or appear as a property of the
global object . - acts as the abstract superclass of the various TypedArray
constructors . - will throw an error when invoked, because it is an abstract class
constructor . The TypedArrayconstructors do not perform asuper
call to it.
23.2.1.1 %TypedArray% ( )
This function performs the following steps when called:
- Throw a
TypeError exception.
The
23.2.2 Properties of the %TypedArray% Intrinsic Object
The
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has a
"name" property whose value is"TypedArray" . - has the following properties:
23.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] )
This method performs the following steps when called:
- Let C be the
this value. - If
IsConstructor (C) isfalse , throw aTypeError exception. - If mapfn is
undefined , then- Let mapping be
false .
- Let mapping be
- Else,
- If
IsCallable (mapfn) isfalse , throw aTypeError exception. - Let mapping be
true .
- If
- Let usingIterator be ?
GetMethod (source,@@iterator ). - If usingIterator is not
undefined , then- Let values be ?
IteratorToList (?GetIteratorFromMethod (source, usingIterator)). - Let len be the number of elements in values.
- Let targetObj be ?
TypedArrayCreateFromConstructor (C, «𝔽 (len) »). - Let k be 0.
- Repeat, while k < len,
Assert : values is now an emptyList .- Return targetObj.
- Let values be ?
- NOTE: source is not an Iterable so assume it is already an
array-like object . - Let arrayLike be !
ToObject (source). - Let len be ?
LengthOfArrayLike (arrayLike). - Let targetObj be ?
TypedArrayCreateFromConstructor (C, «𝔽 (len) »). - Let k be 0.
- Repeat, while k < len,
- Return targetObj.
23.2.2.2 %TypedArray%.of ( ...items )
This method performs the following steps when called:
- Let len be the number of elements in items.
- Let C be the
this value. - If
IsConstructor (C) isfalse , throw aTypeError exception. - Let newObj be ?
TypedArrayCreateFromConstructor (C, «𝔽 (len) »). - Let k be 0.
- Repeat, while k < len,
- Return newObj.
23.2.2.3 %TypedArray%.prototype
The initial value of .prototype
is the
This property has the attributes { [[Writable]]:
23.2.2.4 get %TypedArray% [ @@species ]
[@@species]
is an
- Return the
this value.
The value of the
23.2.3 Properties of the %TypedArray% Prototype Object
The %TypedArray% prototype object:
- has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is %TypedArray.prototype%.
- is an
ordinary object . - does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.
23.2.3.1 %TypedArray%.prototype.at ( index )
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let relativeIndex be ?
ToIntegerOrInfinity (index). - If relativeIndex ≥ 0, then
- Let k be relativeIndex.
- Else,
- Let k be len + relativeIndex.
- If k < 0 or k ≥ len, return
undefined . - Return !
Get (O, !ToString (𝔽 (k))).
23.2.3.2 get %TypedArray%.prototype.buffer
.prototype.buffer
is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- Return buffer.
23.2.3.3 get %TypedArray%.prototype.byteLength
.prototype.byteLength
is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let taRecord be
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - Let size be
TypedArrayByteLength (taRecord). - Return
𝔽 (size).
23.2.3.4 get %TypedArray%.prototype.byteOffset
.prototype.byteOffset
is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let taRecord be
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - If
IsTypedArrayOutOfBounds (taRecord) istrue , return+0 𝔽. - Let offset be O.[[ByteOffset]].
- Return
𝔽 (offset).
23.2.3.5 %TypedArray%.prototype.constructor
The initial value of .prototype.constructor
is
23.2.3.6 %TypedArray%.prototype.copyWithin ( target, start [ , end ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.copyWithin
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let relativeTarget be ?
ToIntegerOrInfinity (target). - If relativeTarget = -∞, let targetIndex be 0.
- Else if relativeTarget < 0, let targetIndex be
max (len + relativeTarget, 0). - Else, let targetIndex be
min (relativeTarget, len). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart = -∞, let startIndex be 0.
- Else if relativeStart < 0, let startIndex be
max (len + relativeStart, 0). - Else, let startIndex be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd = -∞, let endIndex be 0.
- Else if relativeEnd < 0, let endIndex be
max (len + relativeEnd, 0). - Else, let endIndex be
min (relativeEnd, len). - Let count be
min (endIndex - startIndex, len - targetIndex). - If count > 0, then
- NOTE: The copying must be performed in a manner that preserves the bit-level encoding of the source data.
- Let buffer be O.[[ViewedArrayBuffer]].
- Set taRecord to
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - If
IsTypedArrayOutOfBounds (taRecord) istrue , throw aTypeError exception. - Set len to
TypedArrayLength (taRecord). - Let elementSize be
TypedArrayElementSize (O). - Let byteOffset be O.[[ByteOffset]].
- Let bufferByteLimit be (len × elementSize) + byteOffset.
- Let toByteIndex be (targetIndex × elementSize) + byteOffset.
- Let fromByteIndex be (startIndex × elementSize) + byteOffset.
- Let countBytes be count × elementSize.
- If fromByteIndex < toByteIndex and toByteIndex < fromByteIndex + countBytes, then
- Let direction be -1.
- Set fromByteIndex to fromByteIndex + countBytes - 1.
- Set toByteIndex to toByteIndex + countBytes - 1.
- Else,
- Let direction be 1.
- Repeat, while countBytes > 0,
- If fromByteIndex < bufferByteLimit and toByteIndex < bufferByteLimit, then
- Let value be
GetValueFromBuffer (buffer, fromByteIndex,uint8 ,true ,unordered ). - Perform
SetValueInBuffer (buffer, toByteIndex,uint8 , value,true ,unordered ). - Set fromByteIndex to fromByteIndex + direction.
- Set toByteIndex to toByteIndex + direction.
- Set countBytes to countBytes - 1.
- Let value be
- Else,
- Set countBytes to 0.
- If fromByteIndex < bufferByteLimit and toByteIndex < bufferByteLimit, then
- Return O.
23.2.3.7 %TypedArray%.prototype.entries ( )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O,seq-cst ). - Return
CreateArrayIterator (O,key+value ).
23.2.3.8 %TypedArray%.prototype.every ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.every
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
true .
This method is not generic. The
23.2.3.9 %TypedArray%.prototype.fill ( value [ , start [ , end ] ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.fill
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If O.[[ContentType]] is
bigint , set value to ?ToBigInt (value). - Otherwise, set value to ?
ToNumber (value). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart = -∞, let startIndex be 0.
- Else if relativeStart < 0, let startIndex be
max (len + relativeStart, 0). - Else, let startIndex be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd = -∞, let endIndex be 0.
- Else if relativeEnd < 0, let endIndex be
max (len + relativeEnd, 0). - Else, let endIndex be
min (relativeEnd, len). - Set taRecord to
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - If
IsTypedArrayOutOfBounds (taRecord) istrue , throw aTypeError exception. - Set len to
TypedArrayLength (taRecord). - Set endIndex to
min (endIndex, len). - Let k be startIndex.
- Repeat, while k < endIndex,
- Return O.
23.2.3.10 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.filter
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let kept be a new empty
List . - Let captured be 0.
- Let k be 0.
- Repeat, while k < len,
- Let A be ?
TypedArraySpeciesCreate (O, «𝔽 (captured) »). - Let n be 0.
- For each element e of kept, do
- Return A.
This method is not generic. The
23.2.3.11 %TypedArray%.prototype.find ( predicate [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.find
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let findRec be ?
FindViaPredicate (O, len,ascending , predicate, thisArg). - Return findRec.[[Value]].
This method is not generic. The
23.2.3.12 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.findIndex
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let findRec be ?
FindViaPredicate (O, len,ascending , predicate, thisArg). - Return findRec.[[Index]].
This method is not generic. The
23.2.3.13 %TypedArray%.prototype.findLast ( predicate [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.findLast
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let findRec be ?
FindViaPredicate (O, len,descending , predicate, thisArg). - Return findRec.[[Value]].
This method is not generic. The
23.2.3.14 %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.findLastIndex
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let findRec be ?
FindViaPredicate (O, len,descending , predicate, thisArg). - Return findRec.[[Index]].
This method is not generic. The
23.2.3.15 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.forEach
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
undefined .
This method is not generic. The
23.2.3.16 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.includes
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If len = 0, return
false . - Let n be ?
ToIntegerOrInfinity (fromIndex). Assert : If fromIndex isundefined , then n is 0.- If n = +∞, return
false . - Else if n = -∞, set n to 0.
- If n ≥ 0, then
- Let k be n.
- Else,
- Let k be len + n.
- If k < 0, set k to 0.
- Repeat, while k < len,
- Let elementK be !
Get (O, !ToString (𝔽 (k))). - If
SameValueZero (searchElement, elementK) istrue , returntrue . - Set k to k + 1.
- Let elementK be !
- Return
false .
This method is not generic. The
23.2.3.17 %TypedArray%.prototype.indexOf ( searchElement [ , fromIndex ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.indexOf
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If len = 0, return
-1 𝔽. - Let n be ?
ToIntegerOrInfinity (fromIndex). Assert : If fromIndex isundefined , then n is 0.- If n = +∞, return
-1 𝔽. - Else if n = -∞, set n to 0.
- If n ≥ 0, then
- Let k be n.
- Else,
- Let k be len + n.
- If k < 0, set k to 0.
- Repeat, while k < len,
- Let kPresent be !
HasProperty (O, !ToString (𝔽 (k))). - If kPresent is
true , then- Let elementK be !
Get (O, !ToString (𝔽 (k))). - If
IsStrictlyEqual (searchElement, elementK) istrue , return𝔽 (k).
- Let elementK be !
- Set k to k + 1.
- Let kPresent be !
- Return
-1 𝔽.
This method is not generic. The
23.2.3.18 %TypedArray%.prototype.join ( separator )
The interpretation and use of the arguments of this method are the same as for Array.prototype.join
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If separator is
undefined , let sep be"," . - Else, let sep be ?
ToString (separator). - Let R be the empty String.
- Let k be 0.
- Repeat, while k < len,
- If k > 0, set R to the
string-concatenation of R and sep. - Let element be !
Get (O, !ToString (𝔽 (k))). - If element is
undefined , let next be the empty String; otherwise, let next be !ToString (element). - Set R to the
string-concatenation of R and next. - Set k to k + 1.
- If k > 0, set R to the
- Return R.
This method is not generic. The
23.2.3.19 %TypedArray%.prototype.keys ( )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O,seq-cst ). - Return
CreateArrayIterator (O,key ).
23.2.3.20 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.lastIndexOf
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If len = 0, return
-1 𝔽. - If fromIndex is present, let n be ?
ToIntegerOrInfinity (fromIndex); else let n be len - 1. - If n = -∞, return
-1 𝔽. - If n ≥ 0, then
- Let k be
min (n, len - 1).
- Let k be
- Else,
- Let k be len + n.
- Repeat, while k ≥ 0,
- Let kPresent be !
HasProperty (O, !ToString (𝔽 (k))). - If kPresent is
true , then- Let elementK be !
Get (O, !ToString (𝔽 (k))). - If
IsStrictlyEqual (searchElement, elementK) istrue , return𝔽 (k).
- Let elementK be !
- Set k to k - 1.
- Let kPresent be !
- Return
-1 𝔽.
This method is not generic. The
23.2.3.21 get %TypedArray%.prototype.length
.prototype.length
is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has [[ViewedArrayBuffer]] and [[ArrayLength]] internal slots.- Let taRecord be
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - If
IsTypedArrayOutOfBounds (taRecord) istrue , return+0 𝔽. - Let length be
TypedArrayLength (taRecord). - Return
𝔽 (length).
This function is not generic. The
23.2.3.22 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.map
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let A be ?
TypedArraySpeciesCreate (O, «𝔽 (len) »). - Let k be 0.
- Repeat, while k < len,
- Return A.
This method is not generic. The
23.2.3.23 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.reduce
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - If len = 0 and initialValue is not present, throw a
TypeError exception. - Let k be 0.
- Let accumulator be
undefined . - If initialValue is present, then
- Set accumulator to initialValue.
- Else,
- Repeat, while k < len,
- Return accumulator.
This method is not generic. The
23.2.3.24 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.reduceRight
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - If len = 0 and initialValue is not present, throw a
TypeError exception. - Let k be len - 1.
- Let accumulator be
undefined . - If initialValue is present, then
- Set accumulator to initialValue.
- Else,
- Repeat, while k ≥ 0,
- Return accumulator.
This method is not generic. The
23.2.3.25 %TypedArray%.prototype.reverse ( )
The interpretation and use of the arguments of this method are the same as for Array.prototype.reverse
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let middle be
floor (len / 2). - Let lower be 0.
- Repeat, while lower ≠ middle,
- Return O.
This method is not generic. The
23.2.3.26 %TypedArray%.prototype.set ( source [ , offset ] )
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.
It performs the following steps when called:
- Let target be the
this value. - Perform ?
RequireInternalSlot (target, [[TypedArrayName]]). Assert : target has a [[ViewedArrayBuffer]] internal slot.- Let targetOffset be ?
ToIntegerOrInfinity (offset). - If targetOffset < 0, throw a
RangeError exception. - If source
is an Object that has a [[TypedArrayName]] internal slot, then- Perform ?
SetTypedArrayFromTypedArray (target, targetOffset, source).
- Perform ?
- Else,
- Perform ?
SetTypedArrayFromArrayLike (target, targetOffset, source).
- Perform ?
- Return
undefined .
This method is not generic. The
23.2.3.26.1 SetTypedArrayFromTypedArray ( target, targetOffset, source )
The abstract operation SetTypedArrayFromTypedArray takes arguments target (a
- Let targetBuffer be target.[[ViewedArrayBuffer]].
- Let targetRecord be
MakeTypedArrayWithBufferWitnessRecord (target,seq-cst ). - If
IsTypedArrayOutOfBounds (targetRecord) istrue , throw aTypeError exception. - Let targetLength be
TypedArrayLength (targetRecord). - Let srcBuffer be source.[[ViewedArrayBuffer]].
- Let srcRecord be
MakeTypedArrayWithBufferWitnessRecord (source,seq-cst ). - If
IsTypedArrayOutOfBounds (srcRecord) istrue , throw aTypeError exception. - Let srcLength be
TypedArrayLength (srcRecord). - Let targetType be
TypedArrayElementType (target). - Let targetElementSize be
TypedArrayElementSize (target). - Let targetByteOffset be target.[[ByteOffset]].
- Let srcType be
TypedArrayElementType (source). - Let srcElementSize be
TypedArrayElementSize (source). - Let srcByteOffset be source.[[ByteOffset]].
- If targetOffset = +∞, throw a
RangeError exception. - If srcLength + targetOffset > targetLength, throw a
RangeError exception. - If target.[[ContentType]] is not source.[[ContentType]], throw a
TypeError exception. - If
IsSharedArrayBuffer (srcBuffer) istrue ,IsSharedArrayBuffer (targetBuffer) istrue , and srcBuffer.[[ArrayBufferData]] is targetBuffer.[[ArrayBufferData]], let sameSharedArrayBuffer betrue ; otherwise, let sameSharedArrayBuffer befalse . - If
SameValue (srcBuffer, targetBuffer) istrue or sameSharedArrayBuffer istrue , then- Let srcByteLength be
TypedArrayByteLength (srcRecord). - Set srcBuffer to ?
CloneArrayBuffer (srcBuffer, srcByteOffset, srcByteLength). - Let srcByteIndex be 0.
- Let srcByteLength be
- Else,
- Let srcByteIndex be srcByteOffset.
- Let targetByteIndex be (targetOffset × targetElementSize) + targetByteOffset.
- Let limit be targetByteIndex + (targetElementSize × srcLength).
- If srcType is targetType, then
- NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
- Repeat, while targetByteIndex < limit,
- Let value be
GetValueFromBuffer (srcBuffer, srcByteIndex,uint8 ,true ,unordered ). - Perform
SetValueInBuffer (targetBuffer, targetByteIndex,uint8 , value,true ,unordered ). - Set srcByteIndex to srcByteIndex + 1.
- Set targetByteIndex to targetByteIndex + 1.
- Let value be
- Else,
- Repeat, while targetByteIndex < limit,
- Let value be
GetValueFromBuffer (srcBuffer, srcByteIndex, srcType,true ,unordered ). - Perform
SetValueInBuffer (targetBuffer, targetByteIndex, targetType, value,true ,unordered ). - Set srcByteIndex to srcByteIndex + srcElementSize.
- Set targetByteIndex to targetByteIndex + targetElementSize.
- Let value be
- Repeat, while targetByteIndex < limit,
- Return
unused .
23.2.3.26.2 SetTypedArrayFromArrayLike ( target, targetOffset, source )
The abstract operation SetTypedArrayFromArrayLike takes arguments target (a
- Let targetRecord be
MakeTypedArrayWithBufferWitnessRecord (target,seq-cst ). - If
IsTypedArrayOutOfBounds (targetRecord) istrue , throw aTypeError exception. - Let targetLength be
TypedArrayLength (targetRecord). - Let src be ?
ToObject (source). - Let srcLength be ?
LengthOfArrayLike (src). - If targetOffset = +∞, throw a
RangeError exception. - If srcLength + targetOffset > targetLength, throw a
RangeError exception. - Let k be 0.
- Repeat, while k < srcLength,
- Let Pk be !
ToString (𝔽 (k)). - Let value be ?
Get (src, Pk). - Let targetIndex be
𝔽 (targetOffset + k). - Perform ?
TypedArraySetElement (target, targetIndex, value). - Set k to k + 1.
- Let Pk be !
- Return
unused .
23.2.3.27 %TypedArray%.prototype.slice ( start, end )
The interpretation and use of the arguments of this method are the same as for Array.prototype.slice
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let srcArrayLength be
TypedArrayLength (taRecord). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart = -∞, let startIndex be 0.
- Else if relativeStart < 0, let startIndex be
max (srcArrayLength + relativeStart, 0). - Else, let startIndex be
min (relativeStart, srcArrayLength). - If end is
undefined , let relativeEnd be srcArrayLength; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd = -∞, let endIndex be 0.
- Else if relativeEnd < 0, let endIndex be
max (srcArrayLength + relativeEnd, 0). - Else, let endIndex be
min (relativeEnd, srcArrayLength). - Let countBytes be
max (endIndex - startIndex, 0). - Let A be ?
TypedArraySpeciesCreate (O, «𝔽 (countBytes) »). - If countBytes > 0, then
- Set taRecord to
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - If
IsTypedArrayOutOfBounds (taRecord) istrue , throw aTypeError exception. - Set endIndex to
min (endIndex,TypedArrayLength (taRecord)). - Set countBytes to
max (endIndex - startIndex, 0). - Let srcType be
TypedArrayElementType (O). - Let targetType be
TypedArrayElementType (A). - If srcType is targetType, then
- NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
- Let srcBuffer be O.[[ViewedArrayBuffer]].
- Let targetBuffer be A.[[ViewedArrayBuffer]].
- Let elementSize be
TypedArrayElementSize (O). - Let srcByteOffset be O.[[ByteOffset]].
- Let srcByteIndex be (startIndex × elementSize) + srcByteOffset.
- Let targetByteIndex be A.[[ByteOffset]].
- Let endByteIndex be targetByteIndex + (countBytes × elementSize).
- Repeat, while targetByteIndex < endByteIndex,
- Let value be
GetValueFromBuffer (srcBuffer, srcByteIndex,uint8 ,true ,unordered ). - Perform
SetValueInBuffer (targetBuffer, targetByteIndex,uint8 , value,true ,unordered ). - Set srcByteIndex to srcByteIndex + 1.
- Set targetByteIndex to targetByteIndex + 1.
- Let value be
- Else,
- Set taRecord to
- Return A.
This method is not generic. The
23.2.3.28 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of this method are the same as for Array.prototype.some
as defined in
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
false .
This method is not generic. The
23.2.3.29 %TypedArray%.prototype.sort ( comparefn )
This is a distinct method that, except as described below, implements the same requirements as those of Array.prototype.sort
as defined in
This method is not generic. The
It performs the following steps when called:
- If comparefn is not
undefined andIsCallable (comparefn) isfalse , throw aTypeError exception. - Let obj be the
this value. - Let taRecord be ?
ValidateTypedArray (obj,seq-cst ). - Let len be
TypedArrayLength (taRecord). - NOTE: The following closure performs a numeric comparison rather than the string comparison used in
23.1.3.30 . - Let SortCompare be a new
Abstract Closure with parameters (x, y) that captures comparefn and performs the following steps when called:- Return ?
CompareTypedArrayElements (x, y, comparefn).
- Return ?
- Let sortedList be ?
SortIndexedProperties (obj, len, SortCompare,read-through-holes ). - Let j be 0.
- Repeat, while j < len,
- Return obj.
Because
23.2.3.30 %TypedArray%.prototype.subarray ( start, end )
This method returns a new TypedArray whose element type is the element type of this TypedArray and whose ArrayBuffer is the ArrayBuffer of this TypedArray, referencing the elements in the
It performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- Let srcRecord be
MakeTypedArrayWithBufferWitnessRecord (O,seq-cst ). - If
IsTypedArrayOutOfBounds (srcRecord) istrue , then- Let srcLength be 0.
- Else,
- Let srcLength be
TypedArrayLength (srcRecord).
- Let srcLength be
- Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart = -∞, let startIndex be 0.
- Else if relativeStart < 0, let startIndex be
max (srcLength + relativeStart, 0). - Else, let startIndex be
min (relativeStart, srcLength). - Let elementSize be
TypedArrayElementSize (O). - Let srcByteOffset be O.[[ByteOffset]].
- Let beginByteOffset be srcByteOffset + (startIndex × elementSize).
- If O.[[ArrayLength]] is
auto and end isundefined , then- Let argumentsList be « buffer,
𝔽 (beginByteOffset) ».
- Let argumentsList be « buffer,
- Else,
- If end is
undefined , let relativeEnd be srcLength; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd = -∞, let endIndex be 0.
- Else if relativeEnd < 0, let endIndex be
max (srcLength + relativeEnd, 0). - Else, let endIndex be
min (relativeEnd, srcLength). - Let newLength be
max (endIndex - startIndex, 0). - Let argumentsList be « buffer,
𝔽 (beginByteOffset),𝔽 (newLength) ».
- If end is
- Return ?
TypedArraySpeciesCreate (O, argumentsList).
This method is not generic. The
23.2.3.31 %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
This is a distinct method that implements the same algorithm as Array.prototype.toLocaleString
as defined in
This method is not generic.
If the ECMAScript implementation includes the ECMA-402 Internationalization API this method is based upon the algorithm for Array.prototype.toLocaleString
that is in the ECMA-402 specification.
23.2.3.32 %TypedArray%.prototype.toReversed ( )
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let length be
TypedArrayLength (taRecord). - Let A be ?
TypedArrayCreateSameType (O, «𝔽 (length) »). - Let k be 0.
- Repeat, while k < length,
- Return A.
23.2.3.33 %TypedArray%.prototype.toSorted ( comparefn )
This method performs the following steps when called:
- If comparefn is not
undefined andIsCallable (comparefn) isfalse , throw aTypeError exception. - Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let A be ?
TypedArrayCreateSameType (O, «𝔽 (len) »). - NOTE: The following closure performs a numeric comparison rather than the string comparison used in
23.1.3.34 . - Let SortCompare be a new
Abstract Closure with parameters (x, y) that captures comparefn and performs the following steps when called:- Return ?
CompareTypedArrayElements (x, y, comparefn).
- Return ?
- Let sortedList be ?
SortIndexedProperties (O, len, SortCompare,read-through-holes ). - Let j be 0.
- Repeat, while j < len,
- Return A.
23.2.3.34 %TypedArray%.prototype.toString ( )
The initial value of the
23.2.3.35 %TypedArray%.prototype.values ( )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O,seq-cst ). - Return
CreateArrayIterator (O,value ).
23.2.3.36 %TypedArray%.prototype.with ( index, value )
This method performs the following steps when called:
- Let O be the
this value. - Let taRecord be ?
ValidateTypedArray (O,seq-cst ). - Let len be
TypedArrayLength (taRecord). - Let relativeIndex be ?
ToIntegerOrInfinity (index). - If relativeIndex ≥ 0, let actualIndex be relativeIndex.
- Else, let actualIndex be len + relativeIndex.
- If O.[[ContentType]] is
bigint , let numericValue be ?ToBigInt (value). - Else, let numericValue be ?
ToNumber (value). - If
IsValidIntegerIndex (O,𝔽 (actualIndex)) isfalse , throw aRangeError exception. - Let A be ?
TypedArrayCreateSameType (O, «𝔽 (len) »). - Let k be 0.
- Repeat, while k < len,
- Return A.
23.2.3.37 %TypedArray%.prototype [ @@iterator ] ( )
The initial value of the
23.2.3.38 get %TypedArray%.prototype [ @@toStringTag ]
.prototype[@@toStringTag]
is an
- Let O be the
this value. - If O
is not an Object , returnundefined . - If O does not have a [[TypedArrayName]] internal slot, return
undefined . - Let name be O.[[TypedArrayName]].
Assert : nameis a String .- Return name.
This property has the attributes { [[Enumerable]]:
The initial value of the
23.2.4 Abstract Operations for TypedArray Objects
23.2.4.1 TypedArraySpeciesCreate ( exemplar, argumentList )
The abstract operation TypedArraySpeciesCreate takes arguments exemplar (a
- Let defaultConstructor be the intrinsic object associated with the
constructor name exemplar.[[TypedArrayName]] inTable 71 . - Let constructor be ?
SpeciesConstructor (exemplar, defaultConstructor). - Let result be ?
TypedArrayCreateFromConstructor (constructor, argumentList). Assert : result has [[TypedArrayName]] and [[ContentType]] internal slots.- If result.[[ContentType]] is not exemplar.[[ContentType]], throw a
TypeError exception. - Return result.
23.2.4.2 TypedArrayCreateFromConstructor ( constructor, argumentList )
The abstract operation TypedArrayCreateFromConstructor takes arguments constructor (a
- Let newTypedArray be ?
Construct (constructor, argumentList). - Let taRecord be ?
ValidateTypedArray (newTypedArray,seq-cst ). - If the number of elements in argumentList is 1 and argumentList[0]
is a Number , then- If
IsTypedArrayOutOfBounds (taRecord) istrue , throw aTypeError exception. - Let length be
TypedArrayLength (taRecord). - If length <
ℝ (argumentList[0]), throw aTypeError exception.
- If
- Return newTypedArray.
23.2.4.3 TypedArrayCreateSameType ( exemplar, argumentList )
The abstract operation TypedArrayCreateSameType takes arguments exemplar (a
- Let constructor be the intrinsic object associated with the
constructor name exemplar.[[TypedArrayName]] inTable 71 . - Let result be ?
TypedArrayCreateFromConstructor (constructor, argumentList). Assert : result has [[TypedArrayName]] and [[ContentType]] internal slots.Assert : result.[[ContentType]] is exemplar.[[ContentType]].- Return result.
23.2.4.4 ValidateTypedArray ( O, order )
The abstract operation ValidateTypedArray takes arguments O (an
- Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let taRecord be
MakeTypedArrayWithBufferWitnessRecord (O, order). - If
IsTypedArrayOutOfBounds (taRecord) istrue , throw aTypeError exception. - Return taRecord.
23.2.4.5 TypedArrayElementSize ( O )
The abstract operation TypedArrayElementSize takes argument O (a
- Return the Element Size value specified in
Table 71 for O.[[TypedArrayName]].
23.2.4.6 TypedArrayElementType ( O )
The abstract operation TypedArrayElementType takes argument O (a
- Return the Element Type value specified in
Table 71 for O.[[TypedArrayName]].
23.2.4.7 CompareTypedArrayElements ( x, y, comparefn )
The abstract operation CompareTypedArrayElements takes arguments x (a Number or a BigInt), y (a Number or a BigInt), and comparefn (a
Assert : xis a Number and yis a Number , or xis a BigInt and yis a BigInt .- If comparefn is not
undefined , then - If x and y are both
NaN , return+0 𝔽. - If x is
NaN , return1 𝔽. - If y is
NaN , return-1 𝔽. - If x < y, return
-1 𝔽. - If x > y, return
1 𝔽. - If x is
-0 𝔽 and y is+0 𝔽, return-1 𝔽. - If x is
+0 𝔽 and y is-0 𝔽, return1 𝔽. - Return
+0 𝔽.
23.2.5 The TypedArray Constructors
Each TypedArray
- is an intrinsic object that has the structure described below, differing only in the name used as the
constructor name instead of TypedArray, inTable 71 . - is a function whose behaviour differs based upon the number and types of its arguments. The actual behaviour of a call of TypedArray depends upon the number and kind of arguments that are passed to it.
- is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value of an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified TypedArray behaviour must include asuper
call to the TypedArrayconstructor to create and initialize the subclass instance with the internal state necessary to support the%TypedArray% .prototype
built-in methods.
23.2.5.1 TypedArray ( ...args )
Each TypedArray
- If NewTarget is
undefined , throw aTypeError exception. - Let constructorName be the String value of the
Constructor Name value specified inTable 71 for this TypedArrayconstructor . - Let proto be
"%TypedArray.prototype%"
. - Let numberOfArgs be the number of elements in args.
- If numberOfArgs = 0, then
- Return ?
AllocateTypedArray (constructorName, NewTarget, proto, 0).
- Return ?
- Else,
- Let firstArgument be args[0].
- If firstArgument
is an Object , then- Let O be ?
AllocateTypedArray (constructorName, NewTarget, proto). - If firstArgument has a [[TypedArrayName]] internal slot, then
- Perform ?
InitializeTypedArrayFromTypedArray (O, firstArgument).
- Perform ?
- Else if firstArgument has an [[ArrayBufferData]] internal slot, then
- If numberOfArgs > 1, let byteOffset be args[1]; else let byteOffset be
undefined . - If numberOfArgs > 2, let length be args[2]; else let length be
undefined . - Perform ?
InitializeTypedArrayFromArrayBuffer (O, firstArgument, byteOffset, length).
- If numberOfArgs > 1, let byteOffset be args[1]; else let byteOffset be
- Else,
Assert : firstArgumentis an Object and firstArgument does not have either a [[TypedArrayName]] or an [[ArrayBufferData]] internal slot.- Let usingIterator be ?
GetMethod (firstArgument,@@iterator ). - If usingIterator is not
undefined , then- Let values be ?
IteratorToList (?GetIteratorFromMethod (firstArgument, usingIterator)). - Perform ?
InitializeTypedArrayFromList (O, values).
- Let values be ?
- Else,
- NOTE: firstArgument is not an Iterable so assume it is already an
array-like object . - Perform ?
InitializeTypedArrayFromArrayLike (O, firstArgument).
- NOTE: firstArgument is not an Iterable so assume it is already an
- Return O.
- Let O be ?
- Else,
Assert : firstArgumentis not an Object .- Let elementLength be ?
ToIndex (firstArgument). - Return ?
AllocateTypedArray (constructorName, NewTarget, proto, elementLength).
23.2.5.1.1 AllocateTypedArray ( constructorName, newTarget, defaultProto [ , length ] )
The abstract operation AllocateTypedArray takes arguments constructorName (a String which is the name of a
- Let proto be ?
GetPrototypeFromConstructor (newTarget, defaultProto). - Let obj be
TypedArrayCreate (proto). Assert : obj.[[ViewedArrayBuffer]] isundefined .- Set obj.[[TypedArrayName]] to constructorName.
- If constructorName is either
"BigInt64Array" or"BigUint64Array" , set obj.[[ContentType]] tobigint . - Otherwise, set obj.[[ContentType]] to
number . - If length is not present, then
- Set obj.[[ByteLength]] to 0.
- Set obj.[[ByteOffset]] to 0.
- Set obj.[[ArrayLength]] to 0.
- Else,
- Perform ?
AllocateTypedArrayBuffer (obj, length).
- Perform ?
- Return obj.
23.2.5.1.2 InitializeTypedArrayFromTypedArray ( O, srcArray )
The abstract operation InitializeTypedArrayFromTypedArray takes arguments O (a
- Let srcData be srcArray.[[ViewedArrayBuffer]].
- Let elementType be
TypedArrayElementType (O). - Let elementSize be
TypedArrayElementSize (O). - Let srcType be
TypedArrayElementType (srcArray). - Let srcElementSize be
TypedArrayElementSize (srcArray). - Let srcByteOffset be srcArray.[[ByteOffset]].
- Let srcRecord be
MakeTypedArrayWithBufferWitnessRecord (srcArray,seq-cst ). - If
IsTypedArrayOutOfBounds (srcRecord) istrue , throw aTypeError exception. - Let elementLength be
TypedArrayLength (srcRecord). - Let byteLength be elementSize × elementLength.
- If elementType is srcType, then
- Let data be ?
CloneArrayBuffer (srcData, srcByteOffset, byteLength).
- Let data be ?
- Else,
- Let data be ?
AllocateArrayBuffer (%ArrayBuffer% , byteLength). - If srcArray.[[ContentType]] is not O.[[ContentType]], throw a
TypeError exception. - Let srcByteIndex be srcByteOffset.
- Let targetByteIndex be 0.
- Let count be elementLength.
- Repeat, while count > 0,
- Let value be
GetValueFromBuffer (srcData, srcByteIndex, srcType,true ,unordered ). - Perform
SetValueInBuffer (data, targetByteIndex, elementType, value,true ,unordered ). - Set srcByteIndex to srcByteIndex + srcElementSize.
- Set targetByteIndex to targetByteIndex + elementSize.
- Set count to count - 1.
- Let value be
- Let data be ?
- Set O.[[ViewedArrayBuffer]] to data.
- Set O.[[ByteLength]] to byteLength.
- Set O.[[ByteOffset]] to 0.
- Set O.[[ArrayLength]] to elementLength.
- Return
unused .
23.2.5.1.3 InitializeTypedArrayFromArrayBuffer ( O, buffer, byteOffset, length )
The abstract operation InitializeTypedArrayFromArrayBuffer takes arguments O (a
- Let elementSize be
TypedArrayElementSize (O). - Let offset be ?
ToIndex (byteOffset). - If offset
modulo elementSize ≠ 0, throw aRangeError exception. - Let bufferIsFixedLength be
IsFixedLengthArrayBuffer (buffer). - If length is not
undefined , then- Let newLength be ?
ToIndex (length).
- Let newLength be ?
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let bufferByteLength be
ArrayBufferByteLength (buffer,seq-cst ). - If length is
undefined and bufferIsFixedLength isfalse , then- If offset > bufferByteLength, throw a
RangeError exception. - Set O.[[ByteLength]] to
auto . - Set O.[[ArrayLength]] to
auto .
- If offset > bufferByteLength, throw a
- Else,
- If length is
undefined , then- If bufferByteLength
modulo elementSize ≠ 0, throw aRangeError exception. - Let newByteLength be bufferByteLength - offset.
- If newByteLength < 0, throw a
RangeError exception.
- If bufferByteLength
- Else,
- Let newByteLength be newLength × elementSize.
- If offset + newByteLength > bufferByteLength, throw a
RangeError exception.
- Set O.[[ByteLength]] to newByteLength.
- Set O.[[ArrayLength]] to newByteLength / elementSize.
- If length is
- Set O.[[ViewedArrayBuffer]] to buffer.
- Set O.[[ByteOffset]] to offset.
- Return
unused .
23.2.5.1.4 InitializeTypedArrayFromList ( O, values )
The abstract operation InitializeTypedArrayFromList takes arguments O (a
- Let len be the number of elements in values.
- Perform ?
AllocateTypedArrayBuffer (O, len). - Let k be 0.
- Repeat, while k < len,
Assert : values is now an emptyList .- Return
unused .
23.2.5.1.5 InitializeTypedArrayFromArrayLike ( O, arrayLike )
The abstract operation InitializeTypedArrayFromArrayLike takes arguments O (a
- Let len be ?
LengthOfArrayLike (arrayLike). - Perform ?
AllocateTypedArrayBuffer (O, len). - Let k be 0.
- Repeat, while k < len,
- Return
unused .
23.2.5.1.6 AllocateTypedArrayBuffer ( O, length )
The abstract operation AllocateTypedArrayBuffer takes arguments O (a
Assert : O.[[ViewedArrayBuffer]] isundefined .- Let elementSize be
TypedArrayElementSize (O). - Let byteLength be elementSize × length.
- Let data be ?
AllocateArrayBuffer (%ArrayBuffer% , byteLength). - Set O.[[ViewedArrayBuffer]] to data.
- Set O.[[ByteLength]] to byteLength.
- Set O.[[ByteOffset]] to 0.
- Set O.[[ArrayLength]] to length.
- Return
unused .
23.2.6 Properties of the TypedArray Constructors
Each TypedArray
- has a [[Prototype]] internal slot whose value is
%TypedArray% . - has a
"length" property whose value is3 𝔽. - has a
"name" property whose value is the String value of theconstructor name specified for it inTable 71 . - has the following properties:
23.2.6.1 TypedArray.BYTES_PER_ELEMENT
The value of TypedArray.BYTES_PER_ELEMENT
is the Element Size value specified in
This property has the attributes { [[Writable]]:
23.2.6.2 TypedArray.prototype
The initial value of TypedArray.prototype
is the corresponding TypedArray prototype intrinsic object (
This property has the attributes { [[Writable]]:
23.2.7 Properties of the TypedArray Prototype Objects
Each TypedArray prototype object:
- has a [[Prototype]] internal slot whose value is
%TypedArray.prototype% . - is an
ordinary object . - does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.
23.2.7.1 TypedArray.prototype.BYTES_PER_ELEMENT
The value of TypedArray.prototype.BYTES_PER_ELEMENT
is the Element Size value specified in
This property has the attributes { [[Writable]]:
23.2.7.2 TypedArray.prototype.constructor
The initial value of the
23.2.8 Properties of TypedArray Instances
TypedArray instances are