Skip to content

Commit 91f49c2

Browse files
author
François Baldassari
committed
Use port APIs instead of printf in non-debug code
JerryScript-DCO-1.0-Signed-off-by: François Baldassari [email protected]
1 parent 6e687fa commit 91f49c2

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-global.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,20 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t
9797
while (utf8_str_curr_p < utf8_str_end_p)
9898
{
9999
ecma_char_t code_point = lit_utf8_read_next (&utf8_str_curr_p);
100-
101-
if (code_point == LIT_CHAR_NULL)
102-
{
103-
printf ("\\u0000");
104-
}
105-
else if (code_point <= LIT_UTF8_1_BYTE_CODE_POINT_MAX)
106-
{
107-
printf ("%c", (char) code_point);
108-
}
109-
else
110-
{
111-
JERRY_STATIC_ASSERT (sizeof (code_point) == 2,
112-
size_of_code_point_must_be_equal_to_2_bytes);
113-
114-
uint32_t byte_high = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, code_point,
115-
JERRY_BITSINBYTE,
116-
JERRY_BITSINBYTE);
117-
uint32_t byte_low = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, code_point,
118-
0,
119-
JERRY_BITSINBYTE);
120-
121-
printf ("\\u%02x%02x", byte_high, byte_low);
122-
}
100+
lit_char_put(code_point);
123101
}
124102

125103
if (arg_index < args_number - 1)
126104
{
127-
printf (" ");
105+
jerry_port_putchar (' ');
128106
}
129107

130108
MEM_FINALIZE_LOCAL_ARRAY (utf8_str_p);
131109

132110
ECMA_FINALIZE (str_value);
133111
}
134112

135-
printf ("\n");
113+
jerry_port_putchar ('\n');
136114

