summaryrefslogtreecommitdiff
path: root/chromium/v8/src/builtins/promise-constructor.tq
blob: dbf1fe2f4ded2646cf19530f1fbb27ec316d7af9 (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
53
54
55
56
57
58
59
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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 {
extern transitioning runtime
DebugPushPromise(implicit context: Context)(JSAny): JSAny;

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

extern transitioning runtime
PromiseHookInit(implicit context: Context)(Object, Object): JSAny;
}

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

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;
}

extern macro ConstructorBuiltinsAssembler::EmitFastNewObject(
    Context, JSFunction, JSReceiver): JSObject;

extern macro
PromiseBuiltinsAssembler::IsPromiseHookEnabledOrHasAsyncEventDelegate(): bool;

// 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);
  }

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

  const promiseFun = UnsafeCast<JSFunction>(
      context[NativeContextSlot::PROMISE_FUNCTION_INDEX]);

  // Silently fail if the stack looks fishy.
  if (HasAccessCheckFailed(context, promiseFun, executor)) {
    IncrementUseCounter(
        context, SmiConstant(kPromiseConstructorReturnedUndefined));
    return Undefined;
  }

  let result: JSPromise;
  if (promiseFun == newTarget) {
    result = NewJSPromise();
  } else {
    result = UnsafeCast<JSPromise>(EmitFastNewObject(
        context, promiseFun, UnsafeCast<JSReceiver>(newTarget)));
    PromiseInit(result);
    if (IsPromiseHookEnabledOrHasAsyncEventDelegate()) {
      runtime::PromiseHookInit(result, Undefined);
    }
  }

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

  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);
  }

  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 »).
  const nativeContext = LoadNativeContext(context);
  return UnsafeCast<JSAny>(
      InvokeThen(nativeContext, receiver, Undefined, onRejected));
}
}