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_ES2015_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 */
32typedef 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_flags_t;
38
39/**
40 * Indicates the type of the executor in promise construct.
41 */
42typedef enum
43{
44  ECMA_PROMISE_EXECUTOR_FUNCTION, /**< the executor is a function, it is for the usual constructor */
45  ECMA_PROMISE_EXECUTOR_OBJECT, /**< the executor is an object, it is for the `then` routine */
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 the promise resolving functions.
51 */
52typedef struct
53{
54  ecma_value_t resolve; /**< the resolve function */
55  ecma_value_t reject; /**< the reject function */
56} ecma_promise_resolving_functions_t;
57
58/**
59 * Description of the promise object.
60 * It need more space than normal object to store builtin properties.
61 */
62typedef struct
63{
64  ecma_extended_object_t header; /**< extended object part */
65  ecma_collection_t *reactions; /**< list of promise reactions */
66} ecma_promise_object_t;
67
68/* The Promise reaction is a compressed structure, where each item can
69 * be a sequence of up to three ecma object values as seen below:
70 *
71 * [ Capability ][ Optional fullfilled callback ][ Optional rejected callback ]
72 * [ Async function callback ]
73 *
74 * The first member is an object, which lower bits specify the type of the reaction:
75 *   bit 2 is not set: callback reactions
76 *     The first two objects specify the resolve/reject functions of the promise
77 *     returned by the `then` operation which can be used to chain event handlers.
78 *
79 *     bit 0: has a fullfilled callback
80 *     bit 1: has a rejected callback
81 *
82 *   bit 2 is set: async function callback
83 */
84
85bool ecma_is_promise (ecma_object_t *obj_p);
86ecma_value_t ecma_op_create_promise_object (ecma_value_t executor, ecma_promise_executor_type_t type);
87uint16_t ecma_promise_get_flags (ecma_object_t *promise_p);
88ecma_value_t ecma_promise_get_result (ecma_object_t *promise_p);
89ecma_value_t ecma_promise_new_capability (ecma_value_t constructor);
90ecma_value_t ecma_promise_reject_or_resolve (ecma_value_t this_arg, ecma_value_t value, bool is_resolve);
91ecma_value_t ecma_promise_then (ecma_value_t promise, ecma_value_t on_fulfilled, ecma_value_t on_rejected);
92void ecma_promise_create_resolving_functions (ecma_object_t *object_p, ecma_promise_resolving_functions_t *funcs,
93                                              bool create_already_resolved);
94void ecma_promise_free_resolving_functions (ecma_promise_resolving_functions_t *funcs);
95
96/**
97 * @}
98 * @}
99 */
100
101#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */
102#endif /* !ECMA_PROMISE_OBJECT_H */
103