1425bb815Sopenharmony_ci# Reference
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci## Termination
4425bb815Sopenharmony_ci
5425bb815Sopenharmony_ciIt 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.
6425bb815Sopenharmony_ci
7425bb815Sopenharmony_ci```c
8425bb815Sopenharmony_ci/**
9425bb815Sopenharmony_ci * Signal the port that jerry experienced a fatal failure from which it cannot
10425bb815Sopenharmony_ci * recover.
11425bb815Sopenharmony_ci *
12425bb815Sopenharmony_ci * @param code gives the cause of the error.
13425bb815Sopenharmony_ci *
14425bb815Sopenharmony_ci * Note:
15425bb815Sopenharmony_ci *      Jerry expects the function not to return.
16425bb815Sopenharmony_ci *
17425bb815Sopenharmony_ci * Example: a libc-based port may implement this with exit() or abort(), or both.
18425bb815Sopenharmony_ci */
19425bb815Sopenharmony_civoid jerry_port_fatal (jerry_fatal_code_t code);
20425bb815Sopenharmony_ci```
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ciError codes
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_ci```c
25425bb815Sopenharmony_citypedef enum
26425bb815Sopenharmony_ci{
27425bb815Sopenharmony_ci  ERR_OUT_OF_MEMORY = 10,
28425bb815Sopenharmony_ci  ERR_REF_COUNT_LIMIT = 12,
29425bb815Sopenharmony_ci  ERR_DISABLED_BYTE_CODE = 13,
30425bb815Sopenharmony_ci  ERR_FAILED_INTERNAL_ASSERTION = 120
31425bb815Sopenharmony_ci} jerry_fatal_code_t;
32425bb815Sopenharmony_ci```
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_ci## I/O
35425bb815Sopenharmony_ci
36425bb815Sopenharmony_ciThese are the only I/O functions jerry calls.
37425bb815Sopenharmony_ci
38425bb815Sopenharmony_ci```c
39425bb815Sopenharmony_ci/**
40425bb815Sopenharmony_ci * Jerry log levels. The levels are in severity order
41425bb815Sopenharmony_ci * where the most serious levels come first.
42425bb815Sopenharmony_ci */
43425bb815Sopenharmony_citypedef enum
44425bb815Sopenharmony_ci{
45425bb815Sopenharmony_ci  JERRY_LOG_LEVEL_ERROR,    /**< the engine will terminate after the message is printed */
46425bb815Sopenharmony_ci  JERRY_LOG_LEVEL_WARNING,  /**< a request is aborted, but the engine continues its operation */
47425bb815Sopenharmony_ci  JERRY_LOG_LEVEL_DEBUG,    /**< debug messages from the engine, low volume */
48425bb815Sopenharmony_ci  JERRY_LOG_LEVEL_TRACE     /**< detailed info about engine internals, potentially high volume */
49425bb815Sopenharmony_ci} jerry_log_level_t;
50425bb815Sopenharmony_ci
51425bb815Sopenharmony_ci/**
52425bb815Sopenharmony_ci * Display or log a debug/error message, and sends it to the debugger client as well.
53425bb815Sopenharmony_ci * The function should implement a printf-like interface, where the first argument
54425bb815Sopenharmony_ci * specifies the log level and the second argument specifies a format string on how
55425bb815Sopenharmony_ci * to stringify the rest of the parameter list.
56425bb815Sopenharmony_ci *
57425bb815Sopenharmony_ci * This function is only called with messages coming from the jerry engine as
58425bb815Sopenharmony_ci * the result of some abnormal operation or describing its internal operations
59425bb815Sopenharmony_ci * (e.g., data structure dumps or tracing info).
60425bb815Sopenharmony_ci *
61425bb815Sopenharmony_ci * It should be the port that decides whether error and debug messages are logged to
62425bb815Sopenharmony_ci * the console, or saved to a database or to a file.
63425bb815Sopenharmony_ci *
64425bb815Sopenharmony_ci * Example: a libc-based port may implement this with vfprintf(stderr) or
65425bb815Sopenharmony_ci * vfprintf(logfile), or both, depending on log level.
66425bb815Sopenharmony_ci *
67425bb815Sopenharmony_ci * Note:
68425bb815Sopenharmony_ci *      This port function is called by jerry-core when JERRY_LOGGING is
69425bb815Sopenharmony_ci *      enabled. It is also common practice though to use this function in
70425bb815Sopenharmony_ci *      application code.
71425bb815Sopenharmony_ci */
72425bb815Sopenharmony_civoid jerry_port_log (jerry_log_level_t level, const char *fmt, ...);
73425bb815Sopenharmony_ci```
74425bb815Sopenharmony_ci
75425bb815Sopenharmony_ciThe `jerry_port_print_char` is currenlty not used by the jerry-core directly.
76425bb815Sopenharmony_ciHowever, it provides a port specifc way for `jerry-ext` components to print
77425bb815Sopenharmony_ciinformation.
78425bb815Sopenharmony_ci
79425bb815Sopenharmony_ci```c
80425bb815Sopenharmony_ci/**
81425bb815Sopenharmony_ci * Print a character to stdout.
82425bb815Sopenharmony_ci */
83425bb815Sopenharmony_civoid jerry_port_print_char (char c);
84425bb815Sopenharmony_ci```
85425bb815Sopenharmony_ci
86425bb815Sopenharmony_ci### ES2015 Module system
87425bb815Sopenharmony_ci
88425bb815Sopenharmony_ciThe port API provides functions that can be used by the module system to open
89425bb815Sopenharmony_ciand close source files, and normalize file paths.
90425bb815Sopenharmony_ciThe `jerry_port_get_native_module` port function can be used to provide native
91425bb815Sopenharmony_cimodules to the engine. This function will be called when an import/export
92425bb815Sopenharmony_cistatement is encountered with an unknown module specifier, which embedders can
93425bb815Sopenharmony_ciuse to supply native module objects based on the module name argument.
94425bb815Sopenharmony_ci
95425bb815Sopenharmony_ci```c
96425bb815Sopenharmony_ci/**
97425bb815Sopenharmony_ci * Opens file with the given path and reads its source.
98425bb815Sopenharmony_ci * @return the source of the file
99425bb815Sopenharmony_ci */
100425bb815Sopenharmony_ciuint8_t *
101425bb815Sopenharmony_cijerry_port_read_source (const char *file_name_p, /**< file name */
102425bb815Sopenharmony_ci                        size_t *out_size_p) /**< [out] read bytes */
103425bb815Sopenharmony_ci{
104425bb815Sopenharmony_ci  // open file from given path
105425bb815Sopenharmony_ci  // return its source
106425bb815Sopenharmony_ci} /* jerry_port_read_source */
107425bb815Sopenharmony_ci
108425bb815Sopenharmony_ci/**
109425bb815Sopenharmony_ci * Release the previously opened file's content.
110425bb815Sopenharmony_ci */
111425bb815Sopenharmony_civoid
112425bb815Sopenharmony_cijerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */
113425bb815Sopenharmony_ci{
114425bb815Sopenharmony_ci  free (buffer_p);
115425bb815Sopenharmony_ci} /* jerry_port_release_source */
116425bb815Sopenharmony_ci
117425bb815Sopenharmony_ci/**
118425bb815Sopenharmony_ci * Normalize a file path
119425bb815Sopenharmony_ci *
120425bb815Sopenharmony_ci * @return length of the path written to the output buffer
121425bb815Sopenharmony_ci */
122425bb815Sopenharmony_cisize_t
123425bb815Sopenharmony_cijerry_port_normalize_path (const char *in_path_p, /**< input file path */
124425bb815Sopenharmony_ci                           char *out_buf_p,       /**< output buffer */
125425bb815Sopenharmony_ci                           size_t out_buf_size,   /**< size of output buffer */
126425bb815Sopenharmony_ci                           char *base_file_p)     /**< base file path */
127425bb815Sopenharmony_ci{
128425bb815Sopenharmony_ci  // normalize in_path_p by expanding relative paths etc.
129425bb815Sopenharmony_ci  // if base_file_p is not NULL, in_path_p is relative to that file
130425bb815Sopenharmony_ci  // write to out_buf_p the normalized path
131425bb815Sopenharmony_ci  // return length of written path
132425bb815Sopenharmony_ci} /* jerry_port_normalize_path */
133425bb815Sopenharmony_ci
134425bb815Sopenharmony_ci/**
135425bb815Sopenharmony_ci * Get the module object of a native module.
136425bb815Sopenharmony_ci *
137425bb815Sopenharmony_ci * Note:
138425bb815Sopenharmony_ci *      This port function is called by jerry-core when ES2015_MODULE_SYSTEM
139425bb815Sopenharmony_ci *      is enabled.
140425bb815Sopenharmony_ci *
141425bb815Sopenharmony_ci * @param name String value of the module specifier.
142425bb815Sopenharmony_ci *
143425bb815Sopenharmony_ci * @return Undefined, if 'name' is not a native module
144425bb815Sopenharmony_ci *         jerry_value_t containing the module object, otherwise
145425bb815Sopenharmony_ci */
146425bb815Sopenharmony_cijerry_value_t
147425bb815Sopenharmony_cijerry_port_get_native_module (jerry_value_t name) /**< module specifier */
148425bb815Sopenharmony_ci{
149425bb815Sopenharmony_ci  (void) name;
150425bb815Sopenharmony_ci  return jerry_create_undefined ();
151425bb815Sopenharmony_ci}
152425bb815Sopenharmony_ci```
153425bb815Sopenharmony_ci
154425bb815Sopenharmony_ci## Date
155425bb815Sopenharmony_ci
156425bb815Sopenharmony_ci```c
157425bb815Sopenharmony_ci/**
158425bb815Sopenharmony_ci * Get local time zone adjustment, in milliseconds, for the given timestamp.
159425bb815Sopenharmony_ci * The timestamp can be specified in either UTC or local time, depending on
160425bb815Sopenharmony_ci * the value of is_utc. Adding the value returned from this function to
161425bb815Sopenharmony_ci * a timestamp in UTC time should result in local time for the current time
162425bb815Sopenharmony_ci * zone, and subtracting it from a timestamp in local time should result in
163425bb815Sopenharmony_ci * UTC time.
164425bb815Sopenharmony_ci *
165425bb815Sopenharmony_ci * Ideally, this function should satisfy the stipulations applied to LocalTZA
166425bb815Sopenharmony_ci * in section 20.3.1.7 of the ECMAScript version 9.0 spec.
167425bb815Sopenharmony_ci *
168425bb815Sopenharmony_ci * See Also:
169425bb815Sopenharmony_ci *          ECMA-262 v9, 20.3.1.7
170425bb815Sopenharmony_ci *
171425bb815Sopenharmony_ci * Note:
172425bb815Sopenharmony_ci *      This port function is called by jerry-core when
173425bb815Sopenharmony_ci *      JERRY_BUILTIN_DATE is set to 1. Otherwise this function is
174425bb815Sopenharmony_ci *      not used.
175425bb815Sopenharmony_ci *
176425bb815Sopenharmony_ci * @param unix_ms The unix timestamp we want an offset for, given in
177425bb815Sopenharmony_ci *                millisecond precision (could be now, in the future,
178425bb815Sopenharmony_ci *                or in the past). As with all unix timestamps, 0 refers to
179425bb815Sopenharmony_ci *                1970-01-01, a day is exactly 86 400 000 milliseconds, and
180425bb815Sopenharmony_ci *                leap seconds cause the same second to occur twice.
181425bb815Sopenharmony_ci * @param is_utc Is the given timestamp in UTC time? If false, it is in local
182425bb815Sopenharmony_ci *               time.
183425bb815Sopenharmony_ci *
184425bb815Sopenharmony_ci * @return milliseconds between local time and UTC for the given timestamp,
185425bb815Sopenharmony_ci *         if available
186425bb815Sopenharmony_ci *.        0 if not available / we are in UTC.
187425bb815Sopenharmony_ci */
188425bb815Sopenharmony_cidouble jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc);
189425bb815Sopenharmony_ci
190425bb815Sopenharmony_ci/**
191425bb815Sopenharmony_ci * Get system time
192425bb815Sopenharmony_ci *
193425bb815Sopenharmony_ci * Note:
194425bb815Sopenharmony_ci *      This port function is called by jerry-core when
195425bb815Sopenharmony_ci *      JERRY_BUILTIN_DATE is set to 1. It is also common practice
196425bb815Sopenharmony_ci *      in application code to use this function for the initialization of the
197425bb815Sopenharmony_ci *      random number generator.
198425bb815Sopenharmony_ci *
199425bb815Sopenharmony_ci * @return milliseconds since Unix epoch
200425bb815Sopenharmony_ci */
201425bb815Sopenharmony_cidouble jerry_port_get_current_time (void);
202425bb815Sopenharmony_ci```
203425bb815Sopenharmony_ci
204425bb815Sopenharmony_ci## External context
205425bb815Sopenharmony_ci
206425bb815Sopenharmony_ciAllow user to provide external buffer for isolated engine contexts, so that user
207425bb815Sopenharmony_cican configure the heap size at runtime and run multiple JS applications
208425bb815Sopenharmony_cisimultaneously.
209425bb815Sopenharmony_ci
210425bb815Sopenharmony_ci```c
211425bb815Sopenharmony_ci/**
212425bb815Sopenharmony_ci * Get the current context of the engine. Each port should provide its own
213425bb815Sopenharmony_ci * implementation of this interface.
214425bb815Sopenharmony_ci *
215425bb815Sopenharmony_ci * Note:
216425bb815Sopenharmony_ci *      This port function is called by jerry-core when
217425bb815Sopenharmony_ci *      JERRY_EXTERNAL_CONTEXT is enabled. Otherwise this function is not
218425bb815Sopenharmony_ci *      used.
219425bb815Sopenharmony_ci *
220425bb815Sopenharmony_ci * @return the pointer to the engine context.
221425bb815Sopenharmony_ci */
222425bb815Sopenharmony_cistruct jerry_context_t *jerry_port_get_current_context (void);
223425bb815Sopenharmony_ci```
224425bb815Sopenharmony_ci
225425bb815Sopenharmony_ci## Sleep
226425bb815Sopenharmony_ci
227425bb815Sopenharmony_ci```c
228425bb815Sopenharmony_ci/**
229425bb815Sopenharmony_ci * Makes the process sleep for a given time.
230425bb815Sopenharmony_ci *
231425bb815Sopenharmony_ci * Note:
232425bb815Sopenharmony_ci *      This port function is called by jerry-core when JERRY_DEBUGGER is set to 1.
233425bb815Sopenharmony_ci *      Otherwise this function is not used.
234425bb815Sopenharmony_ci *
235425bb815Sopenharmony_ci * @param sleep_time milliseconds to sleep.
236425bb815Sopenharmony_ci */
237425bb815Sopenharmony_civoid jerry_port_sleep (uint32_t sleep_time);
238425bb815Sopenharmony_ci```
239425bb815Sopenharmony_ci
240425bb815Sopenharmony_ci# How to port JerryScript
241425bb815Sopenharmony_ci
242425bb815Sopenharmony_ciThis section describes a basic port implementation which was created for Unix based systems.
243425bb815Sopenharmony_ci
244425bb815Sopenharmony_ci## Termination
245425bb815Sopenharmony_ci
246425bb815Sopenharmony_ci```c
247425bb815Sopenharmony_ci#include <stdlib.h>
248425bb815Sopenharmony_ci#include "jerryscript-port.h"
249425bb815Sopenharmony_ci
250425bb815Sopenharmony_ci/**
251425bb815Sopenharmony_ci * Default implementation of jerry_port_fatal.
252425bb815Sopenharmony_ci */
253425bb815Sopenharmony_civoid jerry_port_fatal (jerry_fatal_code_t code)
254425bb815Sopenharmony_ci{
255425bb815Sopenharmony_ci  exit (code);
256425bb815Sopenharmony_ci} /* jerry_port_fatal */
257425bb815Sopenharmony_ci```
258425bb815Sopenharmony_ci
259425bb815Sopenharmony_ci## I/O
260425bb815Sopenharmony_ci
261425bb815Sopenharmony_ci```c
262425bb815Sopenharmony_ci#include <stdarg.h>
263425bb815Sopenharmony_ci#include "jerryscript-port.h"
264425bb815Sopenharmony_ci
265425bb815Sopenharmony_ci/**
266425bb815Sopenharmony_ci * Provide log message implementation for the engine.
267425bb815Sopenharmony_ci *
268425bb815Sopenharmony_ci * Note:
269425bb815Sopenharmony_ci *      This example ignores the log level.
270425bb815Sopenharmony_ci */
271425bb815Sopenharmony_civoid
272425bb815Sopenharmony_cijerry_port_log (jerry_log_level_t level, /**< log level */
273425bb815Sopenharmony_ci                const char *format, /**< format string */
274425bb815Sopenharmony_ci                ...)  /**< parameters */
275425bb815Sopenharmony_ci{
276425bb815Sopenharmony_ci  va_list args;
277425bb815Sopenharmony_ci  va_start (args, format);
278425bb815Sopenharmony_ci  vfprintf (stderr, format, args);
279425bb815Sopenharmony_ci  va_end (args);
280425bb815Sopenharmony_ci} /* jerry_port_log */
281425bb815Sopenharmony_ci```
282425bb815Sopenharmony_ci
283425bb815Sopenharmony_ci```c
284425bb815Sopenharmony_ci/**
285425bb815Sopenharmony_ci * Print a character to stdout with putchar.
286425bb815Sopenharmony_ci */
287425bb815Sopenharmony_civoid
288425bb815Sopenharmony_cijerry_port_print_char (char c)
289425bb815Sopenharmony_ci{
290425bb815Sopenharmony_ci  putchar (c);
291425bb815Sopenharmony_ci} /* jerr_port_print_char */
292425bb815Sopenharmony_ci```
293425bb815Sopenharmony_ci
294425bb815Sopenharmony_ci## Date
295425bb815Sopenharmony_ci
296425bb815Sopenharmony_ci```c
297425bb815Sopenharmony_ci#include <time.h>
298425bb815Sopenharmony_ci#include <sys/time.h>
299425bb815Sopenharmony_ci#include "jerryscript-port.h"
300425bb815Sopenharmony_ci
301425bb815Sopenharmony_ci/**
302425bb815Sopenharmony_ci * Default implementation of jerry_port_get_local_time_zone_adjustment.
303425bb815Sopenharmony_ci */
304425bb815Sopenharmony_cidouble jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */
305425bb815Sopenharmony_ci                                                  bool is_utc)    /**< is the time above in UTC? */
306425bb815Sopenharmony_ci{
307425bb815Sopenharmony_ci  struct tm tm;
308425bb815Sopenharmony_ci  time_t now = (time_t) (unix_ms / 1000);
309425bb815Sopenharmony_ci  localtime_r (&now, &tm);
310425bb815Sopenharmony_ci  if (!is_utc)
311425bb815Sopenharmony_ci  {
312425bb815Sopenharmony_ci    now -= tm.tm_gmtoff;
313425bb815Sopenharmony_ci    localtime_r (&now, &tm);
314425bb815Sopenharmony_ci  }
315425bb815Sopenharmony_ci  return ((double) tm.tm_gmtoff) * 1000;
316425bb815Sopenharmony_ci} /* jerry_port_get_local_time_zone_adjustment */
317425bb815Sopenharmony_ci
318425bb815Sopenharmony_ci/**
319425bb815Sopenharmony_ci * Default implementation of jerry_port_get_current_time.
320425bb815Sopenharmony_ci */
321425bb815Sopenharmony_cidouble jerry_port_get_current_time (void)
322425bb815Sopenharmony_ci{
323425bb815Sopenharmony_ci  struct timeval tv;
324425bb815Sopenharmony_ci
325425bb815Sopenharmony_ci  if (gettimeofday (&tv, NULL) != 0)
326425bb815Sopenharmony_ci  {
327425bb815Sopenharmony_ci    return 0;
328425bb815Sopenharmony_ci  }
329425bb815Sopenharmony_ci
330425bb815Sopenharmony_ci  return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
331425bb815Sopenharmony_ci} /* jerry_port_get_current_time */
332425bb815Sopenharmony_ci```
333425bb815Sopenharmony_ci
334425bb815Sopenharmony_ci## External context
335425bb815Sopenharmony_ci
336425bb815Sopenharmony_ci```c
337425bb815Sopenharmony_ci#include "jerryscript-port.h"
338425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
339425bb815Sopenharmony_ci
340425bb815Sopenharmony_ci/**
341425bb815Sopenharmony_ci * Pointer to the current context.
342425bb815Sopenharmony_ci * Note that it is a global variable, and is not a thread safe implementation.
343425bb815Sopenharmony_ci */
344425bb815Sopenharmony_cistatic jerry_context_t *current_context_p = NULL;
345425bb815Sopenharmony_ci
346425bb815Sopenharmony_ci/**
347425bb815Sopenharmony_ci * Set the current_context_p as the passed pointer.
348425bb815Sopenharmony_ci */
349425bb815Sopenharmony_civoid
350425bb815Sopenharmony_cijerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
351425bb815Sopenharmony_ci{
352425bb815Sopenharmony_ci  current_context_p = context_p;
353425bb815Sopenharmony_ci} /* jerry_port_default_set_current_context */
354425bb815Sopenharmony_ci
355425bb815Sopenharmony_ci/**
356425bb815Sopenharmony_ci * Get the current context.
357425bb815Sopenharmony_ci *
358425bb815Sopenharmony_ci * @return the pointer to the current context
359425bb815Sopenharmony_ci */
360425bb815Sopenharmony_cijerry_context_t *
361425bb815Sopenharmony_cijerry_port_get_current_context (void)
362425bb815Sopenharmony_ci{
363425bb815Sopenharmony_ci  return current_context_p;
364425bb815Sopenharmony_ci} /* jerry_port_get_current_context */
365425bb815Sopenharmony_ci```
366425bb815Sopenharmony_ci
367425bb815Sopenharmony_ci## Sleep
368425bb815Sopenharmony_ci
369425bb815Sopenharmony_ci```c
370425bb815Sopenharmony_ci#include "jerryscript-port.h"
371425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
372425bb815Sopenharmony_ci
373425bb815Sopenharmony_ci#ifdef HAVE_TIME_H
374425bb815Sopenharmony_ci#include <time.h>
375425bb815Sopenharmony_ci#elif defined (HAVE_UNISTD_H)
376425bb815Sopenharmony_ci#include <unistd.h>
377425bb815Sopenharmony_ci#endif /* HAVE_TIME_H */
378425bb815Sopenharmony_ci
379425bb815Sopenharmony_ci#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
380425bb815Sopenharmony_civoid jerry_port_sleep (uint32_t sleep_time)
381425bb815Sopenharmony_ci{
382425bb815Sopenharmony_ci#ifdef HAVE_TIME_H
383425bb815Sopenharmony_ci  nanosleep (&(const struct timespec)
384425bb815Sopenharmony_ci  {
385425bb815Sopenharmony_ci    (time_t) sleep_time / 1000, ((long int) sleep_time % 1000) * 1000000L /* Seconds, nanoseconds */
386425bb815Sopenharmony_ci  }
387425bb815Sopenharmony_ci  , NULL);
388425bb815Sopenharmony_ci#elif defined (HAVE_UNISTD_H)
389425bb815Sopenharmony_ci  usleep ((useconds_t) sleep_time * 1000);
390425bb815Sopenharmony_ci#endif /* HAVE_TIME_H */
391425bb815Sopenharmony_ci  (void) sleep_time;
392425bb815Sopenharmony_ci} /* jerry_port_sleep */
393425bb815Sopenharmony_ci#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
394425bb815Sopenharmony_ci```
395