Skip to content

Commit

Permalink
Introduce JERRY_ZSTR_ARG macro to avoid call strlen on string literal.
Browse files Browse the repository at this point in the history
JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
  • Loading branch information
lygstate committed Feb 10, 2022
1 parent d00f481 commit 22a01f2
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 43 deletions.
41 changes: 18 additions & 23 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -1460,18 +1460,13 @@ 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 */
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);
Expand Down Expand Up @@ -1613,26 +1608,26 @@ 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);
Expand All @@ -1649,20 +1644,20 @@ 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. */
Expand All @@ -1672,35 +1667,35 @@ 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
{
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);
Expand Down
2 changes: 2 additions & 0 deletions jerry-core/include/jerryscript-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ void *__cdecl _alloca (size_t _Size);
#define JERRY_VLA(type, name, size) type name[size]
#endif /* !JERRY_VLA */

#define JERRY_ZSTR_ARG(str) (str), (sizeof(str) - 1)

/**
* @}
*/
Expand Down
2 changes: 1 addition & 1 deletion jerry-ext/include/jerryscript-ext/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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_string (const char *str_p, size_t str_size);
void jerryx_print_backtrace (unsigned depth);
void jerryx_print_unhandled_exception (jerry_value_t exception);
void jerryx_print_unhandled_rejection (jerry_value_t exception);
Expand Down
2 changes: 1 addition & 1 deletion jerry-ext/include/jerryscript-ext/repl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

JERRY_C_API_BEGIN

void jerryx_repl (const char* prompt_p);
void jerryx_repl (const char* prompt_p, size_t prompt_size);

JERRY_C_API_END

Expand Down
18 changes: 6 additions & 12 deletions jerry-ext/util/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_string (JERRY_ZSTR_ARG ("\\u0000"));
return;
}

Expand Down Expand Up @@ -141,21 +141,15 @@ jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
} /* jerryx_print_buffer */

/**
* Print a zero-terminated string to standard output, also sending it to the debugger, if connected.
* Print a string with length to standard output, also sending it to the debugger, if connected.
*
* @param buffer_p: inptut string buffer
* @param buffer_size: size of the string
* @param str_p: inptut string
* @param str_size: size of the string
*/
void
jerryx_print_string (const char *str_p)
jerryx_print_string (const char *str_p, size_t str_size)
{
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_buffer ((const jerry_char_t *) str_p, (jerry_size_t) str_size);
} /* jerryx_print_string */

/**
Expand Down
4 changes: 2 additions & 2 deletions jerry-ext/util/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
#include "jerryscript-ext/print.h"

void
jerryx_repl (const char *prompt_p)
jerryx_repl (const char *prompt_p, size_t prompt_size)
{
jerry_value_t result;

while (true)
{
jerryx_print_string (prompt_p);
jerryx_print_string (prompt_p, prompt_size);

jerry_size_t length;
jerry_char_t *line_p = jerry_port_line_read (&length);
Expand Down
10 changes: 8 additions & 2 deletions jerry-main/main-desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
2 changes: 1 addition & 1 deletion targets/os/nuttx/jerry-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ jerry_main (int argc, char *argv[])

if (files_counter == 0)
{
jerryx_repl ("jerry> ");
jerryx_repl (JERRY_ZSTR_ARG("jerry> "));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion targets/os/zephyr/src/jerry-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 22a01f2

Please sign in to comment.