promise-constructor.tq 3.63 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-constructor-gen.h'
#include 'src/builtins/builtins-promise-gen.h'

namespace runtime {
9 10
extern transitioning runtime
DebugPushPromise(implicit context: Context)(JSAny): JSAny;
11

12 13
extern transitioning runtime
DebugPopPromise(implicit context: Context)(): JSAny;
14

15 16
extern transitioning runtime
PromiseHookInit(implicit context: Context)(Object, Object): JSAny;
17 18 19 20 21
}

// https://tc39.es/ecma262/#sec-promise-constructor
namespace promise {

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
extern runtime IncrementUseCounter(Context, Smi): void;
type UseCounterFeature extends int31
constexpr 'v8::Isolate::UseCounterFeature';
const kPromiseConstructorReturnedUndefined: constexpr UseCounterFeature
    generates 'v8::Isolate::kPromiseConstructorReturnedUndefined';

extern macro
IsDebugActive(): bool;

transitioning macro
HasAccessCheckFailed(implicit context: Context)(
    nativeContext: NativeContext, promiseFun: JSAny, executor: JSAny): bool {
  BranchIfAccessCheckFailed(nativeContext, promiseFun, executor)
      otherwise return true;
  return false;
}
38

39
extern macro ConstructorBuiltinsAssembler::FastNewObject(
40
    Context, JSFunction, JSReceiver): JSObject;
41

42
extern macro
43
PromiseBuiltinsAssembler::IsPromiseHookEnabledOrHasAsyncEventDelegate(): bool;
44

45 46 47 48 49 50 51 52 53
// https://tc39.es/ecma262/#sec-promise-executor
transitioning javascript builtin
PromiseConstructor(
    js-implicit context: NativeContext, receiver: JSAny,
    newTarget: JSAny)(executor: JSAny): JSAny {
  // 1. If NewTarget is undefined, throw a TypeError exception.
  if (newTarget == Undefined) {
    ThrowTypeError(MessageTemplate::kNotAPromise, newTarget);
  }
54

55 56 57 58
  // 2. If IsCallable(executor) is false, throw a TypeError exception.
  if (!Is<Callable>(executor)) {
    ThrowTypeError(MessageTemplate::kResolverNotAFunction, executor);
  }
59

60
  const promiseFun = *NativeContextSlot(ContextSlot::PROMISE_FUNCTION_INDEX);
61

62 63 64 65 66 67
  // Silently fail if the stack looks fishy.
  if (HasAccessCheckFailed(context, promiseFun, executor)) {
    IncrementUseCounter(
        context, SmiConstant(kPromiseConstructorReturnedUndefined));
    return Undefined;
  }
68

69 70 71 72
  let result: JSPromise;
  if (promiseFun == newTarget) {
    result = NewJSPromise();
  } else {
73 74
    result = UnsafeCast<JSPromise>(
        FastNewObject(context, promiseFun, UnsafeCast<JSReceiver>(newTarget)));
75
    PromiseInit(result);
76 77 78
    if (IsPromiseHookEnabledOrHasAsyncEventDelegate()) {
      runtime::PromiseHookInit(result, Undefined);
    }
79
  }
80

81 82
  const isDebugActive = IsDebugActive();
  if (isDebugActive) runtime::DebugPushPromise(result);
83

84 85 86 87 88 89 90
  const funcs = CreatePromiseResolvingFunctions(result, True, context);
  const resolve = funcs.resolve;
  const reject = funcs.reject;
  try {
    Call(context, UnsafeCast<Callable>(executor), Undefined, resolve, reject);
  } catch (e) {
    Call(context, reject, Undefined, e);
91
  }
92

93 94 95 96 97 98 99 100 101 102 103
  if (isDebugActive) runtime::DebugPopPromise();
  return result;
}

// Promise.prototype.catch ( onRejected )
// https://tc39.es/ecma262/#sec-promise.prototype.catch
transitioning javascript builtin
PromisePrototypeCatch(
    js-implicit context: Context, receiver: JSAny)(onRejected: JSAny): JSAny {
  // 1. Let promise be the this value.
  // 2. Return ? Invoke(promise, "then", « undefined, onRejected »).
104 105 106 107
  // This builtin is attached to JSFunction created by the bootstrapper so
  // `context` is the native context.
  check(Is<NativeContext>(context));
  const nativeContext = UnsafeCast<NativeContext>(context);
108 109 110
  return UnsafeCast<JSAny>(
      InvokeThen(nativeContext, receiver, Undefined, onRejected));
}
111
}