当前位置:   article > 正文

鸿蒙Harmony角落里的知识:从ECMA规范到ArkTS接口(二)

鸿蒙Harmony角落里的知识:从ECMA规范到ArkTS接口(二)

上篇介绍了typedArray.slice方法鸿蒙Harmony角落里的知识:从ECMA规范到ArkTS接口(一)本文介绍一个返回结果和参数和slice非常类似的函数:TypedArray.prototype.subarray。


ECMA对TypedArray.prototype.subarray接口的定义:

按照ECMA的规范,TypedArray.prototype.subarray(begin, end) 方法定义如下:

  • begin: 起始索引,表示新视图的起始点。
  • end: 结束索引,代表新视图的终点(但不包括该索引本身)。

如果 end 参数省略,subarray 将默认包含从 begin 开始到原数组结尾的所有元素。如果 begin 或 end 是负值,则它们表示从数组末尾开始的倒数索引。方法返回一个新的 TypedArray 实例,它表示原始 TypedArray 的一个连续子集。

我们可以直接查看ECMA规范中对该函数的描述:

  1. 23.2.3.30 %TypedArray%.prototype.subarray ( start, end )
  2. 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 interval from start (inclusive) to end (exclusive). If either start or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.
  3. It performs the following steps when called:
  4. 1. Let O be the this value.
  5. 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  6. 3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  7. 4. Let buffer be O.[[ViewedArrayBuffer]].
  8. 5. Let srcRecord be MakeTypedArrayWithBufferWitnessRecord(O, SEQ-CST).
  9. 6. If IsTypedArrayOutOfBounds(srcRecord) is true, then
  10. a. Let srcLength be 0.
  11. 7. Else,
  12. a. Let srcLength be TypedArrayLength(srcRecord).
  13. 8. Let relativeStart be ? ToIntegerOrInfinity(start).
  14. 9. If relativeStart = -∞, let startIndex be 0.
  15. 10. Else if relativeStart < 0, let startIndex be max(srcLength + relativeStart, 0).
  16. 11. Else, let startIndex be min(relativeStart, srcLength).
  17. 12. Let elementSize be TypedArrayElementSize(O).
  18. 13. Let srcByteOffset be O.[[ByteOffset]].
  19. 14. Let beginByteOffset be srcByteOffset + (startIndex × elementSize).
  20. 15. If O.[[ArrayLength]] is AUTO and end is undefined, then
  21. a. Let argumentsList be « buffer,
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/746661
    推荐阅读