1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "config.h"
17425bb815Sopenharmony_ci#include "jerryscript.h"
18425bb815Sopenharmony_ci#include "jerryscript-port.h"
19425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
20425bb815Sopenharmony_ci#include "test-common.h"
21425bb815Sopenharmony_ci#include <gtest/gtest.h>
22425bb815Sopenharmony_ci
23425bb815Sopenharmony_cistatic jerry_value_t
24425bb815Sopenharmony_cicallback_func (const jerry_value_t function_obj,
25425bb815Sopenharmony_ci               const jerry_value_t this_val,
26425bb815Sopenharmony_ci               const jerry_value_t args_p[],
27425bb815Sopenharmony_ci               const jerry_length_t args_count)
28425bb815Sopenharmony_ci{
29425bb815Sopenharmony_ci  JERRY_UNUSED (function_obj);
30425bb815Sopenharmony_ci  JERRY_UNUSED (this_val);
31425bb815Sopenharmony_ci  JERRY_UNUSED (args_p);
32425bb815Sopenharmony_ci  JERRY_UNUSED (args_count);
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_ci  jerry_value_t value = jerry_create_string ((jerry_char_t *) "Abort run!");
35425bb815Sopenharmony_ci  value = jerry_create_abort_from_value (value, true);
36425bb815Sopenharmony_ci  return value;
37425bb815Sopenharmony_ci} /* callback_func */
38425bb815Sopenharmony_ciclass AportTest : public testing::Test{
39425bb815Sopenharmony_cipublic:
40425bb815Sopenharmony_ci    static void SetUpTestCase()
41425bb815Sopenharmony_ci    {
42425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "AportTest SetUpTestCase";
43425bb815Sopenharmony_ci    }
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_ci    static void TearDownTestCase()
46425bb815Sopenharmony_ci    {
47425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "AportTest TearDownTestCase";
48425bb815Sopenharmony_ci    }
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci    void SetUp() override {}
51425bb815Sopenharmony_ci    void TearDown() override {}
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_ci};
54425bb815Sopenharmony_ci
55425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
56425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
57425bb815Sopenharmony_ci{
58425bb815Sopenharmony_ci    (void)cb_data;
59425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
60425bb815Sopenharmony_ci    return malloc(newSize);
61425bb815Sopenharmony_ci}
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_ciHWTEST_F(AportTest, Test001, testing::ext::TestSize.Level1)
64425bb815Sopenharmony_ci{
65425bb815Sopenharmony_ci  jerry_context_t* ctx_p = jerry_create_context(1024 * 1024 * 50, context_alloc_fn, NULL);
66425bb815Sopenharmony_ci  jerry_port_default_set_current_context(ctx_p);
67425bb815Sopenharmony_ci  TEST_INIT ();
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
70425bb815Sopenharmony_ci
71425bb815Sopenharmony_ci  jerry_value_t global = jerry_get_global_object ();
72425bb815Sopenharmony_ci  jerry_value_t callback_name = jerry_create_string ((jerry_char_t *) "callback");
73425bb815Sopenharmony_ci  jerry_value_t func = jerry_create_external_function (callback_func);
74425bb815Sopenharmony_ci  jerry_value_t res = jerry_set_property (global, callback_name, func);
75425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (res));
76425bb815Sopenharmony_ci
77425bb815Sopenharmony_ci  jerry_release_value (res);
78425bb815Sopenharmony_ci  jerry_release_value (func);
79425bb815Sopenharmony_ci  jerry_release_value (callback_name);
80425bb815Sopenharmony_ci  jerry_release_value (global);
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ci  const jerry_char_t inf_loop_code_src1[] = TEST_STRING_LITERAL (
83425bb815Sopenharmony_ci    "while(true) {\n"
84425bb815Sopenharmony_ci    "  with ({}) {\n"
85425bb815Sopenharmony_ci    "    try {\n"
86425bb815Sopenharmony_ci    "      callback();\n"
87425bb815Sopenharmony_ci    "    } catch (e) {\n"
88425bb815Sopenharmony_ci    "    } finally {\n"
89425bb815Sopenharmony_ci    "    }\n"
90425bb815Sopenharmony_ci    "  }\n"
91425bb815Sopenharmony_ci    "}"
92425bb815Sopenharmony_ci  );
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ci  jerry_value_t parsed_code_val = jerry_parse (NULL,
95425bb815Sopenharmony_ci                                               0,
96425bb815Sopenharmony_ci                                               inf_loop_code_src1,
97425bb815Sopenharmony_ci                                               sizeof (inf_loop_code_src1) - 1,
98425bb815Sopenharmony_ci                                               JERRY_PARSE_NO_OPTS);
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
101425bb815Sopenharmony_ci  res = jerry_run (parsed_code_val);
102425bb815Sopenharmony_ci
103425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_abort (res));
104425bb815Sopenharmony_ci
105425bb815Sopenharmony_ci  jerry_release_value (res);
106425bb815Sopenharmony_ci  jerry_release_value (parsed_code_val);
107425bb815Sopenharmony_ci
108425bb815Sopenharmony_ci  const jerry_char_t inf_loop_code_src2[] = TEST_STRING_LITERAL (
109425bb815Sopenharmony_ci    "function f() {"
110425bb815Sopenharmony_ci    "  while(true) {\n"
111425bb815Sopenharmony_ci    "    with ({}) {\n"
112425bb815Sopenharmony_ci    "      try {\n"
113425bb815Sopenharmony_ci    "        callback();\n"
114425bb815Sopenharmony_ci    "      } catch (e) {\n"
115425bb815Sopenharmony_ci    "      } finally {\n"
116425bb815Sopenharmony_ci    "      }\n"
117425bb815Sopenharmony_ci    "    }\n"
118425bb815Sopenharmony_ci    "  }"
119425bb815Sopenharmony_ci    "}\n"
120425bb815Sopenharmony_ci    "function g() {\n"
121425bb815Sopenharmony_ci    "  for (a in { x:5 })\n"
122425bb815Sopenharmony_ci    "    f();\n"
123425bb815Sopenharmony_ci    "}\n"
124425bb815Sopenharmony_ci    "\n"
125425bb815Sopenharmony_ci    "with({})\n"
126425bb815Sopenharmony_ci    " f();\n"
127425bb815Sopenharmony_ci  );
128425bb815Sopenharmony_ci
129425bb815Sopenharmony_ci  parsed_code_val = jerry_parse (NULL,
130425bb815Sopenharmony_ci                                 0,
131425bb815Sopenharmony_ci                                 inf_loop_code_src2,
132425bb815Sopenharmony_ci                                 sizeof (inf_loop_code_src2) - 1,
133425bb815Sopenharmony_ci                                 JERRY_PARSE_NO_OPTS);
134425bb815Sopenharmony_ci
135425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
136425bb815Sopenharmony_ci  res = jerry_run (parsed_code_val);
137425bb815Sopenharmony_ci
138425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_abort (res));
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_ci  jerry_release_value (res);
141425bb815Sopenharmony_ci  jerry_release_value (parsed_code_val);
142425bb815Sopenharmony_ci
143425bb815Sopenharmony_ci  /* Test flag overwrites. */
144425bb815Sopenharmony_ci  jerry_value_t value = jerry_create_string ((jerry_char_t *) "Error description");
145425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_abort (value));
146425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (value));
147425bb815Sopenharmony_ci
148425bb815Sopenharmony_ci  value = jerry_create_abort_from_value (value, true);
149425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_abort (value));
150425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_error (value));
151425bb815Sopenharmony_ci
152425bb815Sopenharmony_ci  value = jerry_create_error_from_value (value, true);
153425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_abort (value));
154425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_error (value));
155425bb815Sopenharmony_ci
156425bb815Sopenharmony_ci  value = jerry_create_abort_from_value (value, true);
157425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_abort (value));
158425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_error (value));
159425bb815Sopenharmony_ci
160425bb815Sopenharmony_ci  jerry_release_value (value);
161425bb815Sopenharmony_ci
162425bb815Sopenharmony_ci  jerry_cleanup ();
163425bb815Sopenharmony_ci  free (ctx_p);
164425bb815Sopenharmony_ci}
165