Skip to content

Commit 22a01f2

Browse files
committed
Introduce JERRY_ZSTR_ARG macro to avoid call strlen on string literal.
JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent d00f481 commit 22a01f2

File tree

9 files changed

+40
-43
lines changed

9 files changed

+40
-43
lines changed

jerry-core/api/jerry-snapshot.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,18 +1460,13 @@ static uint8_t *
14601460
jerry_append_chars_to_buffer (uint8_t *buffer_p, /**< buffer */
14611461
uint8_t *buffer_end_p, /**< the end of the buffer */
14621462
const char *chars, /**< string */
1463-
lit_utf8_size_t string_size) /**< string size */
1463+
size_t string_size) /**< string size */
14641464
{
14651465
if (buffer_p > buffer_end_p)
14661466
{
14671467
return buffer_p;
14681468
}
14691469

1470-
if (string_size == 0)
1471-
{
1472-
string_size = (lit_utf8_size_t) strlen (chars);
1473-
}
1474-
14751470
if (buffer_p + string_size <= buffer_end_p)
14761471
{
14771472
memcpy ((char *) buffer_p, chars, string_size);
@@ -1613,26 +1608,26 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16131608
if (is_c_format)
16141609
{
16151610
/* Save literal count. */
1616-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "jerry_length_t literal_count = ", 0);
1611+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("jerry_length_t literal_count = "));
16171612

16181613
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
16191614

16201615
/* Save the array of literals. */
1621-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ";\n\njerry_char_t *literals[", 0);
1616+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(";\n\njerry_char_t *literals["));
16221617

16231618
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1624-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
1619+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("] =\n{\n"));
16251620

16261621
for (lit_utf8_size_t i = 0; i < literal_count; i++)
16271622
{
1628-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " \"", 0);
1623+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(" \""));
16291624
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
16301625
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
16311626
{
16321627
uint8_t byte = str_buffer_p[j];
16331628
if (byte < 32 || byte > 127)
16341629
{
1635-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\\x", 0);
1630+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("\\x"));
16361631
ecma_char_t hex_digit = (ecma_char_t) (byte >> 4);
16371632
*lit_buf_p++ = (lit_utf8_byte_t) ((hex_digit > 9) ? (hex_digit + ('A' - 10)) : (hex_digit + '0'));
16381633
hex_digit = (lit_utf8_byte_t) (byte & 0xf);
@@ -1649,20 +1644,20 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16491644
}
16501645

16511646
ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
1652-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\"", 0);
1647+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("\""));
16531648

16541649
if (i < literal_count - 1)
16551650
{
1656-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1651+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(","));
16571652
}
16581653

1659-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1654+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("\n"));
16601655
}
16611656

1662-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n\njerry_length_t literal_sizes[", 0);
1657+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("};\n\njerry_length_t literal_sizes["));
16631658

16641659
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1665-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
1660+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("] =\n{\n"));
16661661
}
16671662

16681663
/* Save the literal sizes respectively. */
@@ -1672,35 +1667,35 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16721667

16731668
if (is_c_format)
16741669
{
1675-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1670+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(" "));
16761671
}
16771672

16781673
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, str_size);
1679-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1674+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(" "));
16801675

16811676
if (is_c_format)
16821677
{
16831678
/* Show the given string as a comment. */
1684-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "/* ", 0);
1679+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("/* "));
16851680
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
1686-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " */", 0);
1681+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(" */"));
16871682

16881683
if (i < literal_count - 1)
16891684
{
1690-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1685+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG(","));
16911686
}
16921687
}
16931688
else
16941689
{
16951690
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
16961691
}
16971692

1698-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1693+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("\n"));
16991694
}
17001695

17011696
if (is_c_format)
17021697
{
1703-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n", 0);
1698+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG("};\n"));
17041699
}
17051700

17061701
JMEM_FINALIZE_LOCAL_ARRAY (literal_array);

jerry-core/include/jerryscript-compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ void *__cdecl _alloca (size_t _Size);
185185
#define JERRY_VLA(type, name, size) type name[size]
186186
#endif /* !JERRY_VLA */
187187

188+
#define JERRY_ZSTR_ARG(str) (str), (sizeof(str) - 1)
189+
188190
/**
189191
* @}
190192
*/

