builtinevex.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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:
  81. parse_input_kind = MP_PARSE_SINGLE_INPUT;
  82. break;
  83. case MP_QSTR_exec:
  84. parse_input_kind = MP_PARSE_FILE_INPUT;
  85. break;
  86. case MP_QSTR_eval:
  87. parse_input_kind = MP_PARSE_EVAL_INPUT;
  88. break;
  89. default:
  90. mp_raise_ValueError(MP_ERROR_TEXT("bad compile mode"));
  91. }
  92. mp_obj_code_t *code = m_new_obj(mp_obj_code_t);
  93. code->base.type = &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. // Extract the source code.
  121. mp_buffer_info_t bufinfo;
  122. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  123. // create the lexer
  124. // MP_PARSE_SINGLE_INPUT is used to indicate a file input
  125. mp_lexer_t *lex;
  126. if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
  127. lex = mp_lexer_new_from_file(bufinfo.buf);
  128. parse_input_kind = MP_PARSE_FILE_INPUT;
  129. } else {
  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