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