port.c 3.4 KB

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