Skip to content

Commit dfa9afb

Browse files
authored
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 [email protected]
1 parent e4017f0 commit dfa9afb

File tree

15 files changed

+64
-125
lines changed

15 files changed

+64
-125
lines changed

Diff for: docs/05.PORT-API.md

-12
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,6 @@ void jerry_port_context_free (void);
116116
void jerry_port_log (const char *message_p);
117117
```
118118
119-
```c
120-
/**
121-
* Print a single character to standard output.
122-
*
123-
* This port function is never called from jerry-core directly, it is only used by jerry-ext components to print
124-
* information.
125-
*
126-
* @param byte: the byte to print.
127-
*/
128-
void jerry_port_print_byte (jerry_char_t byte);
129-
```
130-
131119
```c
132120
/**
133121
* Print a buffer to standard output

Diff for: docs/10.EXT-REFERENCE-HANDLER.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,14 @@ jerryx_handler_gc (const jerry_call_info_t *call_info_p,
316316
**Summary**
317317
318318
Provide a `print` implementation for scripts. The routine converts all of its
319-
arguments to strings and outputs them char-by-char using
320-
`jerry_port_print_byte`. The NULL character is output as "\u0000",
321-
other characters are output bytewise.
319+
arguments to strings and outputs them by using `jerry_port_print_buffer`.
320+
The NULL character is output as "\u0000", other characters are output bytewise.
322321
323322
*Note*: This implementation does not use standard C `printf` to print its
324323
output. This allows more flexibility but also extends the core JerryScript
325324
engine port API. Applications that want to use `jerryx_handler_print` must
326325
ensure that their port implementation also provides
327-
`jerry_port_print_byte`.
326+
`jerry_port_print_buffer`.
328327
329328
**Prototype**
330329
@@ -345,7 +344,7 @@ jerryx_handler_print (const jerry_call_info_t *call_info_p,
345344
**See also**
346345

347346
- [jerryx_register_global](#jerryx_register_global)
348-
- [jerry_port_print_byte](05.PORT-API.md#jerry_port_print_char)
347+
- [jerry_port_print_buffer](05.PORT-API.md#jerry_port_print_buffer)
349348

350349

351350
# Handler registration helper

Diff for: docs/16.MIGRATION-GUIDE.md

+1
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,4 @@ In this section the new API functions are listed.
770770
- [`jerry_port_get_current_context`](05.PORT-API.md#jerry_port_get_current_context)
771771
- [`jerry_port_fatal`](05.PORT-API.md#jerry_port_fatal)
772772
- [`jerry_port_sleep`](05.PORT-API.md#jerry_port_sleep)
773+
- [`jerry_port_print_byte`](05.PORT-API.md#jerry_port_print_byte)

Diff for: jerry-core/api/jerry-snapshot.c

+30-28
Original file line numberDiff line numberDiff line change
@@ -1455,22 +1455,17 @@ jerry_save_literals_sort (ecma_string_t *literals[], /**< array of literals */
14551455
static uint8_t *
14561456
jerry_append_chars_to_buffer (uint8_t *buffer_p, /**< buffer */
14571457
uint8_t *buffer_end_p, /**< the end of the buffer */
1458-
const char *chars, /**< string */
1459-
lit_utf8_size_t string_size) /**< string size */
1458+
const jerry_char_t *chars, /**< string */
1459+
jerry_size_t string_size) /**< string size */
14601460
{
14611461
if (buffer_p > buffer_end_p)
14621462
{
14631463
return buffer_p;
14641464
}
14651465

1466-
if (string_size == 0)
1467-
{
1468-
string_size = (lit_utf8_size_t) strlen (chars);
1469-
}
1470-
14711466
if (buffer_p + string_size <= buffer_end_p)
14721467
{
1473-
memcpy ((char *) buffer_p, chars, string_size);
1468+
memcpy ((char *) buffer_p, (const char *) chars, string_size);
14741469

14751470
return buffer_p + string_size;
14761471
}
@@ -1492,8 +1487,10 @@ jerry_append_ecma_string_to_buffer (uint8_t *buffer_p, /**< buffer */
14921487
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer_p, str_buffer_size);
14931488

14941489
/* Append the string to the buffer. */
1495-
uint8_t *new_buffer_p =
1496-
jerry_append_chars_to_buffer (buffer_p, buffer_end_p, (const char *) str_buffer_p, str_buffer_size);
1490+
uint8_t *new_buffer_p = jerry_append_chars_to_buffer (buffer_p,
1491+
buffer_end_p,
1492+
(const jerry_char_t *) str_buffer_p,
1493+
(jerry_size_t) str_buffer_size);
14971494

14981495
ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
14991496

@@ -1516,7 +1513,10 @@ jerry_append_number_to_buffer (uint8_t *buffer_p, /**< buffer */
15161513

15171514
JERRY_ASSERT (utf8_str_size <= ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32);
15181515

1519-
return jerry_append_chars_to_buffer (buffer_p, buffer_end_p, (const char *) uint32_to_str_buffer, utf8_str_size);
1516+
return jerry_append_chars_to_buffer (buffer_p,
1517+
buffer_end_p,
1518+
(const jerry_char_t *) uint32_to_str_buffer,
1519+
(jerry_size_t) utf8_str_size);
15201520
} /* jerry_append_number_to_buffer */
15211521

15221522
#endif /* JERRY_SNAPSHOT_SAVE */
@@ -1609,26 +1609,27 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16091609
if (is_c_format)
16101610
{
16111611
/* Save literal count. */
1612-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "jerry_length_t literal_count = ", 0);
1612+
lit_buf_p =
1613+
jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("jerry_length_t literal_count = "));
16131614

