Skip to content
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

Rename jerry_port_print_char to jerry_port_string_print for printing UTF8 directly #4860

Closed
Closed
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
22 changes: 15 additions & 7 deletions docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ typedef enum
void jerry_port_log (jerry_log_level_t level, const char *fmt, ...);
```

The `jerry_port_print_char` is currently not used by the jerry-core directly.
The `jerry_port_string_print` is currently not used by the jerry-core directly.
However, it provides a port specific way for `jerry-ext` components to print
information.

```c
/**
* Print a character to stdout.
* Print a zero-terminated UTF-8 string with length to standard output.
*
* @param s The zero-terminated UTF-8 string to print
* @param len The length of the UTF-8 string.
*/
void jerry_port_print_char (char c);
void jerry_port_string_print (const char *s, size_t len);
```

### Jerry Module system
Expand Down Expand Up @@ -278,13 +281,18 @@ jerry_port_log (jerry_log_level_t level, /**< log level */

```c
/**
* Print a character to stdout with putchar.
* Provide implementation of jerry_port_string_print.
* Uses 'printf' to print a zero-terminated UTF-8 string to standard output.
*
* @param s The zero-terminated UTF-8 string to print
* @param len The length of the UTF-8 string.
*/
void
jerry_port_print_char (char c)
jerry_port_string_print (const char *s, size_t len)
{
putchar (c);
} /* jerry_port_print_char */
(void) len;
printf ("%s", s);
} /* jerry_port_string_print */
```

## Date
Expand Down
6 changes: 3 additions & 3 deletions docs/10.EXT-REFERENCE-HANDLER.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,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
`jerry_port_print_char`. The NULL character is output as "\u0000",
`jerry_port_string_print`. The NULL 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
`jerry_port_print_char`.
`jerry_port_string_print`.

**Prototype**

Expand All @@ -540,7 +540,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)
- [jerry_port_print_char](05.PORT-API.md#jerry_port_print_char)
- [jerry_port_string_print](05.PORT-API.md#jerry_port_string_print)


# Handler registration helper
Expand Down
2 changes: 1 addition & 1 deletion docs/16.MIGRATION-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ In this section the new API functions are listed.
- [`jerry_port_normalize_path`](05.PORT-API.md#jerry_port_normalize_path)
- [`jerry_port_read_source`](05.PORT-API.md#jerry_port_read_source)
- [`jerry_port_release_source`](05.PORT-API.md#jerry_port_release_source)
- [`jerry_port_print_char`](05.PORT-API.md#jerry_port_print_char)
- [`jerry_port_string_print`](05.PORT-API.md#jerry_port_string_print)
- [`jerry_port_get_current_context`](05.PORT-API.md#jerry_port_get_current_context)
- [`jerry_port_fatal`](05.PORT-API.md#jerry_port_fatal)
- [`jerry_port_sleep`](05.PORT-API.md#jerry_port_sleep)
79 changes: 62 additions & 17 deletions jerry-core/api/jerryscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -3080,42 +3080,87 @@ jerry_string_iterate (const jerry_value_t value,
} /* jerry_string_iterate */

/**
* Print char wrapper that casts the argument to an unsigned type
* Iterate over the input string value, visiting each code point of the string once. If
* the input value is not a string, the function will do nothing.
*
* @param byte encoded byte value
* @param user_p user pointer
* @param value the input string value
* @param callback callback function called for each code point of the string.
* @param user_p User pointer passed to the callback function
*/
static void
jerry_print_char_wrapper (uint8_t byte, void *user_p)
void
jerry_string_iterate_code_point (const jerry_value_t value, jerry_string_iterate_code_point_cb_t callback, void *user_p)
{
JERRY_UNUSED (user_p);
static const char *const null_str_p = "\\u0000";

if (JERRY_UNLIKELY (byte == '\0'))
if (!ecma_is_value_string (value))
{
const char *curr_p = null_str_p;
return;
}

while (*curr_p != '\0')
ecma_string_t *str_p = ecma_get_string_from_value (value);
ECMA_STRING_TO_UTF8_STRING (str_p, buffer_p, buffer_size);

const lit_utf8_byte_t *current_p = buffer_p;
const lit_utf8_byte_t *end_p = buffer_p + buffer_size;

while (current_p < end_p)
{
if (JERRY_UNLIKELY (*current_p >= LIT_UTF8_2_BYTE_MARKER))
{
jerry_port_print_char (*curr_p++);
lit_code_point_t cp;
lit_utf8_size_t read_size = lit_read_code_point_from_cesu8 (current_p, end_p, &cp);
callback (cp, user_p);
current_p += read_size;
continue;
}

return;
callback (*current_p++, user_p);
}
} /* jerry_string_iterate_code_point */

jerry_port_print_char ((char) byte);
} /* jerry_print_char_wrapper */
/**
* Print unicode code point to console
*
* @param code_point unicode code point
*/
void
jerry_code_point_print (uint32_t code_point)
{
if (JERRY_UNLIKELY (code_point == 0))
{
static const char null_str_p[] = "\\u0000";
jerry_port_string_print (null_str_p, sizeof (null_str_p) - 1);
}
else
{
lit_utf8_byte_t bytes[LIT_UTF8_MAX_BYTES_IN_CODE_POINT + 1];
lit_utf8_size_t encoded_size = lit_code_point_to_utf8 (code_point, bytes);
bytes[encoded_size] = 0;
jerry_port_string_print ((const char *) bytes, encoded_size);
}
} /* jerry_code_point_print */

/**
* Print code point to console wrapper
*
* @param code_point unicode code point
* @param user_p user pointer
*/
static void
jerry_print_codepoint_wrapper (uint32_t code_point, void *user_p)
{
JERRY_UNUSED (user_p);
jerry_code_point_print (code_point);
} /* jerry_print_codepoint_wrapper */

/**
* Print the argument string in utf8 encoding using jerry_port_print_char.
* Print the argument string in utf8 encoding using jerry_port_string_print.
* If the argument is not a string, the function does nothing.
*
* @param value the input string value
*/
void
jerry_string_print (const jerry_value_t value)
{
jerry_string_iterate (value, JERRY_ENCODING_UTF8, &jerry_print_char_wrapper, NULL);
jerry_string_iterate_code_point (value, &jerry_print_codepoint_wrapper, NULL);
} /* jerry_string_print */

/**
Expand Down
4 changes: 4 additions & 0 deletions jerry-core/include/jerryscript-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ void jerry_string_iterate (const jerry_value_t value,
jerry_encoding_t encoding,
jerry_string_iterate_cb_t callback,
void *user_p);
void jerry_string_iterate_code_point (const jerry_value_t value,
jerry_string_iterate_code_point_cb_t callback,
void *user_p);
void jerry_code_point_print (uint32_t code_point);
void jerry_string_print (const jerry_value_t value);
/**
* jerry-api-string-op @}
Expand Down
7 changes: 4 additions & 3 deletions jerry-core/include/jerryscript-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,17 @@ struct jerry_context_t *jerry_port_get_current_context (void);
void jerry_port_sleep (uint32_t sleep_time);

/**
* Print a single character.
* Print a zero-terminated UTF-8 string with length to standard output.
*
* 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.
* @param s The zero-terminated UTF-8 string to print
* @param len The length of the UTF-8 string.
*/
void jerry_port_print_char (char c);
void jerry_port_string_print (const char *s, size_t len);

/**
* Open a source file and read its contents into a buffer.
Expand Down
5 changes: 5 additions & 0 deletions jerry-core/include/jerryscript-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ typedef void (*jerry_throw_cb_t) (const jerry_value_t exception_value, void *use
*/
typedef void (*jerry_string_iterate_cb_t) (uint8_t byte, void *user_p);

/**
* Function type applied each unicode code point of a string
*/
typedef void (*jerry_string_iterate_code_point_cb_t) (uint32_t code_point, void *user_p);

/**
* Function type applied for each data property of an object.
*/
Expand Down
8 changes: 4 additions & 4 deletions jerry-ext/handler/handler-print.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Provide a 'print' implementation for scripts.
*
* The routine converts all of its arguments to strings and outputs them
* char-by-char using jerry_port_print_char.
* by using jerry_code_point_print and jerry_string_print.
*
* The NUL character is output as "\u0000", other characters are output
* bytewise.
Expand All @@ -32,7 +32,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 `jerry_port_print_char`.
* provides `jerry_port_string_print`.
*
* @return undefined - if all arguments could be converted to strings,
* error - otherwise.
Expand Down Expand Up @@ -68,13 +68,13 @@ jerryx_handler_print (const jerry_call_info_t *call_info_p, /**< call informatio

if (arg_index > 0)
{
jerry_port_print_char (' ');
jerry_code_point_print (' ');
}

jerry_string_print (str_val);
jerry_value_free (str_val);
}

jerry_port_print_char ('\n');
jerry_code_point_print ('\n');
return ret_val;
} /* jerryx_handler_print */
58 changes: 38 additions & 20 deletions jerry-port/default/default-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "jerryscript-port-default.h"
#include "jerryscript-port.h"

#ifdef _WIN32
#include <windows.h>
#endif /* _WIN32 */

/**
* Actual log level
*/
Expand Down Expand Up @@ -80,30 +84,44 @@ jerry_port_log (jerry_log_level_t level, /**< message log level */
}
} /* jerry_port_log */

#if defined(JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)

#define DEBUG_BUFFER_SIZE (256)
static char debug_buffer[DEBUG_BUFFER_SIZE];
static int debug_buffer_index = 0;

#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */

/**
* Default implementation of jerry_port_print_char. Uses 'putchar' to
* print a single character to standard output.
* Default implementation of jerry_port_string_print.
* On win32 use WriteConsoleW and WriteFile to print UTF-8 string to standard output.
* On non-win32 os use fwrite to print UTF-8 string to standard output.
*
* @param s The zero-terminated UTF-8 string to print
* @param len The length of the UTF-8 string.
*/
void
jerry_port_print_char (char c) /**< the character to print */
jerry_port_string_print (const char *s, size_t len)
{
putchar (c);

#if defined(JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
debug_buffer[debug_buffer_index++] = c;

if ((debug_buffer_index == DEBUG_BUFFER_SIZE) || (c == '\n'))
#ifdef _WIN32
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
if (hOut != INVALID_HANDLE_VALUE)
{
jerry_debugger_send_output ((jerry_char_t *) debug_buffer, (jerry_size_t) debug_buffer_index);
debug_buffer_index = 0;
int hType = GetFileType (hOut);
if (FILE_TYPE_CHAR == hType)
{
DWORD charsWritten = -1;
JERRY_VLA (wchar_t, ws, (len + 1));
int utf16_count = MultiByteToWideChar (CP_UTF8, 0, s, (int) len, ws, (int) (len + 1));
ws[utf16_count] = '\0';
WriteConsoleW (hOut, ws, utf16_count, &charsWritten, 0);
}
else
{
WriteFile (hOut, s, len, NULL, NULL);
}
}
else
{
fwrite (s, 1, len, stdout);
}
#else /* !_WIN32 */
fwrite (s, 1, len, stdout);
#endif /* _WIN32 */

#if defined(JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
jerry_debugger_send_output ((const jerry_char_t *) s, (jerry_size_t) len);
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
} /* jerry_port_print_char */
} /* jerry_port_string_print */
24 changes: 12 additions & 12 deletions targets/baremetal-sdk/esp-idf/jerry_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,24 @@ static int debug_buffer_index = 0;
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */

/**
* Default implementation of jerry_port_print_char. Uses 'putchar' to
* print a single character to standard output.
* Provide implementation of jerry_port_string_print.
* Uses 'putchar' to print each utf8 string characters to standard output one by one.
*
* @param s The zero-terminated UTF-8 string to print
* @param len The length of the UTF-8 string.
*/
void
jerry_port_print_char (char c) /**< the character to print */
jerry_port_string_print (const char *s, size_t len)
{
putchar(c);

#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
debug_buffer[debug_buffer_index++] = c;

if ((debug_buffer_index == DEBUG_BUFFER_SIZE) || (c == '\n'))
for (size_t i = 0; i < len; ++i)
{
jerry_debugger_send_output ((jerry_char_t *) debug_buffer, (jerry_size_t) debug_buffer_index);
debug_buffer_index = 0;
putchar (s[i]);
}

#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
jerry_debugger_send_output ((const jerry_char_t *) s, (jerry_size_t) len);
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
} /* jerry_port_print_char */
} /* jerry_port_string_print */

/**
* Default implementation of jerry_port_fatal. Calls 'abort' if exit code is
Expand Down
14 changes: 9 additions & 5 deletions targets/os/nuttx/jerry_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,18 @@ jerry_port_get_current_time (void)
} /* jerry_port_get_current_time */

/**
* Provide the implementation of jerry_port_print_char.
* Uses 'printf' to print a single character to standard output.
* Provide implementation of jerry_port_string_print.
* Uses 'printf' to print a zero-terminated UTF-8 string to standard output.
*
* @param s The zero-terminated UTF-8 string to print
* @param len The length of the UTF-8 string.
*/
void
jerry_port_print_char (char c) /**< the character to print */
jerry_port_string_print (const char *s, size_t len)
{
printf ("%c", c);
} /* jerry_port_print_char */
(void) len;
printf ("%s", s);
} /* jerry_port_string_print */

/**
* Provide implementation of jerry_port_sleep.
Expand Down
Loading