Skip to content

Commit bcfa39e

Browse files
committed
Implement IO port API
Related issue: #964 JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent b4bba2e commit bcfa39e

File tree

8 files changed

+162
-108
lines changed

8 files changed

+162
-108
lines changed

jerry-core/jerry-port.h

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ extern "C"
2929
* @{
3030
*/
3131

32-
/**
33-
* Target port functions for console output
34-
*/
35-
int jerry_port_logmsg (FILE *stream, const char *format, ...);
36-
int jerry_port_errormsg (const char *format, ...);
37-
3832
/*
3933
* Termination Port API
4034
*
@@ -71,6 +65,54 @@ typedef enum
7165
*/
7266
void jerry_port_fatal (jerry_fatal_code_t code);
7367

68+
/*
69+
* I/O Port API
70+
*/
71+
72+
/**
73+
* Print a string to the console. The function should implement a printf-like
74+
* interface, where the first argument specifies a format string on how to
75+
* stringify the rest of the parameter list.
76+
*
77+
* This function is only called with strings coming from the executed ECMAScript
78+
* wanting to print something as the result of its normal operation.
79+
*
80+
* It should be the port that decides what a "console" is.
81+
*
82+
* Example: a libc-based port may implement this with vprintf().
83+
*/
84+
void jerry_port_console (const char *format, ...);
85+
86+
/**
87+
* Jerry log levels. The levels are in severity order
88+
* where the most serious levels come first.
89+
*/
90+
typedef enum
91+
{
92+
JERRY_LOG_LEVEL_ERROR, /**< the engine will terminate after the message is printed */
93+
JERRY_LOG_LEVEL_WARNING, /**< a request is aborted, but the engine continues its operation */
94+
JERRY_LOG_LEVEL_DEBUG, /**< debug messages from the engine, low volume */
95+
JERRY_LOG_LEVEL_TRACE /**< detailed info about engine internals, potentially high volume */
96+
} jerry_log_level_t;
97+
98+
/**
99+
* Display or log a debug/error message. The function should implement a printf-like
100+
* interface, where the first argument specifies the log level
101+
* and the second argument specifies a format string on how to stringify the rest
102+
* of the parameter list.
103+
*
104+
* This function is only called with messages coming from the jerry engine as
105+
* the result of some abnormal operation or describing its internal operations
106+
* (e.g., data structure dumps or tracing info).
107+
*
108+
* It should be the port that decides whether error and debug messages are logged to
109+
* the console, or saved to a database or to a file.
110+
*
111+
* Example: a libc-based port may implement this with vfprintf(stderr) or
112+
* vfprintf(logfile), or both, depending on log level.
113+
*/
114+
void jerry_port_log (jerry_log_level_t level, const char *format, ...);
115+
74116
/*
75117
* Date Port API
76118
*/

jerry-core/jrt/jrt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch
109109
#define JERRY_DDDLOG(...) JERRY_DLOG (__VA_ARGS__)
110110
#endif /* JERRY_ENABLE_LOG */
111111

112-
#define JERRY_ERROR_MSG(...) jerry_port_errormsg (__VA_ARGS__)
113-
#define JERRY_WARNING_MSG(...) JERRY_ERROR_MSG (__VA_ARGS__)
112+
#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
113+
#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
114114

115115
/**
116116
* Mark for unreachable points and unimplemented cases

main-unix.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ read_file (const char *file_name,
4848
FILE *file = fopen (file_name, "r");
4949
if (file == NULL)
5050
{
51-
jerry_port_errormsg ("Error: failed to open file: %s\n", file_name);
51+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
5252
return NULL;
5353
}
5454

5555
size_t bytes_read = fread (buffer, 1u, sizeof (buffer), file);
5656
if (!bytes_read)
5757
{
58-
jerry_port_errormsg ("Error: failed to read file: %s\n", file_name);
58+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
5959
fclose (file);
6060
return NULL;
6161
}
@@ -85,7 +85,7 @@ assert_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< f
8585
}
8686
else
8787
{
88-
jerry_port_errormsg ("Script error: assertion failed\n");
88+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script error: assertion failed\n");
8989
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
9090
}
9191
} /* assert_handler */
@@ -126,8 +126,10 @@ main (int argc,
126126
{
127127
if (argc > JERRY_MAX_COMMAND_LINE_ARGS)
128128
{
129-
jerry_port_errormsg ("Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n",
130-
argc, JERRY_MAX_COMMAND_LINE_ARGS);
129+
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
130+
"Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n",
131+
argc,
132+
JERRY_MAX_COMMAND_LINE_ARGS);
131133

132134
return JERRY_STANDALONE_EXIT_CODE_FAIL;
133135
}
@@ -193,14 +195,14 @@ main (int argc,
193195

194196
if (save_snapshot_file_name_p != NULL)
195197
{
196-
jerry_port_errormsg ("Error: snapshot file name already specified\n");
198+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: snapshot file name already specified\n");
197199
print_usage (argv[0]);
198200
return JERRY_STANDALONE_EXIT_CODE_FAIL;
199201
}
200202

201203
if (++i >= argc)
202204
{
203-
jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]);
205+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]);
204206
print_usage (argv[0]);
205207
return JERRY_STANDALONE_EXIT_CODE_FAIL;
206208
}
@@ -211,7 +213,7 @@ main (int argc,
211213
{
212214
if (++i >= argc)
213215
{
214-
jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]);
216+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]);
215217
print_usage (argv[0]);
216218
return JERRY_STANDALONE_EXIT_CODE_FAIL;
217219
}
@@ -223,14 +225,14 @@ main (int argc,
223225
{
224226
if (++i >= argc)
225227
{
226-
jerry_port_errormsg ("Error: no level specified for %s\n", argv[i - 1]);
228+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no level specified for %s\n", argv[i - 1]);
227229
print_usage (argv[0]);
228230
return JERRY_STANDALONE_EXIT_CODE_FAIL;
229231
}
230232

231233
if (strlen (argv[i]) != 1 || argv[i][0] < '0' || argv[i][0] > '3')
232234
{
233-
jerry_port_errormsg ("Error: wrong format for %s\n", argv[i - 1]);
235+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format for %s\n", argv[i - 1]);
234236
print_usage (argv[0]);
235237
return JERRY_STANDALONE_EXIT_CODE_FAIL;
236238
}
@@ -244,7 +246,7 @@ main (int argc,
244246
{
245247
if (++i >= argc)
246248
{
247-
jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]);
249+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]);
248250
print_usage (argv[0]);
249251
return JERRY_STANDALONE_EXIT_CODE_FAIL;
250252
}
@@ -260,7 +262,7 @@ main (int argc,
260262
}
261263
else if (!strncmp ("-", argv[i], 1))
262264
{
263-
jerry_port_errormsg ("Error: unrecognized option: %s\n", argv[i]);
265+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: unrecognized option: %s\n", argv[i]);
264266
print_usage (argv[0]);
265267
return JERRY_STANDALONE_EXIT_CODE_FAIL;
266268
}
@@ -274,13 +276,15 @@ main (int argc,
274276
{
275277
if (files_counter != 1)
276278
{
277-
jerry_port_errormsg ("Error: --save-snapshot argument works with exactly one script\n");
279+
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
280+
"Error: --save-snapshot argument works with exactly one script\n");
278281
return JERRY_STANDALONE_EXIT_CODE_FAIL;
279282
}
280283

281284
if (exec_snapshots_count != 0)
282285
{
283-
jerry_port_errormsg ("Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n");
286+
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
287+
"Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n");
284288
return JERRY_STANDALONE_EXIT_CODE_FAIL;
285289
}
286290
}
@@ -297,7 +301,7 @@ main (int argc,
297301
jerry_log_file = fopen (log_file_name, "w");
298302
if (jerry_log_file == NULL)
299303
{
300-
jerry_port_errormsg ("Error: failed to open log file: %s\n", log_file_name);
304+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open log file: %s\n", log_file_name);
301305
return JERRY_STANDALONE_EXIT_CODE_FAIL;
302306
}
303307
}
@@ -321,7 +325,7 @@ main (int argc,
321325

322326
if (!is_assert_added)
323327
{
324-
jerry_port_errormsg ("Warning: failed to register 'assert' method.");
328+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Warning: failed to register 'assert' method.");
325329
}
326330

327331
jerry_value_t ret_value = jerry_create_undefined ();
@@ -489,7 +493,7 @@ main (int argc,
489493
assert (sz == err_str_size);
490494
err_str_buf[err_str_size] = 0;
491495

492-
jerry_port_errormsg ("Script Error: unhandled exception: %s\n", err_str_buf);
496+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: unhandled exception: %s\n", err_str_buf);
493497

494498
jerry_release_value (err_str_val);
495499

targets/default/jerry-port-default-io.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@
1818
#include "jerry-port.h"
1919

2020
/**
21-
* Provide log message to filestream implementation for the engine.
21+
* Provide console message implementation for the engine.
2222
*/
23-
int jerry_port_logmsg (FILE *stream, /**< stream pointer */
24-
const char *format, /**< format string */
25-
...) /**< parameters */
23+
void
24+
jerry_port_console (const char *format, /**< format string */
25+
...) /**< parameters */
2626
{
2727
va_list args;
28-
int count;
2928
va_start (args, format);
30-
count = vfprintf (stream, format, args);
29+
vfprintf (stdout, format, args);
3130
va_end (args);
32-
return count;
33-
} /* jerry_port_logmsg */
31+
} /* jerry_port_console */
3432

3533
/**
36-
* Provide error message to console implementation for the engine.
34+
* Provide log message implementation for the engine.
3735
*/
38-
int jerry_port_errormsg (const char *format, /**< format string */
39-
...) /**< parameters */
36+
void
37+
jerry_port_log (jerry_log_level_t level, /**< log level */
38+
const char *format, /**< format string */
39+
...) /**< parameters */
4040
{
41+
(void) level; /* default port implementation ignores the log level */
42+
4143
va_list args;
42-
int count;
4344
va_start (args, format);
44-
count = vfprintf (stderr, format, args);
45+
vfprintf (stderr, format, args);
4546
va_end (args);
46-
return count;
47-
} /* jerry_port_errormsg */
47+
} /* jerry_port_log */

