Skip to content

Commit 0bcd765

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 8d44eed commit 0bcd765

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
@@ -119,8 +119,11 @@ void jerry_port_context_free (void);
119119
*
120120
* The implementation can decide whether error and debug messages are logged to
121121
* the console, or saved to a database or to a file.
122+
*
123+
* @param buffer_p: input buffer
124+
* @param buffer_size: data size
122125
*/
123-
void jerry_port_log (const char *message_p);
126+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
124127
```
125128
126129
```c

jerry-core/api/jerryscript.c

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,49 +5083,33 @@ jerry_log_set_level (jerry_log_level_t level)
50835083
* Log a zero-terminated string message.
50845084
*
50855085
* @param str_p: message
5086+
* @param str_size: size of message
50865087
*/
50875088
static void
5088-
jerry_log_string (const char *str_p)
5089+
jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size)
50895090
{
5090-
jerry_port_log (str_p);
5091+
jerry_port_log_buffer (str_p, str_size);
50915092

50925093
#if JERRY_DEBUGGER
50935094
if (jerry_debugger_is_connected ())
50945095
{
5095-
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT,
5096-
JERRY_DEBUGGER_OUTPUT_LOG,
5097-
(const uint8_t *) str_p,
5098-
strlen (str_p));
5096+
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, JERRY_DEBUGGER_OUTPUT_LOG, str_p, str_size);
50995097
}
51005098
#endif /* JERRY_DEBUGGER */
51015099
} /* jerry_log_string */
51025100

51035101
/**
5104-
* Log a fixed-size string message.
5102+
* Log a zero-terminated number string message.
51055103
*
5106-
* @param str_p: message
5107-
* @param size: size
5108-
* @param buffer_p: buffer to use
5104+
* @param cursor_p: the number string cursor
5105+
* @param buffer_p: buffer used to construct the number string
51095106
*/
51105107
static void
5111-
jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
5108+
jerry_log_cursor (const jerry_char_t *cursor_p, jerry_char_t *buffer_p)
51125109
{
5113-
const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1;
5114-
5115-
while (size > batch_size)
5116-
{
5117-
memcpy (buffer_p, str_p, batch_size);
5118-
buffer_p[batch_size] = '\0';
5119-
jerry_log_string (buffer_p);
5120-
5121-
str_p += batch_size;
5122-
size -= batch_size;
5123-
}
5124-
5125-
memcpy (buffer_p, str_p, size);
5126-
buffer_p[size] = '\0';
5127-
jerry_log_string (buffer_p);
5128-
} /* jerry_log_string_fixed */
5110+
jerry_char_t *tail_p = buffer_p + JERRY_LOG_BUFFER_SIZE - 1;
5111+
jerry_log_string (cursor_p, (jerry_size_t) (tail_p - cursor_p));
5112+
} /* jerry_log_cursor */
51295113

