port.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "jerry-port.h"
  2. #include <rtthread.h>
  3. /**
  4. * Signal the port that jerry experienced a fatal failure from which it cannot
  5. * recover.
  6. *
  7. * @param code gives the cause of the error.
  8. *
  9. * Note:
  10. * Jerry expects the function not to return.
  11. *
  12. * Example: a libc-based port may implement this with exit() or abort(), or both.
  13. */
  14. void jerry_port_fatal (jerry_fatal_code_t code)
  15. {
  16. rt_kprintf("jerryScritp fatal...\n");
  17. rt_hw_interrupt_disable();
  18. while (1);
  19. }
  20. /*
  21. * I/O Port API
  22. */
  23. #define RT_JS_CONSOLEBUF_SIZE 128
  24. static char rt_log_buf[RT_JS_CONSOLEBUF_SIZE];
  25. /**
  26. * Print a string to the console. The function should implement a printf-like
  27. * interface, where the first argument specifies a format string on how to
  28. * stringify the rest of the parameter list.
  29. *
  30. * This function is only called with strings coming from the executed ECMAScript
  31. * wanting to print something as the result of its normal operation.
  32. *
  33. * It should be the port that decides what a "console" is.
  34. *
  35. * Example: a libc-based port may implement this with vprintf().
  36. */
  37. void jerry_port_console (const char *format, ...)
  38. {
  39. va_list args;
  40. rt_size_t length;
  41. va_start(args, format);
  42. /* the return value of vsnprintf is the number of bytes that would be
  43. * written to buffer had if the size of the buffer been sufficiently
  44. * large excluding the terminating null byte. If the output string
  45. * would be larger than the rt_log_buf, we have to adjust the output
  46. * length. */
  47. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, format, args);
  48. if (length > RT_CONSOLEBUF_SIZE - 1)
  49. length = RT_CONSOLEBUF_SIZE - 1;
  50. #ifdef RT_USING_DEVICE
  51. rt_kprintf("%s", rt_log_buf);
  52. #else
  53. rt_hw_console_output(rt_log_buf);
  54. #endif
  55. va_end(args);
  56. }
  57. /**
  58. * Display or log a debug/error message. The function should implement a printf-like
  59. * interface, where the first argument specifies the log level
  60. * and the second argument specifies a format string on how to stringify the rest
  61. * of the parameter list.
  62. *
  63. * This function is only called with messages coming from the jerry engine as
  64. * the result of some abnormal operation or describing its internal operations
  65. * (e.g., data structure dumps or tracing info).
  66. *
  67. * It should be the port that decides whether error and debug messages are logged to
  68. * the console, or saved to a database or to a file.
  69. *
  70. * Example: a libc-based port may implement this with vfprintf(stderr) or
  71. * vfprintf(logfile), or both, depending on log level.
  72. */
  73. void jerry_port_log (jerry_log_level_t level, const char *format, ...)
  74. {
  75. va_list args;
  76. rt_size_t length;
  77. va_start(args, format);
  78. /* the return value of vsnprintf is the number of bytes that would be
  79. * written to buffer had if the size of the buffer been sufficiently
  80. * large excluding the terminating null byte. If the output string
  81. * would be larger than the rt_log_buf, we have to adjust the output
  82. * length. */
  83. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, format, args);
  84. if (length > RT_CONSOLEBUF_SIZE - 1)
  85. length = RT_CONSOLEBUF_SIZE - 1;
  86. #ifdef RT_USING_DEVICE
  87. rt_kprintf("%s", rt_log_buf);
  88. #else
  89. rt_hw_console_output(rt_log_buf);
  90. #endif
  91. va_end(args);
  92. }
  93. /*
  94. * Date Port API
  95. */
  96. /**
  97. * Get timezone and daylight saving data
  98. *
  99. * @return true - if success
  100. * false - otherwise
  101. */
  102. bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
  103. {
  104. tz_p->offset = 0;
  105. tz_p->daylight_saving_time = 0;
  106. return true;
  107. }
  108. /**
  109. * Get system time
  110. *
  111. * @return milliseconds since Unix epoch
  112. */
  113. double jerry_port_get_current_time (void)
  114. {
  115. return rt_tick_get() * RT_TICK_PER_SECOND / 1000;
  116. }