promise-race.tq 3.8 KB
Newer Older
1 2 3 4 5 6 7 8
// 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-promise-gen.h'

namespace promise {

9 10 11 12 13 14
// https://tc39.es/ecma262/#sec-promise.race
transitioning javascript builtin
PromiseRace(
    js-implicit context: Context, receiver: JSAny)(iterable: JSAny): JSAny {
  const receiver = Cast<JSReceiver>(receiver)
      otherwise ThrowTypeError(MessageTemplate::kCalledOnNonObject, 'Promise.race');
15

16 17
  const nativeContext = LoadNativeContext(context);

18 19 20 21 22 23 24
  // Let promiseCapability be ? NewPromiseCapability(C).
  // Don't fire debugEvent so that forwarding the rejection through all does
  // not trigger redundant ExceptionEvents
  const capability = NewPromiseCapability(receiver, False);
  const resolve = capability.resolve;
  const reject = capability.reject;
  const promise = capability.promise;
25

26 27 28 29
  // NewPromiseCapability guarantees that receiver is Constructor.
  assert(Is<Constructor>(receiver));
  const constructor = UnsafeCast<Constructor>(receiver);

30 31 32 33 34
  // For catch prediction, don't treat the .then calls as handling it;
  // instead, recurse outwards.
  if (IsDebugActive()) deferred {
      SetPropertyStrict(context, reject, kPromiseForwardingHandlerSymbol, True);
    }
35

36
  try {
37
    let promiseResolveFunction: JSAny;
38
    let i: iterator::IteratorRecord;
39
    try {
40 41 42 43 44 45
      // Let promiseResolve be GetPromiseResolve(C).
      // IfAbruptRejectPromise(promiseResolve, promiseCapability).
      promiseResolveFunction = GetPromiseResolve(nativeContext, constructor);

      // Let iterator be GetIterator(iterable).
      // IfAbruptRejectPromise(iterator, promiseCapability).
46 47 48 49
      i = iterator::GetIterator(iterable);
    } catch (e) deferred {
      goto Reject(e);
    }
50

51 52 53
    // Let result be PerformPromiseRace(iteratorRecord, C, promiseCapability).
    try {
      const fastIteratorResultMap = UnsafeCast<Map>(
54
          nativeContext.elements[NativeContextSlot::ITERATOR_RESULT_MAP_INDEX]);
55 56 57 58 59 60 61 62
      while (true) {
        let nextValue: JSAny;
        try {
          // Let next be IteratorStep(iteratorRecord.[[Iterator]]).
          // If next is an abrupt completion, set iteratorRecord.[[Done]] to
          // true. ReturnIfAbrupt(next).
          const next: JSReceiver = iterator::IteratorStep(
              i, fastIteratorResultMap) otherwise return promise;
63

64 65 66 67 68 69 70 71 72 73
          // Let nextValue be IteratorValue(next).
          // If nextValue is an abrupt completion, set iteratorRecord.[[Done]]
          // to true.
          // ReturnIfAbrupt(nextValue).
          nextValue = iterator::IteratorValue(next, fastIteratorResultMap);
        } catch (e) {
          goto Reject(e);
        }
        // Let nextPromise be ? Call(constructor, _promiseResolve_, «
        // nextValue »).
74 75
        const nextPromise =
            CallResolve(constructor, promiseResolveFunction, nextValue);
76

77 78 79 80 81 82
        // Perform ? Invoke(nextPromise, "then", « resolveElement,
        //                  resultCapability.[[Reject]] »).
        const then = GetProperty(nextPromise, kThenString);
        const thenResult = Call(
            context, then, nextPromise, UnsafeCast<JSAny>(resolve),
            UnsafeCast<JSAny>(reject));
83

84 85 86 87 88 89
        // For catch prediction, mark that rejections here are semantically
        // handled by the combined Promise.
        if (IsDebugActive() && !Is<JSPromise>(promise)) deferred {
            SetPropertyStrict(
                context, thenResult, kPromiseHandledBySymbol, promise);
          }
90
      }
91 92 93
    } catch (e) deferred {
      iterator::IteratorCloseOnException(i);
      goto Reject(e);
94
    }
95 96 97 98 99
  } label Reject(exception: Object) deferred {
    Call(
        context, UnsafeCast<JSAny>(reject), Undefined,
        UnsafeCast<JSAny>(exception));
    return promise;
100
  }
101 102
  unreachable;
}
103
}