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 #include <stdlib.h> 17 18 #include "jerryscript-port.h" 19 #include "jerryscript-port-default.h" 20 21 #ifdef JERRY_FOR_IAR_CONFIG 22 #include "jcontext.h" 23 24 #if !ENABLED (JERRY_EXTERNAL_CONTEXT) 25 static fatal_handler_t jerry_fatal_handler = NULL; 26 #endif 27 jerry_port_default_set_fatal_handler(fatal_handler_t handler)28void jerry_port_default_set_fatal_handler (fatal_handler_t handler) 29 { 30 #if ENABLED (JERRY_EXTERNAL_CONTEXT) 31 JERRY_CONTEXT (jerry_fatal_handler) = handler; 32 #else 33 jerry_fatal_handler = handler; 34 #endif 35 } 36 37 /** 38 * Default implementation of jerry_port_fatal. Calls 'abort' if exit code is 39 * non-zero, 'exit' otherwise. 40 */ jerry_port_fatal(jerry_fatal_code_t code)41void jerry_port_fatal (jerry_fatal_code_t code) /**< cause of error */ 42 { 43 #if ENABLED (JERRY_EXTERNAL_CONTEXT) 44 fatal_handler_t jerry_fatal_handler = JERRY_CONTEXT (jerry_fatal_handler); 45 #endif 46 if (jerry_fatal_handler != NULL) 47 { 48 jerry_fatal_handler ((int) code); 49 } 50 if (code != 0 51 && code != ERR_OUT_OF_MEMORY) 52 { 53 abort (); 54 } 55 56 exit ((int) code); 57 } /* jerry_port_fatal */ 58 59 #else // not defined JERRY_FOR_IAR_CONFIG 60 61 static fatal_handler_t jerry_fatal_handler = NULL; 62 jerry_port_default_set_fatal_handler(fatal_handler_t handler)63void jerry_port_default_set_fatal_handler (fatal_handler_t handler) 64 { 65 jerry_fatal_handler = handler; 66 } 67 68 /** 69 * Default implementation of jerry_port_fatal. Calls 'abort' if exit code is 70 * non-zero, 'exit' otherwise. 71 */ jerry_port_fatal(jerry_fatal_code_t code)72void jerry_port_fatal (jerry_fatal_code_t code) /**< cause of error */ 73 { 74 if (jerry_fatal_handler != NULL) 75 { 76 jerry_fatal_handler ((int) code); 77 } 78 if (code != 0 79 && code != ERR_OUT_OF_MEMORY) 80 { 81 abort (); 82 } 83 84 exit ((int) code); 85 } /* jerry_port_fatal */ 86 87 #endif 88