blob: bb15fd4d0f33967054ebea9f205df50433b35a81 [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
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030029#include "mpconfig.h"
Damien Georged02c6d82014-01-15 22:14:03 +000030#include "nlr.h"
31#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000032#include "qstr.h"
Damien Georged02c6d82014-01-15 22:14:03 +000033#include "lexer.h"
34#include "lexerunix.h"
35#include "parse.h"
36#include "obj.h"
Damien Georgec9fc6202014-10-25 21:59:14 +010037#include "objfun.h"
Damien Georgec5966122014-02-15 16:10:44 +000038#include "parsehelper.h"
Damien Georged02c6d82014-01-15 22:14:03 +000039#include "compile.h"
40#include "runtime0.h"
41#include "runtime.h"
Damien Georged02c6d82014-01-15 22:14:03 +000042#include "builtin.h"
43
Damien Georgec9fc6202014-10-25 21:59:14 +010044#if MICROPY_PY_BUILTINS_COMPILE
45
46typedef struct _mp_obj_code_t {
47 mp_obj_base_t base;
48 mp_obj_t module_fun;
49} mp_obj_code_t;
50
51STATIC const mp_obj_type_t mp_type_code = {
52 { &mp_type_type },
53 .name = MP_QSTR_code,
54};
55
56STATIC 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
86STATIC mp_obj_t mp_builtin_compile(mp_uint_t n_args, const mp_obj_t *args) {
87 // get the source
Damien Georged182b982014-08-30 14:19:41 +010088 mp_uint_t str_len;
Damien Georgec4d08682014-10-05 20:13:34 +010089 const char *str = mp_obj_str_get_data(args[0], &str_len);
Damien Georged02c6d82014-01-15 22:14:03 +000090
Damien Georgec9fc6202014-10-25 21:59:14 +010091 // get the filename
92 qstr filename = mp_obj_str_get_qstr(args[1]);
Damien Georged02c6d82014-01-15 22:14:03 +000093
Damien Georgec9fc6202014-10-25 21:59:14 +010094 // 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}
113MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
114
115#endif // MICROPY_PY_BUILTINS_COMPILE
116
117STATIC 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 +0100118 // work out the context
119 mp_obj_dict_t *globals = mp_globals_get();
120 mp_obj_dict_t *locals = mp_locals_get();
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200121 if (n_args > 1) {
Damien Georgec4d08682014-10-05 20:13:34 +0100122 globals = args[1];
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200123 if (n_args > 2) {
124 locals = args[2];
125 } else {
126 locals = globals;
127 }
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200128 }
Damien Georgec4d08682014-10-05 20:13:34 +0100129
Damien Georgec9fc6202014-10-25 21:59:14 +0100130 #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 George2a3e2b92014-12-19 13:36:17 +0000140 // 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 Georgec9fc6202014-10-25 21:59:14 +0100148
Damien Georgec4d08682014-10-05 20:13:34 +0100149 return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
Damien Georgeca476792014-02-03 22:44:10 +0000150}
151
Damien Georgec4d08682014-10-05 20:13:34 +0100152STATIC 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}
155MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
156
157STATIC 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 Sokolovsky2aa217b2014-02-13 00:36:54 +0200160MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
Damien George2a3e2b92014-12-19 13:36:17 +0000161
162#if MICROPY_PY_BUILTINS_EXECFILE
163STATIC 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}
167MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
168#endif