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 kBuiltinNameSome: constexpr string = '%TypedArray%.prototype.some';
9
10transitioning macro SomeAllElements(implicit context: Context)(
11    array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
12    thisArg: JSAny): Boolean {
13  let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
14  const length: uintptr = witness.Get().length;
15  for (let k: uintptr = 0; k < length; k++) {
16    // BUG(4895): We should throw on detached buffers rather than simply exit.
17    witness.Recheck() otherwise break;
18    const value: JSAny = witness.Load(k);
19    // TODO(v8:4153): Consider versioning this loop for Smi and non-Smi
20    // indices to optimize Convert<Number>(k) for the most common case.
21    const result = Call(
22        context, callbackfn, thisArg, value, Convert<Number>(k),
23        witness.GetStable());
24    if (ToBoolean(result)) {
25      return True;
26    }
27  }
28  return False;
29}
30
31// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
32transitioning javascript builtin
33TypedArrayPrototypeSome(
34    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
35  // arguments[0] = callback
36  // arguments[1] = thisArg.
37  try {
38    const array: JSTypedArray = Cast<JSTypedArray>(receiver)
39        otherwise NotTypedArray;
40    const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;
41
42    const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
43    const thisArg = arguments[1];
44    return SomeAllElements(uarray, callbackfn, thisArg);
45  } label NotCallable deferred {
46    ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]);
47  } label NotTypedArray deferred {
48    ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameSome);
49  } label IsDetached deferred {
50    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameSome);
51  }
52}
53}
54