Skip to content

Commit 78ec0e3

Browse files
committed
Rename jerry_port_log to jerry_port_log_buffer
Add buffer_size parameter for function jerry_port_log_buffer, so that jerry_port_log_buffer won't need calculate strlen when printing Optimize util_print_chars use %.*s so that the number of strlen call is reduced. JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent 1eb01da commit 78ec0e3

File tree

17 files changed

+72
-97
lines changed

17 files changed

+72
-97
lines changed

docs/01.CONFIGURATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Enabling this feature provides detailed error messages where available, like lin
155155

156156
### Logging
157157

158-
This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log` port API function to print relevant log messages.
158+
This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log_buffer` port API function to print relevant log messages.
159159
This feature is disabled by default.
160160

161161
| Options | |

docs/05.PORT-API.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ void jerry_port_context_free (void);
130130
* The implementation can decide whether error and debug messages are logged to
131131
* the console, or saved to a database or to a file.
132132
*
133-
* @param message_p: the message to log.
133+
* @param buffer_p: input buffer
134+
* @param buffer_size: data size
134135
*/
135-
void jerry_port_log (const char *message_p);
136+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
136137
```
137138
138139
```c

jerry-core/api/jerryscript.c

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5115,49 +5115,33 @@ jerry_log_set_level (jerry_log_level_t level)
51155115
* Log a zero-terminated string message.
51165116
*
51175117
* @param str_p: message
5118+
* @param str_size: size of message
51185119
*/
51195120
static void
5120-
jerry_log_string (const char *str_p)
5121+
jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size)
51215122
{
5122-
jerry_port_log (str_p);
5123+
jerry_port_log_buffer (str_p, str_size);
51235124

51245125
#if JERRY_DEBUGGER
51255126
if (jerry_debugger_is_connected ())
51265127
{
5127-
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT,
5128-
JERRY_DEBUGGER_OUTPUT_LOG,
5129-
(const uint8_t *) str_p,
5130-
strlen (str_p));
5128+
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, JERRY_DEBUGGER_OUTPUT_LOG, str_p, str_size);
51315129
}
51325130
#endif /* JERRY_DEBUGGER */
51335131
} /* jerry_log_string */
51345132

51355133
/**
5136-
* Log a fixed-size string message.
5134+
* Log a zero-terminated number string message.
51375135
*
5138-
* @param str_p: message
5139-
* @param size: size
5140-
* @param buffer_p: buffer to use
5136+
* @param cursor_p: the number string cursor
5137+
* @param buffer_p: buffer used to construct the number string
51415138
*/
51425139
static void
5143-
jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
5140+
jerry_log_cursor (const jerry_char_t *cursor_p, jerry_char_t *buffer_p)
51445141
{
5145-
const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1;
5146-
5147-
while (size > batch_size)
5148-
{
5149-
memcpy (buffer_p, str_p, batch_size);
5150-
buffer_p[batch_size] = '\0';
5151-
jerry_log_string (buffer_p);
5152-
5153-
str_p += batch_size;
5154-
size -= batch_size;
5155-
}
5156-
5157-
memcpy (buffer_p, str_p, size);
5158-
buffer_p[size] = '\0';
5159-
jerry_log_string (buffer_p);
5160-
} /* jerry_log_string_fixed */
5142+
jerry_char_t *tail_p = buffer_p + JERRY_LOG_BUFFER_SIZE - 1;
5143+
jerry_log_string (cursor_p, (jerry_size_t) (tail_p - cursor_p));
5144+
} /* jerry_log_cursor */
51615145