targets/esp8266/user/jerry_port.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@
1717
#include <stdio.h>
1818
#include <stdarg.h>
1919

20-
2120
/**
22-
* Provide log message to filestream implementation for the engine.
21+
* Provide console message implementation for the engine.
2322
*/
24-
int jerry_port_logmsg (FILE* stream, const char* format, ...)
23+
void
24+
jerry_port_console (const char *format, /**< format string */
25+
...) /**< parameters */
2526
{
2627
va_list args;
27-
int count = 0;
2828
va_start (args, format);
2929
// TODO, uncomment when vfprint link is ok
30-
//count = vfprintf (stream, format, args);
30+
// vfprintf (stdout, format, args);
3131
va_end (args);
32-
return count;
33-
}
32+
} /* jerry_port_console */
3433

3534
/**
36-
* Provide error message to console implementation for the engine.
35+
* Provide log message implementation for the engine.
3736
*/
38-
int jerry_port_errormsg (const char* format, ...)
37+
void
38+
jerry_port_log (jerry_log_level_t level, /**< log level */
39+
const char *format, /**< format string */
40+
...) /**< parameters */
3941
{
42+
(void) level; /* ignore log level */
43+
4044
va_list args;
41-
int count = 0;
4245
va_start (args, format);
4346
// TODO, uncomment when vprint link is ok
44-
//count = vprintf (format, args);
47+
// vprintf (stderr, format, args);
4548
va_end (args);
46-
return count;
47-
}
49+
} /* jerry_port_log */
4850

