mpy_main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <dfs_posix.h>
  32. #endif
  33. #include <py/compile.h>
  34. #include <py/runtime.h>
  35. #include <py/repl.h>
  36. #include <py/gc.h>
  37. #include <py/mperrno.h>
  38. #include <py/stackctrl.h>
  39. #include <py/frozenmod.h>
  40. #include <lib/mp-readline/readline.h>
  41. #include <lib/utils/pyexec.h>
  42. #include "mpgetcharport.h"
  43. #include "mpputsnport.h"
  44. #if MICROPY_ENABLE_COMPILER
  45. void do_str(const char *src, mp_parse_input_kind_t input_kind) {
  46. nlr_buf_t nlr;
  47. if (nlr_push(&nlr) == 0) {
  48. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  49. qstr source_name = lex->source_name;
  50. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  51. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
  52. mp_call_function_0(module_fun);
  53. nlr_pop();
  54. } else {
  55. // uncaught exception
  56. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  57. }
  58. }
  59. #endif
  60. static void *stack_top = RT_NULL;
  61. static char *heap = RT_NULL;
  62. void mpy_main(const char *filename) {
  63. int stack_dummy;
  64. stack_top = (void *)&stack_dummy;
  65. mp_getchar_init();
  66. mp_putsn_init();
  67. if (rt_thread_self()->stack_size < 4096) {
  68. mp_printf(&mp_plat_print, "The stack (%.*s) size for executing MicroPython must be >=4096\n", RT_NAME_MAX, rt_thread_self()->name);
  69. }
  70. #if MICROPY_PY_THREAD
  71. mp_thread_init(rt_thread_self()->stack_addr, ((rt_uint32_t)stack_top - (rt_uint32_t)rt_thread_self()->stack_addr) / 4);
  72. #endif
  73. mp_stack_set_top(stack_top);
  74. // Make MicroPython's stack limit somewhat smaller than full stack available
  75. mp_stack_set_limit(rt_thread_self()->stack_size - 1024);
  76. #if MICROPY_ENABLE_GC
  77. heap = rt_malloc(MICROPY_HEAP_SIZE);
  78. if (!heap) {
  79. mp_printf(&mp_plat_print, "No memory for MicroPython Heap!\n");
  80. return;
  81. }
  82. gc_init(heap, heap + MICROPY_HEAP_SIZE);
  83. #endif
  84. /* MicroPython initialization */
  85. mp_init();
  86. /* system path initialization */
  87. mp_obj_list_init(mp_sys_path, 0);
  88. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  89. mp_obj_list_append(mp_sys_path, mp_obj_new_str(MICROPY_PY_PATH_FIRST, strlen(MICROPY_PY_PATH_FIRST)));
  90. mp_obj_list_append(mp_sys_path, mp_obj_new_str(MICROPY_PY_PATH_SECOND, strlen(MICROPY_PY_PATH_SECOND)));
  91. mp_obj_list_init(mp_sys_argv, 0);
  92. readline_init0();
  93. if (filename) {
  94. #ifndef MICROPYTHON_USING_UOS
  95. mp_printf(&mp_plat_print, "Please enable uos module in sys module option first.\n");
  96. #else
  97. pyexec_file(filename);
  98. #endif
  99. } else {
  100. #ifdef MICROPYTHON_USING_UOS
  101. // run boot-up scripts
  102. void *frozen_data;
  103. const char *_boot_file = "_boot.py", *boot_file = "boot.py", *main_file = MICROPY_MAIN_PY_PATH;
  104. if (mp_find_frozen_module(_boot_file, strlen(_boot_file), &frozen_data) != MP_FROZEN_NONE) {
  105. pyexec_frozen_module(_boot_file);
  106. }
  107. if (!access(boot_file, 0)) {
  108. pyexec_file(boot_file);
  109. }
  110. // run main scripts
  111. if (!access(main_file, 0)) {
  112. if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
  113. pyexec_file(main_file);
  114. }
  115. }
  116. #endif /* MICROPYTHON_USING_UOS */
  117. mp_printf(&mp_plat_print, "\n");
  118. for (;;) {
  119. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  120. if (pyexec_raw_repl() != 0) {
  121. break;
  122. }
  123. } else {
  124. if (pyexec_friendly_repl() != 0) {
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. gc_sweep_all();
  131. mp_deinit();
  132. #if MICROPY_PY_THREAD
  133. mp_thread_deinit();
  134. #endif
  135. rt_free(heap);
  136. mp_putsn_deinit();
  137. mp_getchar_deinit();
  138. }
  139. #if !MICROPY_PY_MODUOS_FILE
  140. mp_import_stat_t mp_import_stat(const char *path) {
  141. return MP_IMPORT_STAT_NO_EXIST;
  142. }
  143. #endif
  144. NORETURN void nlr_jump_fail(void *val) {
  145. mp_printf(MICROPY_ERROR_PRINTER, "nlr_jump_fail\n");
  146. while (1);
  147. }
  148. #ifndef NDEBUG
  149. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  150. mp_printf(MICROPY_ERROR_PRINTER, "Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  151. RT_ASSERT(0);
  152. }
  153. #endif
  154. #include <stdarg.h>
  155. int DEBUG_printf(const char *format, ...)
  156. {
  157. static char log_buf[512];
  158. va_list args;
  159. /* args point to the first variable parameter */
  160. va_start(args, format);
  161. /* must use vprintf to print */
  162. rt_vsprintf(log_buf, format, args);
  163. mp_printf(&mp_plat_print, "%s", log_buf);
  164. va_end(args);
  165. return 0;
  166. }
  167. #ifndef MICROPYTHON_USING_UOS
  168. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  169. mp_raise_OSError(MP_ENOENT);
  170. }
  171. #endif
  172. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  173. #include <finsh.h>
  174. static void python(uint8_t argc, char **argv) {
  175. if (argc > 1) {
  176. mpy_main(argv[1]);
  177. } else {
  178. mpy_main(NULL);
  179. }
  180. }
  181. MSH_CMD_EXPORT(python, MicroPython: `python [file.py]` execute python script);
  182. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */