blob: c14869e350a3a0bb9b7965fa75d6c1062154db63 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 Georged02c6d82014-01-15 22:14:03 +000027#include <stdint.h>
Damien Georged02c6d82014-01-15 22:14:03 +000028
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/nlr.h"
30#include "py/objfun.h"
31#include "py/compile.h"
32#include "py/runtime.h"
33#include "py/builtin.h"
Damien Georged02c6d82014-01-15 22:14:03 +000034
Damien Georgec9fc6202014-10-25 21:59:14 +010035#if MICROPY_PY_BUILTINS_COMPILE
36
37typedef struct _mp_obj_code_t {
38 mp_obj_base_t base;
39 mp_obj_t module_fun;
40} mp_obj_code_t;
41
42STATIC const mp_obj_type_t mp_type_code = {
43 { &mp_type_type },
44 .name = MP_QSTR_code,
45};
46
Damien George999cedb2015-11-27 17:01:44 +000047STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
Damien Georgec9fc6202014-10-25 21:59:14 +010048 // save context and set new context
49 mp_obj_dict_t *old_globals = mp_globals_get();
50 mp_obj_dict_t *old_locals = mp_locals_get();
51 mp_globals_set(globals);
52 mp_locals_set(locals);
53
54 // a bit of a hack: fun_bc will re-set globals, so need to make sure it's
55 // the correct one
56 if (MP_OBJ_IS_TYPE(self->module_fun, &mp_type_fun_bc)) {
Damien George999cedb2015-11-27 17:01:44 +000057 mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
Damien Georgec9fc6202014-10-25 21:59:14 +010058 fun_bc->globals = globals;
59 }
60
61 // execute code
62 nlr_buf_t nlr;
63 if (nlr_push(&nlr) == 0) {
64 mp_obj_t ret = mp_call_function_0(self->module_fun);
65 nlr_pop();
66 mp_globals_set(old_globals);
67 mp_locals_set(old_locals);
68 return ret;
69 } else {
70 // exception; restore context and re-raise same exception
71 mp_globals_set(old_globals);
72 mp_locals_set(old_locals);
Damien George999cedb2015-11-27 17:01:44 +000073 nlr_jump(nlr.ret_val);
Damien Georgec9fc6202014-10-25 21:59:14 +010074 }
75}
76
77STATIC mp_obj_t mp_builtin_compile(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000078 (void)n_args;
79
Damien Georgec9fc6202014-10-25 21:59:14 +010080 // get the source
Damien Georged182b982014-08-30 14:19:41 +010081 mp_uint_t str_len;
Damien Georgec4d08682014-10-05 20:13:34 +010082 const char *str = mp_obj_str_get_data(args[0], &str_len);
Damien Georged02c6d82014-01-15 22:14:03 +000083
Damien Georgec9fc6202014-10-25 21:59:14 +010084 // get the filename
85 qstr filename = mp_obj_str_get_qstr(args[1]);
Damien Georged02c6d82014-01-15 22:14:03 +000086
Damien Georgec9fc6202014-10-25 21:59:14 +010087 // create the lexer
88 mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
89
90 // get the compile mode
91 qstr mode = mp_obj_str_get_qstr(args[2]);
92 mp_parse_input_kind_t parse_input_kind;
93 switch (mode) {
94 case MP_QSTR_single: parse_input_kind = MP_PARSE_SINGLE_INPUT; break;
95 case MP_QSTR_exec: parse_input_kind = MP_PARSE_FILE_INPUT; break;
96 case MP_QSTR_eval: parse_input_kind = MP_PARSE_EVAL_INPUT; break;
97 default:
98 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad compile mode"));
99 }
100
101 mp_obj_code_t *code = m_new_obj(mp_obj_code_t);
102 code->base.type = &mp_type_code;
103 code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
Damien George999cedb2015-11-27 17:01:44 +0000104 return MP_OBJ_FROM_PTR(code);
Damien Georgec9fc6202014-10-25 21:59:14 +0100105}
106MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
107
108#endif // MICROPY_PY_BUILTINS_COMPILE
109
110STATIC 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 Georgec4d08682014-10-05 20:13:34 +0100111 // work out the context
112 mp_obj_dict_t *globals = mp_globals_get();
113 mp_obj_dict_t *locals = mp_locals_get();
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200114 if (n_args > 1) {
Damien George999cedb2015-11-27 17:01:44 +0000115 globals = MP_OBJ_TO_PTR(args[1]);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200116 if (n_args > 2) {
Damien George999cedb2015-11-27 17:01:44 +0000117 locals = MP_OBJ_TO_PTR(args[2]);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200118 } else {
119 locals = globals;
120 }
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200121 }
Damien Georgec4d08682014-10-05 20:13:34 +0100122
Damien Georgec9fc6202014-10-25 21:59:14 +0100123 #if MICROPY_PY_BUILTINS_COMPILE
124 if (MP_OBJ_IS_TYPE(args[0], &mp_type_code)) {
Damien George999cedb2015-11-27 17:01:44 +0000125 return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
Damien Georgec9fc6202014-10-25 21:59:14 +0100126 }
127 #endif
128
129 mp_uint_t str_len;
130 const char *str = mp_obj_str_get_data(args[0], &str_len);
131
132 // create the lexer
Damien George2a3e2b92014-12-19 13:36:17 +0000133 // MP_PARSE_SINGLE_INPUT is used to indicate a file input
134 mp_lexer_t *lex;
135 if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
136 lex = mp_lexer_new_from_file(str);
Damien George08689422015-02-15 00:02:27 +0000137 if (lex == NULL) {
138 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "could not open file '%s'", str));
139 }
Damien George2a3e2b92014-12-19 13:36:17 +0000140 parse_input_kind = MP_PARSE_FILE_INPUT;
141 } else {
142 lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
143 }
Damien Georgec9fc6202014-10-25 21:59:14 +0100144
Damien Georgec4d08682014-10-05 20:13:34 +0100145 return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
Damien Georgeca476792014-02-03 22:44:10 +0000146}
147
Damien Georgec4d08682014-10-05 20:13:34 +0100148STATIC mp_obj_t mp_builtin_eval(mp_uint_t n_args, const mp_obj_t *args) {
149 return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
150}
151MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
152
153STATIC mp_obj_t mp_builtin_exec(mp_uint_t n_args, const mp_obj_t *args) {
154 return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
155}
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200156MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
Damien George2a3e2b92014-12-19 13:36:17 +0000157
158#if MICROPY_PY_BUILTINS_EXECFILE
159STATIC mp_obj_t mp_builtin_execfile(mp_uint_t n_args, const mp_obj_t *args) {
160 // MP_PARSE_SINGLE_INPUT is used to indicate a file input
161 return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
162}
163MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
164#endif