Skip to content

Commit c8f3d33

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 luoyonggang@gmail.com
1 parent b285ee5 commit c8f3d33

File tree

17 files changed

+74
-104
lines changed

17 files changed

+74
-104
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ void jerry_port_context_free (void);
129129
*
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.
132+
*
133+
* @param buffer_p: input buffer
134+
* @param buffer_size: data size
132135
*/
133-
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);
134137
```
135138
136139
```c

jerry-core/api/jerryscript.c

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5109,49 +5109,33 @@ jerry_log_set_level (jerry_log_level_t level)
51095109
* Log a zero-terminated string message.
51105110
*
51115111
* @param str_p: message
5112+
* @param str_size: size of message
51125113
*/
51135114
static void
5114-
jerry_log_string (const char *str_p)
5115+
jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size)
51155116
{
5116-
jerry_port_log (str_p);
5117+
jerry_port_log_buffer (str_p, str_size);
51175118

51185119
#if JERRY_DEBUGGER
51195120
if (jerry_debugger_is_connected ())
51205121
{
5121-
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT,
5122-
JERRY_DEBUGGER_OUTPUT_LOG,
5123-
(const uint8_t *) str_p,
5124-
strlen (str_p));
5122+
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, JERRY_DEBUGGER_OUTPUT_LOG, str_p, str_size);
51255123
}
51265124
#endif /* JERRY_DEBUGGER */
51275125
} /* jerry_log_string */
51285126

51295127
/**
5130-
* Log a fixed-size string message.
5128+
* Log a zero-terminated number string message.
51315129
*
5132-
* @param str_p: message
5133-
* @param size: size
5134-
* @param buffer_p: buffer to use
5130+
* @param cursor_p: the number string cursor
5131+
* @param buffer_p: buffer used to construct the number string
51355132
*/
51365133
static void
5137-
jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
5134+
jerry_log_cursor (const jerry_char_t *cursor_p, jerry_char_t *buffer_p)
51385135
{
5139-
const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1;
5140-
5141-
while (size > batch_size)
5142-
{
5143-
memcpy (buffer_p, str_p, batch_size);
5144-
buffer_p[batch_size] = '\0';
5145-
jerry_log_string (buffer_p);
5146-
5147-
str_p += batch_size;
5148-
size -= batch_size;
5149-
}
5150-
5151-
memcpy (buffer_p, str_p, size);
5152-
buffer_p[size] = '\0';
5153-
jerry_log_string (buffer_p);
5154-
} /* jerry_log_string_fixed */
5136+
jerry_char_t *tail_p = buffer_p + JERRY_LOG_BUFFER_SIZE - 1;
5137+
jerry_log_string (cursor_p, (jerry_size_t) (tail_p - cursor_p));
5138+
} /* jerry_log_cursor */
51555139

51565140
/**
51575141
* Format an unsigned number.
@@ -5164,11 +5148,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
51645148
*
51655149
* @return formatted number as string
51665150
*/
5167-
static char *
5168-
jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p)
5151+
static jerry_char_t *
5152+
jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p)
51695153
{
5170-
static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
5171-
char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
5154+
static const jerry_char_t digits[] = {
5155+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
5156+
};
5157+
jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
51725158
*(--cursor_p) = '\0';
51735159
uint8_t count = 0;
51745160

@@ -5198,8 +5184,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra
51985184
*
51995185
* @return formatted number as string
52005186
*/
5201-
static char *
5202-
jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
5187+
static jerry_char_t *
5188+
jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p)
52035189
{
52045190
if (num >= 0)
52055191
{
@@ -5210,15 +5196,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
52105196

52115197
if (padding == '0' && width > 0)
52125198
{
5213-
char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
5199+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
52145200
*(--cursor_p) = '-';
52155201
return cursor_p;
52165202
}
52175203

5218-
char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
5204+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
52195205
*(--cursor_p) = '-';
52205206

5221-
char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
5207+
jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
52225208

52235209
while (cursor_p > indent_p)
52245210
{
@@ -5252,17 +5238,17 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52525238
}
52535239

52545240
va_list vl;
5255-
char buffer_p[JERRY_LOG_BUFFER_SIZE];
5256-
uint32_t buffer_index = 0;
5257-
const char *cursor_p = format_p;
5241+
jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE];
5242+
jerry_size_t buffer_index = 0;
5243+
const jerry_char_t *cursor_p = (const jerry_char_t *) format_p;
52585244
va_start (vl, format_p);
52595245

52605246
while (*cursor_p != '\0')
52615247
{
52625248
if (*cursor_p == '%' || buffer_index > JERRY_LOG_BUFFER_SIZE - 2)
52635249
{
52645250
buffer_p[buffer_index] = '\0';
5265-
jerry_log_string (buffer_p);
5251+
jerry_log_string (buffer_p, buffer_index);
52665252
buffer_index = 0;
52675253
}
52685254

@@ -5274,8 +5260,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52745260

52755261
++cursor_p;
52765262
uint8_t width = 0;
5277-
size_t precision = 0;
5278-
char padding = ' ';
5263+
jerry_size_t precision = 0;
5264+
jerry_char_t padding = ' ';
52795265

52805266
if (*cursor_p == '0')
52815267
{
@@ -5295,7 +5281,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52955281
}
52965282
else if (*cursor_p == '.' && *(cursor_p + 1) == '*')
52975283
{
5298-
precision = (size_t) va_arg (vl, int);
5284+
precision = (jerry_size_t) va_arg (vl, int);
52995285
cursor_p += 2;
53005286
}
53015287

@@ -5310,36 +5296,36 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53105296
{
53115297
case 's':
53125298
{
5313-
char *str_p = va_arg (vl, char *);
5299+
jerry_char_t *str_p = va_arg (vl, jerry_char_t *);
53145300

53155301
if (precision == 0)
53165302
{
5317-
jerry_log_string (str_p);
5303+
jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p));
53185304
break;
53195305
}
53205306

5321-
jerry_log_string_fixed (str_p, precision, buffer_p);
5307+
jerry_log_string (str_p, precision);
53225308
break;
53235309
}
53245310
case 'c':
53255311
{
53265312
/* Arguments of types narrower than int are promoted to int for variadic functions */
5327-
buffer_p[buffer_index++] = (char) va_arg (vl, int);
5313+
buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int);
53285314
break;
53295315
}
53305316
case 'd':
53315317
{
5332-
jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p));
5318+
jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p);
53335319
break;
53345320
}
53355321
case 'u':
53365322
{
5337-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p));
5323+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p);
53385324
break;
53395325
}
53405326
case 'x':
53415327
{
5342-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p));
5328+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p);
53435329
break;
53445330
}
53455331
default:
@@ -5353,7 +5339,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53535339
if (buffer_index > 0)
53545340
{
53555341
buffer_p[buffer_index] = '\0';
5356-
jerry_log_string (buffer_p);
5342+
jerry_log_string (buffer_p, buffer_index);
53575343
}
53585344

53595345
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ void jerry_port_context_free (void);
152152
*
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.
155+
*
156+
* @param buffer_p: input buffer that is a zero-terminated UTF-8 string
157+
* @param buffer_size: data size
155158
*/
156-
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);
157160

158161
/**
159162
* 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)