array-findindex.tq 5.67 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 {
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
transitioning javascript builtin
ArrayFindIndexLoopEagerDeoptContinuation(
    js-implicit context: NativeContext, receiver: JSAny)(
    callback: JSAny, thisArg: JSAny, initialK: JSAny, length: JSAny): JSAny {
  // All continuation points in the optimized findIndex implementation are
  // after the ToObject(O) call that ensures we are dealing with a
  // JSReceiver.
  //
  // Also, this great mass of casts is necessary because the signature
  // of Torque javascript builtins requires JSAny type for all parameters
  // other than {context}.
  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;

  return ArrayFindIndexLoopContinuation(
      jsreceiver, callbackfn, thisArg, jsreceiver, numberK, numberLength);
}

transitioning javascript builtin
ArrayFindIndexLoopLazyDeoptContinuation(
    js-implicit context: NativeContext, receiver: JSAny)(
    _callback: JSAny, _thisArg: JSAny, _initialK: JSAny, _length: JSAny,
    _result: JSAny): JSAny {
  // This deopt continuation point is never actually called, it just
  // exists to make stack traces correct from a ThrowTypeError if the
  // callback was found to be non-callable.
  unreachable;
}
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// Continuation that is called after a lazy deoptimization from TF that
// happens right after the callback and it's returned value must be handled
// before iteration continues.
transitioning javascript builtin
ArrayFindIndexLoopAfterCallbackLazyDeoptContinuation(
    js-implicit context: NativeContext, receiver: JSAny)(
    callback: JSAny, thisArg: JSAny, initialK: JSAny, length: JSAny,
    foundValue: JSAny, isFound: JSAny): JSAny {
  // All continuation points in the optimized findIndex implementation are
  // after the ToObject(O) call that ensures we are dealing with a
  // JSReceiver.
  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;

  // This custom lazy deopt point is right after the callback. find() needs
  // to pick up at the next step, which is returning the element if the
  // callback value is truthy.  Otherwise, continue the search by calling the
  // continuation.

  if (ToBoolean(isFound)) {
    return foundValue;
60 61
  }

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  return ArrayFindIndexLoopContinuation(
      jsreceiver, callbackfn, thisArg, jsreceiver, numberK, numberLength);
}

transitioning builtin ArrayFindIndexLoopContinuation(implicit context: Context)(
    _receiver: JSReceiver, callbackfn: Callable, thisArg: JSAny, o: JSReceiver,
    initialK: Number, length: Number): Number {
  // 5. Let k be 0.
  // 6. Repeat, while k < len
  for (let k: Number = initialK; k < length; k++) {
    // 6a. Let Pk be ! ToString(k).
    // k is guaranteed to be a positive integer, hence ToString is
    // side-effect free and HasProperty/GetProperty do the conversion inline.

    // 6b. i. Let kValue be ? Get(O, Pk).
    const value: JSAny = GetProperty(o, k);

    // 6c. Let testResult be ToBoolean(? Call(predicate, T, <<kValue, k,
    // O>>)).
    const testResult: JSAny = Call(context, callbackfn, thisArg, value, k, o);

    // 6d. If testResult is true, return k.
    if (ToBoolean(testResult)) {
      return k;
86 87
    }

88
    // 6e. Increase k by 1. (done by the loop).
89
  }
90 91
  return Convert<Smi>(-1);
}
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
transitioning macro FastArrayFindIndex(implicit context: Context)(
    o: JSReceiver, len: Number, callbackfn: Callable, thisArg: JSAny): Number
    labels Bailout(Smi) {
  let k: Smi = 0;
  const smiLen = Cast<Smi>(len) otherwise goto Bailout(k);
  const fastO = Cast<FastJSArray>(o) otherwise goto Bailout(k);
  let fastOW = NewFastJSArrayWitness(fastO);

  // Build a fast loop over the smi array.
  for (; k < smiLen; k++) {
    fastOW.Recheck() otherwise goto Bailout(k);

    // Ensure that we haven't walked beyond a possibly updated length.
    if (k >= fastOW.Get().length) goto Bailout(k);

    const value: JSAny = fastOW.LoadElementOrUndefined(k);
    const testResult: JSAny =
        Call(context, callbackfn, thisArg, value, k, fastOW.Get());
    if (ToBoolean(testResult)) {
      return k;
113 114
    }
  }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  return -1;
}

// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
transitioning javascript builtin
ArrayPrototypeFindIndex(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
  try {
    RequireObjectCoercible(receiver, 'Array.prototype.findIndex');

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

    // 2. Let len be ? ToLength(? Get(O, "length")).
    const len: Number = GetLengthProperty(o);
130

131 132 133
    // 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
    if (arguments.length == 0) {
      goto NotCallableError;
134
    }
135 136 137 138
    const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallableError;

    // 4. If thisArg is present, let T be thisArg; else let T be undefined.
    const thisArg: JSAny = arguments[1];
139

140
    // Special cases.
141
    try {
142 143 144 145
      return FastArrayFindIndex(o, len, callbackfn, thisArg)
          otherwise Bailout;
    } label Bailout(k: Smi) deferred {
      return ArrayFindIndexLoopContinuation(o, callbackfn, thisArg, o, k, len);
146
    }
147 148
  } label NotCallableError deferred {
    ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]);
149 150
  }
}
151
}