1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef ECMA_PROMISE_OBJECT_H
17425bb815Sopenharmony_ci#define ECMA_PROMISE_OBJECT_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015_BUILTIN_PROMISE)
20425bb815Sopenharmony_ci#include "ecma-globals.h"
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci/** \addtogroup ecma ECMA
23425bb815Sopenharmony_ci * @{
24425bb815Sopenharmony_ci *
25425bb815Sopenharmony_ci * \addtogroup ecmaarraybufferobject ECMA ArrayBuffer object related routines
26425bb815Sopenharmony_ci * @{
27425bb815Sopenharmony_ci */
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_ci/**
30425bb815Sopenharmony_ci * The PromiseState of promise object.
31425bb815Sopenharmony_ci */
32425bb815Sopenharmony_citypedef enum
33425bb815Sopenharmony_ci{
34425bb815Sopenharmony_ci  ECMA_PROMISE_IS_PENDING = (1 << 0), /**< pending state */
35425bb815Sopenharmony_ci  ECMA_PROMISE_IS_FULFILLED = (1 << 1), /**< fulfilled state */
36425bb815Sopenharmony_ci  ECMA_PROMISE_ALREADY_RESOLVED = (1 << 2), /**< already resolved */
37425bb815Sopenharmony_ci} ecma_promise_flags_t;
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_ci/**
40425bb815Sopenharmony_ci * Indicates the type of the executor in promise construct.
41425bb815Sopenharmony_ci */
42425bb815Sopenharmony_citypedef enum
43425bb815Sopenharmony_ci{
44425bb815Sopenharmony_ci  ECMA_PROMISE_EXECUTOR_FUNCTION, /**< the executor is a function, it is for the usual constructor */
45425bb815Sopenharmony_ci  ECMA_PROMISE_EXECUTOR_OBJECT, /**< the executor is an object, it is for the `then` routine */
46425bb815Sopenharmony_ci  ECMA_PROMISE_EXECUTOR_EMPTY /**< the executor is empty, it is for external C API */
47425bb815Sopenharmony_ci} ecma_promise_executor_type_t;
48425bb815Sopenharmony_ci
49425bb815Sopenharmony_ci/**
50425bb815Sopenharmony_ci * Description of the promise resolving functions.
51425bb815Sopenharmony_ci */
52425bb815Sopenharmony_citypedef struct
53425bb815Sopenharmony_ci{
54425bb815Sopenharmony_ci  ecma_value_t resolve; /**< the resolve function */
55425bb815Sopenharmony_ci  ecma_value_t reject; /**< the reject function */
56425bb815Sopenharmony_ci} ecma_promise_resolving_functions_t;
57425bb815Sopenharmony_ci
58425bb815Sopenharmony_ci/**
59425bb815Sopenharmony_ci * Description of the promise object.
60425bb815Sopenharmony_ci * It need more space than normal object to store builtin properties.
61425bb815Sopenharmony_ci */
62425bb815Sopenharmony_citypedef struct
63425bb815Sopenharmony_ci{
64425bb815Sopenharmony_ci  ecma_extended_object_t header; /**< extended object part */
65425bb815Sopenharmony_ci  ecma_collection_t *reactions; /**< list of promise reactions */
66425bb815Sopenharmony_ci} ecma_promise_object_t;
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ci/* The Promise reaction is a compressed structure, where each item can
69425bb815Sopenharmony_ci * be a sequence of up to three ecma object values as seen below:
70425bb815Sopenharmony_ci *
71425bb815Sopenharmony_ci * [ Capability ][ Optional fullfilled callback ][ Optional rejected callback ]
72425bb815Sopenharmony_ci * [ Async function callback ]
73425bb815Sopenharmony_ci *
74425bb815Sopenharmony_ci * The first member is an object, which lower bits specify the type of the reaction:
75425bb815Sopenharmony_ci *   bit 2 is not set: callback reactions
76425bb815Sopenharmony_ci *     The first two objects specify the resolve/reject functions of the promise
77425bb815Sopenharmony_ci *     returned by the `then` operation which can be used to chain event handlers.
78425bb815Sopenharmony_ci *
79425bb815Sopenharmony_ci *     bit 0: has a fullfilled callback
80425bb815Sopenharmony_ci *     bit 1: has a rejected callback
81425bb815Sopenharmony_ci *
82425bb815Sopenharmony_ci *   bit 2 is set: async function callback
83425bb815Sopenharmony_ci */
84425bb815Sopenharmony_ci
85425bb815Sopenharmony_cibool ecma_is_promise (ecma_object_t *obj_p);
86425bb815Sopenharmony_ciecma_value_t ecma_op_create_promise_object (ecma_value_t executor, ecma_promise_executor_type_t type);
87425bb815Sopenharmony_ciuint16_t ecma_promise_get_flags (ecma_object_t *promise_p);
88425bb815Sopenharmony_ciecma_value_t ecma_promise_get_result (ecma_object_t *promise_p);
89425bb815Sopenharmony_ciecma_value_t ecma_promise_new_capability (ecma_value_t constructor);
90425bb815Sopenharmony_ciecma_value_t ecma_promise_reject_or_resolve (ecma_value_t this_arg, ecma_value_t value, bool is_resolve);
91425bb815Sopenharmony_ciecma_value_t ecma_promise_then (ecma_value_t promise, ecma_value_t on_fulfilled, ecma_value_t on_rejected);
92425bb815Sopenharmony_civoid ecma_promise_create_resolving_functions (ecma_object_t *object_p, ecma_promise_resolving_functions_t *funcs,
93425bb815Sopenharmony_ci                                              bool create_already_resolved);
94425bb815Sopenharmony_civoid ecma_promise_free_resolving_functions (ecma_promise_resolving_functions_t *funcs);
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_ci/**
97425bb815Sopenharmony_ci * @}
98425bb815Sopenharmony_ci * @}
99425bb815Sopenharmony_ci */
100425bb815Sopenharmony_ci
101425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */
102425bb815Sopenharmony_ci#endif /* !ECMA_PROMISE_OBJECT_H */
103