Skip to content

Use port APIs instead of printf in non-debug code #956

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

Closed
Closed
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
28 changes: 3 additions & 25 deletions jerry-core/ecma/builtin-objects/ecma-builtin-global.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,20 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t
while (utf8_str_curr_p < utf8_str_end_p)
{
ecma_char_t code_point = lit_utf8_read_next (&utf8_str_curr_p);

if (code_point == LIT_CHAR_NULL)
{
printf ("\\u0000");
}
else if (code_point <= LIT_UTF8_1_BYTE_CODE_POINT_MAX)
{
printf ("%c", (char) code_point);
}
else
{
JERRY_STATIC_ASSERT (sizeof (code_point) == 2,
size_of_code_point_must_be_equal_to_2_bytes);

uint32_t byte_high = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, code_point,
JERRY_BITSINBYTE,
JERRY_BITSINBYTE);
uint32_t byte_low = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, code_point,
0,
JERRY_BITSINBYTE);

printf ("\\u%02x%02x", byte_high, byte_low);
}
lit_char_put (code_point);
}

if (arg_index < args_number - 1)
{
printf (" ");
jerry_port_putchar (' ');
}

MEM_FINALIZE_LOCAL_ARRAY (utf8_str_p);

ECMA_FINALIZE (str_value);
}

printf ("\n");
jerry_port_putchar ('\n');

