Skip to content

Make jerryx_port_handler_print_char truly a port function #2789

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 1 commit into from
Mar 13, 2019
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
23 changes: 23 additions & 0 deletions docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 3 additions & 38 deletions docs/10.EXT-REFERENCE-HANDLER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand All @@ -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
Expand Down Expand Up @@ -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)
12 changes: 12 additions & 0 deletions jerry-core/include/jerryscript-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
* @}
*/
Expand Down
11 changes: 6 additions & 5 deletions jerry-ext/handler/handler-print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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]);
}
}
}
Expand All @@ -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;
Expand Down
11 changes: 0 additions & 11 deletions jerry-ext/include/jerryscript-ext/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
29 changes: 29 additions & 0 deletions jerry-port/default/default-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
46 changes: 0 additions & 46 deletions jerry-port/default/defaultx-handler.c

This file was deleted.

6 changes: 3 additions & 3 deletions targets/nuttx-stm32f4/jerry_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
6 changes: 3 additions & 3 deletions targets/tizenrt-artik053/apps/jerryscript/jerry_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions targets/zephyr/src/jerry-port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */