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 * Tests escaping jerry value that passed from scopes which are created on heap.
20 * Also reallocates scopes for one times to test if reallocation works.
21 */
22
23 #include "jerryscript.h"
24 #include "jerryscript-ext/handle-scope.h"
25 #include "test-common.h"
26
27 static int native_free_cb_call_count;
28
29 static void
native_free_cb(void * native_p)30 native_free_cb (void *native_p)
31 {
32 ++native_free_cb_call_count;
33 (void) native_p;
34 } /* native_free_cb */
35
36 static const jerry_object_native_info_t native_info =
37 {
38 .free_cb = native_free_cb,
39 };
40
41 static jerry_value_t
create_object_nested(int times)42 create_object_nested (int times)
43 {
44 jerryx_escapable_handle_scope scope;
45 jerryx_open_escapable_handle_scope (&scope);
46
47 jerry_value_t obj;
48 if (times == 0)
49 {
50 obj = jerryx_create_handle (jerry_create_object ());
51 jerry_set_object_native_pointer (obj, NULL, &native_info);
52 }
53 else
54 {
55 obj = create_object_nested (times - 1);
56 }
57 TEST_ASSERT (jerryx_handle_scope_get_current () == scope);
58
59 // If leaves `escaped` uninitialized, there will be a style error on linux thrown by compiler
60 jerry_value_t escaped = 0;
61 jerryx_handle_scope_status status = jerryx_escape_handle (scope, obj, &escaped);
62 TEST_ASSERT (status == jerryx_handle_scope_ok);
63 TEST_ASSERT (scope->prelist_handle_count == 0);
64 TEST_ASSERT (scope->handle_ptr == NULL);
65
66 jerryx_close_handle_scope (scope);
67 return escaped;
68 } /* create_object_nested */
69
70 static void
test_handle_scope_val(void)71 test_handle_scope_val (void)
72 {
73 jerryx_handle_scope scope;
74 jerryx_open_handle_scope (&scope);
75
76 for (int idx = 0; idx < 2; ++idx)
77 {
78 jerry_value_t obj = create_object_nested (JERRYX_SCOPE_PRELIST_SIZE * 2);
79 (void) obj;
80 }
81
82 TEST_ASSERT (jerryx_handle_scope_get_current () == scope);
83
84 jerry_gc (JERRY_GC_PRESSURE_LOW);
85 TEST_ASSERT (native_free_cb_call_count == 0);
86
87 jerryx_close_handle_scope (scope);
88 } /* test_handle_scope_val */
89
90 int
main(void)91 main (void)
92 {
93 jerry_init (JERRY_INIT_EMPTY);
94
95 native_free_cb_call_count = 0;
96 test_handle_scope_val ();
97
98 jerry_gc (JERRY_GC_PRESSURE_LOW);
99 TEST_ASSERT (native_free_cb_call_count == 2);
100
101 jerry_cleanup ();
102 } /* main */
103