summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/typed-array-some.tq
blob: 994690768047b6d0f990f0ccdf317dc89fb18aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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 {
const kBuiltinNameSome: constexpr string = '%TypedArray%.prototype.some';

// https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
transitioning macro SomeAllElements(implicit context: Context)(
    array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
    thisArg: JSAny): Boolean {
  let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
  const length: uintptr = witness.Get().length;

  // 6. Repeat, while k < len
  for (let k: uintptr = 0; k < length; k++) {
    // 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 {
      witness.Recheck() otherwise goto IsDetached;
      value = witness.Load(k);
    } label IsDetached deferred {
      value = Undefined;
    }

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

    // 6d. If testResult is true, return true.
    if (ToBoolean(result)) {
      return True;
    }

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

  // 7. Return false.
  return False;
}

// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
transitioning javascript builtin
TypedArrayPrototypeSome(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
  // 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(MessageTemplate::kCalledNonCallable, arguments[0]);
  } label NotTypedArray deferred {
    ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameSome);
  } label IsDetached deferred {
    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameSome);
  }
}
}