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