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