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 /**
22 * Empty constructor
23 */
24 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)25 construct_handler (const jerry_value_t func_obj_val, /**< function object */
26 const jerry_value_t this_val, /**< this arg */
27 const jerry_value_t args_p[], /**< function arguments */
28 const jerry_length_t args_cnt) /**< number of function arguments */
29 {
30 JERRY_UNUSED (func_obj_val);
31 JERRY_UNUSED (this_val);
32
33 TEST_ASSERT (args_cnt == 1);
34 TEST_ASSERT (jerry_get_number_value (args_p[0]) == 1.0);
35
36 return jerry_create_undefined ();
37 } /* construct_handler */
38
39 int
main(void)40 main (void)
41 {
42 /* Test JERRY_FEATURE_SYMBOL feature as it is a must-have in ES2015 */
43 if (!jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
44 {
45 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Skipping test, ES2015 support is disabled.\n");
46 return 0;
47 }
48
49 jerry_init (JERRY_INIT_EMPTY);
50
51 {
52 jerry_value_t global_obj_val = jerry_get_global_object ();
53
54 jerry_value_t function_val = jerry_create_external_function (construct_handler);
55 jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) "Demo");
56 jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
57 TEST_ASSERT (!jerry_value_is_error (result_val));
58 TEST_ASSERT (jerry_get_boolean_value (result_val) == true);
59 jerry_release_value (result_val);
60 jerry_release_value (function_name_val);
61 jerry_release_value (global_obj_val);
62 jerry_release_value (function_val);
63 }
64
65 {
66 static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
67 "class Sub1 extends Demo { constructor () { super (1); } };"
68 "new Sub1 ()"
69 );
70
71 jerry_value_t parsed_code_val = jerry_parse (NULL,
72 0,
73 test_source,
74 sizeof (test_source) - 1,
75 JERRY_PARSE_NO_OPTS);
76 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
77
78 jerry_value_t result = jerry_run (parsed_code_val);
79 TEST_ASSERT (!jerry_value_is_error (result));
80
81 jerry_release_value (result);
82 jerry_release_value (parsed_code_val);
83 }
84
85 {
86 static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
87 "class Sub2 extends Demo { };"
88 "new Sub2 (1)"
89 );
90
91 jerry_value_t parsed_code_val = jerry_parse (NULL,
92 0,
93 test_source,
94 sizeof (test_source) - 1,
95 JERRY_PARSE_NO_OPTS);
96 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
97
98 jerry_value_t result = jerry_run (parsed_code_val);
99 TEST_ASSERT (!jerry_value_is_error (result));
100
101 jerry_release_value (result);
102 jerry_release_value (parsed_code_val);
103 }
104
105 jerry_cleanup ();
106 return 0;
107 } /* main */
108