builtinevex.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  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 "py/objfun.h"
  28. #include "py/compile.h"
  29. #include "py/runtime.h"
  30. #include "py/builtin.h"
  31. #if MICROPY_PY_BUILTINS_COMPILE
  32. typedef struct _mp_obj_code_t {
  33. mp_obj_base_t base;
  34. mp_obj_t module_fun;
  35. } mp_obj_code_t;
  36. static MP_DEFINE_CONST_OBJ_TYPE(
  37. mp_type_code,
  38. MP_QSTR_code,
  39. MP_TYPE_FLAG_NONE
  40. );
  41. static mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
  42. // save context
  43. nlr_jump_callback_node_globals_locals_t ctx;
  44. ctx.globals = mp_globals_get();
  45. ctx.locals = mp_locals_get();
  46. // set new context
  47. mp_globals_set(globals);
  48. mp_locals_set(locals);
  49. // set exception handler to restore context if an exception is raised
  50. nlr_push_jump_callback(&ctx.callback, mp_globals_locals_set_from_nlr_jump_callback);
  51. // The call to mp_parse_compile_execute() in mp_builtin_compile() below passes
  52. // NULL for the globals, so repopulate that entry now with the correct globals.
  53. if (mp_obj_is_type(self->module_fun, &mp_type_fun_bc)
  54. #if MICROPY_EMIT_NATIVE
  55. || mp_obj_is_type(self->module_fun, &mp_type_fun_native)
  56. #endif
  57. ) {
  58. mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
  59. ((mp_module_context_t *)fun_bc->context)->module.globals = globals;
  60. }
  61. // execute code
  62. mp_obj_t ret = mp_call_function_0(self->module_fun);
  63. // deregister exception handler and restore context
  64. nlr_pop_jump_callback(true);
  65. // return value
  66. return ret;
  67. }
  68. static mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
  69. (void)n_args;
  70. // get the source
  71. size_t str_len;
  72. const char *str = mp_obj_str_get_data(args[0], &str_len);
  73. // get the filename
  74. qstr filename = mp_obj_str_get_qstr(args[1]);
  75. // create the lexer
  76. mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
  77. // get the compile mode
  78. qstr mode = mp_obj_str_get_qstr(args[2]);
  79. mp_parse_input_kind_t parse_input_kind;
  80. switch (mode) {
  81. case MP_QSTR_single:
  82. parse_input_kind = MP_PARSE_SINGLE_INPUT;
  83. break;
  84. case MP_QSTR_exec:
  85. parse_input_kind = MP_PARSE_FILE_INPUT;
  86. break;
  87. case MP_QSTR_eval:
  88. parse_input_kind = MP_PARSE_EVAL_INPUT;
  89. break;
  90. default:
  91. mp_raise_ValueError(MP_ERROR_TEXT("bad compile mode"));
  92. }
  93. mp_obj_code_t *code = mp_obj_malloc(mp_obj_code_t, &mp_type_code);
  94. code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
  95. return MP_OBJ_FROM_PTR(code);
  96. }
  97. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
  98. #endif // MICROPY_PY_BUILTINS_COMPILE
  99. #if MICROPY_PY_BUILTINS_EVAL_EXEC
  100. static mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
  101. // work out the context
  102. mp_obj_dict_t *globals = mp_globals_get();
  103. mp_obj_dict_t *locals = mp_locals_get();
  104. for (size_t i = 1; i < 3 && i < n_args; ++i) {
  105. if (args[i] != mp_const_none) {
  106. if (!mp_obj_is_type(args[i], &mp_type_dict)) {
  107. mp_raise_TypeError(NULL);
  108. }
  109. locals = MP_OBJ_TO_PTR(args[i]);
  110. if (i == 1) {
  111. globals = locals;
  112. }
  113. }
  114. }
  115. #if MICROPY_PY_BUILTINS_COMPILE
  116. if (mp_obj_is_type(args[0], &mp_type_code)) {
  117. return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
  118. }
  119. #endif
  120. // create the lexer
  121. // MP_PARSE_SINGLE_INPUT is used to indicate a file input
  122. mp_lexer_t *lex;
  123. if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
  124. lex = mp_lexer_new_from_file(mp_obj_str_get_qstr(args[0]));
  125. parse_input_kind = MP_PARSE_FILE_INPUT;
  126. } else {
  127. // Extract the source code.
  128. mp_buffer_info_t bufinfo;
  129. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  130. lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, bufinfo.buf, bufinfo.len, 0);
  131. }
  132. return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
  133. }
  134. static mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
  135. return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
  136. }
  137. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
  138. static mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
  139. return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
  140. }
  141. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
  142. #endif // MICROPY_PY_BUILTINS_EVAL_EXEC
  143. #if MICROPY_PY_BUILTINS_EXECFILE
  144. static mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
  145. // MP_PARSE_SINGLE_INPUT is used to indicate a file input
  146. return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
  147. }
  148. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
  149. #endif