summaryrefslogtreecommitdiffstatshomepage
path: root/lib/utils/pyexec.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-01-31 22:24:16 +0000
committerDamien George <damien.p.george@gmail.com>2016-04-13 16:07:47 +0100
commit0a2e9650f5383bc1190d6b27a3d923e313c3d879 (patch)
treecf8cf4d9e9c69c1d07ef65cf926c47763df72ef3 /lib/utils/pyexec.c
parent0699c6bf9e7a1754002948063390b68590fc3e96 (diff)
downloadmicropython-0a2e9650f5383bc1190d6b27a3d923e313c3d879.tar.gz
micropython-0a2e9650f5383bc1190d6b27a3d923e313c3d879.zip
py: Add ability to have frozen persistent bytecode from .mpy files.
The config variable MICROPY_MODULE_FROZEN is now made of two separate parts: MICROPY_MODULE_FROZEN_STR and MICROPY_MODULE_FROZEN_MPY. This allows to have none, either or both of frozen strings and frozen mpy files (aka frozen bytecode).
Diffstat (limited to 'lib/utils/pyexec.c')
-rw-r--r--lib/utils/pyexec.c43
1 files changed, 32 insertions, 11 deletions
diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c
index 8afa3813cd..a9e058c1c4 100644
--- a/lib/utils/pyexec.c
+++ b/lib/utils/pyexec.c
@@ -50,22 +50,33 @@ STATIC bool repl_display_debugging_info = 0;
#define EXEC_FLAG_PRINT_EOF (1)
#define EXEC_FLAG_ALLOW_DEBUGGING (2)
#define EXEC_FLAG_IS_REPL (4)
+#define EXEC_FLAG_SOURCE_IS_RAW_CODE (8)
// parses, compiles and executes the code in the lexer
// frees the lexer before returning
// EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
// EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
// EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
-STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, int exec_flags) {
+STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, int exec_flags) {
int ret = 0;
uint32_t start = 0;
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
- // parse and compile the script
- qstr source_name = lex->source_name;
- mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
- mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
+ mp_obj_t module_fun;
+ #if MICROPY_MODULE_FROZEN_MPY
+ if (exec_flags & EXEC_FLAG_SOURCE_IS_RAW_CODE) {
+ // source is a raw_code object, create the function
+ module_fun = mp_make_function_from_raw_code(source, MP_OBJ_NULL, MP_OBJ_NULL);
+ } else
+ #endif
+ {
+ // source is a lexer, parse and compile the script
+ mp_lexer_t *lex = source;
+ qstr source_name = lex->source_name;
+ mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
+ module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
+ }
// execute code
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
@@ -488,14 +499,24 @@ int pyexec_file(const char *filename) {
#if MICROPY_MODULE_FROZEN
int pyexec_frozen_module(const char *name) {
- mp_lexer_t *lex = mp_find_frozen_module(name, strlen(name));
+ void *frozen_data;
+ int frozen_type = mp_find_frozen_module(name, strlen(name), &frozen_data);
- if (lex == NULL) {
- printf("could not find module '%s'\n", name);
- return false;
- }
+ switch (frozen_type) {
+ #if MICROPY_MODULE_FROZEN_STR
+ case MP_FROZEN_STR:
+ return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, 0);
+ #endif
- return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
+ #if MICROPY_MODULE_FROZEN_MPY
+ case MP_FROZEN_MPY:
+ return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_RAW_CODE);
+ #endif
+
+ default:
+ printf("could not find module '%s'\n", name);
+ return false;
+ }
}
#endif