1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include "config.h" 17425bb815Sopenharmony_ci#include "jerryscript.h" 18425bb815Sopenharmony_ci#include "jerryscript-port.h" 19425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 20425bb815Sopenharmony_ci#include "test-common.h" 21425bb815Sopenharmony_ci#include <gtest/gtest.h> 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_cistatic jerry_value_t 24425bb815Sopenharmony_civm_exec_stop_callback (void *user_p) 25425bb815Sopenharmony_ci{ 26425bb815Sopenharmony_ci int *int_p = (int *) user_p; 27425bb815Sopenharmony_ci 28425bb815Sopenharmony_ci if (*int_p > 0) 29425bb815Sopenharmony_ci { 30425bb815Sopenharmony_ci (*int_p)--; 31425bb815Sopenharmony_ci 32425bb815Sopenharmony_ci return jerry_create_undefined (); 33425bb815Sopenharmony_ci } 34425bb815Sopenharmony_ci 35425bb815Sopenharmony_ci return jerry_create_string ((const jerry_char_t *) "Abort script"); 36425bb815Sopenharmony_ci} /* vm_exec_stop_callback */ 37425bb815Sopenharmony_ci 38425bb815Sopenharmony_ciclass ExecStopTest : public testing::Test{ 39425bb815Sopenharmony_cipublic: 40425bb815Sopenharmony_ci static void SetUpTestCase() 41425bb815Sopenharmony_ci { 42425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ExecStopTest SetUpTestCase"; 43425bb815Sopenharmony_ci } 44425bb815Sopenharmony_ci 45425bb815Sopenharmony_ci static void TearDownTestCase() 46425bb815Sopenharmony_ci { 47425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ExecStopTest TearDownTestCase"; 48425bb815Sopenharmony_ci } 49425bb815Sopenharmony_ci 50425bb815Sopenharmony_ci void SetUp() override {} 51425bb815Sopenharmony_ci void TearDown() override {} 52425bb815Sopenharmony_ci 53425bb815Sopenharmony_ci}; 54425bb815Sopenharmony_ci 55425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 56425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 57425bb815Sopenharmony_ci{ 58425bb815Sopenharmony_ci (void)cb_data; 59425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 60425bb815Sopenharmony_ci return malloc(newSize); 61425bb815Sopenharmony_ci} 62425bb815Sopenharmony_ciHWTEST_F(ExecStopTest, Test001, testing::ext::TestSize.Level1) 63425bb815Sopenharmony_ci{ 64425bb815Sopenharmony_ci TEST_INIT (); 65425bb815Sopenharmony_ci 66425bb815Sopenharmony_ci /* Test stopping an infinite loop. */ 67425bb815Sopenharmony_ci if (!jerry_is_feature_enabled (JERRY_FEATURE_VM_EXEC_STOP)) 68425bb815Sopenharmony_ci { 69425bb815Sopenharmony_ci jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Exec Stop is disabled!\n"); 70425bb815Sopenharmony_ci // jerry_cleanup (); 71425bb815Sopenharmony_ci } 72425bb815Sopenharmony_ci else{ 73425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 74425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 75425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 76425bb815Sopenharmony_ci 77425bb815Sopenharmony_ci int countdown = 6; 78425bb815Sopenharmony_ci jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16); 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_ci const jerry_char_t inf_loop_code_src1[] = "while(true) {}"; 81425bb815Sopenharmony_ci jerry_value_t parsed_code_val = jerry_parse (NULL, 82425bb815Sopenharmony_ci 0, 83425bb815Sopenharmony_ci inf_loop_code_src1, 84425bb815Sopenharmony_ci sizeof (inf_loop_code_src1) - 1, 85425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 88425bb815Sopenharmony_ci jerry_value_t res = jerry_run (parsed_code_val); 89425bb815Sopenharmony_ci TEST_ASSERT (countdown == 0); 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 92425bb815Sopenharmony_ci 93425bb815Sopenharmony_ci jerry_release_value (res); 94425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci /* A more complex example. Although the callback error is captured 97425bb815Sopenharmony_ci * by the catch block, it is automatically thrown again. */ 98425bb815Sopenharmony_ci 99425bb815Sopenharmony_ci /* We keep the callback function, only the countdown is reset. */ 100425bb815Sopenharmony_ci countdown = 6; 101425bb815Sopenharmony_ci 102425bb815Sopenharmony_ci const jerry_char_t inf_loop_code_src2[] = TEST_STRING_LITERAL ( 103425bb815Sopenharmony_ci "function f() { while (true) ; }\n" 104425bb815Sopenharmony_ci "try { f(); } catch(e) {}" 105425bb815Sopenharmony_ci ); 106425bb815Sopenharmony_ci 107425bb815Sopenharmony_ci parsed_code_val = jerry_parse (NULL, 108425bb815Sopenharmony_ci 0, 109425bb815Sopenharmony_ci inf_loop_code_src2, 110425bb815Sopenharmony_ci sizeof (inf_loop_code_src2) - 1, 111425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 112425bb815Sopenharmony_ci 113425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 114425bb815Sopenharmony_ci res = jerry_run (parsed_code_val); 115425bb815Sopenharmony_ci TEST_ASSERT (countdown == 0); 116425bb815Sopenharmony_ci 117425bb815Sopenharmony_ci /* The result must have an error flag which proves that 118425bb815Sopenharmony_ci * the error is thrown again inside the catch block. */ 119425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 120425bb815Sopenharmony_ci 121425bb815Sopenharmony_ci jerry_release_value (res); 122425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 123425bb815Sopenharmony_ci 124425bb815Sopenharmony_ci jerry_cleanup (); 125425bb815Sopenharmony_ci free (ctx_p); 126425bb815Sopenharmony_ci } 127425bb815Sopenharmony_ci} 128