typed-array-some.tq 2.9 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
const kBuiltinNameSome: constexpr string = '%TypedArray%.prototype.some';
9

10
// https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
11
transitioning macro SomeAllElements(implicit context: Context)(
12
    attachedArrayAndLength: typed_array::AttachedJSTypedArrayAndLength,
13
    callbackfn: Callable, thisArg: JSAny): Boolean {
14 15
  let witness =
      typed_array::NewAttachedJSTypedArrayWitness(attachedArrayAndLength.array);
16

17
  // 5. Let k be 0.
18
  // 6. Repeat, while k < len
19
  for (let k: uintptr = 0; k < attachedArrayAndLength.length; k++) {
20 21 22 23 24 25 26
    // 6a. Let Pk be ! ToString(𝔽(k)).
    // There is no need to cast ToString to load elements.

    // 6b. Let kValue be ! Get(O, Pk).
    // kValue must be undefined when the buffer is detached.
    let value: JSAny;
    try {
27
      witness.RecheckIndex(k) otherwise goto IsDetachedOrOutOfBounds;
28
      value = witness.Load(k);
29
    } label IsDetachedOrOutOfBounds deferred {
30 31 32 33 34
      value = Undefined;
    }

    // 6c. Let testResult be ! ToBoolean(? Call(callbackfn, thisArg, « kValue,
    // 𝔽(k), O »)).
35 36 37 38 39
    // 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());
40 41

    // 6d. If testResult is true, return true.
42 43
    if (ToBoolean(result)) {
      return True;
44
    }
45 46

    // 6e. Set k to k + 1. (done by the loop).
47
  }
48 49

  // 7. Return false.
50 51
  return False;
}
52

53 54 55 56 57
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
transitioning javascript builtin
TypedArrayPrototypeSome(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
  // arguments[0] = callback
58
  // arguments[1] = thisArg
59
  try {
60 61 62
    // 1. Let O be the this value.
    // 2. Perform ? ValidateTypedArray(O).
    // 3. Let len be IntegerIndexedObjectLength(O).
63 64
    const array: JSTypedArray = Cast<JSTypedArray>(receiver)
        otherwise NotTypedArray;
65
    const attachedArrayAndLength = EnsureAttachedAndReadLength(array)
66 67
        otherwise IsDetachedOrOutOfBounds;
    // 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
68 69
    const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
    const thisArg = arguments[1];
70
    return SomeAllElements(attachedArrayAndLength, callbackfn, thisArg);
71 72
  } label NotTypedArray deferred {
    ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameSome);
73
  } label IsDetachedOrOutOfBounds deferred {
74
    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameSome);
75 76
  } label NotCallable deferred {
    ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]);
77 78
  }
}
79
}