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/**
17425bb815Sopenharmony_ci * Unit test for jerry-ext/handle-scope.
18425bb815Sopenharmony_ci */
19425bb815Sopenharmony_ci
20425bb815Sopenharmony_ci#include "jerryscript.h"
21425bb815Sopenharmony_ci#include "jerryscript-ext/handle-scope.h"
22425bb815Sopenharmony_ci#include "test-common.h"
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_cistatic int native_free_cb_call_count;
25425bb815Sopenharmony_cistatic int reusing_times = 10;
26425bb815Sopenharmony_ci
27425bb815Sopenharmony_cistatic void
28425bb815Sopenharmony_cinative_free_cb (void *native_p)
29425bb815Sopenharmony_ci{
30425bb815Sopenharmony_ci  ++native_free_cb_call_count;
31425bb815Sopenharmony_ci  (void) native_p;
32425bb815Sopenharmony_ci} /* native_free_cb */
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_cistatic const jerry_object_native_info_t native_info =
35425bb815Sopenharmony_ci{
36425bb815Sopenharmony_ci  .free_cb = native_free_cb,
37425bb815Sopenharmony_ci};
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_cistatic jerry_value_t
40425bb815Sopenharmony_cicreate_object (void)
41425bb815Sopenharmony_ci{
42425bb815Sopenharmony_ci  jerry_value_t obj = jerry_create_object ();
43425bb815Sopenharmony_ci  jerry_set_object_native_pointer (obj, NULL, &native_info);
44425bb815Sopenharmony_ci  return obj;
45425bb815Sopenharmony_ci} /* create_object */
46425bb815Sopenharmony_ci
47425bb815Sopenharmony_cistatic void
48425bb815Sopenharmony_citest_handle_scope_val (void)
49425bb815Sopenharmony_ci{
50425bb815Sopenharmony_ci  jerryx_escapable_handle_scope root = jerryx_handle_scope_get_root ();
51425bb815Sopenharmony_ci  // root handle scope should always be reusable after close
52425bb815Sopenharmony_ci  for (int i = 0; i < reusing_times; ++i)
53425bb815Sopenharmony_ci  {
54425bb815Sopenharmony_ci    jerry_value_t obj = jerryx_create_handle (create_object ());
55425bb815Sopenharmony_ci    (void) obj;
56425bb815Sopenharmony_ci    jerryx_close_handle_scope (root);
57425bb815Sopenharmony_ci    jerry_gc (JERRY_GC_PRESSURE_LOW);
58425bb815Sopenharmony_ci    TEST_ASSERT (native_free_cb_call_count == (i + 1));
59425bb815Sopenharmony_ci  }
60425bb815Sopenharmony_ci} /* test_handle_scope_val */
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ciint
63425bb815Sopenharmony_cimain (void)
64425bb815Sopenharmony_ci{
65425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci  native_free_cb_call_count = 0;
68425bb815Sopenharmony_ci  test_handle_scope_val ();
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_ci  jerry_gc (JERRY_GC_PRESSURE_LOW);
71425bb815Sopenharmony_ci  TEST_ASSERT (native_free_cb_call_count == reusing_times);
72425bb815Sopenharmony_ci
73425bb815Sopenharmony_ci  jerry_cleanup ();
74425bb815Sopenharmony_ci} /* main */
75