4951

5052
/** exit - cause normal process termination */

targets/mbed/source/port/jerry_port.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,33 @@
2323
#include "mbed-hal/us_ticker_api.h"
2424

2525
/**
26-
* Provide log message to filestream implementation for the engine.
26+
* Provide console message implementation for the engine.
2727
*/
28-
int
29-
jerry_port_logmsg (FILE *stream, /**< stream pointer */
30-
const char *format, /**< format string */
31-
...) /**< parameters */
28+
void
29+
jerry_port_console (const char *format, /**< format string */
30+
...) /**< parameters */
3231
{
3332
va_list args;
34-
int count;
3533
va_start (args, format);
36-
count = vfprintf (stream, format, args);
34+
vfprintf (stdout, format, args);
3735
va_end (args);
38-
return count;
39-
} /* jerry_port_logmsg */
36+
} /* jerry_port_console */
4037

4138
/**
42-
* Provide error message to console implementation for the engine.
39+
* Provide log message implementation for the engine.
4340
*/
44-
int
45-
jerry_port_errormsg (const char *format, /**< format string */
46-
...) /**< parameters */
41+
void
42+
jerry_port_log (jerry_log_level_t level, /**< log level */
43+
const char *format, /**< format string */
44+
...) /**< parameters */
4745
{
46+
(void) level; /* ignore log level */
47+
4848
va_list args;
49-
int count;
5049
va_start (args, format);
51-
count = vfprintf (stderr, format, args);
50+
vfprintf (stderr, format, args);
5251
va_end (args);
53-
return count;
54-
} /* jerry_port_errormsg */
52+
} /* jerry_port_log */
5553

5654
/**
5755
* Implementation of jerry_port_fatal.

0 commit comments

Comments
 (0)