builtinevex.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 const mp_obj_type_t mp_type_code = {
  37. { &mp_type_type },
  38. .name = MP_QSTR_code,
  39. };
  40. STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
  41. // save context and set new context
  42. mp_obj_dict_t *old_globals = mp_globals_get();
  43. mp_obj_dict_t *old_locals = mp_locals_get();
  44. mp_globals_set(globals);
  45. mp_locals_set(locals);
  46. // a bit of a hack: fun_bc will re-set globals, so need to make sure it's
  47. // the correct one
  48. if (MP_OBJ_IS_TYPE(self->module_fun, &mp_type_fun_bc)) {
  49. mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
  50. fun_bc->globals = globals;
  51. }
  52. // execute code
  53. nlr_buf_t nlr;
  54. if (nlr_push(&nlr) == 0) {
  55. mp_obj_t ret = mp_call_function_0(self->module_fun);
  56. nlr_pop();
  57. mp_globals_set(old_globals);
  58. mp_locals_set(old_locals);
  59. return ret;
  60. } else {
  61. // exception; restore context and re-raise same exception
  62. mp_globals_set(old_globals);
  63. mp_locals_set(old_locals);
  64. nlr_jump(nlr.ret_val);
  65. }
  66. }
  67. STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
  68. (void)n_args;
  69. // get the source
  70. size_t str_len;
  71. const char *str = mp_obj_str_get_data(args[0], &str_len);
  72. // get the filename
  73. qstr filename = mp_obj_str_get_qstr(args[1]);
  74. // create the lexer
  75. mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
  76. // get the compile mode
  77. qstr mode = mp_obj_str_get_qstr(args[2]);
  78. mp_parse_input_kind_t parse_input_kind;
  79. switch (mode) {
  80. case MP_QSTR_single: parse_input_kind = MP_PARSE_SINGLE_INPUT; break;
  81. case MP_QSTR_exec: parse_input_kind = MP_PARSE_FILE_INPUT; break;
  82. case MP_QSTR_eval: parse_input_kind = MP_PARSE_EVAL_INPUT; break;
  83. default:
  84. mp_raise_ValueError("bad compile mode");
  85. }
  86. mp_obj_code_t *code = m_new_obj(mp_obj_code_t);
  87. code->base.type = &mp_type_code;
  88. code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
  89. return MP_OBJ_FROM_PTR(code);
  90. }
  91. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
  92. #endif // MICROPY_PY_BUILTINS_COMPILE
  93. #if MICROPY_PY_BUILTINS_EVAL_EXEC
  94. STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
  95. // work out the context
  96. mp_obj_dict_t *globals = mp_globals_get();
  97. mp_obj_dict_t *locals = mp_locals_get();
  98. for (size_t i = 1; i < 3 && i < n_args; ++i) {
  99. if (args[i] != mp_const_none) {
  100. if (!MP_OBJ_IS_TYPE(args[i], &mp_type_dict)) {
  101. mp_raise_TypeError(NULL);
  102. }
  103. locals = MP_OBJ_TO_PTR(args[i]);
  104. if (i == 1) {
  105. globals = locals;
  106. }
  107. }
  108. }
  109. #if MICROPY_PY_BUILTINS_COMPILE
  110. if (MP_OBJ_IS_TYPE(args[0], &mp_type_code)) {
  111. return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
  112. }
  113. #endif
  114. size_t str_len;
  115. const char *str = mp_obj_str_get_data(args[0], &str_len);
  116. // create the lexer
  117. // MP_PARSE_SINGLE_INPUT is used to indicate a file input
  118. mp_lexer_t *lex;
  119. if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
  120. lex = mp_lexer_new_from_file(str);
  121. parse_input_kind = MP_PARSE_FILE_INPUT;
  122. } else {
  123. lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
  124. }
  125. return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
  126. }
  127. STATIC mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
  128. return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
  129. }
  130. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
  131. STATIC mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
  132. return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
  133. }
  134. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
  135. #endif // MICROPY_PY_BUILTINS_EVAL_EXEC
  136. #if MICROPY_PY_BUILTINS_EXECFILE
  137. STATIC mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
  138. // MP_PARSE_SINGLE_INPUT is used to indicate a file input
  139. return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
  140. }
  141. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
  142. #endif