Loading...
Searching...
No Matches
v8-promise.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_PROMISE_H_
6#define INCLUDE_V8_PROMISE_H_
7
8#include "v8-local-handle.h" // NOLINT(build/include_directory)
9#include "v8-object.h" // NOLINT(build/include_directory)
10#include "v8config.h" // NOLINT(build/include_directory)
11
12namespace v8 {
13
14class Context;
15
16#ifndef V8_PROMISE_INTERNAL_FIELD_COUNT
17// Defined using gn arg `v8_promise_internal_field_count`.
18#define V8_PROMISE_INTERNAL_FIELD_COUNT 0
19#endif
20
24class V8_EXPORT Promise : public Object {
25 public:
30 enum PromiseState { kPending, kFulfilled, kRejected };
31
32 class V8_EXPORT Resolver : public Object {
33 public:
38 Local<Context> context);
39
44
50 Local<Value> value);
51
53 Local<Value> value);
54
55 V8_INLINE static Resolver* Cast(Value* value) {
56#ifdef V8_ENABLE_CHECKS
57 CheckCast(value);
58#endif
59 return static_cast<Promise::Resolver*>(value);
60 }
61
62 private:
63 Resolver();
64 static void CheckCast(Value* obj);
65 };
66
85 Local<Function> handler);
86
88 Local<Function> handler);
89
91 Local<Function> on_fulfilled,
92 Local<Function> on_rejected);
93
98 bool HasHandler() const;
99
105
110
115
121
122 V8_INLINE static Promise* Cast(Value* value) {
123#ifdef V8_ENABLE_CHECKS
124 CheckCast(value);
125#endif
126 return static_cast<Promise*>(value);
127 }
128
129 static constexpr int kEmbedderFieldCount = V8_PROMISE_INTERNAL_FIELD_COUNT;
130
131 private:
132 Promise();
133 static void CheckCast(Value* obj);
134};
135
153
154using PromiseHook = void (*)(PromiseHookType type, Local<Promise> promise,
155 Local<Value> parent);
156
157// --- Promise Reject Callback ---
163};
164
166 public:
168 Local<Value> value)
169 : promise_(promise), event_(event), value_(value) {}
170
171 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
172 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
173 V8_INLINE Local<Value> GetValue() const { return value_; }
174
175 private:
176 Local<Promise> promise_;
177 PromiseRejectEvent event_;
178 Local<Value> value_;
179};
180
182
183} // namespace v8
184
185#endif // INCLUDE_V8_PROMISE_H_
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-maybe.h:39
Definition: v8-object.h:234
Definition: v8-promise.h:165
PromiseRejectMessage(Local< Promise > promise, PromiseRejectEvent event, Local< Value > value)
Definition: v8-promise.h:167
Local< Promise > GetPromise() const
Definition: v8-promise.h:171
PromiseRejectEvent GetEvent() const
Definition: v8-promise.h:172
Local< Value > GetValue() const
Definition: v8-promise.h:173
Definition: v8-promise.h:32
static MaybeLocal< Resolver > New(Local< Context > context)
Maybe< bool > Resolve(Local< Context > context, Local< Value > value)
static Resolver * Cast(Value *value)
Definition: v8-promise.h:55
Local< Promise > GetPromise()
Maybe< bool > Reject(Local< Context > context, Local< Value > value)
Definition: v8-promise.h:24
PromiseState
Definition: v8-promise.h:30
@ kFulfilled
Definition: v8-promise.h:30
bool HasHandler() const
void MarkAsHandled()
Local< Value > Result()
void MarkAsSilent()
MaybeLocal< Promise > Then(Local< Context > context, Local< Function > on_fulfilled, Local< Function > on_rejected)
static Promise * Cast(Value *value)
Definition: v8-promise.h:122
PromiseState State()
MaybeLocal< Promise > Catch(Local< Context > context, Local< Function > handler)
MaybeLocal< Promise > Then(Local< Context > context, Local< Function > handler)
Definition: v8-value.h:32
Definition: libplatform.h:15
PromiseRejectEvent
Definition: v8-promise.h:158
@ kPromiseHandlerAddedAfterReject
Definition: v8-promise.h:160
@ kPromiseRejectWithNoHandler
Definition: v8-promise.h:159
@ kPromiseRejectAfterResolved
Definition: v8-promise.h:161
@ kPromiseResolveAfterResolved
Definition: v8-promise.h:162
void(*)(PromiseHookType type, Local< Promise > promise, Local< Value > parent) PromiseHook
Definition: v8-promise.h:155
void(*)(PromiseRejectMessage message) PromiseRejectCallback
Definition: v8-promise.h:181
PromiseHookType
Definition: v8-promise.h:152
#define V8_PROMISE_INTERNAL_FIELD_COUNT
Definition: v8-promise.h:18
#define V8_EXPORT
Definition: v8config.h:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:679