typed-array-some.tq 2.07 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 8
namespace typed_array {
  const kBuiltinNameSome: constexpr string = '%TypedArray%.prototype.some';
9 10 11

  transitioning macro SomeAllElements(implicit context: Context)(
      array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
12
      thisArg: JSAny): Boolean {
13
    let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
14 15
    const length: uintptr = witness.Get().length;
    for (let k: uintptr = 0; k < length; k++) {
16 17
      // BUG(4895): We should throw on detached buffers rather than simply exit.
      witness.Recheck() otherwise break;
18
      const value: JSAny = witness.Load(k);
19 20 21 22 23
      // 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, callbackfn, thisArg, value, Convert<Number>(k),
          witness.GetStable());
24 25 26 27 28 29 30 31 32
      if (ToBoolean(result)) {
        return True;
      }
    }
    return False;
  }

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

      const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
      const thisArg = arguments[1];
      return SomeAllElements(uarray, callbackfn, thisArg);
    }
    label NotCallable deferred {
      ThrowTypeError(kCalledNonCallable, arguments[0]);
    }
    label NotTypedArray deferred {
50
      ThrowTypeError(kNotTypedArray, kBuiltinNameSome);
51 52
    }
    label IsDetached deferred {
53
      ThrowTypeError(kDetachedOperation, kBuiltinNameSome);
54 55 56
    }
  }
}