Skip to content

Commit 54e540a

Browse files
committed
Turn port implementations into proper libraries
This commit changes the concept of JerryScript port implementations from a simple directory of C source files (which get injected among the sources of `jerry-core`) into a proper static library (which may be linked to an application together with `jerry-core`). As a consequence, this commit introduces a new library to the JerryScript component architecture: the sources of the default port implementation form `jerry-port-default`. Changes in more detail: - The sources in `targets/default` are moved to `jerry-port/default` and are turned into a proper static library. - Actually, the default port implementation has two library variants, one that implements the bare minimum only (`jerry-port-default-minimal`) and one that has some extra functionalities specific to this implementation (the "full" `jerry-port-default`). - The new libraries have an interface header in `jerry-port/default/include`, which extends the common `jerryscript-port.h` API with functions specific to these libraries. - All non-standard port functions have now the `jerry_port_default_` prefix (this affects `jobqueue_init` and `jobqueue_run`). - The jobqueue implementation functions became config macro independent: it is now the responsibility of the linker to pick the needed objects from the library, and omit those (e.g., jobqueue-related code) that are not referenced. - Build of the libraries can be controlled with the new `JERRY_PORT_DEFAULT` cmake option. - The cmake option `PORT_DIR` is dropped, and `PORT_DIR/*.c` is not appended to `jerry-core` sources. - Instead, the `jerry` tool of `jerry-main` links to `jerry-port-default`, while `jerry-minimal` links to `jerry-port-default-minimal`. - `tests/unit-core` tests are also linked to `jerry-port-default-minimal`. - Tools adapted. - `build.py` has `--jerry-port-default` instead of `--port-dir`. - `check-*.sh` have paths updated (`jerry-port/default` instead of `targets/default`). - Miscellaneous. - Dropped `#ifndef`s from `jerryscript-port.h`. It is a public header of the `jerry-core` library, which means that it must not contain configuration-dependent parts (once the library is built with some config macros and the archive and the headers are installed, there is no way for the header to tell what those config macrose were). - Added documentation comments to the JobQueue Port API (in `jerryscript-port.h`) and to several default port implementation functions (in `jerry-port/default`). JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 49ae946 commit 54e540a

File tree

15 files changed

+172
-64
lines changed

15 files changed

+172
-64
lines changed

CMakeLists.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,27 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS )
2525
# Components
2626
set(JERRY_CMDLINE ON CACHE BOOL "Build jerry command line tool?")
2727
set(JERRY_CMDLINE_MINIMAL OFF CACHE BOOL "Build jerry minimal command line tool?")
28+
set(JERRY_PORT_DEFAULT ON CACHE BOOL "Build default jerry port implementation?")
2829
set(JERRY_LIBC ON CACHE BOOL "Build and use jerry-libc?")
2930
set(JERRY_LIBM ON CACHE BOOL "Build and use jerry-libm?")
3031
set(UNITTESTS OFF CACHE BOOL "Build unit tests?")
3132

3233
# Optional build settings
33-
set(ENABLE_ALL_IN_ONE OFF CACHE BOOL "Enable all-in-one build?")
34-
set(ENABLE_LTO ON CACHE BOOL "Enable LTO build?")
35-
set(ENABLE_STATIC_LINK ON CACHE BOOL "Enable static linking?")
36-
set(ENABLE_STRIP ON CACHE BOOL "Enable stripping all symbols from release binary?")
37-
set(PORT_DIR "${CMAKE_SOURCE_DIR}/targets/default" CACHE STRING "Use default or external port?")
34+
set(ENABLE_ALL_IN_ONE OFF CACHE BOOL "Enable all-in-one build?")
35+
set(ENABLE_LTO ON CACHE BOOL "Enable LTO build?")
36+
set(ENABLE_STATIC_LINK ON CACHE BOOL "Enable static linking?")
37+
set(ENABLE_STRIP ON CACHE BOOL "Enable stripping all symbols from release binary?")
3838

