blob: 1ef7be8702d96ec036d5c0a1883ca0f3bb26cdda [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 Georgee09ffa12014-02-05 23:57:48 +000022#define PATH_SEP_CHAR '/'
23
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020024mp_obj_t sys_path;
25
Damien Georgee09ffa12014-02-05 23:57:48 +000026mp_import_stat_t stat_dir_or_file(vstr_t *path) {
27 //printf("stat %s\n", vstr_str(path));
28 mp_import_stat_t stat = mp_import_stat(vstr_str(path));
29 if (stat == MP_IMPORT_STAT_DIR) {
30 return stat;
Damien George66028ab2014-01-03 14:03:48 +000031 }
Damien Georgee09ffa12014-02-05 23:57:48 +000032 vstr_add_str(path, ".py");
33 stat = mp_import_stat(vstr_str(path));
34 if (stat == MP_IMPORT_STAT_FILE) {
35 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020036 }
Damien Georgee09ffa12014-02-05 23:57:48 +000037 return MP_IMPORT_STAT_NO_EXIST;
38}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020039
Damien Georgee09ffa12014-02-05 23:57:48 +000040mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
41 // extract the list of paths
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020042 uint path_num = 0;
43 mp_obj_t *path_items;
44 if (sys_path != MP_OBJ_NULL) {
45 mp_obj_list_get(sys_path, &path_num, &path_items);
46 }
47
48 if (path_num == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +000049 // sys_path is empty, so just use the given file name
50 vstr_add_strn(dest, file_str, file_len);
51 return stat_dir_or_file(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020052 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +000053 // go through each path looking for a directory or file
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020054 for (int i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +000055 vstr_reset(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020056 uint p_len;
57 const byte *p = mp_obj_str_get_data(path_items[i], &p_len);
58 if (p_len > 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +000059 vstr_add_strn(dest, (const char*)p, p_len);
60 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020061 }
Damien Georgee09ffa12014-02-05 23:57:48 +000062 vstr_add_strn(dest, file_str, file_len);
63 mp_import_stat_t stat = stat_dir_or_file(dest);
64 if (stat != MP_IMPORT_STAT_NO_EXIST) {
65 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020066 }
67 }
Damien Georgee09ffa12014-02-05 23:57:48 +000068
69 // could not find a directory or file
70 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020071 }
Damien Georgee09ffa12014-02-05 23:57:48 +000072}
73
74void do_load(mp_obj_t module_obj, vstr_t *file) {
75 // create the lexer
76 mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020077
Damien George66028ab2014-01-03 14:03:48 +000078 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +000079 // we verified the file exists using stat, but lexer could still fail
80 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ImportError, "ImportError: No module named '%s'", vstr_str(file)));
Damien George66028ab2014-01-03 14:03:48 +000081 }
82
Damien Georgee09ffa12014-02-05 23:57:48 +000083 qstr source_name = mp_lexer_source_name(lex);
Damien George66028ab2014-01-03 14:03:48 +000084
85 // save the old context
86 mp_map_t *old_locals = rt_locals_get();
87 mp_map_t *old_globals = rt_globals_get();
88
89 // set the new context
90 rt_locals_set(mp_obj_module_get_globals(module_obj));
91 rt_globals_set(mp_obj_module_get_globals(module_obj));
92
93 // parse the imported script
Damien George9528cd62014-01-15 21:23:31 +000094 qstr parse_exc_id;
95 const char *parse_exc_msg;
96 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
Damien George66028ab2014-01-03 14:03:48 +000097 mp_lexer_free(lex);
98
99 if (pn == MP_PARSE_NODE_NULL) {
Damien George9528cd62014-01-15 21:23:31 +0000100 // parse error; clean up and raise exception
Damien George66028ab2014-01-03 14:03:48 +0000101 rt_locals_set(old_locals);
102 rt_globals_set(old_globals);
Damien George9528cd62014-01-15 21:23:31 +0000103 nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg));
Damien George66028ab2014-01-03 14:03:48 +0000104 }
105
Damien George9528cd62014-01-15 21:23:31 +0000106 // compile the imported script
Damien George08335002014-01-18 23:24:36 +0000107 mp_obj_t module_fun = mp_compile(pn, source_name, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000108 mp_parse_node_free(pn);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000109
110 if (module_fun == mp_const_none) {
Damien George66028ab2014-01-03 14:03:48 +0000111 // TODO handle compile error correctly
112 rt_locals_set(old_locals);
113 rt_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000114 return;
Damien George66028ab2014-01-03 14:03:48 +0000115 }
116
117 // complied successfully, execute it
Damien George66028ab2014-01-03 14:03:48 +0000118 nlr_buf_t nlr;
119 if (nlr_push(&nlr) == 0) {
120 rt_call_function_0(module_fun);
121 nlr_pop();
122 } else {
123 // exception; restore context and re-raise same exception
124 rt_locals_set(old_locals);
125 rt_globals_set(old_globals);
126 nlr_jump(nlr.ret_val);
127 }
128 rt_locals_set(old_locals);
129 rt_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000130}
131
132mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
133 /*
134 printf("import:\n");
135 for (int i = 0; i < n; i++) {
136 printf(" ");
137 mp_obj_print(args[i]);
138 printf("\n");
139 }
140 */
141
142 // check if module already exists
143 mp_obj_t module_obj = mp_obj_module_get(mp_obj_str_get_qstr(args[0]));
144 if (module_obj != MP_OBJ_NULL) {
145 return module_obj;
146 }
147
148 uint mod_len;
149 const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len);
150
151 uint last = 0;
Damien George354d15a2014-02-06 21:11:19 +0000152 VSTR_FIXED(path, MICROPY_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000153 module_obj = MP_OBJ_NULL;
154 uint i;
155 for (i = 1; i <= mod_len; i++) {
156 if (i == mod_len || mod_str[i] == '.') {
157 // create a qstr for the module name up to this depth
158 qstr mod_name = qstr_from_strn(mod_str, i);
159
160 // find the file corresponding to the module name
161 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000162 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000163 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000164 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000165 } else {
166 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000167 vstr_add_char(&path, PATH_SEP_CHAR);
168 vstr_add_strn(&path, mod_str + last, i - last);
169 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000170 }
171 last = i + 1;
172
173 // fail if we couldn't find the file
174 if (stat == MP_IMPORT_STAT_NO_EXIST) {
175 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ImportError, "ImportError: No module named '%s'", qstr_str(mod_name)));
176 }
177
178 module_obj = mp_obj_module_get(mod_name);
179 if (module_obj == MP_OBJ_NULL) {
180 // module not already loaded, so load it!
181
182 module_obj = mp_obj_new_module(mod_name);
183
184 if (stat == MP_IMPORT_STAT_DIR) {
Damien George354d15a2014-02-06 21:11:19 +0000185 vstr_add_char(&path, PATH_SEP_CHAR);
186 vstr_add_str(&path, "__init__.py");
187 if (mp_import_stat(vstr_str(&path)) == MP_IMPORT_STAT_FILE) {
188 do_load(module_obj, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000189 }
Damien George354d15a2014-02-06 21:11:19 +0000190 vstr_cut_tail(&path, 12); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000191 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000192 do_load(module_obj, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000193 break;
194 }
195 }
196 }
197 }
198
199 if (i < mod_len) {
200 // we loaded a package, now need to load objects from within that package
201 // TODO
202 assert(0);
203 }
204
Damien George66028ab2014-01-03 14:03:48 +0000205 return module_obj;
206}
Damien Georgee09ffa12014-02-05 23:57:48 +0000207
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200208MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);