Skip to content

Introduce JERRY_ZSTR_ARG macro to avoid call strlen on string literal. #4982

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
Nov 25, 2024
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
12 changes: 0 additions & 12 deletions docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions docs/10.EXT-REFERENCE-HANDLER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/16.MIGRATION-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
58 changes: 30 additions & 28 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);

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

/**
* @}
*/
Expand Down
2 changes: 0 additions & 2 deletions jerry-ext/include/jerryscript-ext/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 jerry_char_t *prompt_p, jerry_size_t prompt_size);

JERRY_C_API_END

Expand Down
10 changes: 5 additions & 5 deletions jerry-ext/util/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
* 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:
* 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`.
* provides `jerry_port_print_buffer`.
*
* @return undefined - if all arguments could be converted to strings,
* error - otherwise.
Expand All @@ -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]);
Expand All @@ -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 */

Expand Down
34 changes: 1 addition & 33 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_buffer (JERRY_ZSTR_ARG ("\\u0000"));
return;
}

Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
8 changes: 4 additions & 4 deletions jerry-ext/util/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
#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;
jerry_char_t *line_p = jerry_port_line_read (&length);

if (line_p == NULL)
{
jerryx_print_byte ('\n');
jerryx_print_buffer (JERRY_ZSTR_ARG ("\n"));
return;
}

Expand Down Expand Up @@ -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 ();
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
Loading
Loading