1// Copyright 2019 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include 'src/builtins/builtins-typed-array-gen.h' 6 7namespace typed_array { 8const kBuiltinNameFindIndex: constexpr string = 9 '%TypedArray%.prototype.findIndex'; 10 11transitioning macro FindIndexAllElements(implicit context: Context)( 12 array: typed_array::AttachedJSTypedArray, callbackfn: Callable, 13 thisArg: JSAny): Number { 14 let witness = typed_array::NewAttachedJSTypedArrayWitness(array); 15 const length: uintptr = witness.Get().length; 16 for (let k: uintptr = 0; k < length; k++) { 17 // BUG(4895): We should throw on detached buffers rather than simply exit. 18 witness.Recheck() otherwise break; 19 const value: JSAny = witness.Load(k); 20 // TODO(v8:4153): Consider versioning this loop for Smi and non-Smi 21 // indices to optimize Convert<Number>(k) for the most common case. 22 const indexNumber: Number = Convert<Number>(k); 23 const result = Call( 24 context, callbackfn, thisArg, value, indexNumber, witness.GetStable()); 25 if (ToBoolean(result)) { 26 return indexNumber; 27 } 28 } 29 return -1; 30} 31 32// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.findIndex 33transitioning javascript builtin 34TypedArrayPrototypeFindIndex( 35 js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny { 36 // arguments[0] = callback 37 // arguments[1] = thisArg. 38 try { 39 const array: JSTypedArray = Cast<JSTypedArray>(receiver) 40 otherwise NotTypedArray; 41 const uarray = typed_array::EnsureAttached(array) otherwise IsDetached; 42 43 const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable; 44 const thisArg = arguments[1]; 45 return FindIndexAllElements(uarray, callbackfn, thisArg); 46 } label NotCallable deferred { 47 ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]); 48 } label NotTypedArray deferred { 49 ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameFindIndex); 50 } label IsDetached deferred { 51 ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameFindIndex); 52 } 53} 54} 55