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 static int reusing_times = 10; 26 27 static void native_free_cb(void * native_p)28native_free_cb (void *native_p) 29 { 30 ++native_free_cb_call_count; 31 (void) native_p; 32 } /* native_free_cb */ 33 34 static const jerry_object_native_info_t native_info = 35 { 36 .free_cb = native_free_cb, 37 }; 38 39 static jerry_value_t create_object(void)40create_object (void) 41 { 42 jerry_value_t obj = jerry_create_object (); 43 jerry_set_object_native_pointer (obj, NULL, &native_info); 44 return obj; 45 } /* create_object */ 46 47 static void test_handle_scope_val(void)48test_handle_scope_val (void) 49 { 50 jerryx_escapable_handle_scope root = jerryx_handle_scope_get_root (); 51 // root handle scope should always be reusable after close 52 for (int i = 0; i < reusing_times; ++i) 53 { 54 jerry_value_t obj = jerryx_create_handle (create_object ()); 55 (void) obj; 56 jerryx_close_handle_scope (root); 57 jerry_gc (JERRY_GC_PRESSURE_LOW); 58 TEST_ASSERT (native_free_cb_call_count == (i + 1)); 59 } 60 } /* test_handle_scope_val */ 61 62 int main(void)63main (void) 64 { 65 jerry_init (JERRY_INIT_EMPTY); 66 67 native_free_cb_call_count = 0; 68 test_handle_scope_val (); 69 70 jerry_gc (JERRY_GC_PRESSURE_LOW); 71 TEST_ASSERT (native_free_cb_call_count == reusing_times); 72 73 jerry_cleanup (); 74 } /* main */ 75