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 /**
23 * Register a JavaScript function in the global object.
24 */
25 static jerry_value_t
register_js_function(const char * name_p,jerry_external_handler_t handler_p)26 register_js_function (const char *name_p, /**< name of the function */
27 jerry_external_handler_t handler_p) /**< function callback */
28 {
29 jerry_value_t global_obj_val = jerry_get_global_object ();
30
31 jerry_value_t function_val = jerry_create_external_function (handler_p);
32 jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) name_p);
33 jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
34
35 jerry_release_value (function_name_val);
36 jerry_release_value (global_obj_val);
37
38 jerry_release_value (result_val);
39
40 return function_val;
41 } /* register_js_function */
42
43 enum
44 {
45 TEST_ID_SIMPLE_CONSTRUCT = 1,
46 TEST_ID_SIMPLE_CALL = 2,
47 TEST_ID_CONSTRUCT_AND_CALL_SUB = 3,
48 };
49
50 static jerry_value_t
construct_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)51 construct_handler (const jerry_value_t func_obj_val, /**< function object */
52 const jerry_value_t this_val, /**< this arg */
53 const jerry_value_t args_p[], /**< function arguments */
54 const jerry_length_t args_cnt) /**< number of function arguments */
55 {
56 JERRY_UNUSED (func_obj_val);
57 JERRY_UNUSED (this_val);
58 JERRY_UNUSED (args_p);
59
60 if (args_cnt != 1 || !jerry_value_is_number (args_p[0]))
61 {
62 TEST_ASSERT (0 && "Invalid arguments for demo method");
63 }
64
65 int test_id = (int) jerry_get_number_value (args_p[0]);
66
67 switch (test_id)
68 {
69 case TEST_ID_SIMPLE_CONSTRUCT:
70 {
71 /* Method was called with "new": new.target should be equal to the function object. */
72 jerry_value_t target = jerry_get_new_target ();
73 TEST_ASSERT (!jerry_value_is_undefined (target));
74 TEST_ASSERT (target == func_obj_val);
75 jerry_release_value (target);
76 break;
77 }
78 case TEST_ID_SIMPLE_CALL:
79 {
80 /* Method was called directly without "new": new.target should be equal undefined. */
81 jerry_value_t target = jerry_get_new_target ();
82 TEST_ASSERT (jerry_value_is_undefined (target));
83 TEST_ASSERT (target != func_obj_val);
84 jerry_release_value (target);
85 break;
86 }
87 case TEST_ID_CONSTRUCT_AND_CALL_SUB:
88 {
89 /* Method was called with "new": new.target should be equal to the function object. */
90 jerry_value_t target = jerry_get_new_target ();
91 TEST_ASSERT (!jerry_value_is_undefined (target));
92 TEST_ASSERT (target == func_obj_val);
93 jerry_release_value (target);
94
95 /* Calling a function should hide the old "new.target". */
96 jerry_value_t sub_arg = jerry_create_number (TEST_ID_SIMPLE_CALL);
97 jerry_value_t func_call_result = jerry_call_function (func_obj_val, this_val, &sub_arg, 1);
98 TEST_ASSERT (!jerry_value_is_error (func_call_result));
99 TEST_ASSERT (jerry_value_is_undefined (func_call_result));
100 break;
101 }
102
103 default:
104 {
105 TEST_ASSERT (0 && "Incorrect test ID");
106 break;
107 }
108 }
109
110 return jerry_create_undefined ();
111 } /* construct_handler */
112
113 class NewtargetTest : public testing::Test{
114 public:
SetUpTestCase()115 static void SetUpTestCase()
116 {
117 GTEST_LOG_(INFO) << "NewtargetTest SetUpTestCase";
118 }
119
TearDownTestCase()120 static void TearDownTestCase()
121 {
122 GTEST_LOG_(INFO) << "NewtargetTest TearDownTestCase";
123 }
124
SetUp()125 void SetUp() override {}
TearDown()126 void TearDown() override {}
127
128 };
129 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)130 static void* context_alloc_fn(size_t size, void* cb_data)
131 {
132 (void)cb_data;
133 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
134 return malloc(newSize);
135 }
136 HWTEST_F(NewtargetTest, Test001, testing::ext::TestSize.Level1)
137 {
138 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
139 jerry_port_default_set_current_context (ctx_p);
140 /* Test JERRY_FEATURE_SYMBOL feature as it is a must-have in ES2015 */
141 if (!jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
142 {
143 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Skipping test, ES2015 support is disabled.\n");
144 }
145
146 jerry_init (JERRY_INIT_EMPTY);
147
148 jerry_value_t demo_func = register_js_function ("Demo", construct_handler);
149
150 {
151 jerry_value_t test_arg = jerry_create_number (TEST_ID_SIMPLE_CONSTRUCT);
152 jerry_value_t constructed = jerry_construct_object (demo_func, &test_arg, 1);
153 TEST_ASSERT (!jerry_value_is_error (constructed));
154 TEST_ASSERT (jerry_value_is_object (constructed));
155 jerry_release_value (test_arg);
156 jerry_release_value (constructed);
157 }
158
159 {
160 jerry_value_t test_arg = jerry_create_number (TEST_ID_SIMPLE_CALL);
161 jerry_value_t this_arg = jerry_create_undefined ();
162 jerry_value_t constructed = jerry_call_function (demo_func, this_arg, &test_arg, 1);
163 TEST_ASSERT (jerry_value_is_undefined (constructed));
164 jerry_release_value (constructed);
165 jerry_release_value (constructed);
166 jerry_release_value (test_arg);
167 }
168
169 {
170 jerry_value_t test_arg = jerry_create_number (TEST_ID_CONSTRUCT_AND_CALL_SUB);
171 jerry_value_t constructed = jerry_construct_object (demo_func, &test_arg, 1);
172 TEST_ASSERT (!jerry_value_is_error (constructed));
173 TEST_ASSERT (jerry_value_is_object (constructed));
174 jerry_release_value (test_arg);
175 jerry_release_value (constructed);
176 }
177
178 {
179 static const jerry_char_t test_source[] = TEST_STRING_LITERAL ("new Demo (1)");
180
181 jerry_value_t parsed_code_val = jerry_parse (NULL,
182 0,
183 test_source,
184 sizeof (test_source) - 1,
185 JERRY_PARSE_NO_OPTS);
186 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
187
188 jerry_value_t res = jerry_run (parsed_code_val);
189 TEST_ASSERT (!jerry_value_is_error (res));
190
191 jerry_release_value (res);
192 jerry_release_value (parsed_code_val);
193 }
194
195 {
196 static const jerry_char_t test_source[] = TEST_STRING_LITERAL ("Demo (2)");
197
198 jerry_value_t parsed_code_val = jerry_parse (NULL,
199 0,
200 test_source,
201 sizeof (test_source) - 1,
202 JERRY_PARSE_NO_OPTS);
203 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
204
205 jerry_value_t res = jerry_run (parsed_code_val);
206 TEST_ASSERT (!jerry_value_is_error (res));
207
208 jerry_release_value (res);
209 jerry_release_value (parsed_code_val);
210 }
211
212 {
213 static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
214 "function base(arg) { new Demo (arg); };"
215 "base (1);"
216 "new base(1);"
217 "new base(3);"
218 );
219
220 jerry_value_t parsed_code_val = jerry_parse (NULL,
221 0,
222 test_source,
223 sizeof (test_source) - 1,
224 JERRY_PARSE_NO_OPTS);
225 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
226
227 jerry_value_t res = jerry_run (parsed_code_val);
228 TEST_ASSERT (!jerry_value_is_error (res));
229
230 jerry_release_value (res);
231 jerry_release_value (parsed_code_val);
232 }
233
234 jerry_release_value (demo_func);
235 jerry_cleanup ();
236 free(ctx_p);
237 }
238