16141615
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
16151616

16161617
/* Save the array of literals. */
1617-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ";\n\njerry_char_t *literals[", 0);
1618+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (";\n\njerry_char_t *literals["));
16181619

16191620
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1620-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
1621+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n"));
16211622

16221623
for (lit_utf8_size_t i = 0; i < literal_count; i++)
16231624
{
1624-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " \"", 0);
1625+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \""));
16251626
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
16261627
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
16271628
{
16281629
uint8_t byte = str_buffer_p[j];
16291630
if (byte < 32 || byte > 127)
16301631
{
1631-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\\x", 0);
1632+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x"));
16321633
ecma_char_t hex_digit = (ecma_char_t) (byte >> 4);
16331634
*lit_buf_p++ = (lit_utf8_byte_t) ((hex_digit > 9) ? (hex_digit + ('A' - 10)) : (hex_digit + '0'));
16341635
hex_digit = (lit_utf8_byte_t) (byte & 0xf);
@@ -1645,20 +1646,21 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16451646
}
16461647

16471648
ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
1648-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\"", 0);
1649+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\""));
16491650

16501651
if (i < literal_count - 1)
16511652
{
1652-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1653+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (","));
16531654
}
16541655

1655-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1656+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n"));
16561657
}
16571658

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

16601662
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1661-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
1663+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n"));
16621664
}
16631665

16641666
/* Save the literal sizes respectively. */
@@ -1668,35 +1670,35 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16681670

16691671
if (is_c_format)
16701672
{
1671-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1673+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" "));
16721674
}
16731675

16741676
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, str_size);
1675-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1677+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" "));
16761678

16771679
if (is_c_format)
16781680
{
16791681
/* Show the given string as a comment. */
1680-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "/* ", 0);
1682+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("/* "));
16811683
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
1682-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " */", 0);
1684+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" */"));
16831685

16841686
if (i < literal_count - 1)
16851687
{
1686-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1688+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (","));
16871689
}
16881690
}
16891691
else
16901692
{
16911693
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
16921694
}
16931695

1694-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1696+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n"));
16951697
}
16961698

16971699
if (is_c_format)
16981700
{
1699-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n", 0);
1701+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("};\n"));
17001702
}
17011703

17021704
JMEM_FINALIZE_LOCAL_ARRAY (literal_array);

Diff for: jerry-core/include/jerryscript-port.h

-10
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,6 @@ void jerry_port_context_free (void);
142142
*/
143143
void jerry_port_log (const char *message_p);
144144

145-
/**
146-
* Print a single character to standard output.
147-
*
148-
* This port function is never called from jerry-core directly, it is only used by jerry-ext components to print
149-
* information.
150-
*
151-
* @param byte: the byte to print.
152-
*/
153-
void jerry_port_print_byte (jerry_char_t byte);
154-
155145
/**
156146
* Print a buffer to standard output
157147
*

Diff for: jerry-core/include/jerryscript-types.h

+5
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,11 @@ typedef void (*jerry_arraybuffer_free_cb_t) (jerry_arraybuffer_type_t buffer_typ
856856
void *arraybuffer_user_p,
857857
void *user_p);
858858

859+
/**
860+
* Helper to expand string literal to [string pointer, string size] argument pair.
861+
*/
862+
#define JERRY_ZSTR_ARG(str) ((const jerry_char_t *) (str)), ((jerry_size_t) (sizeof (str) - 1))
863+
859864
/**
860865
* @}
861866
*/

Diff for: jerry-ext/include/jerryscript-ext/print.h

-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
JERRY_C_API_BEGIN
2323

