Skip to content

Commit 63f739e

Browse files
committed
Add documentation to port API
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent a59cbfe commit 63f739e

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

docs/05.PORT-API.md

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# Reference
2+
3+
## Termination
4+
5+
It is questionable whether a library should be able to terminate an application. Any API function can signal an error (ex.: cannot allocate memory), so the engine use the termination approach with this port function.
6+
7+
```c
8+
/**
9+
* Signal the port that jerry experienced a fatal failure from which it cannot
10+
* recover.
11+
*
12+
* @param code gives the cause of the error.
13+
*
14+
* Note: jerry expects the function not to return.
15+
*
16+
* Example: a libc-based port may implement this with exit() or abort(), or both.
17+
*/
18+
void jerry_port_fatal (jerry_fatal_code_t code);
19+
```
20+
21+
Error codes
22+
23+
```c
24+
typedef enum
25+
{
26+
ERR_OUT_OF_MEMORY = 10,
27+
ERR_SYSCALL = 11,
28+
ERR_REF_COUNT_LIMIT = 12,
29+
ERR_FAILED_INTERNAL_ASSERTION = 120
30+
} jerry_fatal_code_t;
31+
```
32+
33+
## I/O
34+
35+
These are the only I/O functions jerry calls.
36+
37+
```c
38+
/**
39+
* Print a string to the console. The function should implement a printf-like
40+
* interface, where the first argument specifies a format string on how to
41+
* stringify the rest of the parameter list.
42+
*
43+
* This function is only called with strings coming from the executed ECMAScript
44+
* wanting to print something as the result of its normal operation.
45+
*
46+
* It should be the port that decides what a "console" is.
47+
*
48+
* Example: a libc-based port may implement this with vprintf().
49+
*/
50+
void jerry_port_console (const char *fmt, ...);
51+
52+
/**
53+
* Jerry log levels. The levels are in severity order
54+
* where the most serious levels come first.
55+
*/
56+
typedef enum
57+
{
58+
JERRY_LOG_LEVEL_ERROR, /**< the engine will terminate after the message is printed */
59+
JERRY_LOG_LEVEL_WARNING, /**< a request is aborted, but the engine continues its operation */
60+
JERRY_LOG_LEVEL_DEBUG, /**< debug messages from the engine, low volume */
61+
JERRY_LOG_LEVEL_TRACE /**< detailed info about engine internals, potentially high volume */
62+
} jerry_log_level_t;
63+
64+
/**
65+
* Display or log a debug/error message. The function should implement a printf-like
66+
* interface, where the first argument specifies the log level
67+
* and the second argument specifies a format string on how to stringify the rest
68+
* of the parameter list.
69+
*
70+
* This function is only called with messages coming from the jerry engine as
71+
* the result of some abnormal operation or describing its internal operations
72+
* (e.g., data structure dumps or tracing info).
73+
*
74+
* It should be the port that decides whether error and debug messages are logged to
75+
* the console, or saved to a database or to a file.
76+
*
77+
* Example: a libc-based port may implement this with vfprintf(stderr) or
78+
* vfprintf(logfile), or both, depending on log level.
79+
*/
80+
void jerry_port_log (jerry_log_level_t level, const char *fmt, ...);
81+
```
82+
83+
## Date
84+
85+
```c
86+
/**
87+
* Jerry time zone structure
88+
*/
89+
typedef struct
90+
{
91+
int offset; /**< minutes from west */
92+
int daylight_saving_time; /**< daylight saving time (1 - DST applies, 0 - not on DST) */
93+
} jerry_time_zone_t;
94+
95+
/**
96+
* Get timezone and daylight saving data
97+
*
98+
* @return true - if success
99+
* false - otherwise
100+
*/
101+
bool jerry_port_get_time_zone (jerry_time_zone_t *);
102+
103+
/**
104+
* Get system time
105+
*
106+
* @return milliseconds since Unix epoch
107+
*/
108+
double jerry_port_get_current_time (void);
109+
```
110+
111+
# How to port JerryScript
112+
113+
This section describes a basic port implementation which was created for Unix based systems.
114+
115+
## Termination
116+
117+
```c
118+
#include <stdlib.h>
119+
#include "jerry-port.h"
120+
121+
/**
122+
* Default implementation of jerry_port_fatal.
123+
*/
124+
void jerry_port_fatal (jerry_fatal_code_t code)
125+
{
126+
exit (code);
127+
} /* jerry_port_fatal */
128+
```
129+
130+
## I/O
131+
132+
```c
133+
#include <stdarg.h>
134+
#include "jerry-port.h"
135+
136+
/**
137+
* Provide console message implementation for the engine.
138+
*/
139+
void
140+
jerry_port_console (const char *format, /**< format string */
141+
...) /**< parameters */
142+
{
143+
va_list args;
144+
va_start (args, format);
145+
vfprintf (stdout, format, args);
146+
va_end (args);
147+
} /* jerry_port_console */
148+
149+
/**
150+
* Provide log message implementation for the engine.
151+
*
152+
* Note:
153+
* This example ignores the log level.
154+
*/
155+
void
156+
jerry_port_log (jerry_log_level_t level, /**< log level */
157+
const char *format, /**< format string */
158+
...) /**< parameters */
159+
{
160+
va_list args;
161+
va_start (args, format);
162+
vfprintf (stderr, format, args);
163+
va_end (args);
164+
} /* jerry_port_log */
165+
```
166+
167+
## Date
168+
169+
```c
170+
#include <sys/time.h>
171+
#include "jerry-port.h"
172+
173+
/**
174+
* Default implementation of jerry_port_get_time_zone.
175+
*/
176+
bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
177+
{
178+
struct timeval tv;
179+
struct timezone tz;
180+
181+
/* gettimeofday may not fill tz, so zero-initializing */
182+
tz.tz_minuteswest = 0;
183+
tz.tz_dsttime = 0;
184+
185+
if (gettimeofday (&tv, &tz) != 0)
186+
{
187+
return false;
188+
}
189+
190+
tz_p->offset = tz.tz_minuteswest;
191+
tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0;
192+
193+
return true;
194+
} /* jerry_port_get_time_zone */
195+
196+
/**
197+
* Default implementation of jerry_port_get_current_time.
198+
*/
199+
double jerry_port_get_current_time ()
200+
{
201+
struct timeval tv;
202+
203+
if (gettimeofday (&tv, NULL) != 0)
204+
{
205+
return 0;
206+
}
207+
208+
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
209+
} /* jerry_port_get_current_time */
210+
```

0 commit comments

Comments
 (0)