51625146
/**
51635147
* Format an unsigned number.
@@ -5170,11 +5154,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
51705154
*
51715155
* @return formatted number as string
51725156
*/
5173-
static char *
5174-
jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p)
5157+
static jerry_char_t *
5158+
jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p)
51755159
{
5176-
static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
5177-
char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
5160+
static const jerry_char_t digits[] = {
5161+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
5162+
};
5163+
jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
51785164
*(--cursor_p) = '\0';
51795165
uint8_t count = 0;
51805166

@@ -5204,8 +5190,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra
52045190
*
52055191
* @return formatted number as string
52065192
*/
5207-
static char *
5208-
jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
5193+
static jerry_char_t *
5194+
jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p)
52095195
{
52105196
if (num >= 0)
52115197
{
@@ -5216,15 +5202,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
52165202

52175203
if (padding == '0' && width > 0)
52185204
{
5219-
char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
5205+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
52205206
*(--cursor_p) = '-';
52215207
return cursor_p;
52225208
}
52235209

5224-
char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
5210+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
52255211
*(--cursor_p) = '-';
52265212

5227-
char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
5213+
jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
52285214

52295215
while (cursor_p > indent_p)
52305216
{
@@ -5258,17 +5244,17 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52585244
}
52595245

52605246
va_list vl;
5261-
char buffer_p[JERRY_LOG_BUFFER_SIZE];
5262-
uint32_t buffer_index = 0;
5263-
const char *cursor_p = format_p;
5247+
jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE];
5248+
jerry_size_t buffer_index = 0;
5249+
const jerry_char_t *cursor_p = (const jerry_char_t *) format_p;
52645250
va_start (vl, format_p);
52655251

52665252
while (*cursor_p != '\0')
52675253
{
52685254
if (*cursor_p == '%' || buffer_index > JERRY_LOG_BUFFER_SIZE - 2)
52695255
{
52705256
buffer_p[buffer_index] = '\0';
5271-
jerry_log_string (buffer_p);
5257+
jerry_log_string (buffer_p, buffer_index);
52725258
buffer_index = 0;
52735259
}
52745260

@@ -5280,8 +5266,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52805266

52815267
++cursor_p;
52825268
uint8_t width = 0;
5283-
size_t precision = 0;
5284-
char padding = ' ';
5269+
jerry_size_t precision = 0;
5270+
jerry_char_t padding = ' ';
52855271

52865272
if (*cursor_p == '0')
52875273
{
@@ -5301,7 +5287,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53015287
}
53025288
else if (*cursor_p == '.' && *(cursor_p + 1) == '*')
53035289
{
5304-
precision = (size_t) va_arg (vl, int);
5290+
precision = (jerry_size_t) va_arg (vl, int);
53055291
cursor_p += 2;
53065292
}
53075293

@@ -5316,36 +5302,36 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53165302
{
53175303
case 's':
53185304
{
5319-
char *str_p = va_arg (vl, char *);
5305+
jerry_char_t *str_p = va_arg (vl, jerry_char_t *);
53205306

53215307
if (precision == 0)
53225308
{
5323-
jerry_log_string (str_p);
5309+
jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p));
53245310
break;
53255311
}
53265312

5327-
jerry_log_string_fixed (str_p, precision, buffer_p);
5313+
jerry_log_string (str_p, precision);
53285314
break;
53295315
}
53305316
case 'c':
53315317
{
53325318
/* Arguments of types narrower than int are promoted to int for variadic functions */
5333-
buffer_p[buffer_index++] = (char) va_arg (vl, int);
5319+
buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int);
53345320
break;
53355321
}
53365322
case 'd':
53375323
{
5338-
jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p));
5324+
jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p);
53395325
break;
53405326
}
53415327
case 'u':
53425328
{
5343-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p));
5329+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p);
53445330
break;
53455331
}
53465332
case 'x':
53475333
{
5348-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p));
5334+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p);
53495335
break;
53505336
}
53515337
default:
@@ -5359,7 +5345,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53595345
if (buffer_index > 0)
53605346
{
53615347
buffer_p[buffer_index] = '\0';
5362-
jerry_log_string (buffer_p);
5348+
jerry_log_string (buffer_p, buffer_index);
53635349
}
53645350

53655351
va_end (vl);

