|
| 1 | +# Reference |
| 2 | + |
| 3 | +## Termination |
| 4 | + |
| 5 | +It is questionable whether a library should be able to terminate an application. However, as of now, we only have the concept of completion code around `jerry_parse` and `jerry_run`. Most of the other API functions have no way of signaling an error. So, we keep 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 the default port implementation which was created for Unix based systems. |
| 114 | + |
| 115 | +## jerry-port-default.h |
| 116 | + |
| 117 | +This header containt the decleration of port specific helper functions for termination and I/O. |
| 118 | + |
| 119 | +```C |
| 120 | +#include "jerry-port.h" |
| 121 | + |
| 122 | +void jerry_port_default_set_abort_on_fail (bool); |
| 123 | +bool jerry_port_default_is_abort_on_fail (void); |
| 124 | + |
| 125 | +jerry_log_level_t jerry_port_default_get_log_level (void); |
| 126 | +void jerry_port_default_set_log_level (jerry_log_level_t); |
| 127 | +``` |
| 128 | +
|
| 129 | +## Termination |
| 130 | +
|
| 131 | +```C |
| 132 | +#include <stdlib.h> |
| 133 | +
|
| 134 | +#include "jerry-port.h" |
| 135 | +#include "jerry-port-default.h" |
| 136 | +
|
| 137 | +static bool abort_on_fail = false; |
| 138 | +
|
| 139 | +/** |
| 140 | + * Sets whether 'abort' should be called instead of 'exit' upon exiting with |
| 141 | + * non-zero exit code in the default implementation of jerry_port_fatal. |
| 142 | + */ |
| 143 | +void jerry_port_default_set_abort_on_fail (bool flag) /**< new value of 'abort on fail' flag */ |
| 144 | +{ |
| 145 | + abort_on_fail = flag; |
| 146 | +} /* jerry_port_default_set_abort_on_fail */ |
| 147 | +
|
| 148 | +/** |
| 149 | + * Check whether 'abort' should be called instead of 'exit' upon exiting with |
| 150 | + * non-zero exit code in the default implementation of jerry_port_fatal. |
| 151 | + * |
| 152 | + * @return true - if 'abort on fail' flag is set, |
| 153 | + * false - otherwise. |
| 154 | + */ |
| 155 | +bool jerry_port_default_is_abort_on_fail () |
| 156 | +{ |
| 157 | + return abort_on_fail; |
| 158 | +} /* jerry_port_default_is_abort_on_fail */ |
| 159 | +
|
| 160 | +/** |
| 161 | + * Default implementation of jerry_port_fatal. |
| 162 | + */ |
| 163 | +void jerry_port_fatal (jerry_fatal_code_t code) |
| 164 | +{ |
| 165 | + if (code != 0 |
| 166 | + && code != ERR_OUT_OF_MEMORY |
| 167 | + && jerry_port_default_is_abort_on_fail ()) |
| 168 | + { |
| 169 | + abort (); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + exit (code); |
| 174 | + } |
| 175 | +} /* jerry_port_fatal */ |
| 176 | +``` |
| 177 | + |
| 178 | +## I/O |
| 179 | + |
| 180 | +```C |
| 181 | +#include <stdarg.h> |
| 182 | + |
| 183 | +#include "jerry-port.h" |
| 184 | +#include "jerry-port-default.h" |
| 185 | + |
| 186 | +/** |
| 187 | + * Actual log level |
| 188 | + */ |
| 189 | +static jerry_log_level_t jerry_log_level = JERRY_LOG_LEVEL_ERROR; |
| 190 | + |
| 191 | +/** |
| 192 | + * Get the log level |
| 193 | + * |
| 194 | + * @return current log level |
| 195 | + */ |
| 196 | +jerry_log_level_t |
| 197 | +jerry_port_default_get_log_level (void) |
| 198 | +{ |
| 199 | + return jerry_log_level; |
| 200 | +} /* jerry_port_default_get_log_level */ |
| 201 | + |
| 202 | +/** |
| 203 | + * Set the log level |
| 204 | + */ |
| 205 | +void |
| 206 | +jerry_port_default_set_log_level (jerry_log_level_t level) /**< log level */ |
| 207 | +{ |
| 208 | + jerry_log_level = level; |
| 209 | +} /* jerry_port_default_set_log_level */ |
| 210 | + |
| 211 | +/** |
| 212 | + * Provide console message implementation for the engine. |
| 213 | + */ |
| 214 | +void |
| 215 | +jerry_port_console (const char *format, /**< format string */ |
| 216 | + ...) /**< parameters */ |
| 217 | +{ |
| 218 | + va_list args; |
| 219 | + va_start (args, format); |
| 220 | + vfprintf (stdout, format, args); |
| 221 | + va_end (args); |
| 222 | +} /* jerry_port_console */ |
| 223 | + |
| 224 | +/** |
| 225 | + * Provide log message implementation for the engine. |
| 226 | + */ |
| 227 | +void |
| 228 | +jerry_port_log (jerry_log_level_t level, /**< log level */ |
| 229 | + const char *format, /**< format string */ |
| 230 | + ...) /**< parameters */ |
| 231 | +{ |
| 232 | + if (level <= jerry_log_level) |
| 233 | + { |
| 234 | + va_list args; |
| 235 | + va_start (args, format); |
| 236 | + vfprintf (stderr, format, args); |
| 237 | + va_end (args); |
| 238 | + } |
| 239 | +} /* jerry_port_log */ |
| 240 | +``` |
| 241 | +
|
| 242 | +## Date |
| 243 | +
|
| 244 | +```C |
| 245 | +#include <sys/time.h> |
| 246 | +
|
| 247 | +#include "jerry-port.h" |
| 248 | +#include "jerry-port-default.h" |
| 249 | +
|
| 250 | +/** |
| 251 | + * Default implementation of jerry_port_get_time_zone. |
| 252 | + */ |
| 253 | +bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p) |
| 254 | +{ |
| 255 | + struct timeval tv; |
| 256 | + struct timezone tz; |
| 257 | +
|
| 258 | + /* gettimeofday may not fill tz, so zero-initializing */ |
| 259 | + tz.tz_minuteswest = 0; |
| 260 | + tz.tz_dsttime = 0; |
| 261 | +
|
| 262 | + if (gettimeofday (&tv, &tz) != 0) |
| 263 | + { |
| 264 | + return false; |
| 265 | + } |
| 266 | +
|
| 267 | + tz_p->offset = tz.tz_minuteswest; |
| 268 | + tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0; |
| 269 | +
|
| 270 | + return true; |
| 271 | +} /* jerry_port_get_time_zone */ |
| 272 | +
|
| 273 | +/** |
| 274 | + * Default implementation of jerry_port_get_current_time. |
| 275 | + */ |
| 276 | +double jerry_port_get_current_time () |
| 277 | +{ |
| 278 | + struct timeval tv; |
| 279 | +
|
| 280 | + if (gettimeofday (&tv, NULL) != 0) |
| 281 | + { |
| 282 | + return 0; |
| 283 | + } |
| 284 | +
|
| 285 | + return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0; |
| 286 | +} /* jerry_port_get_current_time */ |
| 287 | +``` |
0 commit comments