typed-array-subarray.tq 2.54 KB
Newer Older
1 2 3 4
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
namespace typed_array {
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// ES %TypedArray%.prototype.subarray
transitioning javascript builtin TypedArrayPrototypeSubArray(
    js-implicit context: NativeContext,
    receiver: JSAny)(...arguments): JSTypedArray {
  const methodName: constexpr string = '%TypedArray%.prototype.subarray';

  // 1. Let O be the this value.
  // 3. If O does not have a [[TypedArrayName]] internal slot, throw a
  // TypeError exception.
  const source = Cast<JSTypedArray>(receiver)
      otherwise ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, methodName);

  // 5. Let buffer be O.[[ViewedArrayBuffer]].
  const buffer = typed_array::GetBuffer(source);

  // 6. Let srcLength be O.[[ArrayLength]].
  const srcLength: uintptr = source.length;

  // 7. Let relativeBegin be ? ToInteger(begin).
  // 8. If relativeBegin < 0, let beginIndex be max((srcLength +
  // relativeBegin), 0); else let beginIndex be min(relativeBegin,
  // srcLength).
  const arg0 = arguments[0];
  const begin: uintptr =
      arg0 != Undefined ? ConvertToRelativeIndex(arg0, srcLength) : 0;

  // 9. If end is undefined, let relativeEnd be srcLength;
  //      else, let relativeEnd be ? ToInteger(end).
  // 10. If relativeEnd < 0, let endIndex be max((srcLength + relativeEnd),
  // 0); else let endIndex be min(relativeEnd, srcLength).
  const arg1 = arguments[1];
  const end: uintptr =
      arg1 != Undefined ? ConvertToRelativeIndex(arg1, srcLength) : srcLength;

  // 11. Let newLength be max(endIndex - beginIndex, 0).
  const newLength: uintptr = Unsigned(IntPtrMax(Signed(end - begin), 0));

  // 12. Let constructorName be the String value of O.[[TypedArrayName]].
  // 13. Let elementSize be the Number value of the Element Size value
  // specified in Table 52 for constructorName.
  const elementsInfo = typed_array::GetTypedArrayElementsInfo(source);

  // 14. Let srcByteOffset be O.[[ByteOffset]].
  const srcByteOffset: uintptr = source.byte_offset;

  // 15. Let beginByteOffset be srcByteOffset + beginIndex × elementSize.
  const beginByteOffset =
      srcByteOffset + elementsInfo.CalculateByteLength(begin)
      otherwise ThrowRangeError(MessageTemplate::kInvalidArrayBufferLength);

  // 16. Let argumentsList be « buffer, beginByteOffset, newLength ».
  // 17. Return ? TypedArraySpeciesCreate(O, argumentsList).
  return TypedArraySpeciesCreateByBuffer(
      methodName, source, buffer, beginByteOffset, newLength);
}
62
}