port.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #ifdef JMEM_STATS
  22. extern void jmem_heap_stats_print (void);
  23. #endif
  24. /**
  25. * Signal the port that jerry experienced a fatal failure from which it cannot
  26. * recover.
  27. *
  28. * @param code gives the cause of the error.
  29. *
  30. * Note:
  31. * Jerry expects the function not to return.
  32. *
  33. * Example: a libc-based port may implement this with exit() or abort(), or both.
  34. */
  35. void jerry_port_fatal(jerry_fatal_code_t code)
  36. {
  37. rt_kprintf("jerryScritp fatal [");
  38. switch (code)
  39. {
  40. case ERR_OUT_OF_MEMORY:
  41. rt_kprintf(" ERR_OUT_OF_MEMORY ");
  42. break;
  43. case ERR_SYSCALL:
  44. rt_kprintf(" ERR_SYSCALL ");
  45. break;
  46. case ERR_REF_COUNT_LIMIT:
  47. rt_kprintf(" ERR_REF_COUNT_LIMIT ");
  48. break;
  49. case ERR_DISABLED_BYTE_CODE:
  50. rt_kprintf(" ERR_DISABLED_BYTE_CODE ");
  51. break;
  52. case ERR_FAILED_INTERNAL_ASSERTION:
  53. rt_kprintf(" ERR_FAILED_INTERNAL_ASSERTION ");
  54. break;
  55. };
  56. rt_kprintf("]...\n");
  57. #ifdef JMEM_STATS
  58. jmem_heap_stats_print();
  59. #endif
  60. rt_hw_interrupt_disable();
  61. while (1);
  62. }
  63. /*
  64. * I/O Port API
  65. */
  66. #define RT_JS_CONSOLEBUF_SIZE 256
  67. static char rt_log_buf[RT_JS_CONSOLEBUF_SIZE];
  68. /**
  69. * Display or log a debug/error message. The function should implement a printf-like
  70. * interface, where the first argument specifies the log level
  71. * and the second argument specifies a format string on how to stringify the rest
  72. * of the parameter list.
  73. *
  74. * This function is only called with messages coming from the jerry engine as
  75. * the result of some abnormal operation or describing its internal operations
  76. * (e.g., data structure dumps or tracing info).
  77. *
  78. * It should be the port that decides whether error and debug messages are logged to
  79. * the console, or saved to a database or to a file.
  80. *
  81. * Example: a libc-based port may implement this with vfprintf(stderr) or
  82. * vfprintf(logfile), or both, depending on log level.
  83. */
  84. void jerry_port_log (jerry_log_level_t level, const char *format, ...)
  85. {
  86. va_list args;
  87. rt_size_t length;
  88. va_start(args, format);
  89. /* the return value of vsnprintf is the number of bytes that would be
  90. * written to buffer had if the size of the buffer been sufficiently
  91. * large excluding the terminating null byte. If the output string
  92. * would be larger than the rt_log_buf, we have to adjust the output
  93. * length. */
  94. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, format, args);
  95. if (length > RT_JS_CONSOLEBUF_SIZE - 1)
  96. length = RT_JS_CONSOLEBUF_SIZE - 1;
  97. #ifdef RT_USING_DEVICE
  98. rt_kprintf("%s", rt_log_buf);
  99. #else
  100. rt_hw_console_output(rt_log_buf);
  101. #endif
  102. va_end(args);
  103. }
  104. /**
  105. * Default implementation of jerryx_port_handler_print_char. Uses 'printf' to
  106. * print a single character to standard output.
  107. */
  108. void jerryx_port_handler_print_char (char c) /**< the character to print */
  109. {
  110. rt_kprintf("%c", c);
  111. } /* jerryx_port_handler_print_char */
  112. /*
  113. * Date Port API
  114. */
  115. /**
  116. * Get timezone and daylight saving data
  117. *
  118. * @return true - if success
  119. * false - otherwise
  120. */
  121. bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
  122. {
  123. tz_p->offset = 0;
  124. tz_p->daylight_saving_time = 0;
  125. return true;
  126. }
  127. /**
  128. * Get system time
  129. *
  130. * @return milliseconds since Unix epoch
  131. */
  132. double jerry_port_get_current_time (void)
  133. {
  134. return rt_tick_get() * RT_TICK_PER_SECOND / 1000;
  135. }
  136. /**
  137. * Pointer to the current instance.
  138. * Note that it is a global variable, and is not a thread safe implementation.
  139. */
  140. static jerry_context_t *jerry_default_context = NULL;
  141. /**
  142. * Set the jerry_default_context as the passed pointer.
  143. */
  144. void
  145. jerry_port_set_default_context(jerry_context_t *context)
  146. {
  147. jerry_default_context = context;
  148. }
  149. /**
  150. * Get the current context of the engine. Each port should provide its own
  151. * implementation of this interface.
  152. *
  153. * Note:
  154. * This port function is called by jerry-core when
  155. * JERRY_ENABLE_EXTERNAL_CONTEXT is defined. Otherwise this function is not
  156. * used.
  157. *
  158. * @return the pointer to the engine context.
  159. */
  160. struct jerry_context_t *jerry_port_get_current_context(void)
  161. {
  162. return jerry_default_context;
  163. }
  164. void jerry_port_sleep(uint32_t sleep_time)
  165. {
  166. rt_thread_delay(rt_tick_from_millisecond(sleep_time));
  167. }
  168. #ifdef JMEM_STATS
  169. void jmem_heap(void)
  170. {
  171. jmem_heap_stats_print();
  172. }
  173. MSH_CMD_EXPORT(jmem_heap, jerry mem heap stats print);
  174. #endif