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 * Empty constructor
23 */
24static jerry_value_t
25construct_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
39class RegressionTest : public testing::Test{
40public:
41    static void SetUpTestCase()
42    {
43        GTEST_LOG_(INFO) << "RegressionTest SetUpTestCase";
44    }
45
46    static void TearDownTestCase()
47    {
48        GTEST_LOG_(INFO) << "RegressionTest TearDownTestCase";
49    }
50
51    void SetUp() override {}
52    void TearDown() override {}
53
54};
55
56static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
57static void* context_alloc_fn(size_t size, void* cb_data)
58{
59    (void)cb_data;
60    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
61    return malloc(newSize);
62}
63
64HWTEST_F(RegressionTest, Test001, testing::ext::TestSize.Level1)
65{
66
67  /* Test JERRY_FEATURE_SYMBOL feature as it is a must-have in ES2015 */
68  if (!jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
69  {
70    jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Skipping test, ES2015 support is disabled.\n");
71  }
72  jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
73  jerry_port_default_set_current_context (ctx_p);
74  jerry_init (JERRY_INIT_EMPTY);
75
76  {
77    jerry_value_t global_obj_val = jerry_get_global_object ();
78
79    jerry_value_t function_val = jerry_create_external_function (construct_handler);
80    jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) "Demo");
81    jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
82    TEST_ASSERT (!jerry_value_is_error (result_val));
83    TEST_ASSERT (jerry_get_boolean_value (result_val) == true);
84    jerry_release_value (result_val);
85    jerry_release_value (function_name_val);
86    jerry_release_value (global_obj_val);
87    jerry_release_value (function_val);
88  }
89
90  {
91    static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
92        "class Sub1 extends Demo { constructor () { super (1); } };"
93        "new Sub1 ()"
94    );
95
96    jerry_value_t parsed_code_val = jerry_parse (NULL,
97                                                 0,
98                                                 test_source,
99                                                 sizeof (test_source) - 1,
100                                                 JERRY_PARSE_NO_OPTS);
101    TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
102
103    jerry_value_t result = jerry_run (parsed_code_val);
104    TEST_ASSERT (!jerry_value_is_error (result));
105
106    jerry_release_value (result);
107    jerry_release_value (parsed_code_val);
108  }
109
110  {
111    static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
112      "class Sub2 extends Demo { };"
113      "new Sub2 (1)"
114    );
115
116    jerry_value_t parsed_code_val = jerry_parse (NULL,
117                                                 0,
118                                                 test_source,
119                                                 sizeof (test_source) - 1,
120                                                 JERRY_PARSE_NO_OPTS);
121    TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
122
123    jerry_value_t result = jerry_run (parsed_code_val);
124    TEST_ASSERT (!jerry_value_is_error (result));
125
126    jerry_release_value (result);
127    jerry_release_value (parsed_code_val);
128  }
129
130  jerry_cleanup ();
131  free (ctx_p);
132}
133