Skip to content

Commit 147c98a

Browse files
committed
Introduce the Termination Port API
* Moved the error codes to jerry-port.h and declared port function `jerry_port_fatal`. * Moved "exit or abort on fail" functionality to the newly added jerry-port-default-fatal.c. * This implied that a default port-specific API had to be introduced: functions `jerry_port_default_set_abort_on_fail` and `jerry_port_default_is_abort_on_fail` declared in jerry-port-default.h control the fatal exit behaviour. * For the sake of clarity, renamed jerry-port.c to jerry-port-default-io.c. * Adapted CMakeLists to deal with port implementations consisting of more then one source file and exposing headers. This also required the renaming of `EXTERNAL_PORT_FILE` cmake option to `EXTERNAL_PORT_DIR`. * Adapted main sources to use the default port header for the abort-on-fail functionality, as that is not part of the core jerry API anymore. * Added default port implementation to the static source code checker tools. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
1 parent df1e428 commit 147c98a

File tree

14 files changed

+155
-55
lines changed

14 files changed

+155
-55
lines changed

CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,11 @@ project (Jerry C ASM)
132132
endif()
133133

134134

135-
# Should we use external jerry-port.c?
136-
if(DEFINED EXTERNAL_PORT_FILE AND NOT EXTERNAL_PORT_FILE STREQUAL "UNDEFINED")
137-
set(SOURCE_PORT_IMPLEMENTATION ${EXTERNAL_PORT_FILE})
138-
set(USE_DEFAULT_PORT FALSE)
135+
# Should we use external port?
136+
if(DEFINED EXTERNAL_PORT_DIR AND NOT EXTERNAL_PORT_DIR STREQUAL "UNDEFINED")
137+
set(PORT_DIR ${EXTERNAL_PORT_DIR})
139138
else()
140-
set(USE_DEFAULT_PORT TRUE)
139+
set(PORT_DIR ${CMAKE_SOURCE_DIR}/targets/default)
141140
endif()
142141

143142
# Are there any interfaces for external libraries, other than libc, that should be registered?
@@ -420,6 +419,7 @@ endif()
420419
PROPERTY LINK_FLAGS "${COMPILE_FLAGS_JERRY} ${FLAGS_COMMON_${BUILD_MODE}} ${LINKER_FLAGS_COMMON} ${LINKER_FLAGS_STATIC}")
421420
target_compile_definitions(${TARGET_NAME} PRIVATE ${DEFINES_JERRY})
422421
target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_CORE_INTERFACE})
422+
target_include_directories(${TARGET_NAME} PRIVATE ${PORT_DIR})
423423
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${INCLUDE_LIBC_INTERFACE})
424424
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${INCLUDE_EXTERNAL_LIBS_INTERFACE})
425425
if(("${PLATFORM}" STREQUAL "DARWIN") AND (NOT (CMAKE_COMPILER_IS_GNUCC)))

jerry-core/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ project (JerryCore C ASM)
148148
${SOURCE_CORE_JRT})
149149

