blob: 3cfd64e88716132c0e3435780ede720d399b8827 [file] [log] [blame]
Damien George66028ab2014-01-03 14:03:48 +00001#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 George55baff42014-01-21 21:40:13 +000011#include "qstr.h"
Damien George66028ab2014-01-03 14:03:48 +000012#include "lexer.h"
13#include "lexerunix.h"
14#include "parse.h"
Damien George66028ab2014-01-03 14:03:48 +000015#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000016#include "compile.h"
Damien George66028ab2014-01-03 14:03:48 +000017#include "runtime0.h"
18#include "runtime.h"
19#include "map.h"
20#include "builtin.h"
21
Damien George23005372014-01-13 19:39:01 +000022mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
Damien George66028ab2014-01-03 14:03:48 +000023 /*
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 George5fa93b62014-01-22 14:35:10 +000032 uint mod_name_l;
33 const byte *mod_name_s = mp_obj_str_get_data(args[0], &mod_name_l);
34 qstr mod_name = qstr_from_strn((const char*)mod_name_s, mod_name_l);
35
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020036 mp_obj_t loaded = mp_obj_module_get(mod_name);
37 if (loaded != MP_OBJ_NULL) {
38 return loaded;
39 }
40
41 // find the file to import
Damien George66028ab2014-01-03 14:03:48 +000042 mp_lexer_t *lex = mp_import_open_file(mod_name);
43 if (lex == NULL) {
44 // TODO handle lexer error correctly
45 return mp_const_none;
46 }
47
48 // create a new module object
Damien George5fa93b62014-01-22 14:35:10 +000049 mp_obj_t module_obj = mp_obj_new_module(mod_name);
Damien George66028ab2014-01-03 14:03:48 +000050
51 // save the old context
52 mp_map_t *old_locals = rt_locals_get();
53 mp_map_t *old_globals = rt_globals_get();
54
55 // set the new context
56 rt_locals_set(mp_obj_module_get_globals(module_obj));
57 rt_globals_set(mp_obj_module_get_globals(module_obj));
58
59 // parse the imported script
Damien George9528cd62014-01-15 21:23:31 +000060 qstr parse_exc_id;
61 const char *parse_exc_msg;
62 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
Damien George08335002014-01-18 23:24:36 +000063 qstr source_name = mp_lexer_source_name(lex);
Damien George66028ab2014-01-03 14:03:48 +000064 mp_lexer_free(lex);
65
66 if (pn == MP_PARSE_NODE_NULL) {
Damien George9528cd62014-01-15 21:23:31 +000067 // parse error; clean up and raise exception
Damien George66028ab2014-01-03 14:03:48 +000068 rt_locals_set(old_locals);
69 rt_globals_set(old_globals);
Damien George9528cd62014-01-15 21:23:31 +000070 nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg));
Damien George66028ab2014-01-03 14:03:48 +000071 }
72
Damien George9528cd62014-01-15 21:23:31 +000073 // compile the imported script
Damien George08335002014-01-18 23:24:36 +000074 mp_obj_t module_fun = mp_compile(pn, source_name, false);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000075
76 if (module_fun == mp_const_none) {
Damien George66028ab2014-01-03 14:03:48 +000077 // TODO handle compile error correctly
78 rt_locals_set(old_locals);
79 rt_globals_set(old_globals);
80 return mp_const_none;
81 }
82
83 // complied successfully, execute it
Damien George66028ab2014-01-03 14:03:48 +000084 nlr_buf_t nlr;
85 if (nlr_push(&nlr) == 0) {
86 rt_call_function_0(module_fun);
87 nlr_pop();
88 } else {
89 // exception; restore context and re-raise same exception
90 rt_locals_set(old_locals);
91 rt_globals_set(old_globals);
92 nlr_jump(nlr.ret_val);
93 }
94 rt_locals_set(old_locals);
95 rt_globals_set(old_globals);
96
97 return module_obj;
98}