if (ecma_is_value_empty (ret_value))
{
Expand Down
23 changes: 12 additions & 11 deletions jerry-core/jrt/jrt-fatals.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ void __noreturn
jerry_fatal (jerry_fatal_code_t code) /**< status code */
{
#ifndef JERRY_NDEBUG
printf ("Error: ");
JERRY_ERROR_MSG ("Error: ");

switch (code)
{
case ERR_OUT_OF_MEMORY:
{
printf ("ERR_OUT_OF_MEMORY\n");
JERRY_ERROR_MSG ("ERR_OUT_OF_MEMORY\n");
break;
}
case ERR_SYSCALL:
Expand All @@ -54,12 +54,12 @@ jerry_fatal (jerry_fatal_code_t code) /**< status code */
}
case ERR_UNIMPLEMENTED_CASE:
{
printf ("ERR_UNIMPLEMENTED_CASE\n");
JERRY_ERROR_MSG ("ERR_UNIMPLEMENTED_CASE\n");
break;
}
case ERR_FAILED_INTERNAL_ASSERTION:
{
printf ("ERR_FAILED_INTERNAL_ASSERTION\n");
JERRY_ERROR_MSG ("ERR_FAILED_INTERNAL_ASSERTION\n");
break;
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ jerry_assert_fail (const char *assertion, /**< assertion condition string */
const uint32_t line) /**< line */
{
#if !defined (JERRY_NDEBUG) || !defined (JERRY_DISABLE_HEAVY_DEBUG)
printf ("ICE: Assertion '%s' failed at %s(%s):%lu.\n",
JERRY_ERROR_MSG ("ICE: Assertion '%s' failed at %s(%s):%lu.\n",
assertion, file, function, (unsigned long) line);
#else /* !JERRY_NDEBUG || !JERRY_DISABLE_HEAVY_DEBUG */
(void) assertion;
Expand All @@ -115,7 +115,8 @@ jerry_unreachable (const char *comment, /**< comment to unreachable mark if exis
const uint32_t line) /**< line */
{
#ifndef JERRY_NDEBUG
printf ("ICE: Unreachable control path at %s(%s):%lu was executed", file, function, (unsigned long) line);
JERRY_ERROR_MSG ("ICE: Unreachable control path at %s(%s):%lu was executed",
file, function, (unsigned long) line);
#else /* !JERRY_NDEBUG */
(void) file;
(void) function;
Expand All @@ -124,9 +125,9 @@ jerry_unreachable (const char *comment, /**< comment to unreachable mark if exis

if (comment != NULL)
{
printf ("(%s)", comment);
JERRY_ERROR_MSG ("(%s)", comment);
}
printf (".\n");
JERRY_ERROR_MSG (".\n");

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

if (comment != NULL)
{
printf ("(%s)", comment);
JERRY_ERROR_MSG ("(%s)", comment);
}
printf (".\n");
JERRY_ERROR_MSG (".\n");

jerry_fatal (ERR_UNIMPLEMENTED_CASE);
} /* jerry_unimplemented */
40 changes: 40 additions & 0 deletions jerry-core/lit/lit-char-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
* limitations under the License.
*/

#include "jrt-bit-fields.h"
#include "lit-char-helpers.h"
#include "lit/lit-unicode-ranges.inc.h"
#include "lit-strings.h"

#include <stdlib.h>

#define NUM_OF_ELEMENTS(array) (sizeof (array) / sizeof ((array)[0]))

/**
Expand Down Expand Up @@ -97,6 +100,43 @@ search_char_in_interval_array (ecma_char_t c, /**< code unit */
return false;
} /* search_char_in_interval_array */

/**
* Print a specified character.
*/
void
lit_char_put (ecma_char_t c) /**< code unit */
{
JERRY_STATIC_ASSERT (sizeof (c) == 2,
size_of_code_point_must_be_equal_to_2_bytes);

if (c != LIT_CHAR_NULL && c <= LIT_UTF8_1_BYTE_CODE_POINT_MAX)
{
jerry_port_putchar ((char) c);
}
else
{
uint32_t byte_high = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, c,
JERRY_BITSINBYTE,
JERRY_BITSINBYTE);
uint32_t byte_low = (uint32_t) JRT_EXTRACT_BIT_FIELD (ecma_char_t, c,
0,
JERRY_BITSINBYTE);


jerry_port_putchar ('\\');
jerry_port_putchar ('u');
// print high byte
char out_buf[3];
itoa ((int) byte_high, out_buf, 16);
jerry_port_putchar (out_buf[0]);
jerry_port_putchar (out_buf[1]);
// print low byte
itoa ((int) byte_low, out_buf, 16);
jerry_port_putchar (out_buf[0]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this, but it's the simplest way to achieve what I am trying to achieve without adding more port APIs.

jerry_port_putchar (out_buf[1]);
}
} /* lit_char_put */

/**
* Check if specified character is one of the Format-Control characters
*
Expand Down
2 changes: 2 additions & 0 deletions jerry-core/lit/lit-char-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,6 @@ extern bool lit_char_is_word_char (ecma_char_t);
ecma_length_t lit_char_to_lower_case (ecma_char_t, ecma_char_t *, ecma_length_t);
ecma_length_t lit_char_to_upper_case (ecma_char_t, ecma_char_t *, ecma_length_t);

void lit_char_put (ecma_char_t c);

#endif /* !LIT_CHAR_HELPERS_H */
1 change: 1 addition & 0 deletions jerry-libc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void __attribute__ ((noreturn)) exit (int);
void __attribute__ ((noreturn)) abort (void);
int rand (void);
void srand (unsigned int);
char *itoa (int, char *, int);

#ifdef __cplusplus
}
Expand Down
19 changes: 19 additions & 0 deletions jerry-libc/jerry-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,22 @@ srand (unsigned int seed) /**< new seed */
libc_random_gen_state[2] =
libc_random_gen_state[3] = seed;
} /* srand */

/**
* Convert an integer to a string using base and write the result to buffer.
*/
char *
itoa (int value, char *buffer, int base)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is not quite complete. It doesn't add the - sign for negative numbers and only accepts bases <= 16; but it is good enough for this use case.

{
static char b[32] = {0};
unsigned i;

for (i = 30; value && i; --i, value /= base)
{
b[i] = "0123456789abcdef"[value % base];
}

strncpy (buffer, &b[i+1], 32 - i);

return buffer;
} /* itoa */