// 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' namespace typed_array_find { const kBuiltinName: constexpr string = '%TypedArray%.prototype.find'; transitioning macro FindAllElements(implicit context: Context)( array: typed_array::AttachedJSTypedArray, callbackfn: Callable, thisArg: JSAny): JSAny { let witness = typed_array::NewAttachedJSTypedArrayWitness(array); // TODO(v8:4153): Support huge TypedArrays here. const length = Cast(Convert(witness.Get().length)) otherwise unreachable; for (let k: Smi = 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); const result = Call(context, callbackfn, thisArg, value, k, witness.GetStable()); if (ToBoolean(result)) { return value; } } return Undefined; } // https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.find transitioning javascript builtin TypedArrayPrototypeFind(js-implicit context: Context, receiver: JSAny)( ...arguments): JSAny { // arguments[0] = callback // arguments[1] = thisArg try { const array: JSTypedArray = Cast(receiver) otherwise NotTypedArray; const uarray = typed_array::EnsureAttached(array) otherwise IsDetached; const callbackfn = Cast(arguments[0]) otherwise NotCallable; const thisArg = arguments[1]; return FindAllElements(uarray, callbackfn, thisArg); } label NotCallable deferred { ThrowTypeError(kCalledNonCallable, arguments[0]); } label NotTypedArray deferred { ThrowTypeError(kNotTypedArray, kBuiltinName); } label IsDetached deferred { ThrowTypeError(kDetachedOperation, kBuiltinName); } } }