array-foreach.tq 5.73 KB
Newer Older
1 2 3 4
// Copyright 2018 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.

5
namespace array_foreach {
6 7 8 9 10 11 12
  transitioning javascript builtin
  ArrayForEachLoopEagerDeoptContinuation(implicit context: Context)(
      receiver: Object, callback: Object, thisArg: Object, initialK: Object,
      length: Object): Object {
    // All continuation points in the optimized forEach implemntation are
    // after the ToObject(O) call that ensures we are dealing with a
    // JSReceiver.
13 14 15 16
    const jsreceiver = Cast<JSReceiver>(receiver) otherwise unreachable;
    const callbackfn = Cast<Callable>(callback) otherwise unreachable;
    const numberK = Cast<Number>(initialK) otherwise unreachable;
    const numberLength = Cast<Number>(length) otherwise unreachable;
17 18 19 20 21 22 23 24 25 26 27 28 29

    return ArrayForEachLoopContinuation(
        jsreceiver, callbackfn, thisArg, Undefined, jsreceiver, numberK,
        numberLength, Undefined);
  }

  transitioning javascript builtin
  ArrayForEachLoopLazyDeoptContinuation(implicit context: Context)(
      receiver: Object, callback: Object, thisArg: Object, initialK: Object,
      length: Object, result: Object): Object {
    // All continuation points in the optimized forEach implemntation are
    // after the ToObject(O) call that ensures we are dealing with a
    // JSReceiver.
30 31 32 33
    const jsreceiver = Cast<JSReceiver>(receiver) otherwise unreachable;
    const callbackfn = Cast<Callable>(callback) otherwise unreachable;
    const numberK = Cast<Number>(initialK) otherwise unreachable;
    const numberLength = Cast<Number>(length) otherwise unreachable;
34 35 36 37 38 39 40 41 42 43 44 45

    return ArrayForEachLoopContinuation(
        jsreceiver, callbackfn, thisArg, Undefined, jsreceiver, numberK,
        numberLength, Undefined);
  }

  transitioning builtin ArrayForEachLoopContinuation(implicit context: Context)(
      receiver: JSReceiver, callbackfn: Callable, thisArg: Object,
      array: Object, o: JSReceiver, initialK: Number, len: Number,
      to: Object): Object {
    // variables {array} and {to} are ignored.

46 47
    // 5. Let k be 0.
    // 6. Repeat, while k < len
48
    for (let k: Number = initialK; k < len; k = k + 1) {
49
      // 6a. Let Pk be ! ToString(k).
50 51
      // k is guaranteed to be a positive integer, hence ToString is
      // side-effect free and HasProperty/GetProperty do the conversion inline.
52 53

      // 6b. Let kPresent be ? HasProperty(O, Pk).
54
      const kPresent: Boolean = HasProperty_Inline(o, k);
55 56 57 58

      // 6c. If kPresent is true, then
      if (kPresent == True) {
        // 6c. i. Let kValue be ? Get(O, Pk).
59
        const kValue: Object = GetProperty(o, k);
60 61 62 63 64 65 66 67 68 69

        // 6c. ii. Perform ? Call(callbackfn, T, <kValue, k, O>).
        Call(context, callbackfn, thisArg, kValue, k, o);
      }

      // 6d. Increase k by 1. (done by the loop).
    }
    return Undefined;
  }

70 71 72 73 74
  transitioning macro VisitAllElements<FixedArrayType: type>(implicit context:
                                                                  Context)(
      o: JSArray, len: Smi, callbackfn: Callable, thisArg: Object) labels
  Bailout(Smi) {
    let k: Smi = 0;
75 76
    let fastO =
    FastJSArrayWitness{Cast<FastJSArray>(o) otherwise goto Bailout(k)};
77

78 79 80
    // Build a fast loop over the smi array.
    for (; k < len; k++) {
      // Ensure that we haven't walked beyond a possibly updated length.
81
      if (k >= fastO.Get().length) goto Bailout(k);
82 83 84 85
      const value: Object = LoadElementNoHole<FixedArrayType>(fastO.Get(), k)
          otherwise continue;
      Call(context, callbackfn, thisArg, value, k, fastO.Get());
      fastO.Recheck() otherwise goto Bailout(k + 1);
86 87 88
    }
  }

89 90
  transitioning macro FastArrayForEach(implicit context: Context)(
      o: JSReceiver, len: Number, callbackfn: Callable, thisArg: Object): Object
91
      labels Bailout(Smi) {
92
    let k: Smi = 0;
93 94
    const smiLen = Cast<Smi>(len) otherwise goto Bailout(k);
    let fastO = Cast<FastJSArray>(o) otherwise goto Bailout(k);
95 96 97 98 99 100 101
    const elementsKind: ElementsKind = fastO.map.elements_kind;
    if (IsElementsKindGreaterThan(elementsKind, HOLEY_ELEMENTS)) {
      VisitAllElements<FixedDoubleArray>(fastO, smiLen, callbackfn, thisArg)
          otherwise Bailout;
    } else {
      VisitAllElements<FixedArray>(fastO, smiLen, callbackfn, thisArg)
          otherwise Bailout;
102 103 104 105 106
    }
    return Undefined;
  }

  // https://tc39.github.io/ecma262/#sec-array.prototype.foreach
107 108
  transitioning javascript builtin
  ArrayForEach(context: Context, receiver: Object, ...arguments): Object {
109 110 111 112 113 114
    try {
      if (IsNullOrUndefined(receiver)) {
        goto NullOrUndefinedError;
      }

      // 1. Let O be ? ToObject(this value).
115
      const o: JSReceiver = ToObject_Inline(context, receiver);
116 117

      // 2. Let len be ? ToLength(? Get(O, "length")).
118
      const len: Number = GetLengthProperty(o);
119 120 121 122 123

      // 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
      if (arguments.length == 0) {
        goto TypeError;
      }
124
      const callbackfn = Cast<Callable>(arguments[0]) otherwise TypeError;
125 126

      // 4. If thisArg is present, let T be thisArg; else let T be undefined.
127
      const thisArg: Object = arguments.length > 1 ? arguments[1] : Undefined;
128 129

      // Special cases.
130
      let k: Number = 0;
131
      try {
132
        return FastArrayForEach(o, len, callbackfn, thisArg)
133
            otherwise Bailout;
134
      }
135
      label Bailout(kValue: Smi) deferred {
136
        k = kValue;
137 138
      }

139 140
      return ArrayForEachLoopContinuation(
          o, callbackfn, thisArg, Undefined, o, k, len, Undefined);
141
    }
142
    label TypeError deferred {
143 144
      ThrowTypeError(context, kCalledNonCallable, arguments[0]);
    }
145
    label NullOrUndefinedError deferred {
146 147 148 149 150
      ThrowTypeError(
          context, kCalledOnNullOrUndefined, 'Array.prototype.forEach');
    }
  }
}