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
24 static int native_free_cb_call_count;
25
26 static void
native_free_cb(void * native_p)27 native_free_cb (void *native_p)
28 {
29 ++native_free_cb_call_count;
30 (void) native_p;
31 } /* native_free_cb */
32
33 static const jerry_object_native_info_t native_info =
34 {
35 .free_cb = native_free_cb,
36 };
37
38 static jerry_value_t
create_object(void)39 create_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_remove_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
56 static void
test_handle_scope_val(void)57 test_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 jerry_gc (JERRY_GC_PRESSURE_LOW);
69 TEST_ASSERT (native_free_cb_call_count == 0);
70
71 jerry_release_value (obj);
72 jerry_gc (JERRY_GC_PRESSURE_LOW);
73 TEST_ASSERT (native_free_cb_call_count == 1);
74 } /* test_handle_scope_val */
75
76 int
main(void)77 main (void)
78 {
79 jerry_init (JERRY_INIT_EMPTY);
80
81 native_free_cb_call_count = 0;
82 test_handle_scope_val ();
83
84 jerry_cleanup ();
85 } /* main */
86