Introduce JERRY_ZSTR_ARG macro to avoid jerryx_print_string, jerryx_print_byte, jerry_port_print_byte, strlen (#4982)

Replace usage of jerryx_print_byte, jerryx_print_string with jerryx_print_buffer.

As we now have JERRY_ZSTR_ARG, so we can take advantage of it

With this, the jerry_port_print_byte port api won't need any more
this reduced the port api surface

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
This commit is contained in:
Yonggang Luo
2024-11-26 00:28:55 +08:00
committed by GitHub
parent e4017f0b3a
commit dfa9afbf6e
15 changed files with 64 additions and 125 deletions
-12
View File
@@ -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
+4 -5
View File
@@ -316,15 +316,14 @@ jerryx_handler_gc (const jerry_call_info_t *call_info_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**
@@ -345,7 +344,7 @@ jerryx_handler_print (const jerry_call_info_t *call_info_p,
**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
+1
View File
@@ -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)
+30 -28
View File
@@ -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);
-10
View File
@@ -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
*
+5
View File
@@ -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))
/**
* @}
*/
@@ -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);
+1 -1
View File
@@ -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
+5 -5
View File
@@ -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 */
+1 -33
View File
@@ -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.
*
+4 -4
View File
@@ -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 ();
+8 -2
View File
@@ -222,8 +222,14 @@ restart:
}
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 ();
+3 -21
View File
@@ -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.
+1 -1
View File
@@ -206,7 +206,7 @@ jerry_main (int argc, char *argv[])
if (files_counter == 0)
{
jerryx_repl ("jerry> ");
jerryx_repl (JERRY_ZSTR_ARG ("jerry> "));
}
else
{
+1 -1
View File
@@ -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 */