3939
if(NOT CMAKE_BUILD_TYPE)
4040
set(CMAKE_BUILD_TYPE "MinSizeRel")
4141
endif()
4242

43+
if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL)
44+
set(JERRY_PORT_DEFAULT "ON")
45+
46+
set(JERRY_PORT_DEFAULT_MESSAGE " (FORCED BY CMDLINE)")
47+
endif()
48+
4349
if("${PLATFORM}" STREQUAL "DARWIN")
4450
set(JERRY_LIBC "OFF")
4551
set(JERRY_LIBM "OFF")
@@ -85,9 +91,9 @@ message(STATUS "ENABLE_STATIC_LINK " ${ENABLE_STATIC_LINK} ${ENABLE_STATI
8591
message(STATUS "ENABLE_STRIP " ${ENABLE_STRIP} ${ENABLE_STRIP_MESSAGE})
8692
message(STATUS "JERRY_CMDLINE " ${JERRY_CMDLINE})
8793
message(STATUS "JERRY_CMDLINE_MINIMAL " ${JERRY_CMDLINE_MINIMAL})
94+
message(STATUS "JERRY_PORT_DEFAULT " ${JERRY_PORT_DEFAULT} ${JERRY_PORT_DEFAULT_MESSAGE})
8895
message(STATUS "JERRY_LIBC " ${JERRY_LIBC} ${JERRY_LIBC_MESSAGE})
8996
message(STATUS "JERRY_LIBM " ${JERRY_LIBM} ${JERRY_LIBM_MESSAGE})
90-
message(STATUS "PORT_DIR " ${PORT_DIR})
9197
message(STATUS "UNITTESTS " ${UNITTESTS})
9298

9399
# Setup directories
@@ -156,11 +162,6 @@ if(ENABLE_LTO)
156162
endif()
157163
endif()
158164

159-
# Define _BSD_SOURCE and _DEFAULT_SOURCE if we use default port and compiler default libc
160-
if(${PORT_DIR} STREQUAL "${CMAKE_SOURCE_DIR}/targets/default" AND NOT JERRY_LIBC)
161-
set(DEFINES_JERRY ${DEFINES_JERRY} _BSD_SOURCE _DEFAULT_SOURCE)
162-
endif()
163-
164165
# Compiler / Linker flags
165166
if (USING_GCC OR USING_CLANG)
166167
jerry_add_compile_flags(-fno-builtin)
@@ -232,6 +233,11 @@ endif()
232233
# Jerry's core
233234
add_subdirectory(jerry-core)
234235

236+
# Jerry's default port implementation
237+
if(JERRY_PORT_DEFAULT)
238+
add_subdirectory(jerry-port/default)
239+
endif()
240+
235241
# Jerry command line tool
236242
if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL)
237243
add_subdirectory(jerry-main)

jerry-core/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ if(FEATURE_DEBUGGER)
106106
set(SOURCE_CORE_FILES ${SOURCE_CORE_FILES} ${SOURCE_CORE_DEBUGGER})
107107
endif()
108108

109-
# Jerry port
110-
file(GLOB SOURCE_PORT_FILES "${PORT_DIR}/*.c")
111-
112109
# All-in-one build
113110
if(ENABLE_ALL_IN_ONE)
114111
set(ALL_IN_FILE "${CMAKE_BINARY_DIR}/jerry-all-in.c")

jerry-core/jerryscript-port.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,31 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
140140
*/
141141
double jerry_port_get_current_time (void);
142142

143-
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
144-
145-
#define JERRY_PORT_ENABLE_JOBQUEUE
143+
/*
144+
* JobQueue Port API
145+
*/
146146

147+
/**
148+
* Jerry job handler function type
149+
*/
147150
typedef uint32_t (*jerry_job_handler_t) (void *);
148151

152+
/**
153+
* Enqueue a job described by a pair of function and data pointers. The port is
154+
* expected to call the handler function with the given data at some (later)
155+
* point of time.
156+
*
157+
* @param handler the pointer of the handler function associated with the job.
158+
* @param job_p the data pointer to be passed to handler when called.
159+
*
160+
* Note:
161+
* This port function is only called by the implementation of the Promise
162+
* builtin (mandated by the ES2015 standard). If the engine is built with
163+
* Promise disabled (e.g., with ES5.1 profile), then the port does not have
164+
* to implement this function.
165+
*/
149166
void jerry_port_jobqueue_enqueue (jerry_job_handler_t handler, void *job_p);
150167

151-
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
152-
153168
/**
154169
* @}
155170
*/

jerry-main/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ macro(jerry_create_executable JERRY_NAME SOURCE_JERRY_STANDALONE_MAIN)
4848
set_property(TARGET ${JERRY_NAME}
4949
PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON}")
5050
target_compile_definitions(${JERRY_NAME} PRIVATE ${DEFINES_JERRY})
51-
target_include_directories(${JERRY_NAME} PRIVATE ${PORT_DIR})
5251
link_directories(${CMAKE_BINARY_DIR})
5352

5453
target_link_libraries(${JERRY_NAME} jerry-core)
@@ -59,8 +58,10 @@ endmacro()
5958
# Jerry standalones
6059
if(JERRY_CMDLINE)
6160
jerry_create_executable("jerry" "main-unix.c")
61+
target_link_libraries("jerry" jerry-port-default)
6262
endif()
6363

6464
if(JERRY_CMDLINE_MINIMAL)
6565
jerry_create_executable("jerry-minimal" "main-unix-minimal.c")
66-
endif()
66+
target_link_libraries("jerry-minimal" jerry-port-default-minimal)
67+
endif()

jerry-main/main-unix.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,9 @@ main (int argc,
593593
is_repl_mode = true;
594594
}
595595

596-
#ifdef JERRY_PORT_ENABLE_JOBQUEUE
597-
jerry_port_jobqueue_init ();
598-
#endif /* JERRY_PORT_ENABLE_JOBQUEUE */
596+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
597+
jerry_port_default_jobqueue_init ();
598+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
599599
jerry_init (flags);
600600

601601
register_js_function ("assert", assert_handler);
@@ -776,15 +776,15 @@ main (int argc,
776776
args,
777777
1);
778778
jerry_release_value (ret_val_print);
779-
#ifdef JERRY_PORT_ENABLE_JOBQUEUE
779+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
780780
jerry_release_value (ret_val_eval);
781-
ret_val_eval = jerry_port_jobqueue_run ();
781+
ret_val_eval = jerry_port_default_jobqueue_run ();
782782

783783
if (jerry_value_has_error_flag (ret_value))
784784
{
785785
print_unhandled_exception (ret_value);
786786
}
787-
#endif /* JERRY_PORT_ENABLE_JOBQUEUE */
787+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
788788
}
789789
else
790790
{
@@ -807,19 +807,19 @@ main (int argc,
807807

808808
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
809809
}
810-
#ifdef JERRY_PORT_ENABLE_JOBQUEUE
810+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
811811
else
812812
{
813813
jerry_release_value (ret_value);
814-
ret_value = jerry_port_jobqueue_run ();
814+
ret_value = jerry_port_default_jobqueue_run ();
815815

816816
if (jerry_value_has_error_flag (ret_value))
817817
{
818818
print_unhandled_exception (ret_value);
819819
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
820820
}
821821
}
822-
#endif /* JERRY_PORT_ENABLE_JOBQUEUE */
822+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
823823
jerry_release_value (ret_value);
824824
jerry_cleanup ();
825825

jerry-port/default/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
cmake_minimum_required (VERSION 2.8.12)
16+
set(JERRY_PORT_DEFAULT_NAME jerry-port-default)
17+
project (${JERRY_PORT_DEFAULT_NAME} C)
18+
19+
# Include directories
20+
set(INCLUDE_PORT_DEFAULT "${CMAKE_CURRENT_SOURCE_DIR}/include")
21+
22+
# Source directories
23+
file(GLOB SOURCE_PORT_DEFAULT *.c)
24+
25+
# Define _BSD_SOURCE and _DEFAULT_SOURCE
26+
# (should only be necessary if we used compiler default libc but not checking that)
27+
set(DEFINES_PORT_DEFAULT _BSD_SOURCE _DEFAULT_SOURCE)
28+
29+
# Default Jerry port implementation library variants:
30+
# - default
31+
# - default-minimal (no extra termination and log APIs)
32+
foreach(JERRY_PORT_LIBRARY_NAME ${JERRY_PORT_DEFAULT_NAME} ${JERRY_PORT_DEFAULT_NAME}-minimal)
33+
add_library(${JERRY_PORT_LIBRARY_NAME} STATIC ${SOURCE_PORT_DEFAULT})
34+
target_include_directories(${JERRY_PORT_LIBRARY_NAME} PUBLIC ${INCLUDE_PORT_DEFAULT})
35+
target_compile_definitions(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${DEFINES_PORT_DEFAULT})
36+
target_link_libraries(${JERRY_PORT_LIBRARY_NAME} jerry-core)
37+
endforeach()
38+
39+
target_compile_definitions(${JERRY_PORT_DEFAULT_NAME}-minimal PRIVATE DISABLE_EXTRA_API)
40+
41+
# Installation
42+
install(TARGETS ${JERRY_PORT_DEFAULT_NAME} ${JERRY_PORT_DEFAULT_NAME}-minimal DESTINATION lib)
43+
install(DIRECTORY ${INCLUDE_PORT_DEFAULT}/ DESTINATION include)

targets/default/jerry-port-default-date.c renamed to jerry-port/default/default-date.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
#include "jerryscript-port-default.h"
2222

2323
/**
24-
* Default implementation of jerry_port_get_time_zone.
24+
* Default implementation of jerry_port_get_time_zone. Uses 'gettimeofday' if
25+
* available on the system, does nothing otherwise.
26+
*
27+
* @return true - if 'gettimeofday' is available and executed successfully,
28+
* false - otherwise.
2529
*/
26-
bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
30+
bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p) /**< [out] time zone structure to fill */
2731
{
2832
#ifdef __GNUC__
2933
struct timeval tv;
@@ -48,7 +52,12 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
4852
} /* jerry_port_get_time_zone */
4953