51305114
/**
51315115
* Format an unsigned number.
@@ -5138,11 +5122,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
51385122
*
51395123
* @return formatted number as string
51405124
*/
5141-
static char *
5142-
jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p)
5125+
static jerry_char_t *
5126+
jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p)
51435127
{
5144-
static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
5145-
char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
5128+
static const jerry_char_t digits[] = {
5129+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
5130+
};
5131+
jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
51465132
*(--cursor_p) = '\0';
51475133
uint8_t count = 0;
51485134

@@ -5172,8 +5158,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra
51725158
*
51735159
* @return formatted number as string
51745160
*/
5175-
static char *
5176-
jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
5161+
static jerry_char_t *
5162+
jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p)
51775163
{
51785164
if (num >= 0)
51795165
{
@@ -5184,15 +5170,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
51845170

51855171
if (padding == '0' && width > 0)
51865172
{
5187-
char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
5173+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
51885174
*(--cursor_p) = '-';
51895175
return cursor_p;
51905176
}
51915177

5192-
char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
5178+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
51935179
*(--cursor_p) = '-';
51945180

5195-
char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
5181+
jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
51965182

51975183
while (cursor_p > indent_p)
51985184
{
@@ -5226,17 +5212,17 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52265212
}
52275213

52285214
va_list vl;
5229-
char buffer_p[JERRY_LOG_BUFFER_SIZE];
5230-
uint32_t buffer_index = 0;
5231-
const char *cursor_p = format_p;
5215+
jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE];
5216+
jerry_size_t buffer_index = 0;
5217+
const jerry_char_t *cursor_p = (const jerry_char_t *) format_p;
52325218
va_start (vl, format_p);
52335219

52345220
while (*cursor_p != '\0')
52355221
{
52365222
if (*cursor_p == '%' || buffer_index > JERRY_LOG_BUFFER_SIZE - 2)
52375223
{
52385224
buffer_p[buffer_index] = '\0';
5239-
jerry_log_string (buffer_p);
5225+
jerry_log_string (buffer_p, buffer_index);
52405226
buffer_index = 0;
52415227
}
52425228

@@ -5248,8 +5234,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52485234

52495235
++cursor_p;
52505236
uint8_t width = 0;
5251-
size_t precision = 0;
5252-
char padding = ' ';
5237+
jerry_size_t precision = 0;
5238+
jerry_char_t padding = ' ';
52535239

52545240
if (*cursor_p == '0')
52555241
{
@@ -5269,7 +5255,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52695255
}
52705256
else if (*cursor_p == '.' && *(cursor_p + 1) == '*')
52715257
{
5272-
precision = (size_t) va_arg (vl, int);
5258+
precision = (jerry_size_t) va_arg (vl, int);
52735259
cursor_p += 2;
52745260
}
52755261

@@ -5284,36 +5270,36 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52845270
{
52855271
case 's':
52865272
{
5287-
char *str_p = va_arg (vl, char *);
5273+
jerry_char_t *str_p = va_arg (vl, jerry_char_t *);
52885274

52895275
if (precision == 0)
52905276
{
5291-
jerry_log_string (str_p);
5277+
jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p));
52925278
break;
52935279
}
52945280

5295-
jerry_log_string_fixed (str_p, precision, buffer_p);
5281+
jerry_log_string (str_p, precision);
52965282
break;
52975283
}
52985284
case 'c':
52995285
{
53005286
/* Arguments of types narrower than int are promoted to int for variadic functions */
5301-
buffer_p[buffer_index++] = (char) va_arg (vl, int);
5287+
buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int);
53025288
break;
53035289
}
53045290
case 'd':
53055291
{
5306-
jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p));
5292+
jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p);
53075293
break;
53085294
}
53095295
case 'u':
53105296
{
5311-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p));
5297+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p);
53125298
break;
53135299
}
53145300
case 'x':
53155301
{
5316-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p));
5302+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p);
53175303
break;
53185304
}
53195305
default:
@@ -5327,7 +5313,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53275313
if (buffer_index > 0)
53285314
{
53295315
buffer_p[buffer_index] = '\0';
5330-
jerry_log_string (buffer_p);
5316+
jerry_log_string (buffer_p, buffer_index);
53315317
}
53325318

53335319
va_end (vl);

jerry-core/include/jerryscript-compiler.h

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

188+
/**
189+
* Helper to make sure unused parameters, variables, or expressions trigger no compiler warning.
190+
*/
191+
#ifndef JERRY_UNUSED
192+
#define JERRY_UNUSED(x) ((void) (x))
193+
#endif /* !JERRY_UNUSED */
194+
188195
/**
189196
* @}
190197
*/

jerry-core/include/jerryscript-port.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ void jerry_port_context_free (void);
144144
*
145145
* The implementation can decide whether error and debug messages are logged to
146146
* the console, or saved to a database or to a file.
147+
*
148+
* @param buffer_p: input buffer that is a zero-terminated UTF-8 string
149+
* @param buffer_size: data size
147150
*/
148-
void jerry_port_log (const char *message_p);
151+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
149152

150153
/**
151154
* 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)