Make jerryx_port_handler_print_char truly a port function (#2789)

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
This commit is contained in:
Péter Gál
2019-03-13 10:19:30 +01:00
committed by Dániel Bátyai
parent 90f37a5573
commit 162ba8c116
10 changed files with 82 additions and 109 deletions
+23
View File
@@ -72,6 +72,18 @@ typedef enum
void jerry_port_log (jerry_log_level_t level, const char *fmt, ...); 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 ## Date
```c ```c
@@ -201,6 +213,17 @@ jerry_port_log (jerry_log_level_t level, /**< log level */
} /* jerry_port_log */ } /* 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 ## Date
```c ```c
+3 -38
View File
@@ -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 Provide a `print` implementation for scripts. The routine converts all of its
arguments to strings and outputs them char-by-char using 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. other characters are output bytewise.
*Note*: This implementation does not use standard C `printf` to print its *Note*: This implementation does not use standard C `printf` to print its
output. This allows more flexibility but also extends the core JerryScript output. This allows more flexibility but also extends the core JerryScript
engine port API. Applications that want to use `jerryx_handler_print` must engine port API. Applications that want to use `jerryx_handler_print` must
ensure that their port implementation also provides ensure that their port implementation also provides
`jerryx_port_handler_print_char`. `jerry_port_print_char`.
**Prototype** **Prototype**
@@ -124,7 +124,7 @@ jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this
**See also** **See also**
- [jerryx_handler_register_global](#jerryx_handler_register_global) - [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 # Handler registration helper
@@ -184,38 +184,3 @@ register_common_functions (void)
jerry_release_value (ret); 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)
+12
View File
@@ -179,6 +179,18 @@ struct jerry_context_t *jerry_port_get_current_context (void);
*/ */
void jerry_port_sleep (uint32_t sleep_time); 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);
/** /**
* @} * @}
*/ */
+6 -5
View File
@@ -14,13 +14,14 @@
*/ */
#include "jerryscript-ext/handler.h" #include "jerryscript-ext/handler.h"
#include "jerryscript-port.h"
#include "jerryscript-debugger.h" #include "jerryscript-debugger.h"
/** /**
* Provide a 'print' implementation for scripts. * Provide a 'print' implementation for scripts.
* *
* The routine converts all of its arguments to strings and outputs them * 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 * The NUL character is output as "\u0000", other characters are output
* bytewise. * bytewise.
@@ -30,7 +31,7 @@
* output. This allows more flexibility but also extends the core * output. This allows more flexibility but also extends the core
* JerryScript engine port API. Applications that want to use * JerryScript engine port API. Applications that want to use
* `jerryx_handler_print` must ensure that their port implementation also * `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, * @return undefined - if all arguments could be converted to strings,
* error - otherwise. * error - otherwise.
@@ -103,13 +104,13 @@ jerryx_handler_print (const jerry_value_t func_obj_val, /**< function object */
if (chr != '\0') if (chr != '\0')
{ {
jerryx_port_handler_print_char (chr); jerry_port_print_char (chr);
continue; continue;
} }
for (jerry_size_t null_index = 0; null_str[null_index] != '\0'; null_index++) 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)) if (args_cnt == 0 || jerry_value_is_error (ret_val))
{ {
jerryx_port_handler_print_char ('\n'); jerry_port_print_char ('\n');
} }
return ret_val; return ret_val;
@@ -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, 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); 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 #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
+29
View File
@@ -98,3 +98,32 @@ jerry_port_log (jerry_log_level_t level, /**< message log level */
va_end (args); va_end (args);
} }
} /* jerry_port_log */ } /* 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 */
-46
View File
@@ -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 <stdio.h>
#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 */
+3 -3
View File
@@ -515,11 +515,11 @@ jerry_port_get_current_time (void)
} /* jerry_port_get_current_time */ } /* 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. * Uses 'printf' to print a single character to standard output.
*/ */
void 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); printf ("%c", c);
} /* jerryx_port_handler_print_char */ } /* jerry_port_print_char */
@@ -497,14 +497,14 @@ jerry_port_get_current_time (void)
} /* jerry_port_get_current_time */ } /* 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. * Uses 'printf' to print a single character to standard output.
*/ */
void 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); printf ("%c", c);
} /* jerryx_port_handler_print_char */ } /* jerry_port_print_char */
/** /**
* Main program. * Main program.
+3 -3
View File
@@ -71,11 +71,11 @@ jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc)
} /* jerry_port_get_local_time_zone_adjustment */ } /* 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. * Uses 'printf' to print a single character to standard output.
*/ */
void 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); printf ("%c", c);
} /* jerryx_port_handler_print_char */ } /* jerry_port_print_char */