137115
if (ecma_is_value_empty (ret_value))
138116
{

jerry-core/jrt/jrt-fatals.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ void __noreturn
3333
jerry_fatal (jerry_fatal_code_t code) /**< status code */
3434
{
3535
#ifndef JERRY_NDEBUG
36-
printf ("Error: ");
36+
JERRY_ERROR_MSG ("Error: ");
3737

3838
switch (code)
3939
{
4040
case ERR_OUT_OF_MEMORY:
4141
{
42-
printf ("ERR_OUT_OF_MEMORY\n");
42+
JERRY_ERROR_MSG ("ERR_OUT_OF_MEMORY\n");
4343
break;
4444
}
4545
case ERR_SYSCALL:
@@ -54,12 +54,12 @@ jerry_fatal (jerry_fatal_code_t code) /**< status code */
5454
}
5555
case ERR_UNIMPLEMENTED_CASE:
5656
{
57-
printf ("ERR_UNIMPLEMENTED_CASE\n");
57+
JERRY_ERROR_MSG ("ERR_UNIMPLEMENTED_CASE\n");
5858
break;
5959
}
6060
case ERR_FAILED_INTERNAL_ASSERTION:
6161
{
62-
printf ("ERR_FAILED_INTERNAL_ASSERTION\n");
62+
JERRY_ERROR_MSG ("ERR_FAILED_INTERNAL_ASSERTION\n");
6363
break;
6464
}
6565
}
@@ -92,7 +92,7 @@ jerry_assert_fail (const char *assertion, /**< assertion condition string */
9292
const uint32_t line) /**< line */
9393
{
9494
#if !defined (JERRY_NDEBUG) || !defined (JERRY_DISABLE_HEAVY_DEBUG)
95-
printf ("ICE: Assertion '%s' failed at %s(%s):%lu.\n",
95+
JERRY_ERROR_MSG ("ICE: Assertion '%s' failed at %s(%s):%lu.\n",
9696
assertion, file, function, (unsigned long) line);
9797
#else /* !JERRY_NDEBUG || !JERRY_DISABLE_HEAVY_DEBUG */
9898
(void) assertion;
@@ -115,7 +115,8 @@ jerry_unreachable (const char *comment, /**< comment to unreachable mark if exis
115115
const uint32_t line) /**< line */
116116
{
117117
#ifndef JERRY_NDEBUG
118-
printf ("ICE: Unreachable control path at %s(%s):%lu was executed", file, function, (unsigned long) line);
118+
JERRY_ERROR_MSG ("ICE: Unreachable control path at %s(%s):%lu was executed",
119+
file, function, (unsigned long) line);
119120
#else /* !JERRY_NDEBUG */
120121
(void) file;
121122
(void) function;
@@ -124,9 +125,9 @@ jerry_unreachable (const char *comment, /**< comment to unreachable mark if exis
124125

125126
if (comment != NULL)
126127
{
127-
printf ("(%s)", comment);
128+
JERRY_ERROR_MSG ("(%s)", comment);
128129
}
129-
printf (".\n");
130+
JERRY_ERROR_MSG (".\n");
130131

131132
jerry_fatal (ERR_FAILED_INTERNAL_ASSERTION);
132133
} /* jerry_unreachable */
@@ -142,7 +143,7 @@ jerry_unimplemented (const char *comment, /**< comment to unimplemented mark if
142143
const uint32_t line) /**< line */
143144
{
144145
#ifndef JERRY_NDEBUG
145-
printf ("SORRY: Unimplemented case at %s(%s):%lu was executed", file, function, (unsigned long) line);
146+
JERRY_ERROR_MSG ("SORRY: Unimplemented case at %s(%s):%lu was executed", file, function, (unsigned long) line);
146147
#else /* !JERRY_NDEBUG */
147148
(void) file;
148149
(void) function;
@@ -151,9 +152,9 @@ jerry_unimplemented (const char *comment, /**< comment to unimplemented mark if
151152

152153
if (comment != NULL)
153154
{
154-
printf ("(%s)", comment);
155+
JERRY_ERROR_MSG ("(%s)", comment);
155156
}
156-
printf (".\n");
157+
JERRY_ERROR_MSG (".\n");
157158

158159
jerry_fatal (ERR_UNIMPLEMENTED_CASE);
159160
} /* jerry_unimplemented */

jerry-core/lit/lit-char-helpers.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include "jrt-bit-fields.h"
1617
#include "lit-char-helpers.h"
1718
#include "lit/lit-unicode-ranges.inc.h"
1819
#include "lit-strings.h"
1920

21+
#include <stdio.h>
22+
2023
#define NUM_OF_ELEMENTS(array) (sizeof (array) / sizeof ((array)[0]))
2124

2225
/**
@@ -97,6 +100,43 @@ search_char_in_interval_array (ecma_char_t c, /**< code unit */
97100
return false;
98101
} /* search_char_in_interval_array */
99102

103+
/**
104+
* Print a specified character.
105+
*/
106+
void
107+
lit_char_put (ecma_char_t c) /**< code unit */
108+
{
109+
JERRY_STATIC_ASSERT (sizeof (c) == 2,
110+
size_of_code_point_must_be_equal_to_2_bytes);
111+
112+
if (c != LIT_CHAR_NULL && c <= LIT_UTF8_1_BYTE_CODE_POINT_MAX)
113+
{
114+
jerry_port_putchar ((char) c);
115+
}
116+
else
117+
{
118+
uint32_t byte_high = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, c,
119+
JERRY_BITSINBYTE,
120+
JERRY_BITSINBYTE);
121+
uint32_t byte_low = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, c,
122+
0,
123+
JERRY_BITSINBYTE);
124+
125+
126+
jerry_port_putchar ('\\');
127+
jerry_port_putchar ('u');
128+
// print high byte
129+
char out_buf[3];
130+
itoa(byte_high, out_buf, 16);
131+
jerry_port_putchar (out_buf[0]);
132+
jerry_port_putchar (out_buf[1]);
133+
// print low byte
134+
itoa(byte_low, out_buf, 16);
135+
jerry_port_putchar (out_buf[0]);
136+
jerry_port_putchar (out_buf[1]);
137+
}
138+
} /* lit_char_put */
139+
100140
/**
101141
* Check if specified character is one of the Format-Control characters
102142
*

jerry-core/lit/lit-char-helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,6 @@ extern bool lit_char_is_word_char (ecma_char_t);
237237
ecma_length_t lit_char_to_lower_case (ecma_char_t, ecma_char_t *, ecma_length_t);
238238
ecma_length_t lit_char_to_upper_case (ecma_char_t, ecma_char_t *, ecma_length_t);
239239

240+
void lit_char_put (ecma_char_t c);
241+
240242
#endif /* !LIT_CHAR_HELPERS_H */

0 commit comments

Comments
 (0)