builtinevex.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // a bit of a hack: fun_bc will re-set globals, so need to make sure it's
  52. // the correct one
  53. if (mp_obj_is_type(self->module_fun, &mp_type_fun_bc)) {
  54. mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
  55. ((mp_module_context_t *)fun_bc->context)->module.globals = globals;
  56. }
  57. // execute code
  58. mp_obj_t ret = mp_call_function_0(self->module_fun);
  59. // deregister exception handler and restore context
  60. nlr_pop_jump_callback(true);
  61. // return value
  62. return ret;
  63. }
  64. STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
  65. (void)n_args;
  66. // get the source
  67. size_t str_len;
  68. const char *str = mp_obj_str_get_data(args[0], &str_len);
  69. // get the filename
  70. qstr filename = mp_obj_str_get_qstr(args[1]);
  71. // create the lexer
  72. mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
  73. // get the compile mode
  74. qstr mode = mp_obj_str_get_qstr(args[2]);
  75. mp_parse_input_kind_t parse_input_kind;
  76. switch (mode) {
  77. case MP_QSTR_single:
  78. parse_input_kind = MP_PARSE_SINGLE_INPUT;
  79. break;
  80. case MP_QSTR_exec:
  81. parse_input_kind = MP_PARSE_FILE_INPUT;
  82. break;
  83. case MP_QSTR_eval:
  84. parse_input_kind = MP_PARSE_EVAL_INPUT;
  85. break;
  86. default:
  87. mp_raise_ValueError(MP_ERROR_TEXT("bad compile mode"));
  88. }
  89. mp_obj_code_t *code = mp_obj_malloc(mp_obj_code_t, &mp_type_code);
  90. code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
  91. return MP_OBJ_FROM_PTR(code);
  92. }
  93. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
  94. #endif // MICROPY_PY_BUILTINS_COMPILE
  95. #if MICROPY_PY_BUILTINS_EVAL_EXEC
  96. STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
  97. // work out the context
  98. mp_obj_dict_t *globals = mp_globals_get();
  99. mp_obj_dict_t *locals = mp_locals_get();
  100. for (size_t i = 1; i < 3 && i < n_args; ++i) {
  101. if (args[i] != mp_const_none) {
  102. if (!mp_obj_is_type(args[i], &mp_type_dict)) {
  103. mp_raise_TypeError(NULL);
  104. }
  105. locals = MP_OBJ_TO_PTR(args[i]);
  106. if (i == 1) {
  107. globals = locals;
  108. }
  109. }
  110. }
  111. #if MICROPY_PY_BUILTINS_COMPILE
  112. if (mp_obj_is_type(args[0], &mp_type_code)) {
  113. return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
  114. }
  115. #endif
  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(mp_obj_str_get_qstr(args[0]));
  121. parse_input_kind = MP_PARSE_FILE_INPUT;
  122. } else {
  123. // Extract the source code.
  124. mp_buffer_info_t bufinfo;
  125. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  126. lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, bufinfo.buf, bufinfo.len, 0);
  127. }
  128. return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
  129. }
  130. STATIC mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
  131. return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
  132. }
  133. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
  134. STATIC mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
  135. return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
  136. }
  137. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
  138. #endif // MICROPY_PY_BUILTINS_EVAL_EXEC
  139. #if MICROPY_PY_BUILTINS_EXECFILE
  140. STATIC mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
  141. // MP_PARSE_SINGLE_INPUT is used to indicate a file input
  142. return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
  143. }
  144. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
  145. #endif