Skip to content

Introduce the Termination Port API #1015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,11 @@ project (Jerry C ASM)
endif()


# Should we use external jerry-port.c?
if(DEFINED EXTERNAL_PORT_FILE AND NOT EXTERNAL_PORT_FILE STREQUAL "UNDEFINED")
set(SOURCE_PORT_IMPLEMENTATION ${EXTERNAL_PORT_FILE})
set(USE_DEFAULT_PORT FALSE)
# Should we use external port?
if(DEFINED EXTERNAL_PORT_DIR AND NOT EXTERNAL_PORT_DIR STREQUAL "UNDEFINED")
set(PORT_DIR ${EXTERNAL_PORT_DIR})
else()
set(USE_DEFAULT_PORT TRUE)
set(PORT_DIR ${CMAKE_SOURCE_DIR}/targets/default)
endif()

# Are there any interfaces for external libraries, other than libc, that should be registered?
Expand Down Expand Up @@ -420,6 +419,7 @@ endif()
PROPERTY LINK_FLAGS "${COMPILE_FLAGS_JERRY} ${FLAGS_COMMON_${BUILD_MODE}} ${LINKER_FLAGS_COMMON} ${LINKER_FLAGS_STATIC}")
target_compile_definitions(${TARGET_NAME} PRIVATE ${DEFINES_JERRY})
target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_CORE_INTERFACE})
target_include_directories(${TARGET_NAME} PRIVATE ${PORT_DIR})
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${INCLUDE_LIBC_INTERFACE})
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${INCLUDE_EXTERNAL_LIBS_INTERFACE})
if(("${PLATFORM}" STREQUAL "DARWIN") AND (NOT (CMAKE_COMPILER_IS_GNUCC)))
Expand Down
6 changes: 1 addition & 5 deletions jerry-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ project (JerryCore C ASM)
${SOURCE_CORE_JRT})

# Jerry port
if(USE_DEFAULT_PORT)
file(GLOB SOURCE_PORT_FILES ${CMAKE_SOURCE_DIR}/targets/default/*.c)
else()
set(SOURCE_PORT_FILES ${SOURCE_PORT_IMPLEMENTATION})
endif()
file(GLOB SOURCE_PORT_FILES ${PORT_DIR}/*.c)

# All-in-one build
if("${ENABLE_ALL_IN_ONE}" STREQUAL "ON")
Expand Down
3 changes: 0 additions & 3 deletions jerry-core/jerry-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,4 @@ jerry_dispatch_external_function (ecma_object_t *, ecma_external_pointer_t, ecma
extern void
jerry_dispatch_object_free_callback (ecma_external_pointer_t, ecma_external_pointer_t);

extern bool
jerry_is_abort_on_fail (void);

#endif /* !JERRY_INTERNAL_H */
36 changes: 36 additions & 0 deletions jerry-core/jerry-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ int jerry_port_logmsg (FILE *stream, const char *format, ...);
int jerry_port_errormsg (const char *format, ...);
int jerry_port_putchar (int c);

/*
* Termination Port API
*
* Note:
* It is questionable whether a library should be able to terminate an
* application. However, as of now, we only have the concept of completion
* code around jerry_parse and jerry_run. Most of the other API functions
* have no way of signaling an error. So, we keep the termination approach
* with this port function.
*/

/**
* Error codes
*/
typedef enum
{
ERR_OUT_OF_MEMORY = 10,
ERR_SYSCALL = 11,
ERR_REF_COUNT_LIMIT = 12,
ERR_UNIMPLEMENTED_CASE = 118,
ERR_FAILED_INTERNAL_ASSERTION = 120
} jerry_fatal_code_t;

/**
* Signal the port that jerry experienced a fatal failure from which it cannot
* recover.
*
* @param code gives the cause of the error.
*
* Note:
* Jerry expects the function not to return.
*
* Example: a libc-based port may implement this with exit() or abort(), or both.
*/
void jerry_port_fatal (jerry_fatal_code_t code);

/**
* @}
*/
Expand Down
12 changes: 0 additions & 12 deletions jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -1689,18 +1689,6 @@ jerry_get_memory_limits (size_t *out_data_bss_brk_limit_p, /**< [out] Jerry's ma
*out_stack_limit_p = CONFIG_MEM_STACK_LIMIT;
} /* jerry_get_memory_limits */

/**
* Check whether 'abort' should be called instead of 'exit' upon exiting with non-zero exit code.
*
* @return true - if 'abort on fail' flag is set,
* false - otherwise.
*/
bool
jerry_is_abort_on_fail (void)
{
return ((jerry_flags & JERRY_FLAG_ABORT_ON_FAIL) != 0);
} /* jerry_is_abort_on_fail */

/**
* Parse script for specified context
*
Expand Down
13 changes: 0 additions & 13 deletions jerry-core/jerry.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,8 @@ typedef enum
JERRY_FLAG_PARSE_ONLY = (1u << 3), /**< parse only, prevents script execution (only for testing)
* TODO: Remove. */
JERRY_FLAG_ENABLE_LOG = (1u << 4), /**< enable logging */
JERRY_FLAG_ABORT_ON_FAIL = (1u << 5), /**< abort instead of exit in case of failure */
} jerry_flag_t;

/**
* Error codes
*/
typedef enum
{
ERR_OUT_OF_MEMORY = 10,
ERR_SYSCALL = 11,
ERR_REF_COUNT_LIMIT = 12,
ERR_UNIMPLEMENTED_CASE = 118,
ERR_FAILED_INTERNAL_ASSERTION = 120
} jerry_fatal_code_t;

/**
* Jerry engine build date
*/
Expand Down
11 changes: 1 addition & 10 deletions jerry-core/jrt/jrt-fatals.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,7 @@ jerry_fatal (jerry_fatal_code_t code) /**< status code */
}
#endif /* !JERRY_NDEBUG */

if (code != 0
&& code != ERR_OUT_OF_MEMORY
&& jerry_is_abort_on_fail ())
{
abort ();
}
else
{
exit (code);
}
jerry_port_fatal (code);

/* to make compiler happy for some RTOS: 'control reaches end of non-void function' */
while (true)
Expand Down
3 changes: 2 additions & 1 deletion main-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "jerry.h"
#include "jerry-port.h"
#include "jerry-port-default.h"

/**
* Maximum command line arguments number
Expand Down Expand Up @@ -358,7 +359,7 @@ main (int argc,
}
else if (!strcmp ("--abort-on-fail", argv[i]))
{
flags |= JERRY_FLAG_ABORT_ON_FAIL;
jerry_port_default_set_abort_on_fail (true);
}
else if (!strncmp ("-", argv[i], 1))
{
Expand Down
60 changes: 60 additions & 0 deletions targets/default/jerry-port-default-fatal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 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.
*/

#include <stdlib.h>

#include "jerry-port.h"
#include "jerry-port-default.h"

static bool abort_on_fail = false;

/**
* Sets whether 'abort' should be called instead of 'exit' upon exiting with
* non-zero exit code in the default implementation of jerry_port_fatal.
*/
void jerry_port_default_set_abort_on_fail (bool flag) /**< new value of 'abort on fail' flag */
{
abort_on_fail = flag;
} /* jerry_port_default_set_abort_on_fail */

/**
* Check whether 'abort' should be called instead of 'exit' upon exiting with
* non-zero exit code in the default implementation of jerry_port_fatal.
*
* @return true - if 'abort on fail' flag is set,
* false - otherwise.
*/
bool jerry_port_default_is_abort_on_fail ()
{
return abort_on_fail;
} /* jerry_port_default_is_abort_on_fail */

/**
* Default implementation of jerry_port_fatal.
*/
void jerry_port_fatal (jerry_fatal_code_t code)
{
if (code != 0
&& code != ERR_OUT_OF_MEMORY
&& jerry_port_default_is_abort_on_fail ())
{
abort ();
}
else
{
exit (code);
}
} /* jerry_port_fatal */
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* limitations under the License.
*/

