23.2.3 %TypedArray% 原型对象的属性
%TypedArray% 原型对象:
- 具有 [[Prototype]] 内部槽,其值为 %Object.prototype%。
- 是 %TypedArray.prototype%。
- 是一个普通对象。
- 没有 [[ViewedArrayBuffer]] 或其他特定于 TypedArray 实例对象的内部槽。
23.2.3.1 %TypedArray%.prototype.at ( index )
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 relativeIndex 为 ? ToIntegerOrInfinity(index)。
- 若 relativeIndex ≥ 0,则
- 令 k = relativeIndex。
- 否则,
- 令 k = len + relativeIndex。
- 若 k < 0 或 k ≥ len,返回 undefined。
- 返回 ! Get(O, ! ToString(𝔽(k)))。
23.2.3.2 get %TypedArray%.prototype.buffer
%TypedArray%.prototype.buffer 是一个存取器属性,其 set 访问器函数为 undefined。其 get 访问器函数被调用时执行:
- 令 O 为 this 值。
- 执行 ? RequireInternalSlot(O, [[TypedArrayName]])。
- 断言:O 具有 [[ViewedArrayBuffer]] 内部槽。
- 令 buffer 为 O.[[ViewedArrayBuffer]]。
- 返回 buffer。
23.2.3.3 get %TypedArray%.prototype.byteLength
%TypedArray%.prototype.byteLength 是一个存取器属性,其 set 访问器函数为 undefined。其 get 访问器函数被调用时执行:
- 令 O 为 this 值。
- 执行 ? RequireInternalSlot(O, [[TypedArrayName]])。
- 断言:O 拥有 [[ViewedArrayBuffer]] 内部槽。
- 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 如果 IsTypedArrayOutOfBounds(taRecord) 为 true,返回 +0𝔽。
- 令 size 为 TypedArrayByteLength(taRecord)。
- 返回 𝔽(size)。
23.2.3.4 get %TypedArray%.prototype.byteOffset
%TypedArray%.prototype.byteOffset 是一个存取器属性,其 set 访问器函数为 undefined。其 get 访问器函数被调用时执行:
- 令 O 为 this 值。
- 执行 ? RequireInternalSlot(O, [[TypedArrayName]])。
- 断言:O 具有 [[ViewedArrayBuffer]] 内部槽。
- 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 若 IsTypedArrayOutOfBounds(taRecord) 为 true,返回 +0𝔽。
- 令 offset 为 O.[[ByteOffset]]。
- 返回 𝔽(offset)。
23.2.3.5 %TypedArray%.prototype.constructor
%TypedArray%.prototype.constructor 的初始值为 %TypedArray%。
23.2.3.6 %TypedArray%.prototype.copyWithin ( target, start [ , end ] )
此方法参数的解释与用法与 23.1.3.4 中定义的 Array.prototype.copyWithin 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 relativeTarget 为 ? ToIntegerOrInfinity(target)。
- 如果 relativeTarget = -∞,则令 targetIndex 为 0。
- 否则如果 relativeTarget < 0,则令 targetIndex 为 max(len + relativeTarget, 0)。
- 否则,令 targetIndex 为 min(relativeTarget, len)。
- 令 relativeStart 为 ? ToIntegerOrInfinity(start)。
- 如果 relativeStart = -∞,则令 startIndex 为 0。
- 否则如果 relativeStart < 0,则令 startIndex 为 max(len + relativeStart, 0)。
- 否则,令 startIndex 为 min(relativeStart, len)。
- 如果 end 为 undefined,则令 relativeEnd 为 len;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
- 如果 relativeEnd = -∞,则令 endIndex 为 0。
- 否则如果 relativeEnd < 0,则令 endIndex 为 max(len + relativeEnd, 0)。
- 否则,令 endIndex 为 min(relativeEnd, len)。
- 令 count 为 min(endIndex - startIndex, len - targetIndex)。
- 如果 count > 0,则
- 注:复制操作必须以保持源数据比特级编码的方式进行。
- 令 buffer 为 O.[[ViewedArrayBuffer]]。
- 将 taRecord 设为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 如果 IsTypedArrayOutOfBounds(taRecord) 为 true,抛出 TypeError 异常。
- 将 len 设为 TypedArrayLength(taRecord)。
- 注:上述步骤的副作用可能会缩小 O 的大小,此时应以仍然适用的最长前缀进行复制。
- 将 count 设为 min(count, len - startIndex, len - targetIndex)。
- 令 elementSize 为 TypedArrayElementSize(O)。
- 令 byteOffset 为 O.[[ByteOffset]]。
- 令 toByteIndex 为 (targetIndex × elementSize) + byteOffset。
- 令 fromByteIndex 为 (startIndex × elementSize) + byteOffset。
- 令 countBytes 为 count × elementSize。
- 如果 fromByteIndex < toByteIndex 并且 toByteIndex < fromByteIndex + countBytes,则
- 令 direction 为 -1。
- 将 fromByteIndex 设为 fromByteIndex + countBytes - 1。
- 将 toByteIndex 设为 toByteIndex + countBytes - 1。
- 否则,
- 令 direction 为 1。
- 重复,条件为 countBytes > 0,
- 令 value 为 GetValueFromBuffer(buffer, fromByteIndex, uint8, true, unordered)。
- 执行 SetValueInBuffer(buffer, toByteIndex, uint8, value, true, unordered)。
- 将 fromByteIndex 设为 fromByteIndex + direction。
- 将 toByteIndex 设为 toByteIndex + direction。
- 将 countBytes 设为 countBytes - 1。
- 返回 O。
23.2.3.7 %TypedArray%.prototype.entries ( )
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 执行 ? ValidateTypedArray(O, seq-cst)。
- 返回 CreateArrayIterator(O, key+value)。
23.2.3.8 %TypedArray%.prototype.every ( callback [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.6 中的 Array.prototype.every 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 令 k = 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 令 testResult 为 ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), O »))。
- 若 testResult 为 false,返回 false。
- 设 k = k + 1。
- 返回 true。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.9 %TypedArray%.prototype.fill ( value [ , start [ , end ] ] )
该方法参数的解释与用法与 23.1.3.7 中的 Array.prototype.fill 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 O.[[ContentType]] 为 bigint,设 value 为 ? ToBigInt(value)。
- 否则,设 value 为 ? ToNumber(value)。
- 令 relativeStart 为 ? ToIntegerOrInfinity(start)。
- 若 relativeStart = -∞,令 startIndex = 0。
- 否则若 relativeStart < 0,令 startIndex = max(len + relativeStart, 0)。
- 否则令 startIndex = min(relativeStart, len)。
- 若 end 为 undefined,令 relativeEnd = len;否则令 relativeEnd = ? ToIntegerOrInfinity(end)。
- 若 relativeEnd = -∞,令 endIndex = 0。
- 否则若 relativeEnd < 0,令 endIndex = max(len + relativeEnd, 0)。
- 否则令 endIndex = min(relativeEnd, len)。
- 设 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 若 IsTypedArrayOutOfBounds(taRecord) 为 true,抛出 TypeError 异常。
- 设 len 为 TypedArrayLength(taRecord)。
- 设 endIndex = min(endIndex, len)。
- 令 k = startIndex。
- 当 k < endIndex 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 执行 ! Set(O, Pk, value, true)。
- 设 k = k + 1。
- 返回 O。
23.2.3.10 %TypedArray%.prototype.filter ( callback [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.8 中的 Array.prototype.filter 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 令 kept 为一个新的空列表。
- 令 captured 为 0。
- 令 k 为 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 令 selected = ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), O »))。
- 若 selected 为 true,则
- 将 kValue 追加到 kept。
- 设 captured = captured + 1。
- 设 k = k + 1。
- 令 A 为 ? TypedArraySpeciesCreate(O, « 𝔽(captured) »)。
- 令 n = 0。
- 对 kept 中每个元素 e:
- 执行 ! Set(A, ! ToString(𝔽(n)), e, true)。
- 设 n = n + 1。
- 返回 A。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.11 %TypedArray%.prototype.find ( predicate [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.9 中的 Array.prototype.find 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 findRec 为 ? FindViaPredicate(O, len, ascending, predicate, thisArg)。
- 返回 findRec.[[Value]]。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.12 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.10 中的 Array.prototype.findIndex 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 findRec 为 ? FindViaPredicate(O, len, ascending, predicate, thisArg)。
- 返回 findRec.[[Index]]。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.13 %TypedArray%.prototype.findLast ( predicate [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.11 中的 Array.prototype.findLast 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 findRec 为 ? FindViaPredicate(O, len, descending, predicate, thisArg)。
- 返回 findRec.[[Value]]。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.14 %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.12 中的 Array.prototype.findLastIndex 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 findRec 为 ? FindViaPredicate(O, len, descending, predicate, thisArg)。
- 返回 findRec.[[Index]]。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.15 %TypedArray%.prototype.forEach ( callback [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.15 中的 Array.prototype.forEach 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 令 k = 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 执行 ? Call(callback, thisArg, « kValue, 𝔽(k), O »)。
- 设 k = k + 1。
- 返回 undefined。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.16 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
该方法参数的解释与用法与 23.1.3.16 中的 Array.prototype.includes 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 len = 0,返回 false。
- 令 n 为 ? ToIntegerOrInfinity(fromIndex)。
- 断言:若 fromIndex 为 undefined,则 n 为 0。
- 若 n = +∞,返回 false。
- 否则若 n = -∞,设 n = 0。
- 若 n ≥ 0,则
- 令 k = n。
- 否则,
- 令 k = len + n。
- 若 k < 0,设 k = 0。
- 当 k < len 重复,
- 令 elementK 为 ! Get(O, ! ToString(𝔽(k)))。
- 若 SameValueZero(searchElement, elementK) 为 true,返回 true。
- 设 k = k + 1。
- 返回 false。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.17 %TypedArray%.prototype.indexOf ( searchElement [ , fromIndex ] )
该方法参数的解释与用法与 23.1.3.17 中的 Array.prototype.indexOf 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 len = 0,返回 -1𝔽。
- 令 n 为 ? ToIntegerOrInfinity(fromIndex)。
- 断言:若 fromIndex 为 undefined,则 n 为 0。
- 若 n = +∞,返回 -1𝔽。
- 否则若 n = -∞,设 n = 0。
- 若 n ≥ 0,则
- 令 k = n。
- 否则,
- 令 k = len + n。
- 若 k < 0,设 k = 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kPresent = ! HasProperty(O, Pk)。
- 若 kPresent 为 true,则
- 令 elementK = ! Get(O, Pk)。
- 若 IsStrictlyEqual(searchElement, elementK) 为 true,返回 𝔽(k)。
- 设 k = k + 1。
- 返回 -1𝔽。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.18 %TypedArray%.prototype.join ( separator )
该方法参数的解释与用法与 23.1.3.18 中的 Array.prototype.join 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 separator 为 undefined,令 sep = ","。
- 否则,令 sep = ? ToString(separator)。
- 令 R 为空字符串。
- 令 k = 0。
- 当 k < len 重复,
- 若 k > 0,设 R 为 R 与 sep 的字符串拼接。
- 令 element 为 ! Get(O, ! ToString(𝔽(k)))。
- 若 element 不为 undefined,则
- 令 S 为 ! ToString(element)。
- 设 R 为 R 与 S 的字符串拼接。
- 设 k = k + 1。
- 返回 R。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.19 %TypedArray%.prototype.keys ( )
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 执行 ? ValidateTypedArray(O, seq-cst)。
- 返回 CreateArrayIterator(O, key)。
23.2.3.20 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
该方法参数的解释与用法与 23.1.3.20 中的 Array.prototype.lastIndexOf 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 len = 0,返回 -1𝔽。
- 若提供 fromIndex,令 n 为 ? ToIntegerOrInfinity(fromIndex);否则令 n = len - 1。
- 若 n = -∞,返回 -1𝔽。
- 若 n ≥ 0,则
- 令 k = min(n, len - 1)。
- 否则,
- 令 k = len + n。
- 当 k ≥ 0 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kPresent = ! HasProperty(O, Pk)。
- 若 kPresent 为 true,则
- 令 elementK = ! Get(O, Pk)。
- 若 IsStrictlyEqual(searchElement, elementK) 为 true,返回 𝔽(k)。
- 设 k = k - 1。
- 返回 -1𝔽。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.21 get %TypedArray%.prototype.length
%TypedArray%.prototype.length 是一个存取器属性,其 set 访问器函数为 undefined。其 get 访问器函数被调用时执行:
- 令 O 为 this 值。
- 执行 ? RequireInternalSlot(O, [[TypedArrayName]])。
- 断言:O 具有 [[ViewedArrayBuffer]] 与 [[ArrayLength]] 内部槽。
- 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 若 IsTypedArrayOutOfBounds(taRecord) 为 true,返回 +0𝔽。
- 令 length 为 TypedArrayLength(taRecord)。
- 返回 𝔽(length)。
该函数不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.22 %TypedArray%.prototype.map ( callback [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.21 中的 Array.prototype.map 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 令 A 为 ? TypedArraySpeciesCreate(O, « 𝔽(len) »)。
- 令 k = 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 令 mappedValue = ? Call(callback, thisArg, « kValue, 𝔽(k), O »)。
- 执行 ? Set(A, Pk, mappedValue, true)。
- 设 k = k + 1。
- 返回 A。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.23 %TypedArray%.prototype.reduce ( callback [ , initialValue ] )
该方法参数的解释与用法与 23.1.3.24 中的 Array.prototype.reduce 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 若 len = 0 且 initialValue 不存在,抛出 TypeError 异常。
- 令 k = 0。
- 令 accumulator = undefined。
- 若存在 initialValue,则
- 设 accumulator = initialValue。
- 否则,
- 令 Pk = ! ToString(𝔽(k))。
- 设 accumulator = ! Get(O, Pk)。
- 设 k = k + 1。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 设 accumulator = ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), O »)。
- 设 k = k + 1。
- 返回 accumulator。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.24 %TypedArray%.prototype.reduceRight ( callback [ , initialValue ] )
该方法参数的解释与用法与 23.1.3.25 中的 Array.prototype.reduceRight 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 若 len = 0 且 initialValue 不存在,抛出 TypeError 异常。
- 令 k = len - 1。
- 令 accumulator = undefined。
- 若存在 initialValue,则
- 设 accumulator = initialValue。
- 否则,
- 令 Pk = ! ToString(𝔽(k))。
- 设 accumulator = ! Get(O, Pk)。
- 设 k = k - 1。
- 当 k ≥ 0 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 设 accumulator = ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), O »)。
- 设 k = k - 1。
- 返回 accumulator。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.25 %TypedArray%.prototype.reverse ( )
该方法参数的解释与用法与 23.1.3.26 中的 Array.prototype.reverse 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 middle = floor(len / 2)。
- 令 lower = 0。
- 当 lower ≠ middle 重复,
- 令 upper = len - lower - 1。
- 令 upperP = ! ToString(𝔽(upper))。
- 令 lowerP = ! ToString(𝔽(lower))。
- 令 lowerValue = ! Get(O, lowerP)。
- 令 upperValue = ! Get(O, upperP)。
- 执行 ! Set(O, lowerP, upperValue, true)。
- 执行 ! Set(O, upperP, lowerValue, true)。
- 设 lower = lower + 1。
- 返回 O。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.26 %TypedArray%.prototype.set ( source [ , offset ] )
该方法在此 TypedArray 中设置多个值,从 source 读取这些值。具体细节依据 source 的类型不同而不同。可选的 offset 指定开始写入的首个元素索引。如省略则视为 0。
其被调用时执行以下步骤:
- 令 target 为 this 值。
- 执行 ? RequireInternalSlot(target, [[TypedArrayName]])。
- 断言:target 具有 [[ViewedArrayBuffer]] 内部槽。
- 令 targetOffset 为 ? ToIntegerOrInfinity(offset)。
- 若 targetOffset < 0,抛出 RangeError 异常。
- 若 source 是具有 [[TypedArrayName]] 内部槽的对象,则
- 执行 ? SetTypedArrayFromTypedArray(target, targetOffset, source)。
- 否则,
- 执行 ? SetTypedArrayFromArrayLike(target, targetOffset, source)。
- 返回 undefined。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.26.1 SetTypedArrayFromArrayLike ( target, targetOffset, source )
The abstract operation SetTypedArrayFromArrayLike takes arguments target (a TypedArray), targetOffset (a non-negative integer or +∞), and source (an ECMAScript language value, but not a TypedArray) and returns 返回 unused 的正常完成或抛出完成. 从 source 读取值并自 targetOffset 开始在 target 中设置多个值。 It performs the following steps when called:
- 令 targetRecord 为 MakeTypedArrayWithBufferWitnessRecord(target, seq-cst)。
- 若 IsTypedArrayOutOfBounds(targetRecord) 为 true,抛出 TypeError 异常。
- 令 targetLength 为 TypedArrayLength(targetRecord)。
- 令 src 为 ? ToObject(source)。
- 令 srcLength 为 ? LengthOfArrayLike(src)。
- 若 targetOffset = +∞,抛出 RangeError 异常。
- 若 srcLength + targetOffset > targetLength,抛出 RangeError 异常。
- 令 k = 0。
- 当 k < srcLength 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 value = ? Get(src, Pk)。
- 令 targetIndex = 𝔽(targetOffset + k)。
- 执行 ? TypedArraySetElement(target, targetIndex, value)。
- 设 k = k + 1。
- 返回 unused。
23.2.3.26.2 SetTypedArrayFromTypedArray ( target, targetOffset, source )
The abstract operation SetTypedArrayFromTypedArray takes arguments target (a TypedArray), targetOffset (a non-negative integer or +∞), and source (a TypedArray) and returns 返回 unused 的正常完成或抛出完成. 从 source 读取值并自 targetOffset 开始在 target 中设置多个值。 It performs the following steps when called:
- 令 targetBuffer 为 target.[[ViewedArrayBuffer]]。
- 令 targetRecord 为 MakeTypedArrayWithBufferWitnessRecord(target, seq-cst)。
- 若 IsTypedArrayOutOfBounds(targetRecord) 为 true,抛出 TypeError 异常。
- 令 targetLength 为 TypedArrayLength(targetRecord)。
- 令 srcBuffer 为 source.[[ViewedArrayBuffer]]。
- 令 srcRecord 为 MakeTypedArrayWithBufferWitnessRecord(source, seq-cst)。
- 若 IsTypedArrayOutOfBounds(srcRecord) 为 true,抛出 TypeError 异常。
- 令 srcLength 为 TypedArrayLength(srcRecord)。
- 令 targetType 为 TypedArrayElementType(target)。
- 令 targetElementSize 为 TypedArrayElementSize(target)。
- 令 targetByteOffset 为 target.[[ByteOffset]]。
- 令 srcType 为 TypedArrayElementType(source)。
- 令 srcElementSize 为 TypedArrayElementSize(source)。
- 令 srcByteOffset 为 source.[[ByteOffset]]。
- 若 targetOffset = +∞,抛出 RangeError 异常。
- 若 srcLength + targetOffset > targetLength,抛出 RangeError 异常。
- 若 target.[[ContentType]] 不等于 source.[[ContentType]],抛出 TypeError 异常。
- 若 IsSharedArrayBuffer(srcBuffer) 为 true、IsSharedArrayBuffer(targetBuffer) 为 true 且 srcBuffer.[[ArrayBufferData]] 为 targetBuffer.[[ArrayBufferData]],令 sameSharedArrayBuffer = true;否则令 sameSharedArrayBuffer = false。
- 若 SameValue(srcBuffer, targetBuffer) 为 true 或 sameSharedArrayBuffer 为 true,则
- 令 srcByteLength 为 TypedArrayByteLength(srcRecord)。
- 设 srcBuffer 为 ? CloneArrayBuffer(srcBuffer, srcByteOffset, srcByteLength)。
- 令 srcByteIndex = 0。
- 否则,
- 令 srcByteIndex = srcByteOffset。
- 令 targetByteIndex = (targetOffset × targetElementSize) + targetByteOffset。
- 令 limit = targetByteIndex + (targetElementSize × srcLength)。
- 若 srcType 是 targetType,则
- 注:复制必须保持源数据的位级编码。
- 当 targetByteIndex < limit 重复,
- 令 value 为 GetValueFromBuffer(srcBuffer, srcByteIndex, uint8, true, unordered)。
- 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
- 设 srcByteIndex = srcByteIndex + 1。
- 设 targetByteIndex = targetByteIndex + 1。
- 否则,
- 当 targetByteIndex < limit 重复,
- 令 value 为 GetValueFromBuffer(srcBuffer, srcByteIndex, srcType, true, unordered)。
- 执行 SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, unordered)。
- 设 srcByteIndex = srcByteIndex + srcElementSize。
- 设 targetByteIndex = targetByteIndex + targetElementSize。
- 返回 unused。
23.2.3.27 %TypedArray%.prototype.slice ( start, end )
该方法参数的解释与用法与 23.1.3.28 中的 Array.prototype.slice 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 srcArrayLength 为 TypedArrayLength(taRecord)。
- 令 relativeStart 为 ? ToIntegerOrInfinity(start)。
- 若 relativeStart = -∞,令 startIndex = 0。
- 否则若 relativeStart < 0,令 startIndex = max(srcArrayLength + relativeStart, 0)。
- 否则令 startIndex = min(relativeStart, srcArrayLength)。
- 若 end 为 undefined,令 relativeEnd = srcArrayLength;否则令 relativeEnd = ? ToIntegerOrInfinity(end)。
- 若 relativeEnd = -∞,令 endIndex = 0。
- 否则若 relativeEnd < 0,令 endIndex = max(srcArrayLength + relativeEnd, 0)。
- 否则令 endIndex = min(relativeEnd, srcArrayLength)。
- 令 countBytes = max(endIndex - startIndex, 0)。
- 令 A 为 ? TypedArraySpeciesCreate(O, « 𝔽(countBytes) »)。
- 若 countBytes > 0,则
- 设 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 若 IsTypedArrayOutOfBounds(taRecord) 为 true,抛出 TypeError 异常。
- 设 endIndex = min(endIndex, TypedArrayLength(taRecord))。
- 设 countBytes = max(endIndex - startIndex, 0)。
- 令 srcType = TypedArrayElementType(O)。
- 令 targetType = TypedArrayElementType(A)。
- 若 srcType 是 targetType,则
- 注:传输必须保持源数据的位级编码。
- 令 srcBuffer = O.[[ViewedArrayBuffer]]。
- 令 targetBuffer = A.[[ViewedArrayBuffer]]。
- 令 elementSize = TypedArrayElementSize(O)。
- 令 srcByteOffset = O.[[ByteOffset]]。
- 令 srcByteIndex = (startIndex × elementSize) + srcByteOffset。
- 令 targetByteIndex = A.[[ByteOffset]]。
- 令 endByteIndex = targetByteIndex + (countBytes × elementSize)。
- 当 targetByteIndex < endByteIndex 重复,
- 令 value 为 GetValueFromBuffer(srcBuffer, srcByteIndex, uint8, true, unordered)。
- 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
- 设 srcByteIndex = srcByteIndex + 1。
- 设 targetByteIndex = targetByteIndex + 1。
- 否则,
- 令 n = 0。
- 令 k = startIndex。
- 当 k < endIndex 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 执行 ! Set(A, ! ToString(𝔽(n)), kValue, true)。
- 设 k = k + 1。
- 设 n = n + 1。
- 返回 A。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.28 %TypedArray%.prototype.some ( callback [ , thisArg ] )
该方法参数的解释与用法与 23.1.3.29 中的 Array.prototype.some 相同。
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 若 IsCallable(callback) 为 false,抛出 TypeError 异常。
- 令 k = 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 令 kValue = ! Get(O, Pk)。
- 令 testResult = ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), O »))。
- 若 testResult 为 true,返回 true。
- 设 k = k + 1。
- 返回 false。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.29 %TypedArray%.prototype.sort ( comparator )
这是一个不同的方法;除下面描述的情况外,其实现与 23.1.3.30 中 Array.prototype.sort 的要求相同。该方法的实现可以利用 this 值是长度固定且其整数索引属性非稀疏的对象这一知识进行优化。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
其被调用时执行以下步骤:
- 若 comparator 不为 undefined 且 IsCallable(comparator) 为 false,抛出 TypeError 异常。
- 令 obj 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 注:以下闭包执行数值比较而非 23.1.3.30 中使用的字符串比较。
- 令 SortCompare 为具有参数 (x, y) 并捕获 comparator 的新抽象闭包,调用时执行:
- 返回 ? CompareTypedArrayElements(x, y, comparator)。
- 令 sortedList 为 ? SortIndexedProperties(obj, len, SortCompare, read-through-holes)。
- 令 j = 0。
- 当 j < len 重复,
- 执行 ! Set(obj, ! ToString(𝔽(j)), sortedList[j], true)。
- 设 j = j + 1。
- 返回 obj。
Note
因为 NaN 总是比较大于任何其他值(见 CompareTypedArrayElements),当未提供 comparator 时,NaN 属性值总是排在结果末尾。
23.2.3.30 %TypedArray%.prototype.subarray ( start, end )
该方法返回一个新的 TypedArray,其元素类型与该 TypedArray 相同,其 ArrayBuffer 为该 TypedArray 的 ArrayBuffer,引用从 start(含)到 end(不含)区间的元素。若 start 或 end 为负值,则表示自数组末尾起的索引。
其被调用时执行以下步骤:
- 令 O 为 this 值。
- 执行 ? RequireInternalSlot(O, [[TypedArrayName]])。
- 断言:O 具有 [[ViewedArrayBuffer]] 内部槽。
- 令 buffer 为 O.[[ViewedArrayBuffer]]。
- 令 srcRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, seq-cst)。
- 若 IsTypedArrayOutOfBounds(srcRecord) 为 true,则
- 令 srcLength = 0。
- 否则,
- 令 srcLength = TypedArrayLength(srcRecord)。
- 令 relativeStart 为 ? ToIntegerOrInfinity(start)。
- 若 relativeStart = -∞,令 startIndex = 0。
- 否则若 relativeStart < 0,令 startIndex = max(srcLength + relativeStart, 0)。
- 否则令 startIndex = min(relativeStart, srcLength)。
- 令 elementSize 为 TypedArrayElementSize(O)。
- 令 srcByteOffset 为 O.[[ByteOffset]]。
- 令 beginByteOffset = srcByteOffset + (startIndex × elementSize)。
- 若 O.[[ArrayLength]] 为 auto 且 end 为 undefined,则
- 令 argumentsList 为 « buffer, 𝔽(beginByteOffset) »。
- 否则,
- 若 end 为 undefined,令 relativeEnd = srcLength;否则令 relativeEnd = ? ToIntegerOrInfinity(end)。
- 若 relativeEnd = -∞,令 endIndex = 0。
- 否则若 relativeEnd < 0,令 endIndex = max(srcLength + relativeEnd, 0)。
- 否则令 endIndex = min(relativeEnd, srcLength)。
- 令 newLength = max(endIndex - startIndex, 0)。
- 令 argumentsList = « buffer, 𝔽(beginByteOffset), 𝔽(newLength) »。
- 返回 ? TypedArraySpeciesCreate(O, argumentsList)。
该方法不是泛型。this 值必须为具有 [[TypedArrayName]] 内部槽的对象。
23.2.3.31 %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
这是一个不同的方法,实现与 23.1.3.32 中 Array.prototype.toLocaleString 的算法相同,只是用 TypedArrayLength 取代对 "length" 的 [[Get]]。在底层缓冲区不可调整大小且整数索引属性非稀疏时,实现可以利用 this 值长度固定的知识进行优化。然而,该优化不得引入任何可观测的指定行为变化。
该方法不是泛型。在求值算法前,使用 this 值与 seq-cst 作为参数调用 ValidateTypedArray。若其结果为异常完成,则抛出该异常而不再执行算法。
Note
若 ECMAScript 实现包含 ECMA-402 国际化 API,则该方法基于 ECMA-402 规范中 Array.prototype.toLocaleString 的算法。
23.2.3.32 %TypedArray%.prototype.toReversed ( )
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 A 为 ? TypedArrayCreateSameType(O, len)。
- 令 k = 0。
- 当 k < len 重复,
- 令 from = ! ToString(𝔽(len - k - 1))。
- 令 Pk = ! ToString(𝔽(k))。
- 令 fromValue = ! Get(O, from)。
- 执行 ! Set(A, Pk, fromValue, true)。
- 设 k = k + 1。
- 返回 A。
23.2.3.33 %TypedArray%.prototype.toSorted ( comparator )
该方法被调用时执行以下步骤:
- 若 comparator 不为 undefined 且 IsCallable(comparator) 为 false,抛出 TypeError 异常。
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 A 为 ? TypedArrayCreateSameType(O, len)。
- 注:以下闭包执行数值比较,而非 23.1.3.34 中使用的字符串比较。
- 令 SortCompare 为具有参数 (x, y) 并捕获 comparator 的新抽象闭包,调用时执行:
- 返回 ? CompareTypedArrayElements(x, y, comparator)。
- 令 sortedList 为 ? SortIndexedProperties(O, len, SortCompare, read-through-holes)。
- 令 j = 0。
- 当 j < len 重复,
- 执行 ! Set(A, ! ToString(𝔽(j)), sortedList[j], true)。
- 设 j = j + 1。
- 返回 A。
23.2.3.34 %TypedArray%.prototype.toString ( )
"toString" 属性的初始值为 %Array.prototype.toString%,定义见 23.1.3.36。
23.2.3.35 %TypedArray%.prototype.values ( )
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 执行 ? ValidateTypedArray(O, seq-cst)。
- 返回 CreateArrayIterator(O, value)。
23.2.3.36 %TypedArray%.prototype.with ( index, value )
该方法被调用时执行以下步骤:
- 令 O 为 this 值。
- 令 taRecord 为 ? ValidateTypedArray(O, seq-cst)。
- 令 len 为 TypedArrayLength(taRecord)。
- 令 relativeIndex 为 ? ToIntegerOrInfinity(index)。
- 若 relativeIndex ≥ 0,令 actualIndex = relativeIndex。
- 否则,令 actualIndex = len + relativeIndex。
- 若 O.[[ContentType]] 为 bigint,令 numericValue = ? ToBigInt(value)。
- 否则,令 numericValue = ? ToNumber(value)。
- 若 IsValidIntegerIndex(O, 𝔽(actualIndex)) 为 false,抛出 RangeError 异常。
- 令 A 为 ? TypedArrayCreateSameType(O, len)。
- 令 k = 0。
- 当 k < len 重复,
- 令 Pk = ! ToString(𝔽(k))。
- 若 k = actualIndex,令 fromValue = numericValue;否则令 fromValue = ! Get(O, Pk)。
- 执行 ! Set(A, Pk, fromValue, true)。
- 设 k = k + 1。
- 返回 A。
23.2.3.37 %TypedArray%.prototype [ %Symbol.iterator% ] ( )
%Symbol.iterator% 属性的初始值为 %TypedArray.prototype.values%,定义见 23.2.3.35。
23.2.3.38 get %TypedArray%.prototype [ %Symbol.toStringTag% ]
%TypedArray%.prototype[%Symbol.toStringTag%] 是一个存取器属性,其 set 访问器函数为 undefined。其 get 访问器函数被调用时执行:
- 令 O 为 this 值。
- 若 O 不是对象,返回 undefined。
- 若 O 不具有 [[TypedArrayName]] 内部槽,返回 undefined。
- 令 name = O.[[TypedArrayName]]。
- 断言:name 是一个字符串。
- 返回 name。
该属性特性为 { [[Enumerable]]: false, [[Configurable]]: true }。
该函数 "name" 属性的初始值为 "get [Symbol.toStringTag]"。
23.2.4 TypedArray 对象的抽象操作
23.2.4.1 TypedArrayCreateFromConstructor ( constructor, argumentList )
The abstract operation TypedArrayCreateFromConstructor takes arguments constructor (a constructor) and argumentList (a List of ECMAScript language values) and returns 返回一个 TypedArray 的正常完成或抛出完成. 用于通过构造函数创建一个新的 TypedArray。 It performs the following steps when called:
- 令 newTypedArray 为 ? Construct(constructor, argumentList)。
- 令 taRecord 为 ? ValidateTypedArray(newTypedArray, seq-cst)。
- 断言:newTypedArray 具有 TypedArray 实例的属性 中提及的所有内部槽。
- 若 argumentList 中元素数量为 1 且 argumentList[0] 是 Number,则
- 若 IsTypedArrayOutOfBounds(taRecord) 为 true,抛出 TypeError 异常。
- 令 length 为 TypedArrayLength(taRecord)。
- 若 length < ℝ(argumentList[0]),抛出 TypeError 异常。
- 返回 newTypedArray。
23.2.4.2 TypedArrayCreateSameType ( exemplar, length )
The abstract operation TypedArrayCreateSameType takes arguments exemplar (a TypedArray) and length (a non-negative integer) and returns 返回一个 TypedArray 的正常完成或抛出完成. 用于使用从 exemplar 派生的构造函数创建一个新的 TypedArray。与 TypedArraySpeciesCreate 不同,后者可以通过 %Symbol.species% 构造自定义 TypedArray 子类,此操作始终使用内置的 TypedArray 构造函数之一。 It performs the following steps when called:
- 令 constructor 为与 exemplar.[[TypedArrayName]] 的构造函数名称关联的内在对象,见 Table 73。
- 令 result 为 ? TypedArrayCreateFromConstructor(constructor, « 𝔽(length) »)。
- 断言:result 具有 [[TypedArrayName]] 与 [[ContentType]] 内部槽。
- 断言:result.[[ContentType]] 为 exemplar.[[ContentType]]。
- 返回 result。
23.2.4.3 TypedArraySpeciesCreate ( exemplar, argumentList )
The abstract operation TypedArraySpeciesCreate takes arguments exemplar (a TypedArray) and argumentList (a List of ECMAScript language values) and returns 返回一个 TypedArray 的正常完成或抛出完成. 用于通过从 exemplar 派生的构造函数创建新的 TypedArray。不同于可通过 %Symbol.species% 创建非 Array 对象的 ArraySpeciesCreate,该操作强制构造函数必须创建一个实际的 TypedArray。 It performs the following steps when called:
- 令 defaultConstructor 为与 exemplar.[[TypedArrayName]] 的构造函数名称关联的内在对象,见 Table 73。
- 令 constructor 为 ? SpeciesConstructor(exemplar, defaultConstructor)。
- 令 result 为 ? TypedArrayCreateFromConstructor(constructor, argumentList)。
- 若 result.[[ContentType]] 不为 exemplar.[[ContentType]],抛出 TypeError 异常。
- 返回 result。
23.2.4.4 ValidateTypedArray ( O, order )
The abstract operation ValidateTypedArray takes arguments O (an ECMAScript language value) and order (seq-cst or unordered) and returns 返回一个带缓冲见证记录的 TypedArray 的正常完成或抛出完成. It performs the following steps when called:
- 执行 ? RequireInternalSlot(O, [[TypedArrayName]])。
- 断言:O 具有 [[ViewedArrayBuffer]] 内部槽。
- 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(O, order)。
- 若 IsTypedArrayOutOfBounds(taRecord) 为 true,抛出 TypeError 异常。
- 返回 taRecord。
23.2.4.5 TypedArrayElementSize ( O )
The abstract operation TypedArrayElementSize takes argument O (a TypedArray) and returns a non-negative integer. It performs the following steps when called:
- 返回在 Table 73 中为 O.[[TypedArrayName]] 指定的元素大小值。
23.2.4.6 TypedArrayElementType ( O )
The abstract operation TypedArrayElementType takes argument O (a TypedArray) and returns a TypedArray element type. It performs the following steps when called:
- 返回在 Table 73 中为 O.[[TypedArrayName]] 指定的元素类型值。
23.2.4.7 CompareTypedArrayElements ( x, y, comparator )
The abstract operation CompareTypedArrayElements takes arguments x (a Number or a BigInt), y (a Number or a BigInt), and comparator (a function object or undefined) and returns 返回一个 Number 的正常完成或一个突然完成. It performs the following steps when called:
- 断言:x 与 y 同为 Number,或 x 与 y 同为 BigInt。
- 若 comparator 不为 undefined,则
- 令 v 为 ? ToNumber(? Call(comparator, undefined, « x, y »))。
- 若 v 为 NaN,返回 +0𝔽。
- 返回 v。
- 若 x 和 y 均为 NaN,返回 +0𝔽。
- 若 x 为 NaN,返回 1𝔽。
- 若 y 为 NaN,返回 -1𝔽。
- 若 x < y,返回 -1𝔽。
- 若 x > y,返回 1𝔽。
- 若 x 为 -0𝔽 且 y 为 +0𝔽,返回 -1𝔽。
- 若 x 为 +0𝔽 且 y 为 -0𝔽,返回 1𝔽。
- 返回 +0𝔽。
Note
该操作执行数值比较,而非
23.1.3.30.2 中使用的字符串比较。