diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index 4577f598e2..298a1c4402 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -116,18 +116,6 @@ void jerry_port_context_free (void); void jerry_port_log (const char *message_p); ``` -```c -/** - * Print a single character to standard output. - * - * This port function is never called from jerry-core directly, it is only used by jerry-ext components to print - * information. - * - * @param byte: the byte to print. - */ -void jerry_port_print_byte (jerry_char_t byte); -``` - ```c /** * Print a buffer to standard output diff --git a/docs/10.EXT-REFERENCE-HANDLER.md b/docs/10.EXT-REFERENCE-HANDLER.md index d14daafaba..59d4390b80 100644 --- a/docs/10.EXT-REFERENCE-HANDLER.md +++ b/docs/10.EXT-REFERENCE-HANDLER.md @@ -351,15 +351,14 @@ jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_value_t this_p, **Summary** 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_byte`. The NULL character is output as "\u0000", -other characters are output bytewise. +arguments to strings and outputs them by using `jerry_port_print_buffer`. +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_byte`. +`jerry_port_print_buffer`. **Prototype** @@ -379,7 +378,7 @@ jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this **See also** - [jerryx_register_global](#jerryx_register_global) -- [jerry_port_print_byte](05.PORT-API.md#jerry_port_print_char) +- [jerry_port_print_buffer](05.PORT-API.md#jerry_port_print_buffer) # Handler registration helper diff --git a/docs/16.MIGRATION-GUIDE.md b/docs/16.MIGRATION-GUIDE.md index 9c71d63a1b..553330cb1b 100644 --- a/docs/16.MIGRATION-GUIDE.md +++ b/docs/16.MIGRATION-GUIDE.md @@ -770,3 +770,4 @@ In this section the new API functions are listed. - [`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) +- [`jerry_port_print_byte`](05.PORT-API.md#jerry_port_print_byte) diff --git a/jerry-core/api/jerry-snapshot.c b/jerry-core/api/jerry-snapshot.c index 5c13eaf30f..33760b299f 100644 --- a/jerry-core/api/jerry-snapshot.c +++ b/jerry-core/api/jerry-snapshot.c @@ -1455,22 +1455,17 @@ jerry_save_literals_sort (ecma_string_t *literals[], /**< array of literals */ static uint8_t * jerry_append_chars_to_buffer (uint8_t *buffer_p, /**< buffer */ uint8_t *buffer_end_p, /**< the end of the buffer */ - const char *chars, /**< string */ - lit_utf8_size_t string_size) /**< string size */ + const jerry_char_t *chars, /**< string */ + jerry_size_t string_size) /**< string size */ { if (buffer_p > buffer_end_p) { return buffer_p; } - if (string_size == 0) - { - string_size = (lit_utf8_size_t) strlen (chars); - } - if (buffer_p + string_size <= buffer_end_p) { - memcpy ((char *) buffer_p, chars, string_size); + memcpy ((char *) buffer_p, (const char *) chars, string_size); return buffer_p + string_size; } @@ -1492,8 +1487,10 @@ jerry_append_ecma_string_to_buffer (uint8_t *buffer_p, /**< buffer */ ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer_p, str_buffer_size); /* Append the string to the buffer. */ - uint8_t *new_buffer_p = - jerry_append_chars_to_buffer (buffer_p, buffer_end_p, (const char *) str_buffer_p, str_buffer_size); + uint8_t *new_buffer_p = jerry_append_chars_to_buffer (buffer_p, + buffer_end_p, + (const jerry_char_t *) str_buffer_p, + (jerry_size_t) str_buffer_size); ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size); @@ -1516,7 +1513,10 @@ jerry_append_number_to_buffer (uint8_t *buffer_p, /**< buffer */ JERRY_ASSERT (utf8_str_size <= ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32); - return jerry_append_chars_to_buffer (buffer_p, buffer_end_p, (const char *) uint32_to_str_buffer, utf8_str_size); + return jerry_append_chars_to_buffer (buffer_p, + buffer_end_p, + (const jerry_char_t *) uint32_to_str_buffer, + (jerry_size_t) utf8_str_size); } /* jerry_append_number_to_buffer */ #endif /* JERRY_SNAPSHOT_SAVE */ @@ -1609,26 +1609,27 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho if (is_c_format) { /* Save literal count. */ - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "jerry_length_t literal_count = ", 0); + lit_buf_p = + jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("jerry_length_t literal_count = ")); lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count); /* Save the array of literals. */ - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ";\n\njerry_char_t *literals[", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (";\n\njerry_char_t *literals[")); lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count); - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n")); for (lit_utf8_size_t i = 0; i < literal_count; i++) { - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " \"", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \"")); ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size); for (lit_utf8_size_t j = 0; j < str_buffer_size; j++) { uint8_t byte = str_buffer_p[j]; if (byte < 32 || byte > 127) { - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\\x", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x")); ecma_char_t hex_digit = (ecma_char_t) (byte >> 4); *lit_buf_p++ = (lit_utf8_byte_t) ((hex_digit > 9) ? (hex_digit + ('A' - 10)) : (hex_digit + '0')); hex_digit = (lit_utf8_byte_t) (byte & 0xf); @@ -1645,20 +1646,21 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho } ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size); - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\"", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\"")); if (i < literal_count - 1) { - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (",")); } - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n")); } - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n\njerry_length_t literal_sizes[", 0); + lit_buf_p = + jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("};\n\njerry_length_t literal_sizes[")); lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count); - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n")); } /* Save the literal sizes respectively. */ @@ -1668,22 +1670,22 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho if (is_c_format) { - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" ")); } lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, str_size); - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" ")); if (is_c_format) { /* Show the given string as a comment. */ - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "/* ", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("/* ")); lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]); - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " */", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" */")); if (i < literal_count - 1) { - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (",")); } } else @@ -1691,12 +1693,12 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]); } - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n")); } if (is_c_format) { - lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n", 0); + lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("};\n")); } JMEM_FINALIZE_LOCAL_ARRAY (literal_array); diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index 1b882488dc..0d8b34e1d4 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -142,16 +142,6 @@ void jerry_port_context_free (void); */ void jerry_port_log (const char *message_p); -/** - * Print a single character to standard output. - * - * This port function is never called from jerry-core directly, it is only used by jerry-ext components to print - * information. - * - * @param byte: the byte to print. - */ -void jerry_port_print_byte (jerry_char_t byte); - /** * Print a buffer to standard output * diff --git a/jerry-core/include/jerryscript-types.h b/jerry-core/include/jerryscript-types.h index 4f0b98f03f..f0766b91ca 100644 --- a/jerry-core/include/jerryscript-types.h +++ b/jerry-core/include/jerryscript-types.h @@ -856,6 +856,11 @@ typedef void (*jerry_arraybuffer_free_cb_t) (jerry_arraybuffer_type_t buffer_typ void *arraybuffer_user_p, void *user_p); +/** + * Helper to expand string literal to [string pointer, string size] argument pair. + */ +#define JERRY_ZSTR_ARG(str) ((const jerry_char_t *) (str)), ((jerry_size_t) (sizeof (str) - 1)) + /** * @} */ diff --git a/jerry-ext/include/jerryscript-ext/print.h b/jerry-ext/include/jerryscript-ext/print.h index d4257b6f21..2350316789 100644 --- a/jerry-ext/include/jerryscript-ext/print.h +++ b/jerry-ext/include/jerryscript-ext/print.h @@ -22,9 +22,7 @@ JERRY_C_API_BEGIN jerry_value_t jerryx_print_value (const jerry_value_t value); -void jerryx_print_byte (jerry_char_t ch); void jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size); -void jerryx_print_string (const char *str_p); void jerryx_print_backtrace (unsigned depth); void jerryx_print_unhandled_exception (jerry_value_t exception); void jerryx_print_unhandled_rejection (jerry_value_t exception); diff --git a/jerry-ext/include/jerryscript-ext/repl.h b/jerry-ext/include/jerryscript-ext/repl.h index 6d6e62aca8..bd3488345e 100644 --- a/jerry-ext/include/jerryscript-ext/repl.h +++ b/jerry-ext/include/jerryscript-ext/repl.h @@ -20,7 +20,7 @@ JERRY_C_API_BEGIN -void jerryx_repl (const char* prompt_p); +void jerryx_repl (const jerry_char_t *prompt_p, jerry_size_t prompt_size); JERRY_C_API_END diff --git a/jerry-ext/util/handlers.c b/jerry-ext/util/handlers.c index 35d5f78a8f..02bd896c25 100644 --- a/jerry-ext/util/handlers.c +++ b/jerry-ext/util/handlers.c @@ -25,9 +25,9 @@ * 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_byte. + * by using jerry_port_print_buffer. * - * The NUL character is output as "\u0000", other characters are output + * The NULL character is output as "\u0000", other characters are output * bytewise. * * Note: @@ -35,7 +35,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_byte`. + * provides `jerry_port_print_buffer`. * * @return undefined - if all arguments could be converted to strings, * error - otherwise. @@ -51,7 +51,7 @@ jerryx_handler_print (const jerry_call_info_t *call_info_p, /**< call informatio { if (index > 0) { - jerryx_print_byte (' '); + jerryx_print_buffer (JERRY_ZSTR_ARG (" ")); } jerry_value_t result = jerryx_print_value (args_p[index]); @@ -62,7 +62,7 @@ jerryx_handler_print (const jerry_call_info_t *call_info_p, /**< call informatio } } - jerryx_print_byte ('\n'); + jerryx_print_buffer (JERRY_ZSTR_ARG ("\n")); return jerry_undefined (); } /* jerryx_handler_print */ diff --git a/jerry-ext/util/print.c b/jerry-ext/util/print.c index 51524131ac..3701b58468 100644 --- a/jerry-ext/util/print.c +++ b/jerry-ext/util/print.c @@ -61,7 +61,7 @@ jerryx_buffered_print (uint32_t value, void *user_p) jerryx_print_buffer (buffer_p->data, buffer_p->index); buffer_p->index = 0; - jerryx_print_string ("\\u0000"); + jerryx_print_buffer (JERRY_ZSTR_ARG ("\\u0000")); return; } @@ -111,20 +111,6 @@ jerryx_print_value (const jerry_value_t value) return jerry_undefined (); } /* jerryx_print */ -/** - * Print a character to standard output, also sending it to the debugger, if connected. - * - * @param ch: input character - */ -void -jerryx_print_byte (jerry_char_t byte) -{ - jerry_port_print_byte (byte); -#if JERRY_DEBUGGER - jerry_debugger_send_output (&byte, 1); -#endif /* JERRY_DEBUGGER */ -} /* jerryx_print_char */ - /** * Print a buffer to standard output, also sending it to the debugger, if connected. * @@ -140,24 +126,6 @@ jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) #endif /* JERRY_DEBUGGER */ } /* jerryx_print_buffer */ -/** - * Print a zero-terminated string to standard output, also sending it to the debugger, if connected. - * - * @param buffer_p: inptut string buffer - * @param buffer_size: size of the string - */ -void -jerryx_print_string (const char *str_p) -{ - const jerry_char_t *buffer_p = (jerry_char_t *) str_p; - jerry_size_t buffer_size = (jerry_size_t) (strlen (str_p)); - - jerry_port_print_buffer (buffer_p, buffer_size); -#if JERRY_DEBUGGER - jerry_debugger_send_output (buffer_p, buffer_size); -#endif /* JERRY_DEBUGGER */ -} /* jerryx_print_string */ - /** * Print backtrace as log messages up to a specific depth. * diff --git a/jerry-ext/util/repl.c b/jerry-ext/util/repl.c index 1d5257a986..ad58ffc1f9 100644 --- a/jerry-ext/util/repl.c +++ b/jerry-ext/util/repl.c @@ -24,13 +24,13 @@ #include "jerryscript-ext/print.h" void -jerryx_repl (const char *prompt_p) +jerryx_repl (const jerry_char_t *prompt_p, jerry_size_t prompt_size) { jerry_value_t result; while (true) { - jerryx_print_string (prompt_p); + jerryx_print_buffer (prompt_p, prompt_size); fflush (stdout); jerry_size_t length; @@ -38,7 +38,7 @@ jerryx_repl (const char *prompt_p) if (line_p == NULL) { - jerryx_print_byte ('\n'); + jerryx_print_buffer (JERRY_ZSTR_ARG ("\n")); return; } @@ -80,7 +80,7 @@ jerryx_repl (const char *prompt_p) goto exception; } - jerryx_print_byte ('\n'); + jerryx_print_buffer (JERRY_ZSTR_ARG ("\n")); jerry_value_free (result); result = jerry_run_jobs (); diff --git a/jerry-main/main-desktop.c b/jerry-main/main-desktop.c index 3c321c9674..920ea9ca6f 100644 --- a/jerry-main/main-desktop.c +++ b/jerry-main/main-desktop.c @@ -222,8 +222,14 @@ main (int argc, char **argv) } else if (arguments.source_count == 0) { - const char *prompt_p = (arguments.option_flags & OPT_FLAG_NO_PROMPT) ? "" : "jerry> "; - jerryx_repl (prompt_p); + if ((arguments.option_flags & OPT_FLAG_NO_PROMPT)) + { + jerryx_repl (JERRY_ZSTR_ARG ("")); + } + else + { + jerryx_repl (JERRY_ZSTR_ARG ("jerry> ")); + } } result = jerry_run_jobs (); diff --git a/jerry-port/common/jerry-port-io.c b/jerry-port/common/jerry-port-io.c index 761c5a79e5..26ae7c4445 100644 --- a/jerry-port/common/jerry-port-io.c +++ b/jerry-port/common/jerry-port-io.c @@ -28,29 +28,11 @@ jerry_port_log (const char *message_p) /**< message */ fputs (message_p, stderr); } /* jerry_port_log */ -/** - * Default implementation of jerry_port_print_byte. Uses 'putchar' to - * print a single character to standard output. - */ void JERRY_ATTR_WEAK -jerry_port_print_byte (jerry_char_t byte) /**< the character to print */ +jerry_port_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - putchar (byte); -} /* jerry_port_print_byte */ - -/** - * Default implementation of jerry_port_print_buffer. Uses 'jerry_port_print_byte' to - * print characters of the input buffer. - */ -void JERRY_ATTR_WEAK -jerry_port_print_buffer (const jerry_char_t *buffer_p, /**< string buffer */ - jerry_size_t buffer_size) /**< string size*/ -{ - for (jerry_size_t i = 0; i < buffer_size; i++) - { - jerry_port_print_byte (buffer_p[i]); - } -} /* jerry_port_print_byte */ + fwrite (buffer_p, 1, buffer_size, stdout); +} /* jerry_port_print_buffer */ /** * Read a line from standard input as a zero-terminated string. diff --git a/targets/os/nuttx/jerry-main.c b/targets/os/nuttx/jerry-main.c index 917aa53017..d09c438355 100644 --- a/targets/os/nuttx/jerry-main.c +++ b/targets/os/nuttx/jerry-main.c @@ -206,7 +206,7 @@ jerry_main (int argc, char *argv[]) if (files_counter == 0) { - jerryx_repl ("jerry> "); + jerryx_repl (JERRY_ZSTR_ARG ("jerry> ")); } else { diff --git a/targets/os/zephyr/src/jerry-main.c b/targets/os/zephyr/src/jerry-main.c index 63849d5e22..6042fa69fe 100644 --- a/targets/os/zephyr/src/jerry-main.c +++ b/targets/os/zephyr/src/jerry-main.c @@ -49,7 +49,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerryx_register_global ("print", jerryx_handler_print); - jerryx_repl ("js> "); + jerryx_repl (JERRY_ZSTR_ARG ("js> ")); jerry_cleanup (); } /* main */