#include "jerry-port.h"
#include <stdarg.h>

#include "jerry-port.h"

/**
* Provide log message to filestream implementation for the engine.
*/
Expand Down
42 changes: 42 additions & 0 deletions targets/default/jerry-port-default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright 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 JERRY_PORT_DEFAULT_H
#define JERRY_PORT_DEFAULT_H

#include <stdbool.h>

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

/** \addtogroup jerry_port_default Default Jerry engine port API
* These functions are only available if the default port of Jerry is used.
* @{
*/

void jerry_port_default_set_abort_on_fail (bool);
bool jerry_port_default_is_abort_on_fail (void);

/**
* @}
*/

#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* !JERRY_PORT_DEFAULT_H */
3 changes: 2 additions & 1 deletion targets/nuttx-stm32f4/main-nuttx.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "jerry.h"
#include "jerry-port.h"
#include "jerry-port-default.h"

/**
* The module interface routine
Expand Down Expand Up @@ -199,7 +200,7 @@ int jerryscript_entry (int argc, char *argv[])
}
else if (!strcmp ("--abort-on-fail", argv[i]))
{
flags |= JERRY_FLAG_ABORT_ON_FAIL;
jerry_port_default_set_abort_on_fail (true);
}
else if (!strcmp ("--log-level", argv[i]))
{
Expand Down
5 changes: 3 additions & 2 deletions tools/check-cppcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ else
fi

JERRY_CORE_DIRS=`find jerry-core -type d`
JERRY_PORT_DEFAULT_DIRS=`find targets/default -type d`
JERRY_LIBC_DIRS=`find jerry-libc -type d`
JERRY_LIBM_DIRS=`find jerry-libm -type d`

INCLUDE_DIRS=()
for DIR in $JERRY_CORE_DIRS $JERRY_LIBC_DIRS $JERRY_LIBM_DIRS
for DIR in $JERRY_CORE_DIRS $JERRY_PORT_DEFAULT_DIRS $JERRY_LIBC_DIRS $JERRY_LIBM_DIRS
do
INCLUDE_DIRS=("${INCLUDE_DIRS[@]}" "-I$DIR")
done
Expand All @@ -39,4 +40,4 @@ cppcheck -j$CPPCHECK_JOBS --force \
--error-exitcode=1 \
--exitcode-suppressions=tools/cppcheck/suppressions-list \
"${INCLUDE_DIRS[@]}" \
jerry-core jerry-libc jerry-libm *.c *h tests/unit
jerry-core targets/default jerry-libc jerry-libm *.c *h tests/unit
3 changes: 2 additions & 1 deletion tools/check-vera.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
# limitations under the License.

JERRY_CORE_FILES=`find ./jerry-core -name "*.c" -or -name "*.h"`
JERRY_PORT_DEFAULT_FILES=`find ./targets/default -name "*.c" -or -name "*.h"`
JERRY_LIBC_FILES=`find ./jerry-libc -name "*.c" -or -name "*.h"`
JERRY_LIBM_FILES=`find ./jerry-libm -name "*.c" -or -name "*.h"`
JERRY_MAIN_FILES=`find . -maxdepth 1 -name "*.c" -or -name "*.h"`
UNIT_TEST_FILES=`find ./tests/unit -name "*.c" -or -name "*.h"`

vera++ -r tools/vera++ -p jerry \
-e --no-duplicate \
$JERRY_CORE_FILES $JERRY_LIBC_FILES $JERRY_LIBM_FILES $JERRY_MAIN_FILES $UNIT_TEST_FILES
$JERRY_CORE_FILES $JERRY_PORT_DEFAULT_FILES $JERRY_LIBC_FILES $JERRY_LIBM_FILES $JERRY_MAIN_FILES $UNIT_TEST_FILES