|
| 1 | +/* Copyright JS Foundation and other contributors, http://js.foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef JERRYSCRIPT_PORT_H |
| 17 | +#define JERRYSCRIPT_PORT_H |
| 18 | + |
| 19 | +#include <stdbool.h> |
| 20 | +#include <stdint.h> |
| 21 | +#include <stdio.h> |
| 22 | + |
| 23 | +#ifdef __cplusplus |
| 24 | +extern "C" |
| 25 | +{ |
| 26 | +#endif /* __cplusplus */ |
| 27 | + |
| 28 | +/** \addtogroup jerry_port Jerry engine port |
| 29 | + * @{ |
| 30 | + */ |
| 31 | + |
| 32 | +/* |
| 33 | + * Termination Port API |
| 34 | + * |
| 35 | + * Note: |
| 36 | + * It is questionable whether a library should be able to terminate an |
| 37 | + * application. However, as of now, we only have the concept of completion |
| 38 | + * code around jerry_parse and jerry_run. Most of the other API functions |
| 39 | + * have no way of signaling an error. So, we keep the termination approach |
| 40 | + * with this port function. |
| 41 | + */ |
| 42 | + |
| 43 | +/** |
| 44 | + * Error codes |
| 45 | + */ |
| 46 | +typedef enum |
| 47 | +{ |
| 48 | + ERR_OUT_OF_MEMORY = 10, |
| 49 | + ERR_SYSCALL = 11, |
| 50 | + ERR_REF_COUNT_LIMIT = 12, |
| 51 | + ERR_FAILED_INTERNAL_ASSERTION = 120 |
| 52 | +} jerry_fatal_code_t; |
| 53 | + |
| 54 | +/** |
| 55 | + * Signal the port that jerry experienced a fatal failure from which it cannot |
| 56 | + * recover. |
| 57 | + * |
| 58 | + * @param code gives the cause of the error. |
| 59 | + * |
| 60 | + * Note: |
| 61 | + * Jerry expects the function not to return. |
| 62 | + * |
| 63 | + * Example: a libc-based port may implement this with exit() or abort(), or both. |
| 64 | + */ |
| 65 | +void jerry_port_fatal (jerry_fatal_code_t code); |
| 66 | + |
| 67 | +/* |
| 68 | + * I/O Port API |
| 69 | + */ |
| 70 | + |
| 71 | +/** |
| 72 | + * Print a string to the console. The function should implement a printf-like |
| 73 | + * interface, where the first argument specifies a format string on how to |
| 74 | + * stringify the rest of the parameter list. |
| 75 | + * |
| 76 | + * This function is only called with strings coming from the executed ECMAScript |
| 77 | + * wanting to print something as the result of its normal operation. |
| 78 | + * |
| 79 | + * It should be the port that decides what a "console" is. |
| 80 | + * |
| 81 | + * Example: a libc-based port may implement this with vprintf(). |
| 82 | + */ |
| 83 | +void jerry_port_console (const char *format, ...) __attribute__ ((format (printf, 1, 2))); |
| 84 | + |
| 85 | +/** |
| 86 | + * Jerry log levels. The levels are in severity order |
| 87 | + * where the most serious levels come first. |
| 88 | + */ |
| 89 | +typedef enum |
| 90 | +{ |
| 91 | + JERRY_LOG_LEVEL_ERROR, /**< the engine will terminate after the message is printed */ |
| 92 | + JERRY_LOG_LEVEL_WARNING, /**< a request is aborted, but the engine continues its operation */ |
| 93 | + JERRY_LOG_LEVEL_DEBUG, /**< debug messages from the engine, low volume */ |
| 94 | + JERRY_LOG_LEVEL_TRACE /**< detailed info about engine internals, potentially high volume */ |
| 95 | +} jerry_log_level_t; |
| 96 | + |
| 97 | +/** |
| 98 | + * Display or log a debug/error message. The function should implement a printf-like |
| 99 | + * interface, where the first argument specifies the log level |
| 100 | + * and the second argument specifies a format string on how to stringify the rest |
| 101 | + * of the parameter list. |
| 102 | + * |
| 103 | + * This function is only called with messages coming from the jerry engine as |
| 104 | + * the result of some abnormal operation or describing its internal operations |
| 105 | + * (e.g., data structure dumps or tracing info). |
| 106 | + * |
| 107 | + * It should be the port that decides whether error and debug messages are logged to |
| 108 | + * the console, or saved to a database or to a file. |
| 109 | + * |
| 110 | + * Example: a libc-based port may implement this with vfprintf(stderr) or |
| 111 | + * vfprintf(logfile), or both, depending on log level. |
| 112 | + */ |
| 113 | +void jerry_port_log (jerry_log_level_t level, const char *format, ...) __attribute__ ((format (printf, 2, 3))); |
| 114 | + |
| 115 | +/* |
| 116 | + * Date Port API |
| 117 | + */ |
| 118 | + |
| 119 | +/** |
| 120 | + * Jerry time zone structure |
| 121 | + */ |
| 122 | +typedef struct |
| 123 | +{ |
| 124 | + int offset; /**< minutes from west */ |
| 125 | + int daylight_saving_time; /**< daylight saving time (1 - DST applies, 0 - not on DST) */ |
| 126 | +} jerry_time_zone_t; |
| 127 | + |
| 128 | +/** |
| 129 | + * Get timezone and daylight saving data |
| 130 | + * |
| 131 | + * @return true - if success |
| 132 | + * false - otherwise |
| 133 | + */ |
| 134 | +bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p); |
| 135 | + |
| 136 | +/** |
| 137 | + * Get system time |
| 138 | + * |
| 139 | + * @return milliseconds since Unix epoch |
| 140 | + */ |
| 141 | +double jerry_port_get_current_time (void); |
| 142 | + |
| 143 | +#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN |
| 144 | + |
| 145 | +#define JERRY_PORT_ENABLE_JOBQUEUE |
| 146 | + |
| 147 | +typedef uint32_t (*jerry_job_handler_t) (void *); |
| 148 | + |
| 149 | +void jerry_port_jobqueue_enqueue (jerry_job_handler_t handler, void *job_p); |
| 150 | + |
| 151 | +#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */ |
| 152 | + |
| 153 | +/** |
| 154 | + * @} |
| 155 | + */ |
| 156 | + |
| 157 | +#ifdef __cplusplus |
| 158 | +} |
| 159 | +#endif /* __cplusplus */ |
| 160 | +#endif /* !JERRYSCRIPT_PORT_H */ |
0 commit comments