Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 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 | |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 27 | #include <stdint.h> |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 28 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 29 | #include "py/objfun.h" |
| 30 | #include "py/compile.h" |
| 31 | #include "py/runtime.h" |
| 32 | #include "py/builtin.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 33 | |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 34 | #if MICROPY_PY_BUILTINS_COMPILE |
| 35 | |
| 36 | typedef struct _mp_obj_code_t { |
| 37 | mp_obj_base_t base; |
| 38 | mp_obj_t module_fun; |
| 39 | } mp_obj_code_t; |
| 40 | |
| 41 | STATIC const mp_obj_type_t mp_type_code = { |
| 42 | { &mp_type_type }, |
| 43 | .name = MP_QSTR_code, |
| 44 | }; |
| 45 | |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 46 | STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) { |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 47 | // save context and set new context |
| 48 | mp_obj_dict_t *old_globals = mp_globals_get(); |
| 49 | mp_obj_dict_t *old_locals = mp_locals_get(); |
| 50 | mp_globals_set(globals); |
| 51 | mp_locals_set(locals); |
| 52 | |
| 53 | // a bit of a hack: fun_bc will re-set globals, so need to make sure it's |
| 54 | // the correct one |
| 55 | if (MP_OBJ_IS_TYPE(self->module_fun, &mp_type_fun_bc)) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 56 | mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun); |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 57 | fun_bc->globals = globals; |
| 58 | } |
| 59 | |
| 60 | // execute code |
| 61 | nlr_buf_t nlr; |
| 62 | if (nlr_push(&nlr) == 0) { |
| 63 | mp_obj_t ret = mp_call_function_0(self->module_fun); |
| 64 | nlr_pop(); |
| 65 | mp_globals_set(old_globals); |
| 66 | mp_locals_set(old_locals); |
| 67 | return ret; |
| 68 | } else { |
| 69 | // exception; restore context and re-raise same exception |
| 70 | mp_globals_set(old_globals); |
| 71 | mp_locals_set(old_locals); |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 72 | nlr_jump(nlr.ret_val); |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 76 | STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 77 | (void)n_args; |
| 78 | |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 79 | // get the source |
Damien George | 6b34107 | 2017-03-25 19:48:18 +1100 | [diff] [blame] | 80 | size_t str_len; |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 81 | const char *str = mp_obj_str_get_data(args[0], &str_len); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 82 | |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 83 | // get the filename |
| 84 | qstr filename = mp_obj_str_get_qstr(args[1]); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 85 | |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 86 | // create the lexer |
| 87 | mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0); |
| 88 | |
| 89 | // get the compile mode |
| 90 | qstr mode = mp_obj_str_get_qstr(args[2]); |
| 91 | mp_parse_input_kind_t parse_input_kind; |
| 92 | switch (mode) { |
| 93 | case MP_QSTR_single: parse_input_kind = MP_PARSE_SINGLE_INPUT; break; |
| 94 | case MP_QSTR_exec: parse_input_kind = MP_PARSE_FILE_INPUT; break; |
| 95 | case MP_QSTR_eval: parse_input_kind = MP_PARSE_EVAL_INPUT; break; |
| 96 | default: |
Damien George | 94c41bb | 2017-03-28 22:37:26 +1100 | [diff] [blame] | 97 | mp_raise_ValueError("bad compile mode"); |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | mp_obj_code_t *code = m_new_obj(mp_obj_code_t); |
| 101 | code->base.type = &mp_type_code; |
| 102 | code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL); |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 103 | return MP_OBJ_FROM_PTR(code); |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 104 | } |
| 105 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile); |
| 106 | |
| 107 | #endif // MICROPY_PY_BUILTINS_COMPILE |
| 108 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 109 | #if MICROPY_PY_BUILTINS_EVAL_EXEC |
| 110 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 111 | STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) { |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 112 | // work out the context |
| 113 | mp_obj_dict_t *globals = mp_globals_get(); |
| 114 | mp_obj_dict_t *locals = mp_locals_get(); |
Tom Collins | bb3bdda | 2017-07-11 15:27:42 -0700 | [diff] [blame] | 115 | for (size_t i = 1; i < 3 && i < n_args; ++i) { |
| 116 | if (args[i] != mp_const_none) { |
| 117 | if (!MP_OBJ_IS_TYPE(args[i], &mp_type_dict)) { |
| 118 | mp_raise_TypeError(NULL); |
| 119 | } |
| 120 | locals = MP_OBJ_TO_PTR(args[i]); |
| 121 | if (i == 1) { |
| 122 | globals = locals; |
| 123 | } |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 124 | } |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 125 | } |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 126 | |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 127 | #if MICROPY_PY_BUILTINS_COMPILE |
| 128 | if (MP_OBJ_IS_TYPE(args[0], &mp_type_code)) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 129 | return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals); |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 130 | } |
| 131 | #endif |
| 132 | |
Damien George | 6b34107 | 2017-03-25 19:48:18 +1100 | [diff] [blame] | 133 | size_t str_len; |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 134 | const char *str = mp_obj_str_get_data(args[0], &str_len); |
| 135 | |
| 136 | // create the lexer |
Damien George | 2a3e2b9 | 2014-12-19 13:36:17 +0000 | [diff] [blame] | 137 | // MP_PARSE_SINGLE_INPUT is used to indicate a file input |
| 138 | mp_lexer_t *lex; |
| 139 | if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) { |
| 140 | lex = mp_lexer_new_from_file(str); |
| 141 | parse_input_kind = MP_PARSE_FILE_INPUT; |
| 142 | } else { |
| 143 | lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0); |
| 144 | } |
Damien George | c9fc620 | 2014-10-25 21:59:14 +0100 | [diff] [blame] | 145 | |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 146 | return mp_parse_compile_execute(lex, parse_input_kind, globals, locals); |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 149 | STATIC mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) { |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 150 | return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT); |
| 151 | } |
| 152 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval); |
| 153 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 154 | STATIC mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) { |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 155 | return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT); |
| 156 | } |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 157 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec); |
Damien George | 2a3e2b9 | 2014-12-19 13:36:17 +0000 | [diff] [blame] | 158 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 159 | #endif // MICROPY_PY_BUILTINS_EVAL_EXEC |
| 160 | |
Damien George | 2a3e2b9 | 2014-12-19 13:36:17 +0000 | [diff] [blame] | 161 | #if MICROPY_PY_BUILTINS_EXECFILE |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 162 | STATIC mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) { |
Damien George | 2a3e2b9 | 2014-12-19 13:36:17 +0000 | [diff] [blame] | 163 | // MP_PARSE_SINGLE_INPUT is used to indicate a file input |
| 164 | return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT); |
| 165 | } |
| 166 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile); |
| 167 | #endif |