summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/typed-array-foreach.tq
blob: 656a22e07d362a732252d8fab2e9ae78eaac1ef8 (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
// 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_foreach {
  const kBuiltinName: constexpr string = '%TypedArray%.prototype.forEach';

  transitioning macro ForEachAllElements(implicit context: Context)(
      array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
      thisArg: Object): Object {
    let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
    // TODO(v8:4153): Support huge TypedArrays here.
    const length =
        Cast<Smi>(Convert<Number>(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: Object = witness.Load(k);
      Call(context, callbackfn, thisArg, value, k, witness.GetStable());
    }
    return Undefined;
  }

  // https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.every
  transitioning javascript builtin
  TypedArrayPrototypeForEach(js-implicit context: Context, receiver: Object)(
      ...arguments): Object {
    // arguments[0] = callback
    // arguments[1] = this_arg.

    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 ForEachAllElements(uarray, callbackfn, thisArg);
    }
    label NotCallable deferred {
      ThrowTypeError(kCalledNonCallable, arguments[0]);
    }
    label NotTypedArray deferred {
      ThrowTypeError(kNotTypedArray, kBuiltinName);
    }
    label IsDetached deferred {
      ThrowTypeError(kDetachedOperation, kBuiltinName);
    }
  }
}