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// The number of required internal fields can be defined by embedder.
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
74 Local<Function> handler);
75
77 Local<Function> handler);
78
80 Local<Function> on_fulfilled,
81 Local<Function> on_rejected);
82
87 bool HasHandler() const;
88
94
99
104
110
111 V8_INLINE static Promise* Cast(Value* value) {
112#ifdef V8_ENABLE_CHECKS
113 CheckCast(value);
114#endif
115 return static_cast<Promise*>(value);
116 }
117
118 static const int kEmbedderFieldCount = V8_PROMISE_INTERNAL_FIELD_COUNT;
119
120 private:
121 Promise();
122 static void CheckCast(Value* obj);
123};
124
142
143using PromiseHook = void (*)(PromiseHookType type, Local<Promise> promise,
144 Local<Value> parent);
145
146// --- Promise Reject Callback ---
152};
153
155 public:
157 Local<Value> value)
158 : promise_(promise), event_(event), value_(value) {}
159
160 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
161 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
162 V8_INLINE Local<Value> GetValue() const { return value_; }
163
164 private:
165 Local<Promise> promise_;
166 PromiseRejectEvent event_;
167 Local<Value> value_;
168};
169
171
172} // namespace v8
173
174#endif // INCLUDE_V8_PROMISE_H_
Definition: v8-local-handle.h:258
Definition: v8-local-handle.h:619
Definition: v8-maybe.h:32
Definition: v8-object.h:238
Definition: v8-promise.h:154
PromiseRejectMessage(Local< Promise > promise, PromiseRejectEvent event, Local< Value > value)
Definition: v8-promise.h:156
Local< Promise > GetPromise() const
Definition: v8-promise.h:160
PromiseRejectEvent GetEvent() const
Definition: v8-promise.h:161
Local< Value > GetValue() const
Definition: v8-promise.h:162
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:111
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:147
@ kPromiseHandlerAddedAfterReject
Definition: v8-promise.h:149
@ kPromiseRejectWithNoHandler
Definition: v8-promise.h:148
@ kPromiseRejectAfterResolved
Definition: v8-promise.h:150
@ kPromiseResolveAfterResolved
Definition: v8-promise.h:151
void(*)(PromiseHookType type, Local< Promise > promise, Local< Value > parent) PromiseHook
Definition: v8-promise.h:144
void(*)(PromiseRejectMessage message) PromiseRejectCallback
Definition: v8-promise.h:170
PromiseHookType
Definition: v8-promise.h:141
#define V8_PROMISE_INTERNAL_FIELD_COUNT
Definition: v8-promise.h:18
#define V8_EXPORT
Definition: v8config.h:762
#define V8_INLINE
Definition: v8config.h:477
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:637