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/**
17 * Unit test for jerry-ext/handle-scope.
18 */
19
20#include "jerryscript.h"
21#include "jerryscript-ext/handle-scope.h"
22#include "test-common.h"
23
24static int native_free_cb_call_count;
25
26static void
27native_free_cb (void *native_p)
28{
29  ++native_free_cb_call_count;
30  (void) native_p;
31} /* native_free_cb */
32
33static const jerry_object_native_info_t native_info =
34{
35  .free_cb = native_free_cb,
36};
37
38static jerry_value_t
39create_object (void)
40{
41  jerryx_escapable_handle_scope scope;
42  jerryx_open_escapable_handle_scope (&scope);
43  jerry_value_t obj = jerryx_create_handle (jerry_create_object ());
44  jerry_set_object_native_pointer (obj, NULL, &native_info);
45
46  // If leaves `escaped` uninitialized, there will be a style error on linux thrown by compiler
47  jerry_value_t escaped = 0;
48  jerryx_escape_handle (scope, obj, &escaped);
49  TEST_ASSERT (scope->prelist_handle_count == 0);
50  TEST_ASSERT (scope->handle_ptr == NULL);
51
52  jerryx_close_handle_scope (scope);
53  return escaped;
54} /* create_object */
55
56static void
57test_handle_scope_val (void)
58{
59  jerryx_handle_scope scope;
60  jerryx_open_handle_scope (&scope);
61  jerry_value_t obj = create_object ();
62  (void) obj;
63
64  jerry_gc (JERRY_GC_PRESSURE_LOW);
65  TEST_ASSERT (native_free_cb_call_count == 0);
66
67  jerryx_close_handle_scope (scope);
68} /* test_handle_scope_val */
69
70int
71main (void)
72{
73  jerry_init (JERRY_INIT_EMPTY);
74
75  native_free_cb_call_count = 0;
76  test_handle_scope_val ();
77
78  jerry_gc (JERRY_GC_PRESSURE_LOW);
79  TEST_ASSERT (native_free_cb_call_count == 1);
80
81  jerry_cleanup ();
82} /* main */
83