blob: 5ffb368bf5c524a25433d292459f997365ad861e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George28708622014-01-02 21:30:26 +000027#include <stdlib.h>
Damien George28708622014-01-02 21:30:26 +000028#include <assert.h>
29
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damien George28708622014-01-02 21:30:26 +000031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damien George28708622014-01-02 21:30:26 +000034#include "obj.h"
Damien Georgecaac5422014-03-25 14:18:18 +000035#include "objmodule.h"
Damien George28708622014-01-02 21:30:26 +000036#include "runtime.h"
Damien George78d702c2014-12-09 16:19:48 +000037#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000038
39STATIC mp_map_t mp_loaded_modules_map; // TODO: expose as sys.modules
Damien George28708622014-01-02 21:30:26 +000040
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020041STATIC void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George28708622014-01-02 21:30:26 +000042 mp_obj_module_t *self = self_in;
Damien George377b80b2014-09-08 10:45:23 +010043 const char *name = qstr_str(self->name);
44
45#if MICROPY_PY___FILE__
46 // If we store __file__ to imported modules then try to lookup this
47 // symbol to give more information about the module.
48 mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
49 if (elem != NULL) {
50 print(env, "<module '%s' from '%s'>", name, mp_obj_str_get_str(elem->value));
51 return;
52 }
53#endif
54
55 print(env, "<module '%s'>", name);
Damien George28708622014-01-02 21:30:26 +000056}
57
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020058STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +000059 mp_obj_module_t *self = self_in;
Damien George8b0535e2014-04-05 21:53:54 +010060 mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
Damien George062478e2014-01-09 20:57:50 +000061 if (elem != NULL) {
Damien George20006db2014-01-18 14:10:48 +000062 dest[0] = elem->value;
Damien George062478e2014-01-09 20:57:50 +000063 }
64}
65
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020066STATIC bool module_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
Damien George062478e2014-01-09 20:57:50 +000067 mp_obj_module_t *self = self_in;
Damien George78d702c2014-12-09 16:19:48 +000068 mp_obj_dict_t *dict = self->globals;
69 if (dict->map.table_is_fixed_array) {
70 #if MICROPY_CAN_OVERRIDE_BUILTINS
71 if (dict == &mp_module_builtins_globals) {
72 if (mp_module_builtins_override_dict == NULL) {
73 mp_module_builtins_override_dict = mp_obj_new_dict(1);
74 }
75 dict = mp_module_builtins_override_dict;
76 } else
77 #endif
78 {
79 // can't delete or store to fixed map
80 return false;
81 }
82 }
Damien George1d24ea52014-04-08 21:11:49 +010083 if (value == MP_OBJ_NULL) {
84 // delete attribute
Damien George78d702c2014-12-09 16:19:48 +000085 mp_obj_dict_delete(dict, MP_OBJ_NEW_QSTR(attr));
Damien George1d24ea52014-04-08 21:11:49 +010086 } else {
87 // store attribute
88 // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
Damien George78d702c2014-12-09 16:19:48 +000089 mp_obj_dict_store(dict, MP_OBJ_NEW_QSTR(attr), value);
Damien George1d24ea52014-04-08 21:11:49 +010090 }
Damien George062478e2014-01-09 20:57:50 +000091 return true;
92}
93
Damien George0c36da02014-03-08 15:24:39 +000094const mp_obj_type_t mp_type_module = {
Damien Georgec5966122014-02-15 16:10:44 +000095 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000096 .name = MP_QSTR_module,
Damien George97209d32014-01-07 15:58:30 +000097 .print = module_print,
Damien George062478e2014-01-09 20:57:50 +000098 .load_attr = module_load_attr,
99 .store_attr = module_store_attr,
Damien George28708622014-01-02 21:30:26 +0000100};
101
102mp_obj_t mp_obj_new_module(qstr module_name) {
Damien Georgecaac5422014-03-25 14:18:18 +0000103 mp_map_elem_t *el = mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200104 // We could error out if module already exists, but let C extensions
105 // add new members to existing modules.
106 if (el->value != MP_OBJ_NULL) {
107 return el->value;
108 }
109
Damien George0c36da02014-03-08 15:24:39 +0000110 // create new module object
Damien George28708622014-01-02 21:30:26 +0000111 mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
Damien George0c36da02014-03-08 15:24:39 +0000112 o->base.type = &mp_type_module;
Damien George28708622014-01-02 21:30:26 +0000113 o->name = module_name;
Paul Sokolovsky346aacf2014-11-05 00:27:15 +0200114 o->globals = mp_obj_new_dict(MICROPY_MODULE_DICT_SIZE);
Damien George0c36da02014-03-08 15:24:39 +0000115
116 // store __name__ entry in the module
Damien George8b0535e2014-04-05 21:53:54 +0100117 mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name));
Damien George0c36da02014-03-08 15:24:39 +0000118
119 // store the new module into the slot in the global dict holding all modules
120 el->value = o;
121
122 // return the new module
Damien George28708622014-01-02 21:30:26 +0000123 return o;
124}
125
Damien George8b0535e2014-04-05 21:53:54 +0100126mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in) {
Damien Georgecaac5422014-03-25 14:18:18 +0000127 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
128 mp_obj_module_t *self = self_in;
129 return self->globals;
130}
131
132/******************************************************************************/
133// Global module table and related functions
134
Damien George78d702c2014-12-09 16:19:48 +0000135STATIC const mp_map_elem_t mp_builtin_module_table[] = {
136 { MP_OBJ_NEW_QSTR(MP_QSTR___main__), (mp_obj_t)&mp_module___main__ },
137 { MP_OBJ_NEW_QSTR(MP_QSTR_builtins), (mp_obj_t)&mp_module_builtins },
138 { MP_OBJ_NEW_QSTR(MP_QSTR_micropython), (mp_obj_t)&mp_module_micropython },
139
140#if MICROPY_PY_ARRAY
141 { MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_module_array },
142#endif
143#if MICROPY_PY_IO
144 { MP_OBJ_NEW_QSTR(MP_QSTR__io), (mp_obj_t)&mp_module_io },
145#endif
146#if MICROPY_PY_COLLECTIONS
147 { MP_OBJ_NEW_QSTR(MP_QSTR__collections), (mp_obj_t)&mp_module_collections },
148#endif
149#if MICROPY_PY_STRUCT
150 { MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&mp_module_struct },
151#endif
152
153#if MICROPY_PY_BUILTINS_FLOAT
154#if MICROPY_PY_MATH
155 { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },
156#endif
157#if MICROPY_PY_CMATH
158 { MP_OBJ_NEW_QSTR(MP_QSTR_cmath), (mp_obj_t)&mp_module_cmath },
159#endif
160#endif
161#if MICROPY_PY_SYS
162 { MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },
163#endif
164#if MICROPY_PY_GC && MICROPY_ENABLE_GC
165 { MP_OBJ_NEW_QSTR(MP_QSTR_gc), (mp_obj_t)&mp_module_gc },
166#endif
167
168 // extmod modules
169
170#if MICROPY_PY_UCTYPES
171 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
172#endif
173#if MICROPY_PY_UZLIB
174 { MP_OBJ_NEW_QSTR(MP_QSTR_uzlib), (mp_obj_t)&mp_module_uzlib },
175#endif
176#if MICROPY_PY_UJSON
177 { MP_OBJ_NEW_QSTR(MP_QSTR_ujson), (mp_obj_t)&mp_module_ujson },
178#endif
179#if MICROPY_PY_URE
180 { MP_OBJ_NEW_QSTR(MP_QSTR_ure), (mp_obj_t)&mp_module_ure },
181#endif
182#if MICROPY_PY_UHEAPQ
183 { MP_OBJ_NEW_QSTR(MP_QSTR_uheapq), (mp_obj_t)&mp_module_uheapq },
184#endif
185#if MICROPY_PY_UHASHLIB
186 { MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib },
187#endif
188#if MICROPY_PY_UBINASCII
189 { MP_OBJ_NEW_QSTR(MP_QSTR_ubinascii), (mp_obj_t)&mp_module_ubinascii },
190#endif
191
192 // extra builtin modules as defined by a port
193 MICROPY_PORT_BUILTIN_MODULES
194};
195
196STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
197
Damien Georgecaac5422014-03-25 14:18:18 +0000198void mp_module_init(void) {
199 mp_map_init(&mp_loaded_modules_map, 3);
200}
201
202void mp_module_deinit(void) {
203 mp_map_deinit(&mp_loaded_modules_map);
204}
205
206// returns MP_OBJ_NULL if not found
207mp_obj_t mp_module_get(qstr module_name) {
Damien George0c36da02014-03-08 15:24:39 +0000208 // lookup module
Damien Georgecaac5422014-03-25 14:18:18 +0000209 mp_map_elem_t *el = mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
Damien George0c36da02014-03-08 15:24:39 +0000210
Damien George7efc5b32014-04-05 22:36:42 +0100211 if (el == NULL) {
212 // module not found, look for builtin module names
Damien George78d702c2014-12-09 16:19:48 +0000213 el = mp_map_lookup((mp_map_t*)&mp_builtin_module_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
Damien George7efc5b32014-04-05 22:36:42 +0100214 if (el == NULL) {
215 return MP_OBJ_NULL;
216 }
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200217 }
Damien George0c36da02014-03-08 15:24:39 +0000218
Damien George7efc5b32014-04-05 22:36:42 +0100219 // module found, return it
220 return el->value;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200221}
222
Damien Georgecaac5422014-03-25 14:18:18 +0000223void mp_module_register(qstr qstr, mp_obj_t module) {
224 mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
Damien George28708622014-01-02 21:30:26 +0000225}