5054
/**
51-
* Default implementation of jerry_port_get_current_time.
55+
* Default implementation of jerry_port_get_current_time. Uses 'gettimeofday' if
56+
* available on the system, does nothing otherwise.
57+
*
58+
* @return milliseconds since Unix epoch - if 'gettimeofday' is available and
59+
* executed successfully,
60+
* 0 - otherwise.
5261
*/
5362
double jerry_port_get_current_time (void)
5463
{

targets/default/jerry-port-default-fatal.c renamed to jerry-port/default/default-fatal.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
#include "jerryscript-port.h"
1919
#include "jerryscript-port-default.h"
2020

21+
#ifndef DISABLE_EXTRA_API
22+
2123
static bool abort_on_fail = false;
2224

2325
/**
2426
* Sets whether 'abort' should be called instead of 'exit' upon exiting with
2527
* non-zero exit code in the default implementation of jerry_port_fatal.
28+
*
29+
* Note:
30+
* This function is only available if the port implementation library is
31+
* compiled without the DISABLE_EXTRA_API macro.
2632
*/
2733
void jerry_port_default_set_abort_on_fail (bool flag) /**< new value of 'abort on fail' flag */
2834
{
@@ -35,17 +41,29 @@ void jerry_port_default_set_abort_on_fail (bool flag) /**< new value of 'abort o
3541
*
3642
* @return true - if 'abort on fail' flag is set,
3743
* false - otherwise
44+
*
45+
* Note:
46+
* This function is only available if the port implementation library is
47+
* compiled without the DISABLE_EXTRA_API macro.
3848
*/
3949
bool jerry_port_default_is_abort_on_fail (void)
4050
{
4151
return abort_on_fail;
4252
} /* jerry_port_default_is_abort_on_fail */
4353

54+
#endif /* !DISABLE_EXTRA_API */
55+
4456
/**
45-
* Default implementation of jerry_port_fatal.
57+
* Default implementation of jerry_port_fatal. Calls 'abort' if exit code is
58+
* non-zero and "abort-on-fail" behaviour is enabled, 'exit' otherwise.
59+
*
60+
* Note:
61+
* The "abort-on-fail" behaviour is only available if the port
62+
* implementation library is compiled without the DISABLE_EXTRA_API macro.
4663
*/
4764
void jerry_port_fatal (jerry_fatal_code_t code)
4865
{
66+
#ifndef DISABLE_EXTRA_API
4967
if (code != 0
5068
&& code != ERR_OUT_OF_MEMORY
5169
&& jerry_port_default_is_abort_on_fail ())
@@ -54,6 +72,9 @@ void jerry_port_fatal (jerry_fatal_code_t code)
5472
}
5573
else
5674
{
75+
#endif /* !DISABLE_EXTRA_API */
5776
exit (code);
77+
#ifndef DISABLE_EXTRA_API
5878
}
79+
#endif /* !DISABLE_EXTRA_API */
5980
} /* jerry_port_fatal */

targets/default/jerry-port-default-io.c renamed to jerry-port/default/default-io.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,47 @@
1818
#include "jerryscript-port.h"
1919
#include "jerryscript-port-default.h"
2020

21+
#ifndef DISABLE_EXTRA_API
22+
2123
/**
2224
* Actual log level
2325
*/
24-
static jerry_log_level_t jerry_log_level = JERRY_LOG_LEVEL_ERROR;
26+
static jerry_log_level_t jerry_port_default_log_level = JERRY_LOG_LEVEL_ERROR;
27+
28+
#define JERRY_PORT_DEFAULT_LOG_LEVEL jerry_port_default_log_level
2529

2630
/**
2731
* Get the log level
2832
*
2933
* @return current log level
34+
*
35+
* Note:
36+
* This function is only available if the port implementation library is
37+
* compiled without the DISABLE_EXTRA_API macro.
3038
*/
3139
jerry_log_level_t
3240
jerry_port_default_get_log_level (void)
3341
{
34-
return jerry_log_level;
42+
return jerry_port_default_log_level;
3543
} /* jerry_port_default_get_log_level */
3644

3745
/**
3846
* Set the log level
47+
*
48+
* Note:
49+
* This function is only available if the port implementation library is
50+
* compiled without the DISABLE_EXTRA_API macro.
3951
*/
4052
void
4153
jerry_port_default_set_log_level (jerry_log_level_t level) /**< log level */
4254
{
43-
jerry_log_level = level;
55+
jerry_port_default_log_level = level;
4456
} /* jerry_port_default_set_log_level */
4557

58+
#else /* DISABLE_EXTRA_API */
59+
#define JERRY_PORT_DEFAULT_LOG_LEVEL JERRY_LOG_LEVEL_ERROR
60+
#endif /* !DISABLE_EXTRA_API */
61+
4662
/**
4763
* Provide console message implementation for the engine.
4864
*/
@@ -57,14 +73,21 @@ jerry_port_console (const char *format, /**< format string */
5773
} /* jerry_port_console */
5874

5975
/**
60-
* Provide log message implementation for the engine.
76+
* Default implementation of jerry_port_log. Prints log message to standard
77+
* error with 'vfprintf' if message level is less than or equal to the set log
78+
* level.
79+
*
80+
* Note:
81+
* Changing the log level from JERRY_LOG_LEVEL_ERROR is only possible if
82+
* the port implementation library is compiled without the
83+
* DISABLE_EXTRA_API macro.
6184
*/
6285
void
6386
jerry_port_log (jerry_log_level_t level, /**< log level */
6487
const char *format, /**< format string */
6588
...) /**< parameters */
6689
{
67-
if (level <= jerry_log_level)
90+
if (level <= JERRY_PORT_DEFAULT_LOG_LEVEL)
6891
{
6992
va_list args;
7093
va_start (args, format);

0 commit comments

Comments
 (0)