From 09251845d8c571625bb39fc738bb62fbe39e6b78 Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Tue, 5 Mar 2019 13:49:51 +0100 Subject: [PATCH] Make jerryx_port_handler_print_char truly a port function In the previous approach `jerryx_port_handler_print_char` was implemented in by the jerry-default-port. This implementation however required the jerry-ext handler header file which created a requirement in the jerry-default-port for the jerry-ext headers. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com --- docs/05.PORT-API.md | 23 ++++++++++ docs/10.EXT-REFERENCE-HANDLER.md | 41 ++--------------- jerry-core/include/jerryscript-port.h | 12 +++++ jerry-ext/handler/handler-print.c | 11 +++-- jerry-ext/include/jerryscript-ext/handler.h | 11 ----- jerry-port/default/default-io.c | 29 ++++++++++++ jerry-port/default/defaultx-handler.c | 46 ------------------- targets/nuttx-stm32f4/jerry_main.c | 6 +-- .../apps/jerryscript/jerry_main.c | 6 +-- targets/zephyr/src/jerry-port.c | 6 +-- 10 files changed, 82 insertions(+), 109 deletions(-) delete mode 100644 jerry-port/default/defaultx-handler.c diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index bfdab9e134..efed147434 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -72,6 +72,18 @@ typedef enum void jerry_port_log (jerry_log_level_t level, const char *fmt, ...); ``` +The `jerry_port_print_char` is currenlty not used by the jerry-core directly. +However, it provides a port specifc way for `jerry-ext` components to print +information. + +```c +/** + * Print a character to stdout. + */ +void jerry_port_print_char (char c); +``` + + ## Date ```c @@ -201,6 +213,17 @@ jerry_port_log (jerry_log_level_t level, /**< log level */ } /* jerry_port_log */ ``` +```c +/** + * Print a character to stdout with putchar. + */ +void +jerry_port_print_char (char c) +{ + putchar (c); +} /* jerr_port_print_char */ +``` + ## Date ```c diff --git a/docs/10.EXT-REFERENCE-HANDLER.md b/docs/10.EXT-REFERENCE-HANDLER.md index 9d6bf46561..c942cea172 100644 --- a/docs/10.EXT-REFERENCE-HANDLER.md +++ b/docs/10.EXT-REFERENCE-HANDLER.md @@ -97,14 +97,14 @@ jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_value_t this_p, Provide a `print` implementation for scripts. The routine converts all of its arguments to strings and outputs them char-by-char using -`jerryx_port_handler_print_char`. The NUL character is output as "\u0000", +`jerry_port_print_char`. The NUL character is output as "\u0000", other characters are output bytewise. *Note*: This implementation does not use standard C `printf` to print its output. This allows more flexibility but also extends the core JerryScript engine port API. Applications that want to use `jerryx_handler_print` must ensure that their port implementation also provides -`jerryx_port_handler_print_char`. +`jerry_port_print_char`. **Prototype** @@ -124,7 +124,7 @@ jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this **See also** - [jerryx_handler_register_global](#jerryx_handler_register_global) -- [jerryx_port_handler_print_char](#jerryx_port_handler_print_char) +- [jerry_port_print_char](05.PORT-API.md#jerry_port_print_char) # Handler registration helper @@ -184,38 +184,3 @@ register_common_functions (void) jerry_release_value (ret); } ``` - - -# Port API extension - -## jerryx_port_handler_print_char - -**Summary** - -Print a single character. - -**Prototype** - -```c -void -jerryx_port_handler_print_char (char c); -``` - -- `c` - the character to print. - -**Example** - -```c -/** - * Print a character to stdout with printf. - */ -void -jerryx_port_handler_print_char (char c) -{ - printf ("%c", c); -} /* jerryx_port_handler_print_char */ -``` - -**See also** - -- [jerryx_handler_print](#jerryx_handler_print) diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index f79efed0bf..0a631f4569 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -179,6 +179,18 @@ struct jerry_context_t *jerry_port_get_current_context (void); */ void jerry_port_sleep (uint32_t sleep_time); +/** + * Print a single character. + * + * Note: + * This port function is here so the jerry-ext components would have + * a common way to print out information. + * If possible do not use from the jerry-core. + * + * @param c the character to print. + */ +void jerry_port_print_char (char c); + /** * @} */ diff --git a/jerry-ext/handler/handler-print.c b/jerry-ext/handler/handler-print.c index 1175b17b42..8de5838472 100644 --- a/jerry-ext/handler/handler-print.c +++ b/jerry-ext/handler/handler-print.c @@ -14,13 +14,14 @@ */ #include "jerryscript-ext/handler.h" +#include "jerryscript-port.h" #include "jerryscript-debugger.h" /** * Provide a 'print' implementation for scripts. * * The routine converts all of its arguments to strings and outputs them - * char-by-char using jerryx_port_handler_print_char. + * char-by-char using jerry_port_print_char. * * The NUL character is output as "\u0000", other characters are output * bytewise. @@ -30,7 +31,7 @@ * output. This allows more flexibility but also extends the core * JerryScript engine port API. Applications that want to use * `jerryx_handler_print` must ensure that their port implementation also - * provides `jerryx_port_handler_print_char`. + * provides `jerry_port_print_char`. * * @return undefined - if all arguments could be converted to strings, * error - otherwise. @@ -103,13 +104,13 @@ jerryx_handler_print (const jerry_value_t func_obj_val, /**< function object */ if (chr != '\0') { - jerryx_port_handler_print_char (chr); + jerry_port_print_char (chr); continue; } for (jerry_size_t null_index = 0; null_str[null_index] != '\0'; null_index++) { - jerryx_port_handler_print_char (null_str[null_index]); + jerry_port_print_char (null_str[null_index]); } } } @@ -120,7 +121,7 @@ jerryx_handler_print (const jerry_value_t func_obj_val, /**< function object */ if (args_cnt == 0 || jerry_value_is_error (ret_val)) { - jerryx_port_handler_print_char ('\n'); + jerry_port_print_char ('\n'); } return ret_val; diff --git a/jerry-ext/include/jerryscript-ext/handler.h b/jerry-ext/include/jerryscript-ext/handler.h index 0f0ff13fca..c9641d5fa2 100644 --- a/jerry-ext/include/jerryscript-ext/handler.h +++ b/jerry-ext/include/jerryscript-ext/handler.h @@ -45,17 +45,6 @@ jerry_value_t jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_v jerry_value_t jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this_p, const jerry_value_t args_p[], const jerry_length_t args_cnt); -/* - * Port API extension - */ - -/** - * Print a single character. - * - * @param c the character to print. - */ -void jerryx_port_handler_print_char (char c); - #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/jerry-port/default/default-io.c b/jerry-port/default/default-io.c index affab21b2b..2230a13a76 100644 --- a/jerry-port/default/default-io.c +++ b/jerry-port/default/default-io.c @@ -98,3 +98,32 @@ jerry_port_log (jerry_log_level_t level, /**< message log level */ va_end (args); } } /* jerry_port_log */ + + +#ifdef JERRY_DEBUGGER + +#define DEBUG_BUFFER_SIZE (256) +static char debug_buffer[DEBUG_BUFFER_SIZE]; +static int debug_buffer_index = 0; + +#endif /* JERRY_DEBUGGER */ + +/** + * Default implementation of jerry_port_print_char. Uses 'putchar' to + * print a single character to standard output. + */ +void +jerry_port_print_char (char c) /**< the character to print */ +{ + putchar (c); + +#ifdef JERRY_DEBUGGER + debug_buffer[debug_buffer_index++] = c; + + if ((debug_buffer_index == DEBUG_BUFFER_SIZE) || (c == '\n')) + { + jerry_debugger_send_output ((jerry_char_t *) debug_buffer, (jerry_size_t) debug_buffer_index); + debug_buffer_index = 0; + } +#endif /* JERRY_DEBUGGER */ +} /* jerry_port_print_char */ diff --git a/jerry-port/default/defaultx-handler.c b/jerry-port/default/defaultx-handler.c deleted file mode 100644 index 8e9c93934a..0000000000 --- a/jerry-port/default/defaultx-handler.c +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * 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 - -#include "jerryscript-ext/handler.h" - -#ifdef JERRY_DEBUGGER - -#define DEBUG_BUFFER_SIZE (256) -static char debug_buffer[DEBUG_BUFFER_SIZE]; -static int debug_buffer_index = 0; - -#endif /* JERRY_DEBUGGER */ - -/** - * Default implementation of jerryx_port_handler_print_char. Uses 'printf' to - * print a single character to standard output. - */ -void -jerryx_port_handler_print_char (char c) /**< the character to print */ -{ - printf ("%c", c); - -#ifdef JERRY_DEBUGGER - debug_buffer[debug_buffer_index++] = c; - - if ((debug_buffer_index == DEBUG_BUFFER_SIZE) || (c == '\n')) - { - jerry_debugger_send_output ((jerry_char_t *) debug_buffer, (jerry_size_t) debug_buffer_index); - debug_buffer_index = 0; - } -#endif /* JERRY_DEBUGGER */ -} /* jerryx_port_handler_print_char */ diff --git a/targets/nuttx-stm32f4/jerry_main.c b/targets/nuttx-stm32f4/jerry_main.c index 61419b99b9..e32a747279 100644 --- a/targets/nuttx-stm32f4/jerry_main.c +++ b/targets/nuttx-stm32f4/jerry_main.c @@ -515,11 +515,11 @@ jerry_port_get_current_time (void) } /* jerry_port_get_current_time */ /** - * Provide the implementation of jerryx_port_handler_print_char. + * Provide the implementation of jerry_port_print_char. * Uses 'printf' to print a single character to standard output. */ void -jerryx_port_handler_print_char (char c) /**< the character to print */ +jerry_port_print_char (char c) /**< the character to print */ { printf ("%c", c); -} /* jerryx_port_handler_print_char */ +} /* jerry_port_print_char */ diff --git a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c index 2774c58193..90abb439dd 100644 --- a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c +++ b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c @@ -497,14 +497,14 @@ jerry_port_get_current_time (void) } /* jerry_port_get_current_time */ /** - * Provide the implementation of jerryx_port_handler_print_char. + * Provide the implementation of jerry_port_print_char. * Uses 'printf' to print a single character to standard output. */ void -jerryx_port_handler_print_char (char c) /**< the character to print */ +jerry_port_print_char (char c) /**< the character to print */ { printf ("%c", c); -} /* jerryx_port_handler_print_char */ +} /* jerry_port_print_char */ /** * Main program. diff --git a/targets/zephyr/src/jerry-port.c b/targets/zephyr/src/jerry-port.c index 5a129d8fbe..016510696d 100644 --- a/targets/zephyr/src/jerry-port.c +++ b/targets/zephyr/src/jerry-port.c @@ -71,11 +71,11 @@ jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc) } /* jerry_port_get_local_time_zone_adjustment */ /** - * Provide the implementation of jerryx_port_handler_print_char. + * Provide the implementation of jerry_port_print_char. * Uses 'printf' to print a single character to standard output. */ void -jerryx_port_handler_print_char (char c) /**< the character to print */ +jerry_port_print_char (char c) /**< the character to print */ { printf ("%c", c); -} /* jerryx_port_handler_print_char */ +} /* jerry_port_print_char */