Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <string.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include "nlr.h" |
| 9 | #include "misc.h" |
| 10 | #include "mpconfig.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 11 | #include "qstr.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 12 | #include "lexer.h" |
| 13 | #include "lexerunix.h" |
| 14 | #include "parse.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 15 | #include "obj.h" |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 16 | #include "compile.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 17 | #include "runtime0.h" |
| 18 | #include "runtime.h" |
| 19 | #include "map.h" |
| 20 | #include "builtin.h" |
| 21 | |
Damien George | 2300537 | 2014-01-13 19:39:01 +0000 | [diff] [blame] | 22 | mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 23 | /* |
| 24 | printf("import:\n"); |
| 25 | for (int i = 0; i < n; i++) { |
| 26 | printf(" "); |
| 27 | mp_obj_print(args[i]); |
| 28 | printf("\n"); |
| 29 | } |
| 30 | */ |
| 31 | |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame^] | 32 | qstr mod_name = mp_obj_str_get_qstr(args[0]); |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 33 | |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 34 | mp_obj_t loaded = mp_obj_module_get(mod_name); |
| 35 | if (loaded != MP_OBJ_NULL) { |
| 36 | return loaded; |
| 37 | } |
| 38 | |
| 39 | // find the file to import |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 40 | mp_lexer_t *lex = mp_import_open_file(mod_name); |
| 41 | if (lex == NULL) { |
| 42 | // TODO handle lexer error correctly |
| 43 | return mp_const_none; |
| 44 | } |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame^] | 45 | qstr source_name = mp_lexer_source_name(lex); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 46 | |
| 47 | // create a new module object |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 48 | mp_obj_t module_obj = mp_obj_new_module(mod_name); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 49 | |
| 50 | // save the old context |
| 51 | mp_map_t *old_locals = rt_locals_get(); |
| 52 | mp_map_t *old_globals = rt_globals_get(); |
| 53 | |
| 54 | // set the new context |
| 55 | rt_locals_set(mp_obj_module_get_globals(module_obj)); |
| 56 | rt_globals_set(mp_obj_module_get_globals(module_obj)); |
| 57 | |
| 58 | // parse the imported script |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 59 | qstr parse_exc_id; |
| 60 | const char *parse_exc_msg; |
| 61 | mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 62 | mp_lexer_free(lex); |
| 63 | |
| 64 | if (pn == MP_PARSE_NODE_NULL) { |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 65 | // parse error; clean up and raise exception |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 66 | rt_locals_set(old_locals); |
| 67 | rt_globals_set(old_globals); |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 68 | nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg)); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 71 | // compile the imported script |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 72 | mp_obj_t module_fun = mp_compile(pn, source_name, false); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame^] | 73 | mp_parse_node_free(pn); |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 74 | |
| 75 | if (module_fun == mp_const_none) { |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 76 | // TODO handle compile error correctly |
| 77 | rt_locals_set(old_locals); |
| 78 | rt_globals_set(old_globals); |
| 79 | return mp_const_none; |
| 80 | } |
| 81 | |
| 82 | // complied successfully, execute it |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 83 | nlr_buf_t nlr; |
| 84 | if (nlr_push(&nlr) == 0) { |
| 85 | rt_call_function_0(module_fun); |
| 86 | nlr_pop(); |
| 87 | } else { |
| 88 | // exception; restore context and re-raise same exception |
| 89 | rt_locals_set(old_locals); |
| 90 | rt_globals_set(old_globals); |
| 91 | nlr_jump(nlr.ret_val); |
| 92 | } |
| 93 | rt_locals_set(old_locals); |
| 94 | rt_globals_set(old_globals); |
| 95 | |
| 96 | return module_obj; |
| 97 | } |