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 #include "jerryscript.h"
17 #include "jerryscript-port.h"
18 #include "jerryscript-port-default.h"
19 #include "test-common.h"
20
21 static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
22 "var p1 = create_promise1();"
23 "var p2 = create_promise2();"
24 "p1.then(function(x) { "
25 " assert(x==='resolved'); "
26 "}); "
27 "p2.catch(function(x) { "
28 " assert(x==='rejected'); "
29 "}); "
30 );
31
32 static int count_in_assert = 0;
33 static jerry_value_t my_promise1;
34 static jerry_value_t my_promise2;
35 static const jerry_char_t s1[] = "resolved";
36 static const jerry_char_t s2[] = "rejected";
37
38 static jerry_value_t
create_promise1_handler(const jerry_value_t func_obj_val,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_cnt)39 create_promise1_handler (const jerry_value_t func_obj_val, /**< function object */
40 const jerry_value_t this_val, /**< this value */
41 const jerry_value_t args_p[], /**< arguments list */
42 const jerry_length_t args_cnt) /**< arguments length */
43 {
44 JERRY_UNUSED (func_obj_val);
45 JERRY_UNUSED (this_val);
46 JERRY_UNUSED (args_p);
47 JERRY_UNUSED (args_cnt);
48
49 jerry_value_t ret = jerry_create_promise ();
50 my_promise1 = jerry_acquire_value (ret);
51
52 return ret;
53 } /* create_promise1_handler */
54
55 static jerry_value_t
create_promise2_handler(const jerry_value_t func_obj_val,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_cnt)56 create_promise2_handler (const jerry_value_t func_obj_val, /**< function object */
57 const jerry_value_t this_val, /**< this value */
58 const jerry_value_t args_p[], /**< arguments list */
59 const jerry_length_t args_cnt) /**< arguments length */
60 {
61 JERRY_UNUSED (func_obj_val);
62 JERRY_UNUSED (this_val);
63 JERRY_UNUSED (args_p);
64 JERRY_UNUSED (args_cnt);
65
66 jerry_value_t ret = jerry_create_promise ();
67 my_promise2 = jerry_acquire_value (ret);
68
69 return ret;
70 } /* create_promise2_handler */
71
72 static jerry_value_t
assert_handler(const jerry_value_t func_obj_val,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_cnt)73 assert_handler (const jerry_value_t func_obj_val, /**< function object */
74 const jerry_value_t this_val, /**< this arg */
75 const jerry_value_t args_p[], /**< function arguments */
76 const jerry_length_t args_cnt) /**< number of function arguments */
77 {
78 JERRY_UNUSED (func_obj_val);
79 JERRY_UNUSED (this_val);
80
81 count_in_assert++;
82
83 if (args_cnt == 1
84 && jerry_value_is_boolean (args_p[0])
85 && jerry_get_boolean_value (args_p[0]))
86 {
87 return jerry_create_boolean (true);
88 }
89 else
90 {
91 TEST_ASSERT (false);
92 }
93 } /* assert_handler */
94
95 /**
96 * Register a JavaScript function in the global object.
97 */
98 static void
register_js_function(const char * name_p,jerry_external_handler_t handler_p)99 register_js_function (const char *name_p, /**< name of the function */
100 jerry_external_handler_t handler_p) /**< function callback */
101 {
102 jerry_value_t global_obj_val = jerry_get_global_object ();
103
104 jerry_value_t function_val = jerry_create_external_function (handler_p);
105 jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) name_p);
106 jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
107
108 jerry_release_value (function_name_val);
109 jerry_release_value (function_val);
110 jerry_release_value (global_obj_val);
111
112 jerry_release_value (result_val);
113 } /* register_js_function */
114
115 int
main(void)116 main (void)
117 {
118 jerry_init (JERRY_INIT_EMPTY);
119
120 if (!jerry_is_feature_enabled (JERRY_FEATURE_PROMISE))
121 {
122 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Promise is disabled!\n");
123 jerry_cleanup ();
124 return 0;
125 }
126
127 register_js_function ("create_promise1", create_promise1_handler);
128 register_js_function ("create_promise2", create_promise2_handler);
129 register_js_function ("assert", assert_handler);
130
131 jerry_value_t parsed_code_val = jerry_parse (NULL,
132 0,
133 test_source,
134 sizeof (test_source) - 1,
135 JERRY_PARSE_NO_OPTS);
136 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
137
138 jerry_value_t res = jerry_run (parsed_code_val);
139 TEST_ASSERT (!jerry_value_is_error (res));
140
141 jerry_release_value (res);
142 jerry_release_value (parsed_code_val);
143
144 /* Test jerry_create_promise and jerry_value_is_promise. */
145 TEST_ASSERT (jerry_value_is_promise (my_promise1));
146 TEST_ASSERT (jerry_value_is_promise (my_promise2));
147
148 TEST_ASSERT (count_in_assert == 0);
149
150 /* Test jerry_resolve_or_reject_promise. */
151 jerry_value_t str_resolve = jerry_create_string (s1);
152 jerry_value_t str_reject = jerry_create_string (s2);
153
154 jerry_resolve_or_reject_promise (my_promise1, str_resolve, true);
155 jerry_resolve_or_reject_promise (my_promise2, str_reject, false);
156
157 /* The resolve/reject function should be invalid after the promise has the result. */
158 jerry_resolve_or_reject_promise (my_promise2, str_resolve, true);
159 jerry_resolve_or_reject_promise (my_promise1, str_reject, false);
160
161 /* Run the jobqueue. */
162 res = jerry_run_all_enqueued_jobs ();
163
164 TEST_ASSERT (!jerry_value_is_error (res));
165 TEST_ASSERT (count_in_assert == 2);
166
167 jerry_release_value (my_promise1);
168 jerry_release_value (my_promise2);
169 jerry_release_value (str_resolve);
170 jerry_release_value (str_reject);
171
172 jerry_cleanup ();
173
174 return 0;
175 } /* main */
176