697442434d
Until now, jerry had 3 different assert-like routines: `jerry_assert_fail`, `jerry_unreachable`, and `jerry_unimplemented`, and 3 corresponding macros (`JERRY_ASSERT`, `JERRY_UNREACHABLE`, and `JERRY_UNIMPLEMENTED`). They had some irregularities, namely: * All of them had a string parameter, although `jerry_unreachable` never got anything there but NULL. * Both `jerry_unreachable` and `jerry_unimplemented` checked its string parameter for NULL, although it was always NULL for the first one and never NULL for the second. * `jerry_unreachable` is just a regular assert with a fixed error message (i.e., control should not have got here), however, the expansion of its corresponding macro in debug and release modes differs from the behaviour of `JERRY_ASSERT`: `JERRY_ASSERT` is a no-op in release, however, `JERRY_UNREACHABLE` was triggering a crash even there. * Moreover, `JERRY_UNIMPLEMENTED` was almost never used anymore but in a few places (where often an `#ifdef` selected between `JERRY_UNIMPLEMENTED` and `JERRY_UNREACHABLE`). Because of the above, this patch makes the following changes: * Drops `JERRY_UNIMPLEMENTED` completely and whereever it was still used, replaces it with `JERRY_UNREACHABLE`. As a consequence, the `jerry_unimplemented` function and the `ERR_UNIMPLEMENTED_CASE` fatal error code are also removed. * Makes `JERRY_UNREACHABLE` expand to no-op in release builds. (Actually, to `__builtin_unreachable ()` to avoid warnings.) As a consequence, makes both `jerry_assert_fail` and `jerry_unreachable` be guarded by `#ifndef JERRY_NDEBUG`. Also, changes `jerry_unreachable` not to expect a string parameter. * Rewrites `TEST_ASSERT` not to rely on `jerry_assert_fail` as `TEST_ASSERT` has to work in release builds as well. This also allows changing the error message not to mention "ICE", which would misleadingly suggest an assert within the engine, but "TEST" instead. As a side-effect of the cleanup, some refactorings happened in jrt.h: * Removed the definition of the unnecessary `__extension__` macro. * Re-used `JERRY_UNUSED` and `unlikely` where possible. * Moved some parts of the file around. * Fixed some comments (`/**` should only be used for the docstring of a single entity, for groups header comments, the regular `/*` should be used). JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
167 lines
4.6 KiB
C
167 lines
4.6 KiB
C
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
|
|
* Copyright 2016 University of Szeged.
|
|
|
|
* 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.
|
|
*/
|
|
|
|
#ifndef JRT_H
|
|
#define JRT_H
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "jerry-api.h"
|
|
#include "jerry-port.h"
|
|
#include "jrt-types.h"
|
|
|
|
/*
|
|
* Attributes
|
|
*/
|
|
#define __noreturn __attribute__((noreturn))
|
|
#define __attr_noinline___ __attribute__((noinline))
|
|
#define __attr_return_value_should_be_checked___ __attribute__((warn_unused_result))
|
|
#ifndef __attr_always_inline___
|
|
# define __attr_always_inline___ __attribute__((always_inline))
|
|
#endif /* !__attr_always_inline___ */
|
|
#ifndef __attr_const___
|
|
# define __attr_const___ __attribute__((const))
|
|
#endif /* !__attr_const___ */
|
|
#ifndef __attr_pure___
|
|
# define __attr_pure___ __attribute__((pure))
|
|
#endif /* !__attr_pure___ */
|
|
|
|
/*
|
|
* Conditions' likeliness, unlikeliness.
|
|
*/
|
|
#define likely(x) __builtin_expect (!!(x), 1)
|
|
#define unlikely(x) __builtin_expect (!!(x) , 0)
|
|
|
|
/*
|
|
* Normally compilers store const(ant)s in ROM. Thus saving RAM.
|
|
* But if your compiler does not support it then the directive below can force it.
|
|
*
|
|
* For the moment it is mainly meant for the following targets:
|
|
* - ESP8266
|
|
*/
|
|
#ifndef JERRY_CONST_DATA
|
|
# define JERRY_CONST_DATA
|
|
#endif /* JERRY_CONST_DATA */
|
|
|
|
/*
|
|
* Constants
|
|
*/
|
|
#define JERRY_BITSINBYTE 8
|
|
|
|
/*
|
|
* Make sure unused parameters, variables, or expressions trigger no compiler warning.
|
|
*/
|
|
#define JERRY_UNUSED(x) ((void) (x))
|
|
|
|
/*
|
|
* Asserts
|
|
*
|
|
* Warning:
|
|
* Don't use JERRY_STATIC_ASSERT in headers, because
|
|
* __LINE__ may be the same for asserts in a header
|
|
* and in an implementation file.
|
|
*/
|
|
#define JERRY_STATIC_ASSERT_GLUE_(a, b, c) a ## b ## _ ## c
|
|
#define JERRY_STATIC_ASSERT_GLUE(a, b, c) JERRY_STATIC_ASSERT_GLUE_ (a, b, c)
|
|
#define JERRY_STATIC_ASSERT(x, msg) \
|
|
enum { JERRY_STATIC_ASSERT_GLUE (static_assertion_failed_, __LINE__, msg) = 1 / (!!(x)) }
|
|
|
|
#ifndef JERRY_NDEBUG
|
|
extern void __noreturn jerry_assert_fail (const char *, const char *, const char *, const uint32_t);
|
|
extern void __noreturn jerry_unreachable (const char *, const char *, const uint32_t);
|
|
|
|
#define JERRY_ASSERT(x) \
|
|
do \
|
|
{ \
|
|
if (unlikely (!(x))) \
|
|
{ \
|
|
jerry_assert_fail (#x, __FILE__, __func__, __LINE__); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define JERRY_UNREACHABLE() \
|
|
do \
|
|
{ \
|
|
jerry_unreachable (__FILE__, __func__, __LINE__); \
|
|
} while (0)
|
|
#else /* JERRY_NDEBUG */
|
|
#define JERRY_ASSERT(x) \
|
|
do \
|
|
{ \
|
|
if (false) \
|
|
{ \
|
|
JERRY_UNUSED (x); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define JERRY_UNREACHABLE() __builtin_unreachable ()
|
|
#endif /* !JERRY_NDEBUG */
|
|
|
|
/**
|
|
* Exit on fatal error
|
|
*/
|
|
extern void __noreturn jerry_fatal (jerry_fatal_code_t);
|
|
|
|
/*
|
|
* Logging
|
|
*/
|
|
#ifdef JERRY_ENABLE_LOG
|
|
#define JERRY_DLOG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
|
|
#define JERRY_DDLOG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
|
|
#else /* !JERRY_ENABLE_LOG */
|
|
/**
|
|
* Mark for unreachable points and unimplemented cases
|
|
*/
|
|
extern void jerry_ref_unused_variables (void *, ...);
|
|
|
|
#define JERRY_DLOG(...) \
|
|
do \
|
|
{ \
|
|
if (false) \
|
|
{ \
|
|
jerry_ref_unused_variables (0, __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
#define JERRY_DDLOG(...) JERRY_DLOG (__VA_ARGS__)
|
|
#endif /* JERRY_ENABLE_LOG */
|
|
|
|
#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
|
|
#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
|
|
|
|
/**
|
|
* Size of struct member
|
|
*/
|
|
#define JERRY_SIZE_OF_STRUCT_MEMBER(struct_name, member_name) sizeof (((struct_name *) NULL)->member_name)
|
|
|
|
/**
|
|
* Aligns @a value to @a alignment. @a must be the power of 2.
|
|
*
|
|
* Returns minimum positive value, that divides @a alignment and is more than or equal to @a value
|
|
*/
|
|
#define JERRY_ALIGNUP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1))
|
|
|
|
/*
|
|
* min, max
|
|
*/
|
|
#define JERRY_MIN(v1, v2) (((v1) < (v2)) ? (v1) : (v2))
|
|
#define JERRY_MAX(v1, v2) (((v1) < (v2)) ? (v2) : (v1))
|
|
|
|
extern bool jrt_read_from_buffer_by_offset (const uint8_t *, size_t, size_t *, void *, size_t);
|
|
extern bool jrt_write_to_buffer_by_offset (uint8_t *, size_t, size_t *, const void *, size_t);
|
|
|
|
#endif /* !JRT_H */
|