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#ifndef JEXT_COMMON_H
17#define JEXT_COMMON_H
18
19#include <stdio.h>
20#include <string.h>
21
22#include "jerryscript.h"
23#include "jerryscript-port.h"
24
25/*
26 * Make sure unused parameters, variables, or expressions trigger no compiler warning.
27 */
28#define JERRYX_UNUSED(x) ((void) (x))
29
30/*
31 * Asserts
32 *
33 * Warning:
34 * Don't use JERRY_STATIC_ASSERT in headers, because
35 * __LINE__ may be the same for asserts in a header
36 * and in an implementation file.
37 */
38#define JERRYX_STATIC_ASSERT_GLUE_(a, b, c) a ## b ## _ ## c
39#define JERRYX_STATIC_ASSERT_GLUE(a, b, c) JERRYX_STATIC_ASSERT_GLUE_ (a, b, c)
40#define JERRYX_STATIC_ASSERT(x, msg) \
41 enum { JERRYX_STATIC_ASSERT_GLUE (static_assertion_failed_, __LINE__, msg) = 1 / (!!(x)) }
42
43#ifndef JERRY_NDEBUG
44void JERRY_ATTR_NORETURN
45jerry_assert_fail (const char *assertion, const char *file, const char *function, const uint32_t line);
46void JERRY_ATTR_NORETURN
47jerry_unreachable (const char *file, const char *function, const uint32_t line);
48
49#define JERRYX_ASSERT(x) \
50 do \
51 { \
52 if (JERRY_UNLIKELY (!(x))) \
53 { \
54 jerry_assert_fail (#x, __FILE__, __func__, __LINE__); \
55 } \
56 } while (0)
57
58#define JERRYX_UNREACHABLE() \
59 do \
60 { \
61 jerry_unreachable (__FILE__, __func__, __LINE__); \
62 } while (0)
63#else /* JERRY_NDEBUG */
64#define JERRYX_ASSERT(x) \
65 do \
66 { \
67 if (false) \
68 { \
69 JERRYX_UNUSED (x); \
70 } \
71 } while (0)
72
73#ifdef __GNUC__
74#define JERRYX_UNREACHABLE() __builtin_unreachable ()
75#endif /* __GNUC__ */
76
77#ifdef _MSC_VER
78#define JERRYX_UNREACHABLE() _assume (0)
79#endif /* _MSC_VER */
80
81#ifndef JERRYX_UNREACHABLE
82#define JERRYX_UNREACHABLE()
83#endif /* !JERRYX_UNREACHABLE */
84
85#endif /* !JERRY_NDEBUG */
86
87/*
88 * Logging
89 */
90#define JERRYX_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
91#define JERRYX_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
92#define JERRYX_DEBUG_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
93#define JERRYX_TRACE_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
94
95#endif /* !JEXT_COMMON_H */
96