jerry-core/include/jerryscript-compiler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ void *__cdecl _alloca (size_t _Size);
196196
#define JERRY_VLA(type, name, size) type name[size]
197197
#endif /* !JERRY_VLA */
198198

199+
/**
200+
* Helper to make sure unused parameters, variables, or expressions trigger no compiler warning.
201+
*/
202+
#ifndef JERRY_UNUSED
203+
#define JERRY_UNUSED(x) ((void) (x))
204+
#endif /* !JERRY_UNUSED */
205+
199206
/**
200207
* @}
201208
*/

jerry-core/include/jerryscript-port.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ void jerry_port_context_free (void);
153153
* The implementation can decide whether error and debug messages are logged to
154154
* the console, or saved to a database or to a file.
155155
*
156-
* @param message_p: the message to log.
156+
* @param buffer_p: input buffer that is a zero-terminated UTF-8 string
157+
* @param buffer_size: data size
157158
*/
158-
void jerry_port_log (const char *message_p);
159+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
159160

160161
/**
161162
* Print a buffer to standard output

jerry-core/jrt/jrt.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
*/
3131
#define JERRY_BITSINBYTE 8
3232

33-
/*
34-
* Make sure unused parameters, variables, or expressions trigger no compiler warning.
35-
*/
36-
#define JERRY_UNUSED(x) ((void) (x))
37-
3833
#define JERRY_UNUSED_1(_1) JERRY_UNUSED (_1)
3934
#define JERRY_UNUSED_2(_1, _2) JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2)
4035
#define JERRY_UNUSED_3(_1, _2, _3) JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3)

jerry-core/parser/js/common.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ static void
6565
util_print_chars (const uint8_t *char_p, /**< character pointer */
6666
size_t size) /**< size */
6767
{
68-
while (size > 0)
69-
{
70-
JERRY_DEBUG_MSG ("%c", *char_p++);
71-
size--;
72-
}
68+
JERRY_DEBUG_MSG ("%.*s", (int) size, char_p);
7369
} /* util_print_chars */
7470

7571
/**
@@ -81,7 +77,7 @@ util_print_number (ecma_number_t num_p) /**< number to print */
8177
lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
8278
lit_utf8_size_t str_size = ecma_number_to_utf8_string (num_p, str_buf, sizeof (str_buf));
8379
str_buf[str_size] = 0;
84-
JERRY_DEBUG_MSG ("%s", str_buf);
80+
JERRY_DEBUG_MSG ("%.*s", (int) str_size, str_buf);
8581
} /* util_print_number */
8682

8783
#if JERRY_BUILTIN_BIGINT

jerry-ext/common/jext-common.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include "jerryscript-port.h"
2323
#include "jerryscript.h"
2424

25-
/*
26-
* Make sure unused parameters, variables, or expressions trigger no compiler warning.
27-
*/
28-
#define JERRYX_UNUSED(x) ((void) (x))
29-
3025
/*
3126
* Asserts
3227
*
@@ -70,7 +65,7 @@ void JERRY_ATTR_NORETURN jerry_unreachable (const char *file, const char *functi
7065
{ \
7166
if (false) \
7267
{ \
73-
JERRYX_UNUSED (x); \
68+
JERRY_UNUSED (x); \
7469
} \
7570
} while (0)
7671

jerry-ext/debugger/debugger-common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jerryx_debugger_after_connect (bool success) /**< tells whether the connection
3535
jerry_debugger_transport_close ();
3636
}
3737
#else /* !(defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)) */
38-
JERRYX_UNUSED (success);
38+
JERRY_UNUSED (success);
3939
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
4040
} /* jerryx_debugger_after_connect */
4141

jerry-ext/debugger/debugger-serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ jerryx_debugger_serial_create (const char *config) /**< specify the configuratio
402402
bool
403403
jerryx_debugger_serial_create (const char *config)
404404
{
405-
JERRYX_UNUSED (config);
405+
JERRY_UNUSED (config);
406406
return false;
407407
} /* jerryx_debugger_serial_create */
408408

0 commit comments

Comments
 (0)