Skip to content

Commit 9b35e52

Browse files
committed
Introduce JERRY_ZSTR_ARG macro to avoid call strlen on string literal.
JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
1 parent d00f481 commit 9b35e52

File tree

9 files changed

+45
-43
lines changed

9 files changed

+45
-43
lines changed

jerry-core/api/jerry-snapshot.c

Lines changed: 20 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,27 @@ 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 =
1612+
jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("jerry_length_t literal_count = "));
16171613

16181614
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
16191615

16201616
/* 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);
1617+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (";\n\njerry_char_t *literals["));
16221618

16231619
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);
1620+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n"));
16251621

16261622
for (lit_utf8_size_t i = 0; i < literal_count; i++)
16271623
{
1628-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " \"", 0);
1624+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \""));
16291625
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
16301626
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
16311627
{
16321628
uint8_t byte = str_buffer_p[j];
16331629
if (byte < 32 || byte > 127)
16341630
{
1635-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\\x", 0);
1631+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x"));
16361632
ecma_char_t hex_digit = (ecma_char_t) (byte >> 4);
16371633
*lit_buf_p++ = (lit_utf8_byte_t) ((hex_digit > 9) ? (hex_digit + ('A' - 10)) : (hex_digit + '0'));
16381634
hex_digit = (lit_utf8_byte_t) (byte & 0xf);
@@ -1649,20 +1645,21 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16491645
}
16501646

16511647
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);
1648+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\""));
16531649

16541650
if (i < literal_count - 1)
16551651
{
1656-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1652+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (","));
16571653
}
16581654

1659-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1655+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n"));
16601656
}
16611657

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

16641661
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);
1662+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n"));
16661663
}
16671664

16681665
/* Save the literal sizes respectively. */
@@ -1672,35 +1669,35 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16721669

16731670
if (is_c_format)
16741671
{
1675-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1672+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" "));
16761673
}
16771674

16781675
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);
1676+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" "));
16801677

16811678
if (is_c_format)
16821679
{
16831680
/* Show the given string as a comment. */
1684-
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 ("/* "));
16851682
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);
1683+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" */"));
16871684

16881685
if (i < literal_count - 1)
16891686
{
1690-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1687+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (","));
16911688
}
16921689
}
16931690
else
16941691
{
16951692
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
16961693
}
16971694

1698-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1695+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n"));
16991696
}
17001697

17011698
if (is_c_format)
17021699
{
1703-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n", 0);
1700+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("};\n"));
17041701
}
17051702

17061703
JMEM_FINALIZE_LOCAL_ARRAY (literal_array);

jerry-core/include/jerryscript-compiler.h

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

188+
/**
189+
* Helper to expand string literal to (string pointer, string size) arg pair.
190+
*/
191+
#define JERRY_ZSTR_ARG(str) (str), (sizeof (str) - 1)
192+
188193
/**
189194
* @}
190195
*/

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)