default-fatal.c 2.1 KB
Newer Older
1
/* Copyright JS Foundation and other contributors, http://js.foundation
A
Akos Kiss 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>

18 19
#include "jerryscript-port.h"
#include "jerryscript-port-default.h"
A
Akos Kiss 已提交
20

M
mamingshuai 已提交
21 22 23 24
#ifdef JERRY_FOR_IAR_CONFIG
#include "jcontext.h"

#if !ENABLED (JERRY_EXTERNAL_CONTEXT)
A
a00533735 已提交
25
static fatal_handler_t jerry_fatal_handler = NULL;
M
mamingshuai 已提交
26
#endif
A
a00533735 已提交
27 28 29

void jerry_port_default_set_fatal_handler (fatal_handler_t handler)
{
M
mamingshuai 已提交
30 31 32
#if ENABLED (JERRY_EXTERNAL_CONTEXT)
  JERRY_CONTEXT (jerry_fatal_handler) = handler;
#else
A
a00533735 已提交
33
  jerry_fatal_handler = handler;
M
mamingshuai 已提交
34
#endif
A
a00533735 已提交
35 36
}

A
Akos Kiss 已提交
37
/**
38
 * Default implementation of jerry_port_fatal. Calls 'abort' if exit code is
39
 * non-zero, 'exit' otherwise.
A
Akos Kiss 已提交
40
 */
41
void jerry_port_fatal (jerry_fatal_code_t code) /**< cause of error */
A
Akos Kiss 已提交
42
{
M
mamingshuai 已提交
43 44 45
#if ENABLED (JERRY_EXTERNAL_CONTEXT)
  fatal_handler_t jerry_fatal_handler = JERRY_CONTEXT (jerry_fatal_handler);
#endif
A
a00533735 已提交
46 47 48 49
  if (jerry_fatal_handler != NULL)
  {
    jerry_fatal_handler ((int) code);
  }
A
Akos Kiss 已提交
50
  if (code != 0
51
      && code != ERR_OUT_OF_MEMORY)
A
Akos Kiss 已提交
52 53 54
  {
    abort ();
  }
55

56
  exit ((int) code);
A
Akos Kiss 已提交
57
} /* jerry_port_fatal */
M
mamingshuai 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#else // not defined JERRY_FOR_IAR_CONFIG

static fatal_handler_t jerry_fatal_handler = NULL;

void jerry_port_default_set_fatal_handler (fatal_handler_t handler)
{
  jerry_fatal_handler = handler;
}

/**
 * Default implementation of jerry_port_fatal. Calls 'abort' if exit code is
 * non-zero, 'exit' otherwise.
 */
void jerry_port_fatal (jerry_fatal_code_t code) /**< cause of error */
{
  if (jerry_fatal_handler != NULL)
  {
    jerry_fatal_handler ((int) code);
  }
  if (code != 0
      && code != ERR_OUT_OF_MEMORY)
  {
    abort ();
  }

  exit ((int) code);
} /* jerry_port_fatal */

#endif