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