mpy_main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Armink (armink.ztl@gmail.com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <rtthread.h>
  30. #ifdef RT_USING_DFS
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <dfs.h>
  34. #endif
  35. #include <py/compile.h>
  36. #include <py/runtime.h>
  37. #include <py/repl.h>
  38. #include <py/gc.h>
  39. #include <py/mperrno.h>
  40. #include <py/stackctrl.h>
  41. #include <py/frozenmod.h>
  42. #include <lib/mp-readline/readline.h>
  43. #include <lib/utils/pyexec.h>
  44. #include "mpgetcharport.h"
  45. #include "mpputsnport.h"
  46. #define THREAD_STACK_NO_SYNC 4096
  47. #define THREAD_STACK_WITH_SYNC 8192
  48. #if MICROPY_ENABLE_COMPILER
  49. void do_str(const char *src, mp_parse_input_kind_t input_kind) {
  50. nlr_buf_t nlr;
  51. if (nlr_push(&nlr) == 0) {
  52. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  53. qstr source_name = lex->source_name;
  54. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  55. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, true);
  56. mp_call_function_0(module_fun);
  57. nlr_pop();
  58. } else {
  59. // uncaught exception
  60. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  61. }
  62. }
  63. #endif
  64. #ifdef RT_USING_DFS
  65. static int mp_sys_resource_bak(struct dfs_fdtable **table_bak)
  66. {
  67. struct dfs_fdtable *fd_table;
  68. struct dfs_fdtable *fd_table_bak;
  69. struct dfs_fd **fds;
  70. fd_table = dfs_fdtable_get();
  71. if (!fd_table)
  72. {
  73. return RT_FALSE;
  74. }
  75. fd_table_bak = (struct dfs_fdtable *)rt_malloc(sizeof(struct dfs_fdtable));
  76. if (!fd_table_bak)
  77. {
  78. goto _exit_tab;
  79. }
  80. fds = (struct dfs_fd **)rt_malloc((int)fd_table->maxfd * sizeof(struct dfs_fd *));
  81. if (!fds)
  82. {
  83. goto _exit_fds;
  84. }
  85. else
  86. {
  87. rt_memcpy(fds, fd_table->fds, (int)fd_table->maxfd * sizeof(struct dfs_fd *));
  88. fd_table_bak->maxfd = (int)fd_table->maxfd;
  89. fd_table_bak->fds = fds;
  90. }
  91. *table_bak = fd_table_bak;
  92. return RT_TRUE;
  93. _exit_fds:
  94. rt_free(fd_table_bak);
  95. _exit_tab:
  96. return RT_FALSE;
  97. }
  98. static void mp_sys_resource_gc(struct dfs_fdtable *fd_table_bak)
  99. {
  100. struct dfs_fdtable *fd_table;
  101. if (!fd_table_bak) return;
  102. fd_table = dfs_fdtable_get();
  103. for(int i = 0; i < fd_table->maxfd; i++)
  104. {
  105. if (fd_table->fds[i] != RT_NULL)
  106. {
  107. if ((i < fd_table_bak->maxfd && fd_table_bak->fds[i] == RT_NULL) || (i >= fd_table_bak->maxfd))
  108. {
  109. close(i + DFS_FD_OFFSET);
  110. }
  111. }
  112. }
  113. rt_free(fd_table_bak->fds);
  114. rt_free(fd_table_bak);
  115. }
  116. #endif
  117. static void *stack_top = RT_NULL;
  118. static char *heap = RT_NULL;
  119. void mpy_main(const char *filename) {
  120. int stack_dummy;
  121. int stack_size_check;
  122. stack_top = (void *)&stack_dummy;
  123. #ifdef RT_USING_DFS
  124. struct dfs_fdtable *fd_table_bak = NULL;
  125. mp_sys_resource_bak(&fd_table_bak);
  126. #endif
  127. mp_getchar_init();
  128. mp_putsn_init();
  129. #if defined(MICROPYTHON_USING_FILE_SYNC_VIA_IDE)
  130. stack_size_check = THREAD_STACK_WITH_SYNC;
  131. #else
  132. stack_size_check = THREAD_STACK_NO_SYNC;
  133. #endif
  134. if (rt_thread_self()->stack_size < stack_size_check)
  135. {
  136. #if (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 0, 1))
  137. mp_printf(&mp_plat_print, "The stack (%.*s) size for executing MicroPython must be >= %d\n", RT_NAME_MAX, rt_thread_self()->name, stack_size_check);
  138. #else
  139. mp_printf(&mp_plat_print, "The stack (%.*s) size for executing MicroPython must be >= %d\n", RT_NAME_MAX, rt_thread_self()->parent.name, stack_size_check);
  140. #endif
  141. }
  142. #if MICROPY_PY_THREAD
  143. mp_thread_init(rt_thread_self()->stack_addr, ((rt_uint32_t)stack_top - (rt_uint32_t)rt_thread_self()->stack_addr) / 4);
  144. #endif
  145. mp_stack_set_top(stack_top);
  146. // Make MicroPython's stack limit somewhat smaller than full stack available
  147. mp_stack_set_limit(rt_thread_self()->stack_size - 1024);
  148. #if MICROPY_ENABLE_GC
  149. heap = rt_malloc(MICROPY_HEAP_SIZE);
  150. if (!heap) {
  151. mp_printf(&mp_plat_print, "No memory for MicroPython Heap!\n");
  152. return;
  153. }
  154. gc_init(heap, heap + MICROPY_HEAP_SIZE);
  155. #endif
  156. /* MicroPython initialization */
  157. mp_init();
  158. /* system path initialization */
  159. mp_obj_list_init(mp_sys_path, 0);
  160. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  161. mp_obj_list_append(mp_sys_path, mp_obj_new_str(MICROPY_PY_PATH_FIRST, strlen(MICROPY_PY_PATH_FIRST)));
  162. mp_obj_list_append(mp_sys_path, mp_obj_new_str(MICROPY_PY_PATH_SECOND, strlen(MICROPY_PY_PATH_SECOND)));
  163. mp_obj_list_init(mp_sys_argv, 0);
  164. readline_init0();
  165. if (filename) {
  166. #ifndef MICROPYTHON_USING_UOS
  167. mp_printf(&mp_plat_print, "Please enable uos module in sys module option first.\n");
  168. #else
  169. pyexec_file(filename);
  170. #endif
  171. } else {
  172. #ifdef MICROPYTHON_USING_UOS
  173. // run boot-up scripts
  174. void *frozen_data;
  175. const char *_boot_file = "_boot.py", *boot_file = "boot.py", *main_file = MICROPY_MAIN_PY_PATH;
  176. if (mp_find_frozen_module(_boot_file, strlen(_boot_file), &frozen_data) != MP_FROZEN_NONE) {
  177. pyexec_frozen_module(_boot_file);
  178. }
  179. if (!access(boot_file, 0)) {
  180. pyexec_file(boot_file);
  181. }
  182. // run main scripts
  183. if (!access(main_file, 0)) {
  184. if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
  185. pyexec_file(main_file);
  186. }
  187. }
  188. #endif /* MICROPYTHON_USING_UOS */
  189. mp_printf(&mp_plat_print, "\n");
  190. for (;;) {
  191. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  192. if (pyexec_raw_repl() != 0) {
  193. break;
  194. }
  195. } else {
  196. if (pyexec_friendly_repl() != 0) {
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. gc_sweep_all();
  203. mp_deinit();
  204. #if MICROPY_PY_THREAD
  205. mp_thread_deinit();
  206. #endif
  207. rt_free(heap);
  208. mp_putsn_deinit();
  209. mp_getchar_deinit();
  210. #ifdef RT_USING_DFS
  211. mp_sys_resource_gc(fd_table_bak);
  212. #endif
  213. }
  214. #if !MICROPY_PY_MODUOS_FILE
  215. mp_import_stat_t mp_import_stat(const char *path) {
  216. return MP_IMPORT_STAT_NO_EXIST;
  217. }
  218. #endif
  219. NORETURN void nlr_jump_fail(void *val) {
  220. mp_printf(MICROPY_ERROR_PRINTER, "nlr_jump_fail\n");
  221. while (1);
  222. }
  223. #ifndef NDEBUG
  224. NORETURN void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  225. mp_printf(MICROPY_ERROR_PRINTER, "Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  226. while (1);
  227. }
  228. #endif
  229. #include <stdarg.h>
  230. int DEBUG_printf(const char *format, ...)
  231. {
  232. static char log_buf[512];
  233. va_list args;
  234. /* args point to the first variable parameter */
  235. va_start(args, format);
  236. /* must use vprintf to print */
  237. rt_vsprintf(log_buf, format, args);
  238. mp_printf(&mp_plat_print, "%s", log_buf);
  239. va_end(args);
  240. return 0;
  241. }
  242. #ifndef MICROPYTHON_USING_UOS
  243. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  244. mp_raise_OSError(MP_ENOENT);
  245. }
  246. #endif
  247. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  248. #include <finsh.h>
  249. static void python(uint8_t argc, char **argv) {
  250. if (argc > 1) {
  251. mpy_main(argv[1]);
  252. } else {
  253. mpy_main(NULL);
  254. }
  255. }
  256. MSH_CMD_EXPORT(python, MicroPython: `python [file.py]` execute python script);
  257. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */