File indexing completed on 2026-09-13 09:14:39
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_PROMISE_H_
0006 #define INCLUDE_V8_PROMISE_H_
0007
0008 #include "v8-local-handle.h" // NOLINT(build/include_directory)
0009 #include "v8-object.h" // NOLINT(build/include_directory)
0010 #include "v8config.h" // NOLINT(build/include_directory)
0011
0012 namespace v8 {
0013
0014 class Context;
0015
0016 #ifndef V8_PROMISE_INTERNAL_FIELD_COUNT
0017
0018 #define V8_PROMISE_INTERNAL_FIELD_COUNT 0
0019 #endif
0020
0021
0022
0023
0024 class V8_EXPORT Promise : public Object {
0025 public:
0026
0027
0028
0029
0030 enum PromiseState { kPending, kFulfilled, kRejected };
0031
0032 class V8_EXPORT Resolver : public Object {
0033 public:
0034
0035
0036
0037 static V8_WARN_UNUSED_RESULT MaybeLocal<Resolver> New(
0038 Local<Context> context);
0039
0040
0041
0042
0043 Local<Promise> GetPromise();
0044
0045
0046
0047
0048
0049 V8_WARN_UNUSED_RESULT Maybe<bool> Resolve(Local<Context> context,
0050 Local<Value> value);
0051
0052 V8_WARN_UNUSED_RESULT Maybe<bool> Reject(Local<Context> context,
0053 Local<Value> value);
0054
0055 V8_INLINE static Resolver* Cast(Value* value) {
0056 #ifdef V8_ENABLE_CHECKS
0057 CheckCast(value);
0058 #endif
0059 return static_cast<Promise::Resolver*>(value);
0060 }
0061
0062 private:
0063 Resolver();
0064 static void CheckCast(Value* obj);
0065 };
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Catch(Local<Context> context,
0085 Local<Function> handler);
0086
0087 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context,
0088 Local<Function> handler);
0089
0090 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context,
0091 Local<Function> on_fulfilled,
0092 Local<Function> on_rejected);
0093
0094
0095
0096
0097
0098 bool HasHandler() const;
0099
0100
0101
0102
0103
0104 Local<Value> Result();
0105
0106
0107
0108
0109 PromiseState State();
0110
0111
0112
0113
0114 void MarkAsHandled();
0115
0116
0117
0118
0119
0120 void MarkAsSilent();
0121
0122 V8_INLINE static Promise* Cast(Value* value) {
0123 #ifdef V8_ENABLE_CHECKS
0124 CheckCast(value);
0125 #endif
0126 return static_cast<Promise*>(value);
0127 }
0128
0129 static constexpr int kEmbedderFieldCount = V8_PROMISE_INTERNAL_FIELD_COUNT;
0130
0131 private:
0132 Promise();
0133 static void CheckCast(Value* obj);
0134 };
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 enum class PromiseHookType { kInit, kResolve, kBefore, kAfter };
0153
0154 using PromiseHook = void (*)(PromiseHookType type, Local<Promise> promise,
0155 Local<Value> parent);
0156
0157
0158 enum PromiseRejectEvent {
0159 kPromiseRejectWithNoHandler = 0,
0160 kPromiseHandlerAddedAfterReject = 1,
0161 kPromiseRejectAfterResolved = 2,
0162 kPromiseResolveAfterResolved = 3,
0163 };
0164
0165 class PromiseRejectMessage {
0166 public:
0167 PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event,
0168 Local<Value> value)
0169 : promise_(promise), event_(event), value_(value) {}
0170
0171 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
0172 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
0173 V8_INLINE Local<Value> GetValue() const { return value_; }
0174
0175 private:
0176 Local<Promise> promise_;
0177 PromiseRejectEvent event_;
0178 Local<Value> value_;
0179 };
0180
0181 using PromiseRejectCallback = void (*)(PromiseRejectMessage message);
0182
0183 }
0184
0185 #endif