typed-array-findlast.tq 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2021 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.

#include 'src/builtins/builtins-typed-array-gen.h'

namespace typed_array {
const kBuiltinNameFindLast: constexpr string =
    '%TypedArray%.prototype.findLast';

// https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast
transitioning macro FindLastAllElements(implicit context: Context)(
13 14 15 16
    attachedArrayAndLength: typed_array::AttachedJSTypedArrayAndLength,
    predicate: Callable, thisArg: JSAny): JSAny {
  let witness =
      typed_array::NewAttachedJSTypedArrayWitness(attachedArrayAndLength.array);
17 18
  // 5. Let k be len - 1.
  // 6. Repeat, while k ≥ 0
19
  for (let k: uintptr = attachedArrayAndLength.length; k-- > 0;) {
20
    // 6a. Let Pk be ! ToString(𝔽(k)).
21
    // There is no need to cast ToString to load elements.
22 23

    // 6b. Let kValue be ! Get(O, Pk).
24 25 26
    // kValue must be undefined when the buffer was detached.
    let value: JSAny;
    try {
27
      witness.RecheckIndex(k) otherwise goto IsDetachedOrOutOfBounds;
28
      value = witness.Load(k);
29
    } label IsDetachedOrOutOfBounds deferred {
30 31
      value = Undefined;
    }
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    // 6c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue,
    // 𝔽(k), O »)).
    // TODO(v8:4153): Consider versioning this loop for Smi and non-Smi
    // indices to optimize Convert<Number>(k) for the most common case.
    const result = Call(
        context, predicate, thisArg, value, Convert<Number>(k),
        witness.GetStable());
    // 6d. If testResult is true, return kValue.
    if (ToBoolean(result)) {
      return value;
    }

    // 6e. Set k to k - 1. (done by the loop).
  }

  // 7. Return undefined.
  return Undefined;
}

// https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast
transitioning javascript builtin
TypedArrayPrototypeFindLast(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
56
  // arguments[0] = predicate
57 58 59
  // arguments[1] = thisArg
  try {
    // 1. Let O be the this value.
60 61
    // 2. Perform ? ValidateTypedArray(O).
    // 3. Let len be IntegerIndexedObjectLength(O).
62 63
    const array: JSTypedArray = Cast<JSTypedArray>(receiver)
        otherwise NotTypedArray;
64 65
    const attachedArrayAndLength = EnsureAttachedAndReadLength(array)
        otherwise IsDetachedOrOutOfBounds;
66 67 68
    // 4. If IsCallable(predicate) is false, throw a TypeError exception.
    const predicate = Cast<Callable>(arguments[0]) otherwise NotCallable;
    const thisArg = arguments[1];
69
    return FindLastAllElements(attachedArrayAndLength, predicate, thisArg);
70 71 72 73
  } label NotCallable deferred {
    ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]);
  } label NotTypedArray deferred {
    ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameFindLast);
74
  } label IsDetachedOrOutOfBounds deferred {
75 76 77 78
    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameFindLast);
  }
}
}