2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-15 17:14:03 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/objfun.h"
|
|
|
|
#include "py/compile.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/builtin.h"
|
2014-01-15 17:14:03 -05:00
|
|
|
|
2018-08-08 21:24:49 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2014-10-25 16:59:14 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPILE
|
|
|
|
|
|
|
|
typedef struct _mp_obj_code_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t module_fun;
|
|
|
|
} mp_obj_code_t;
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_code = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_code,
|
|
|
|
};
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
|
2014-10-25 16:59:14 -04:00
|
|
|
// save context and set new context
|
|
|
|
mp_obj_dict_t *old_globals = mp_globals_get();
|
|
|
|
mp_obj_dict_t *old_locals = mp_locals_get();
|
|
|
|
mp_globals_set(globals);
|
|
|
|
mp_locals_set(locals);
|
|
|
|
|
|
|
|
// a bit of a hack: fun_bc will re-set globals, so need to make sure it's
|
|
|
|
// the correct one
|
|
|
|
if (MP_OBJ_IS_TYPE(self->module_fun, &mp_type_fun_bc)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
|
2014-10-25 16:59:14 -04:00
|
|
|
fun_bc->globals = globals;
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute code
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_obj_t ret = mp_call_function_0(self->module_fun);
|
|
|
|
nlr_pop();
|
|
|
|
mp_globals_set(old_globals);
|
|
|
|
mp_locals_set(old_locals);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
// exception; restore context and re-raise same exception
|
|
|
|
mp_globals_set(old_globals);
|
|
|
|
mp_locals_set(old_locals);
|
2015-11-27 12:01:44 -05:00
|
|
|
nlr_jump(nlr.ret_val);
|
2014-10-25 16:59:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args;
|
|
|
|
|
2014-10-25 16:59:14 -04:00
|
|
|
// get the source
|
2017-03-25 04:48:18 -04:00
|
|
|
size_t str_len;
|
2014-10-05 15:13:34 -04:00
|
|
|
const char *str = mp_obj_str_get_data(args[0], &str_len);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
2014-10-25 16:59:14 -04:00
|
|
|
// get the filename
|
|
|
|
qstr filename = mp_obj_str_get_qstr(args[1]);
|
|
|
|
|
2014-01-15 17:14:03 -05:00
|
|
|
// create the lexer
|
2014-10-25 16:59:14 -04:00
|
|
|
mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
|
|
|
|
|
|
|
|
// get the compile mode
|
|
|
|
qstr mode = mp_obj_str_get_qstr(args[2]);
|
|
|
|
mp_parse_input_kind_t parse_input_kind;
|
|
|
|
switch (mode) {
|
|
|
|
case MP_QSTR_single: parse_input_kind = MP_PARSE_SINGLE_INPUT; break;
|
|
|
|
case MP_QSTR_exec: parse_input_kind = MP_PARSE_FILE_INPUT; break;
|
|
|
|
case MP_QSTR_eval: parse_input_kind = MP_PARSE_EVAL_INPUT; break;
|
|
|
|
default:
|
2018-08-08 21:24:49 -04:00
|
|
|
mp_raise_ValueError(translate("bad compile mode"));
|
2014-10-25 16:59:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_code_t *code = m_new_obj(mp_obj_code_t);
|
|
|
|
code->base.type = &mp_type_code;
|
|
|
|
code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(code);
|
2014-10-25 16:59:14 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_BUILTINS_COMPILE
|
2014-01-15 17:14:03 -05:00
|
|
|
|
2015-12-18 07:35:44 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_EVAL_EXEC
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
|
2014-10-05 15:13:34 -04:00
|
|
|
// work out the context
|
|
|
|
mp_obj_dict_t *globals = mp_globals_get();
|
|
|
|
mp_obj_dict_t *locals = mp_locals_get();
|
2017-07-11 18:27:42 -04:00
|
|
|
for (size_t i = 1; i < 3 && i < n_args; ++i) {
|
|
|
|
if (args[i] != mp_const_none) {
|
|
|
|
if (!MP_OBJ_IS_TYPE(args[i], &mp_type_dict)) {
|
|
|
|
mp_raise_TypeError(NULL);
|
|
|
|
}
|
|
|
|
locals = MP_OBJ_TO_PTR(args[i]);
|
|
|
|
if (i == 1) {
|
|
|
|
globals = locals;
|
|
|
|
}
|
2014-02-12 17:36:54 -05:00
|
|
|
}
|
|
|
|
}
|
2014-10-05 15:13:34 -04:00
|
|
|
|
2014-10-25 16:59:14 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPILE
|
|
|
|
if (MP_OBJ_IS_TYPE(args[0], &mp_type_code)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
|
2014-10-25 16:59:14 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-25 04:48:18 -04:00
|
|
|
size_t str_len;
|
2014-10-25 16:59:14 -04:00
|
|
|
const char *str = mp_obj_str_get_data(args[0], &str_len);
|
|
|
|
|
|
|
|
// create the lexer
|
2014-12-19 08:36:17 -05:00
|
|
|
// MP_PARSE_SINGLE_INPUT is used to indicate a file input
|
|
|
|
mp_lexer_t *lex;
|
|
|
|
if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
|
|
|
|
lex = mp_lexer_new_from_file(str);
|
|
|
|
parse_input_kind = MP_PARSE_FILE_INPUT;
|
|
|
|
} else {
|
|
|
|
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
|
|
|
|
}
|
2014-10-25 16:59:14 -04:00
|
|
|
|
2014-10-05 15:13:34 -04:00
|
|
|
return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
|
|
|
|
}
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
|
2014-10-05 15:13:34 -04:00
|
|
|
return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
|
2014-02-03 17:44:10 -05:00
|
|
|
}
|
2014-10-05 15:13:34 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
|
2014-02-03 17:44:10 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
|
2014-10-05 15:13:34 -04:00
|
|
|
return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
|
|
|
|
}
|
2014-02-12 17:36:54 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
|
2014-12-19 08:36:17 -05:00
|
|
|
|
2015-12-18 07:35:44 -05:00
|
|
|
#endif // MICROPY_PY_BUILTINS_EVAL_EXEC
|
|
|
|
|
2014-12-19 08:36:17 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_EXECFILE
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
|
2014-12-19 08:36:17 -05:00
|
|
|
// MP_PARSE_SINGLE_INPUT is used to indicate a file input
|
|
|
|
return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
|
|
|
|
#endif
|