1 | // Copyright (C) 2018 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #ifndef QV4PROMISEOBJECT_H |
4 | #define QV4PROMISEOBJECT_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include "qv4object_p.h" |
18 | #include "qv4functionobject_p.h" |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | namespace QV4 { |
23 | |
24 | struct PromiseCapability; |
25 | |
26 | namespace Promise { |
27 | |
28 | struct ReactionEvent; |
29 | struct ResolveThenableEvent; |
30 | |
31 | class ReactionHandler : public QObject |
32 | { |
33 | Q_OBJECT |
34 | |
35 | public: |
36 | ReactionHandler(QObject *parent = nullptr); |
37 | ~ReactionHandler() override; |
38 | |
39 | void addReaction(ExecutionEngine *e, const Value *reaction, const Value *value); |
40 | void addResolveThenable(ExecutionEngine *e, const PromiseObject *promise, const Object *thenable, const FunctionObject *then); |
41 | |
42 | protected: |
43 | void customEvent(QEvent *event) override; |
44 | void executeReaction(ReactionEvent *event); |
45 | void executeResolveThenable(ResolveThenableEvent *event); |
46 | }; |
47 | |
48 | } // Promise |
49 | |
50 | namespace Heap { |
51 | |
52 | struct PromiseCtor : FunctionObject { |
53 | void init(QV4::ExecutionContext *scope); |
54 | }; |
55 | |
56 | #define PromiseObjectMembers(class, Member) \ |
57 | Member(class, HeapValue, HeapValue, resolution) \ |
58 | Member(class, HeapValue, HeapValue, fulfillReactions) \ |
59 | Member(class, HeapValue, HeapValue, rejectReactions) |
60 | |
61 | DECLARE_HEAP_OBJECT(PromiseObject, Object) { |
62 | DECLARE_MARKOBJECTS(PromiseObject) |
63 | void init(ExecutionEngine *e); |
64 | |
65 | enum State { |
66 | Pending, |
67 | Fulfilled, |
68 | Rejected |
69 | }; |
70 | |
71 | void setState(State); |
72 | bool isSettled() const; |
73 | bool isPending() const; |
74 | bool isFulfilled() const; |
75 | bool isRejected() const; |
76 | |
77 | State state; |
78 | |
79 | void triggerFullfillReactions(ExecutionEngine *e); |
80 | void triggerRejectReactions(ExecutionEngine *e); |
81 | }; |
82 | |
83 | #define PromiseCapabilityMembers(class, Member) \ |
84 | Member(class, HeapValue, HeapValue, promise) \ |
85 | Member(class, HeapValue, HeapValue, resolve) \ |
86 | Member(class, HeapValue, HeapValue, reject) |
87 | |
88 | DECLARE_HEAP_OBJECT(PromiseCapability, Object) { |
89 | DECLARE_MARKOBJECTS(PromiseCapability) |
90 | }; |
91 | |
92 | #define PromiseReactionMembers(class, Member) \ |
93 | Member(class, HeapValue, HeapValue, handler) \ |
94 | Member(class, Pointer, PromiseCapability*, capability) |
95 | |
96 | DECLARE_HEAP_OBJECT(PromiseReaction, Object) { |
97 | DECLARE_MARKOBJECTS(PromiseReaction) |
98 | |
99 | static Heap::PromiseReaction *createFulfillReaction(ExecutionEngine* e, const QV4::PromiseCapability *capability, const QV4::FunctionObject *onFulfilled); |
100 | static Heap::PromiseReaction *createRejectReaction(ExecutionEngine* e, const QV4::PromiseCapability *capability, const QV4::FunctionObject *onRejected); |
101 | |
102 | void triggerWithValue(ExecutionEngine *e, const Value *value); |
103 | |
104 | enum Type { |
105 | Function, |
106 | Identity, |
107 | Thrower |
108 | }; |
109 | |
110 | Type type; |
111 | |
112 | friend class ReactionHandler; |
113 | }; |
114 | |
115 | #define CapabilitiesExecutorWrapperMembers(class, Member) \ |
116 | Member(class, Pointer, PromiseCapability*, capabilities) |
117 | |
118 | DECLARE_HEAP_OBJECT(CapabilitiesExecutorWrapper, FunctionObject) { |
119 | DECLARE_MARKOBJECTS(CapabilitiesExecutorWrapper) |
120 | void init(); |
121 | void destroy(); |
122 | }; |
123 | |
124 | #define PromiseExecutionStateMembers(class, Member) \ |
125 | Member(class, HeapValue, HeapValue, values) \ |
126 | Member(class, HeapValue, HeapValue, capability) |
127 | |
128 | DECLARE_HEAP_OBJECT(PromiseExecutionState, FunctionObject) { |
129 | DECLARE_MARKOBJECTS(PromiseExecutionState) |
130 | void init(); |
131 | |
132 | uint index; |
133 | uint remainingElementCount; |
134 | }; |
135 | |
136 | #define ResolveElementWrapperMembers(class, Member) \ |
137 | Member(class, HeapValue, HeapValue, state) |
138 | |
139 | DECLARE_HEAP_OBJECT(ResolveElementWrapper, FunctionObject) { |
140 | DECLARE_MARKOBJECTS(ResolveElementWrapper) |
141 | void init(); |
142 | |
143 | uint index; |
144 | bool alreadyResolved; |
145 | }; |
146 | |
147 | #define ResolveWrapperMembers(class, Member) \ |
148 | Member(class, Pointer, PromiseObject*, promise) |
149 | |
150 | DECLARE_HEAP_OBJECT(ResolveWrapper, FunctionObject) { |
151 | DECLARE_MARKOBJECTS(ResolveWrapper) |
152 | void init(); |
153 | |
154 | bool alreadyResolved; |
155 | }; |
156 | |
157 | #define RejectWrapperMembers(class, Member) \ |
158 | Member(class, Pointer, PromiseObject*, promise) |
159 | |
160 | DECLARE_HEAP_OBJECT(RejectWrapper, FunctionObject) { |
161 | DECLARE_MARKOBJECTS(RejectWrapper) |
162 | void init(); |
163 | |
164 | bool alreadyResolved; |
165 | }; |
166 | |
167 | } // Heap |
168 | |
169 | struct PromiseReaction : Object |
170 | { |
171 | V4_OBJECT2(PromiseReaction, Object) |
172 | }; |
173 | |
174 | struct PromiseCapability : Object |
175 | { |
176 | V4_OBJECT2(PromiseCapability, Object) |
177 | }; |
178 | |
179 | struct PromiseExecutionState : Object |
180 | { |
181 | V4_OBJECT2(PromiseExecutionState, Object) |
182 | }; |
183 | |
184 | struct Q_QML_PRIVATE_EXPORT PromiseObject : Object |
185 | { |
186 | V4_OBJECT2(PromiseObject, Object) |
187 | V4_NEEDS_DESTROY |
188 | V4_PROTOTYPE(promisePrototype) |
189 | }; |
190 | |
191 | struct PromiseCtor: FunctionObject |
192 | { |
193 | V4_OBJECT2(PromiseCtor, FunctionObject) |
194 | |
195 | static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *); |
196 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
197 | |
198 | static ReturnedValue method_resolve(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
199 | static ReturnedValue method_reject(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
200 | |
201 | static ReturnedValue method_all(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
202 | static ReturnedValue method_race(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
203 | }; |
204 | |
205 | struct PromisePrototype : Object |
206 | { |
207 | void init(ExecutionEngine *engine, Object *ctor); |
208 | |
209 | static ReturnedValue method_then(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
210 | static ReturnedValue method_catch(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
211 | }; |
212 | |
213 | struct CapabilitiesExecutorWrapper: FunctionObject { |
214 | V4_OBJECT2(CapabilitiesExecutorWrapper, FunctionObject) |
215 | |
216 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
217 | }; |
218 | |
219 | struct ResolveElementWrapper : FunctionObject { |
220 | V4_OBJECT2(ResolveElementWrapper, FunctionObject) |
221 | |
222 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
223 | }; |
224 | |
225 | struct ResolveWrapper : FunctionObject { |
226 | V4_OBJECT2(ResolveWrapper, FunctionObject) |
227 | |
228 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
229 | }; |
230 | |
231 | struct RejectWrapper : FunctionObject { |
232 | V4_OBJECT2(RejectWrapper, FunctionObject) |
233 | |
234 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
235 | }; |
236 | |
237 | } // QV4 |
238 | |
239 | QT_END_NAMESPACE |
240 | |
241 | #endif // QV4PROMISEOBJECT_H |
242 | |