jerry-ext/include/jerryscript-ext/print.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ JERRY_C_API_BEGIN
2424
jerry_value_t jerryx_print_value (const jerry_value_t value);
2525
void jerryx_print_byte (jerry_char_t ch);
2626
void jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
27-
void jerryx_print_string (const char *str_p);
27+
void jerryx_print_string (const char *str_p, size_t str_size);
2828
void jerryx_print_backtrace (unsigned depth);
2929
void jerryx_print_unhandled_exception (jerry_value_t exception);
3030
void jerryx_print_unhandled_rejection (jerry_value_t exception);

jerry-ext/include/jerryscript-ext/repl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
JERRY_C_API_BEGIN
2222

23-
void jerryx_repl (const char* prompt_p);
23+
void jerryx_repl (const char* prompt_p, size_t prompt_size);
2424

2525
JERRY_C_API_END
2626

jerry-ext/util/print.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jerryx_buffered_print (uint32_t value, void *user_p)
6161
jerryx_print_buffer (buffer_p->data, buffer_p->index);
6262
buffer_p->index = 0;
6363

64-
jerryx_print_string ("\\u0000");
64+
jerryx_print_string (JERRY_ZSTR_ARG ("\\u0000"));
6565
return;
6666
}
6767

@@ -141,21 +141,15 @@ jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
141141
} /* jerryx_print_buffer */
142142

143143
/**
144-
* Print a zero-terminated string to standard output, also sending it to the debugger, if connected.
144+
* Print a string with length to standard output, also sending it to the debugger, if connected.
145145
*
146-
* @param buffer_p: inptut string buffer
147-
* @param buffer_size: size of the string
146+
* @param str_p: inptut string
147+
* @param str_size: size of the string
148148
*/
149149
void
150-
jerryx_print_string (const char *str_p)
150+
jerryx_print_string (const char *str_p, size_t str_size)
151151
{
152-
const jerry_char_t *buffer_p = (jerry_char_t *) str_p;
153-
jerry_size_t buffer_size = (jerry_size_t) (strlen (str_p));
154-
155-
jerry_port_print_buffer (buffer_p, buffer_size);
156-
#if JERRY_DEBUGGER
157-
jerry_debugger_send_output (buffer_p, buffer_size);
158-
#endif /* JERRY_DEBUGGER */
152+
jerryx_print_buffer ((const jerry_char_t *) str_p, (jerry_size_t) str_size);
159153
} /* jerryx_print_string */
160154

161155
/**

jerry-ext/util/repl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
#include "jerryscript-ext/print.h"
2424

2525
void
26-
jerryx_repl (const char *prompt_p)
26+
jerryx_repl (const char *prompt_p, size_t prompt_size)
2727
{
2828
jerry_value_t result;
2929

3030
while (true)
3131
{
32-
jerryx_print_string (prompt_p);
32+
jerryx_print_string (prompt_p, prompt_size);
3333

3434
jerry_size_t length;
3535
jerry_char_t *line_p = jerry_port_line_read (&length);

jerry-main/main-desktop.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,14 @@ main (int argc, char **argv)
222222
}
223223
else if (arguments.source_count == 0)
224224
{
225-
const char *prompt_p = (arguments.option_flags & OPT_FLAG_NO_PROMPT) ? "" : "jerry> ";
226-
jerryx_repl (prompt_p);
225+
if ((arguments.option_flags & OPT_FLAG_NO_PROMPT))
226+
{
227+
jerryx_repl (JERRY_ZSTR_ARG (""));
228+
}
229+
else
230+
{
231+
jerryx_repl (JERRY_ZSTR_ARG ("jerry> "));
232+
}
227233
}
228234

229235
result = jerry_run_jobs ();

targets/os/nuttx/jerry-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ jerry_main (int argc, char *argv[])
206206

207207
if (files_counter == 0)
208208
{
209-
jerryx_repl ("jerry> ");
209+
jerryx_repl (JERRY_ZSTR_ARG("jerry> "));
210210
}
211211
else
212212
{

targets/os/zephyr/src/jerry-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ main (void)
4949
jerry_init (JERRY_INIT_EMPTY);
5050
jerryx_register_global ("print", jerryx_handler_print);
5151

52-
jerryx_repl ("js> ");
52+
jerryx_repl (JERRY_ZSTR_ARG("js> "));
5353

5454
jerry_cleanup ();
5555
} /* main */

0 commit comments

Comments
 (0)