Reference

Termination

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.

  1. /**
  2. * Signal the port that jerry experienced a fatal failure from which it cannot
  3. * recover.
  4. *
  5. * @param code gives the cause of the error.
  6. *
  7. * Note: jerry expects the function not to return.
  8. *
  9. * Example: a libc-based port may implement this with exit() or abort(), or both.
  10. */
  11. void jerry_port_fatal (jerry_fatal_code_t code);

Error codes

  1. typedef enum
  2. {
  3. ERR_OUT_OF_MEMORY = 10,
  4. ERR_SYSCALL = 11,
  5. ERR_REF_COUNT_LIMIT = 12,
  6. ERR_FAILED_INTERNAL_ASSERTION = 120
  7. } jerry_fatal_code_t;

I/O

These are the only I/O functions jerry calls.

  1. /**
  2. * Print a string to the console. The function should implement a printf-like
  3. * interface, where the first argument specifies a format string on how to
  4. * stringify the rest of the parameter list.
  5. *
  6. * This function is only called with strings coming from the executed ECMAScript
  7. * wanting to print something as the result of its normal operation.
  8. *
  9. * It should be the port that decides what a "console" is.
  10. *
  11. * Example: a libc-based port may implement this with vprintf().
  12. */
  13. void jerry_port_console (const char *fmt, ...);
  14. /**
  15. * Jerry log levels. The levels are in severity order
  16. * where the most serious levels come first.
  17. */
  18. typedef enum
  19. {
  20. JERRY_LOG_LEVEL_ERROR, /**< the engine will terminate after the message is printed */
  21. JERRY_LOG_LEVEL_WARNING, /**< a request is aborted, but the engine continues its operation */
  22. JERRY_LOG_LEVEL_DEBUG, /**< debug messages from the engine, low volume */
  23. JERRY_LOG_LEVEL_TRACE /**< detailed info about engine internals, potentially high volume */
  24. } jerry_log_level_t;
  25. /**
  26. * Display or log a debug/error message. The function should implement a printf-like
  27. * interface, where the first argument specifies the log level
  28. * and the second argument specifies a format string on how to stringify the rest
  29. * of the parameter list.
  30. *
  31. * This function is only called with messages coming from the jerry engine as
  32. * the result of some abnormal operation or describing its internal operations
  33. * (e.g., data structure dumps or tracing info).
  34. *
  35. * It should be the port that decides whether error and debug messages are logged to
  36. * the console, or saved to a database or to a file.
  37. *
  38. * Example: a libc-based port may implement this with vfprintf(stderr) or
  39. * vfprintf(logfile), or both, depending on log level.
  40. */
  41. void jerry_port_log (jerry_log_level_t level, const char *fmt, ...);

Date

  1. /**
  2. * Jerry time zone structure
  3. */
  4. typedef struct
  5. {
  6. int offset; /**< minutes from west */
  7. int daylight_saving_time; /**< daylight saving time (1 - DST applies, 0 - not on DST) */
  8. } jerry_time_zone_t;
  9. /**
  10. * Get timezone and daylight saving data
  11. *
  12. * @return true - if success
  13. * false - otherwise
  14. */
  15. bool jerry_port_get_time_zone (jerry_time_zone_t *);
  16. /**
  17. * Get system time
  18. *
  19. * @return milliseconds since Unix epoch
  20. */
  21. double jerry_port_get_current_time (void);

How to port JerryScript

This section describes a basic port implementation which was created for Unix based systems.

Termination

  1. #include <stdlib.h>
  2. #include "jerry-port.h"
  3. /**
  4. * Default implementation of jerry_port_fatal.
  5. */
  6. void jerry_port_fatal (jerry_fatal_code_t code)
  7. {
  8. exit (code);
  9. } /* jerry_port_fatal */

I/O

  1. #include <stdarg.h>
  2. #include "jerry-port.h"
  3. /**
  4. * Provide console message implementation for the engine.
  5. */
  6. void
  7. jerry_port_console (const char *format, /**< format string */
  8. ...) /**< parameters */
  9. {
  10. va_list args;
  11. va_start (args, format);
  12. vfprintf (stdout, format, args);
  13. va_end (args);
  14. } /* jerry_port_console */
  15. /**
  16. * Provide log message implementation for the engine.
  17. *
  18. * Note:
  19. * This example ignores the log level.
  20. */
  21. void
  22. jerry_port_log (jerry_log_level_t level, /**< log level */
  23. const char *format, /**< format string */
  24. ...) /**< parameters */
  25. {
  26. va_list args;
  27. va_start (args, format);
  28. vfprintf (stderr, format, args);
  29. va_end (args);
  30. } /* jerry_port_log */

Date

  1. #include <sys/time.h>
  2. #include "jerry-port.h"
  3. /**
  4. * Default implementation of jerry_port_get_time_zone.
  5. */
  6. bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
  7. {
  8. struct timeval tv;
  9. struct timezone tz;
  10. /* gettimeofday may not fill tz, so zero-initializing */
  11. tz.tz_minuteswest = 0;
  12. tz.tz_dsttime = 0;
  13. if (gettimeofday (&tv, &tz) != 0)
  14. {
  15. return false;
  16. }
  17. tz_p->offset = tz.tz_minuteswest;
  18. tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0;
  19. return true;
  20. } /* jerry_port_get_time_zone */
  21. /**
  22. * Default implementation of jerry_port_get_current_time.
  23. */
  24. double jerry_port_get_current_time ()
  25. {
  26. struct timeval tv;
  27. if (gettimeofday (&tv, NULL) != 0)
  28. {
  29. return 0;
  30. }
  31. return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
  32. } /* jerry_port_get_current_time */