2424
jerry_value_t jerryx_print_value (const jerry_value_t value);
25-
void jerryx_print_byte (jerry_char_t ch);
2625
void jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
27-
void jerryx_print_string (const char *str_p);
2826
void jerryx_print_backtrace (unsigned depth);
2927
void jerryx_print_unhandled_exception (jerry_value_t exception);
3028
void jerryx_print_unhandled_rejection (jerry_value_t exception);

Diff for: jerry-ext/include/jerryscript-ext/repl.h

+1-1
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 jerry_char_t *prompt_p, jerry_size_t prompt_size);
2424

2525
JERRY_C_API_END
2626

Diff for: jerry-ext/util/handlers.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
* Provide a 'print' implementation for scripts.
2626
*
2727
* The routine converts all of its arguments to strings and outputs them
28-
* char-by-char using jerry_port_print_byte.
28+
* by using jerry_port_print_buffer.
2929
*
30-
* The NUL character is output as "\u0000", other characters are output
30+
* The NULL character is output as "\u0000", other characters are output
3131
* bytewise.
3232
*
3333
* Note:
3434
* This implementation does not use standard C `printf` to print its
3535
* output. This allows more flexibility but also extends the core
3636
* JerryScript engine port API. Applications that want to use
3737
* `jerryx_handler_print` must ensure that their port implementation also
38-
* provides `jerry_port_print_byte`.
38+
* provides `jerry_port_print_buffer`.
3939
*
4040
* @return undefined - if all arguments could be converted to strings,
4141
* error - otherwise.
@@ -51,7 +51,7 @@ jerryx_handler_print (const jerry_call_info_t *call_info_p, /**< call informatio
5151
{
5252
if (index > 0)
5353
{
54-
jerryx_print_byte (' ');
54+
jerryx_print_buffer (JERRY_ZSTR_ARG (" "));
5555
}
5656

5757
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
6262
}
6363
}
6464

65-
jerryx_print_byte ('\n');
65+
jerryx_print_buffer (JERRY_ZSTR_ARG ("\n"));
6666
return jerry_undefined ();
6767
} /* jerryx_handler_print */
6868

Diff for: jerry-ext/util/print.c

+1-33
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_buffer (JERRY_ZSTR_ARG ("\\u0000"));
6565
return;
6666
}
6767

@@ -111,20 +111,6 @@ jerryx_print_value (const jerry_value_t value)
111111
return jerry_undefined ();
112112
} /* jerryx_print */
113113

114-
/**
115-
* Print a character to standard output, also sending it to the debugger, if connected.
116-
*
117-
* @param ch: input character
118-
*/
119-
void
120-
jerryx_print_byte (jerry_char_t byte)
121-
{
122-
jerry_port_print_byte (byte);
123-
#if JERRY_DEBUGGER
124-
jerry_debugger_send_output (&byte, 1);
125-
#endif /* JERRY_DEBUGGER */
126-
} /* jerryx_print_char */
127-
128114
/**
129115
* Print a buffer to standard output, also sending it to the debugger, if connected.
130116
*
@@ -140,24 +126,6 @@ jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
140126
#endif /* JERRY_DEBUGGER */
141127
} /* jerryx_print_buffer */
142128

143-
/**
144-
* Print a zero-terminated string to standard output, also sending it to the debugger, if connected.
145-
*
146-
* @param buffer_p: inptut string buffer
147-
* @param buffer_size: size of the string
148-
*/
149-
void
150-
jerryx_print_string (const char *str_p)
151-
{
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 */
159-
} /* jerryx_print_string */
160-
161129
/**
162130
* Print backtrace as log messages up to a specific depth.
163131
*

Diff for: jerry-ext/util/repl.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
#include "jerryscript-ext/print.h"
2525

2626
void
27-
jerryx_repl (const char *prompt_p)
27+
jerryx_repl (const jerry_char_t *prompt_p, jerry_size_t prompt_size)
2828
{
2929
jerry_value_t result;
3030

3131
while (true)
3232
{
33-
jerryx_print_string (prompt_p);
33+
jerryx_print_buffer (prompt_p, prompt_size);
3434
fflush (stdout);
3535

3636
jerry_size_t length;
3737
jerry_char_t *line_p = jerry_port_line_read (&length);
3838

3939
if (line_p == NULL)
4040
{
41-
jerryx_print_byte ('\n');
41+
jerryx_print_buffer (JERRY_ZSTR_ARG ("\n"));
4242
return;
4343
}
4444

@@ -80,7 +80,7 @@ jerryx_repl (const char *prompt_p)
8080
goto exception;
8181
}
8282

83-
jerryx_print_byte ('\n');
83+
jerryx_print_buffer (JERRY_ZSTR_ARG ("\n"));
8484

8585
jerry_value_free (result);
8686
result = jerry_run_jobs ();

Diff for: jerry-main/main-desktop.c

+8-2
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 ();

0 commit comments

Comments
 (0)