150150
# Jerry port
151-
if(USE_DEFAULT_PORT)
152-
file(GLOB SOURCE_PORT_FILES ${CMAKE_SOURCE_DIR}/targets/default/*.c)
153-
else()
154-
set(SOURCE_PORT_FILES ${SOURCE_PORT_IMPLEMENTATION})
155-
endif()
151+
file(GLOB SOURCE_PORT_FILES ${PORT_DIR}/*.c)
156152

157153
# All-in-one build
158154
if("${ENABLE_ALL_IN_ONE}" STREQUAL "ON")

jerry-core/jerry-internal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,4 @@ jerry_dispatch_external_function (ecma_object_t *, ecma_external_pointer_t, ecma
2929
extern void
3030
jerry_dispatch_object_free_callback (ecma_external_pointer_t, ecma_external_pointer_t);
3131

32-
extern bool
33-
jerry_is_abort_on_fail (void);
34-
3532
#endif /* !JERRY_INTERNAL_H */

jerry-core/jerry-port.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@ int jerry_port_logmsg (FILE *stream, const char *format, ...);
3535
int jerry_port_errormsg (const char *format, ...);
3636
int jerry_port_putchar (int c);
3737

38+
/*
39+
* Termination Port API
40+
*
41+
* Note: It is questionable whether a library should be able to terminate an
42+
* application. However, as of now, we only have the concept of completion code
43+
* around jerry_parse and jerry_run. Most of the other API functions have no way
44+
* of signaling an error. So, we keep the termination approach with this port
45+
* function.
46+
*/
47+
48+
/**
49+
* Error codes
50+
*/
51+
typedef enum
52+
{
53+
ERR_OUT_OF_MEMORY = 10,
54+
ERR_SYSCALL = 11,
55+
ERR_REF_COUNT_LIMIT = 12,
56+
ERR_UNIMPLEMENTED_CASE = 118,
57+
ERR_FAILED_INTERNAL_ASSERTION = 120
58+
} jerry_fatal_code_t;
59+
60+
/**
61+
* Signal the port that jerry experienced a fatal failure from which it cannot
62+
* recover.
63+
*
64+
* @param code gives the cause of the error.
65+
*
66+
* Note: jerry expects the function not to return.
67+
*
68+
* Example: a libc-based port may implement this with exit() or abort(), or both.
69+
*/
70+
void jerry_port_fatal (jerry_fatal_code_t code);
71+
3872
/**
3973
* @}
4074
*/

jerry-core/jerry.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,18 +1689,6 @@ jerry_get_memory_limits (size_t *out_data_bss_brk_limit_p, /**< [out] Jerry's ma
16891689
*out_stack_limit_p = CONFIG_MEM_STACK_LIMIT;
16901690
} /* jerry_get_memory_limits */
16911691

1692-
/**
1693-
* Check whether 'abort' should be called instead of 'exit' upon exiting with non-zero exit code.
1694-
*
1695-
* @return true - if 'abort on fail' flag is set,
1696-
* false - otherwise.
1697-
*/
1698-
bool
1699-
jerry_is_abort_on_fail (void)
1700-
{
1701-
return ((jerry_flags & JERRY_FLAG_ABORT_ON_FAIL) != 0);
1702-
} /* jerry_is_abort_on_fail */
1703-
17041692
/**
17051693
* Parse script for specified context
17061694
*

jerry-core/jerry.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,8 @@ typedef enum
4444
JERRY_FLAG_PARSE_ONLY = (1u << 3), /**< parse only, prevents script execution (only for testing)
4545
* TODO: Remove. */
4646
JERRY_FLAG_ENABLE_LOG = (1u << 4), /**< enable logging */
47-
JERRY_FLAG_ABORT_ON_FAIL = (1u << 5), /**< abort instead of exit in case of failure */
4847
} jerry_flag_t;
4948

50-
/**
51-
* Error codes
52-
*/
53-
typedef enum
54-
{
55-
ERR_OUT_OF_MEMORY = 10,
56-
ERR_SYSCALL = 11,
57-
ERR_REF_COUNT_LIMIT = 12,
58-
ERR_UNIMPLEMENTED_CASE = 118,
59-
ERR_FAILED_INTERNAL_ASSERTION = 120
60-
} jerry_fatal_code_t;
61-
6249
/**
6350
* Jerry engine build date
6451
*/

jerry-core/jrt/jrt-fatals.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -65,16 +65,7 @@ jerry_fatal (jerry_fatal_code_t code) /**< status code */
6565
}
6666
#endif /* !JERRY_NDEBUG */
6767

68-
if (code != 0
69-
&& code != ERR_OUT_OF_MEMORY
70-
&& jerry_is_abort_on_fail ())
71-
{
72-
abort ();
73-
}
74-
else
75-
{
76-
exit (code);
77-
}
68+
jerry_port_fatal (code);
7869

7970
/* to make compiler happy for some RTOS: 'control reaches end of non-void function' */
8071
while (true)

main-unix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "jerry.h"
2222
#include "jerry-port.h"
23+
#include "jerry-port-default.h"
2324

2425
/**
2526
* Maximum command line arguments number
@@ -358,7 +359,7 @@ main (int argc,
358359
}
359360
else if (!strcmp ("--abort-on-fail", argv[i]))
360361
{
361-
flags |= JERRY_FLAG_ABORT_ON_FAIL;
362+
jerry_port_default_set_abort_on_fail (true);
362363
}
363364
else if (!strncmp ("-", argv[i], 1))
364365
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <stdlib.h>
18+
19+
#include "jerry-port.h"
20+
#include "jerry-port-default.h"
21+
22+
static bool abort_on_fail = false;
23+
24+
/**
25+
* Sets whether 'abort' should be called instead of 'exit' upon exiting with
26+
* non-zero exit code in the default implementation of jerry_port_fatal.
27+
*/
28+
void jerry_port_default_set_abort_on_fail (bool flag) /**< new value of 'abort on fail' flag */
29+
{
30+
abort_on_fail = flag;
31+
} /* jerry_port_default_set_abort_on_fail */
32+
33+
/**
34+
* Check whether 'abort' should be called instead of 'exit' upon exiting with
35+
* non-zero exit code in the default implementation of jerry_port_fatal.
36+
*
37+
* @return true - if 'abort on fail' flag is set,
38+
* false - otherwise.
39+
*/
40+
bool jerry_port_default_is_abort_on_fail ()
41+
{
42+
return abort_on_fail;
43+
} /* jerry_port_default_is_abort_on_fail */
44+
45+
/**
46+
* Default implementation of jerry_port_fatal.
47+
*/
48+
void jerry_port_fatal (jerry_fatal_code_t code)
49+
{
50+
if (code != 0
51+
&& code != ERR_OUT_OF_MEMORY
52+
&& jerry_port_default_is_abort_on_fail ())
53+
{
54+
abort ();
55+
}
56+
else
57+
{
58+
exit (code);
59+
}
60+
} /* jerry_port_fatal */
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16-
#include "jerry-port.h"
1716
#include <stdarg.h>
1817

18+
#include "jerry-port.h"
19+
1920
/**
2021
* Provide log message to filestream implementation for the engine.
2122
*/

0 commit comments

Comments
 (0)