diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-global.c b/jerry-core/ecma/builtin-objects/ecma-builtin-global.c index 620081ae07..72478e10c7 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-global.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-global.c @@ -97,34 +97,12 @@ 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); @@ -132,7 +110,7 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t ECMA_FINALIZE (str_value); } - printf ("\n"); + jerry_port_putchar ('\n'); if (ecma_is_value_empty (ret_value)) { diff --git a/jerry-core/jrt/jrt-fatals.c b/jerry-core/jrt/jrt-fatals.c index 498df0c47d..16275fba8f 100644 --- a/jerry-core/jrt/jrt-fatals.c +++ b/jerry-core/jrt/jrt-fatals.c @@ -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: @@ -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; } } @@ -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; @@ -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; @@ -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 */ @@ -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; @@ -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 */ diff --git a/jerry-core/lit/lit-char-helpers.c b/jerry-core/lit/lit-char-helpers.c index e884bcb64f..53ea595ea4 100644 --- a/jerry-core/lit/lit-char-helpers.c +++ b/jerry-core/lit/lit-char-helpers.c @@ -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 + #define NUM_OF_ELEMENTS(array) (sizeof (array) / sizeof ((array)[0])) /** @@ -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]); + jerry_port_putchar (out_buf[1]); + } +} /* lit_char_put */ + /** * Check if specified character is one of the Format-Control characters * diff --git a/jerry-core/lit/lit-char-helpers.h b/jerry-core/lit/lit-char-helpers.h index c96616c6d7..3f146435fc 100644 --- a/jerry-core/lit/lit-char-helpers.h +++ b/jerry-core/lit/lit-char-helpers.h @@ -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 */ diff --git a/jerry-libc/include/stdlib.h b/jerry-libc/include/stdlib.h index cc73ad44eb..c2917d4c1e 100644 --- a/jerry-libc/include/stdlib.h +++ b/jerry-libc/include/stdlib.h @@ -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 } diff --git a/jerry-libc/jerry-libc.c b/jerry-libc/jerry-libc.c index e6d056c1e3..6da44907ac 100644 --- a/jerry-libc/jerry-libc.c +++ b/jerry-libc/jerry-libc.c @@ -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) +{ + 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 */