1 | /* Copyright JS Foundation and other contributors, http://js.foundation |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | * you may not use this file except in compliance with the License. |
5 | * You may obtain a copy of the License at |
6 | * |
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software |
10 | * distributed under the License is distributed on an "AS IS" BASIS |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | * See the License for the specific language governing permissions and |
13 | * limitations under the License. |
14 | */ |
15 | |
16 | #ifndef ECMA_PROMISE_OBJECT_H |
17 | #define ECMA_PROMISE_OBJECT_H |
18 | |
19 | #if ENABLED (JERRY_BUILTIN_PROMISE) |
20 | #include "ecma-globals.h" |
21 | |
22 | /** \addtogroup ecma ECMA |
23 | * @{ |
24 | * |
25 | * \addtogroup ecmaarraybufferobject ECMA ArrayBuffer object related routines |
26 | * @{ |
27 | */ |
28 | |
29 | /** |
30 | * The PromiseState of promise object. |
31 | */ |
32 | typedef enum |
33 | { |
34 | ECMA_PROMISE_IS_PENDING = (1 << 0), /**< pending state */ |
35 | ECMA_PROMISE_IS_FULFILLED = (1 << 1), /**< fulfilled state */ |
36 | ECMA_PROMISE_ALREADY_RESOLVED = (1 << 2), /**< already resolved */ |
37 | ECMA_PROMISE_HANDLED = (1 << 3), /**< ES11: 25.6.6 [[PromiseIsHandled]] internal slot */ |
38 | } ecma_promise_flags_t; |
39 | |
40 | /** |
41 | * Indicates the type of the executor in promise construct. |
42 | */ |
43 | typedef enum |
44 | { |
45 | ECMA_PROMISE_EXECUTOR_FUNCTION, /**< the executor is a function, it is for the usual constructor */ |
46 | ECMA_PROMISE_EXECUTOR_EMPTY /**< the executor is empty, it is for external C API */ |
47 | } ecma_promise_executor_type_t; |
48 | |
49 | /** |
50 | * Description of a promise resolving function. |
51 | */ |
52 | typedef struct |
53 | { |
54 | ecma_extended_object_t ; |
55 | ecma_value_t promise; |
56 | } ecma_promise_resolver_t; |
57 | |
58 | /** |
59 | * Description of the promise object. |
60 | * It need more space than normal object to store builtin properties. |
61 | */ |
62 | typedef struct |
63 | { |
64 | ecma_extended_object_t ; /**< extended object part */ |
65 | ecma_collection_t *reactions; /**< list of promise reactions */ |
66 | ecma_value_t resolve; /**< resolve function */ |
67 | ecma_value_t reject; /**< reject function */ |
68 | } ecma_promise_object_t; |
69 | |
70 | /** |
71 | * Description of the finally function object |
72 | */ |
73 | typedef struct |
74 | { |
75 | ecma_extended_object_t ; /**< extended object part */ |
76 | ecma_value_t constructor; /**< [[Constructor]] internal slot */ |
77 | ecma_value_t on_finally; /**< [[OnFinally]] internal slot */ |
78 | } ecma_promise_finally_function_t; |
79 | |
80 | /** |
81 | * Description of the thunk function object |
82 | */ |
83 | typedef struct |
84 | { |
85 | ecma_extended_object_t ; /**< extended object part */ |
86 | ecma_value_t value; /**< value thunk */ |
87 | } ecma_promise_value_thunk_t; |
88 | |
89 | /* The Promise reaction is a compressed structure, where each item can |
90 | * be a sequence of up to three ecma object values as seen below: |
91 | * |
92 | * [ Capability ][ Optional fullfilled callback ][ Optional rejected callback ] |
93 | * [ Async function callback ] |
94 | * |
95 | * The first member is an object, which lower bits specify the type of the reaction: |
96 | * bit 2 is not set: callback reactions |
97 | * The first two objects specify the resolve/reject functions of the promise |
98 | * returned by the `then` operation which can be used to chain event handlers. |
99 | * |
100 | * bit 0: has a fullfilled callback |
101 | * bit 1: has a rejected callback |
102 | * |
103 | * bit 2 is set: async function callback |
104 | */ |
105 | |
106 | bool ecma_is_promise (ecma_object_t *obj_p); |
107 | ecma_value_t ecma_op_create_promise_object (ecma_value_t executor, ecma_promise_executor_type_t type); |
108 | uint16_t ecma_promise_get_flags (ecma_object_t *promise_p); |
109 | ecma_value_t ecma_promise_get_result (ecma_object_t *promise_p); |
110 | void ecma_reject_promise (ecma_value_t promise, ecma_value_t reason); |
111 | void ecma_fulfill_promise (ecma_value_t promise, ecma_value_t value); |
112 | ecma_object_t *ecma_promise_new_capability (ecma_value_t constructor); |
113 | ecma_value_t ecma_promise_reject_or_resolve (ecma_value_t this_arg, ecma_value_t value, bool is_resolve); |
114 | ecma_value_t ecma_promise_then (ecma_value_t promise, ecma_value_t on_fulfilled, ecma_value_t on_rejected); |
115 | ecma_value_t ecma_value_thunk_helper_cb (const ecma_value_t function_obj, |
116 | const ecma_value_t this_val, |
117 | const ecma_value_t args_p[], |
118 | const uint32_t args_count); |
119 | ecma_value_t ecma_value_thunk_thrower_cb (const ecma_value_t function_obj, |
120 | const ecma_value_t this_val, |
121 | const ecma_value_t args_p[], |
122 | const uint32_t args_count); |
123 | ecma_value_t ecma_promise_then_finally_cb (const ecma_value_t function_obj, |
124 | const ecma_value_t this_val, |
125 | const ecma_value_t args_p[], |
126 | const uint32_t args_count); |
127 | ecma_value_t ecma_promise_catch_finally_cb (const ecma_value_t function_obj, |
128 | const ecma_value_t this_val, |
129 | const ecma_value_t args_p[], |
130 | const uint32_t args_count); |
131 | ecma_value_t |
132 | ecma_promise_reject_handler (const ecma_value_t function, |
133 | const ecma_value_t this_arg, |
134 | const ecma_value_t argv[], |
135 | const uint32_t argc); |
136 | ecma_value_t |
137 | ecma_promise_resolve_handler (const ecma_value_t function, |
138 | const ecma_value_t this_arg, |
139 | const ecma_value_t argv[], |
140 | const uint32_t argc); |
141 | |
142 | ecma_value_t ecma_promise_finally (ecma_value_t promise, ecma_value_t on_finally); |
143 | void ecma_promise_async_then (ecma_value_t promise, ecma_value_t executable_object); |
144 | ecma_value_t ecma_promise_async_await (ecma_extended_object_t *async_generator_object_p, ecma_value_t value); |
145 | void ecma_promise_create_resolving_functions (ecma_promise_object_t *object_p); |
146 | |
147 | uint32_t ecma_promise_remaining_inc_or_dec (ecma_value_t remaining, bool is_inc); |
148 | ecma_value_t ecma_promise_all_handler_cb (const ecma_value_t function_obj, const ecma_value_t this_val, |
149 | const ecma_value_t args_p[], const uint32_t args_count); |
150 | |
151 | ecma_value_t ecma_op_get_capabilities_executor_cb (const ecma_value_t function_obj, const ecma_value_t this_val, |
152 | const ecma_value_t args_p[], const uint32_t args_count); |
153 | |
154 | /** |
155 | * @} |
156 | * @} |
157 | */ |
158 | |
159 | #endif /* ENABLED (JERRY_BUILTIN_PROMISE) */ |
160 | #endif /* !ECMA_PROMISE_OBJECT_H */ |
161 | |