typed-array-findindex.tq 2.11 KB
Newer Older
1 2 3 4 5 6
// 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.

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

7
namespace typed_array {
8 9
const kBuiltinNameFindIndex: constexpr string =
    '%TypedArray%.prototype.findIndex';
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
transitioning macro FindIndexAllElements(implicit context: Context)(
    array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
    thisArg: JSAny): Number {
  let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
  const length: uintptr = witness.Get().length;
  for (let k: uintptr = 0; k < length; k++) {
    // BUG(4895): We should throw on detached buffers rather than simply exit.
    witness.Recheck() otherwise break;
    const value: JSAny = witness.Load(k);
    // TODO(v8:4153): Consider versioning this loop for Smi and non-Smi
    // indices to optimize Convert<Number>(k) for the most common case.
    const indexNumber: Number = Convert<Number>(k);
    const result = Call(
        context, callbackfn, thisArg, value, indexNumber, witness.GetStable());
    if (ToBoolean(result)) {
      return indexNumber;
27 28
    }
  }
29 30
  return -1;
}
31

32 33 34 35 36 37 38 39 40 41
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.findIndex
transitioning javascript builtin
TypedArrayPrototypeFindIndex(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
  // arguments[0] = callback
  // arguments[1] = thisArg.
  try {
    const array: JSTypedArray = Cast<JSTypedArray>(receiver)
        otherwise NotTypedArray;
    const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;
42

43 44 45 46 47 48 49 50 51
    const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
    const thisArg = arguments[1];
    return FindIndexAllElements(uarray, callbackfn, thisArg);
  } label NotCallable deferred {
    ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]);
  } label NotTypedArray deferred {
    ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameFindIndex);
  } label IsDetached deferred {
    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameFindIndex);
52 53
  }
}
54
}