Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
Paul Sokolovsky | f54bcbf | 2014-05-02 17:47:01 +0300 | [diff] [blame] | 30 | #include "mpconfig.h" |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 31 | #include "nlr.h" |
| 32 | #include "misc.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 33 | #include "qstr.h" |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 34 | #include "obj.h" |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 35 | #include "objmodule.h" |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 37 | #include "builtintables.h" |
| 38 | |
| 39 | STATIC mp_map_t mp_loaded_modules_map; // TODO: expose as sys.modules |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 40 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 41 | STATIC void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 42 | mp_obj_module_t *self = self_in; |
Damien George | 377b80b | 2014-09-08 10:45:23 +0100 | [diff] [blame] | 43 | 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 George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 58 | STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
Damien George | 062478e | 2014-01-09 20:57:50 +0000 | [diff] [blame] | 59 | mp_obj_module_t *self = self_in; |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 60 | mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); |
Damien George | 062478e | 2014-01-09 20:57:50 +0000 | [diff] [blame] | 61 | if (elem != NULL) { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 62 | dest[0] = elem->value; |
Damien George | 062478e | 2014-01-09 20:57:50 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 66 | STATIC bool module_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { |
Damien George | 062478e | 2014-01-09 20:57:50 +0000 | [diff] [blame] | 67 | mp_obj_module_t *self = self_in; |
Damien George | 1d24ea5 | 2014-04-08 21:11:49 +0100 | [diff] [blame] | 68 | if (value == MP_OBJ_NULL) { |
| 69 | // delete attribute |
| 70 | mp_obj_dict_delete(self->globals, MP_OBJ_NEW_QSTR(attr)); |
| 71 | } else { |
| 72 | // store attribute |
| 73 | // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation? |
| 74 | mp_obj_dict_store(self->globals, MP_OBJ_NEW_QSTR(attr), value); |
| 75 | } |
Damien George | 062478e | 2014-01-09 20:57:50 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 79 | const mp_obj_type_t mp_type_module = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 80 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 81 | .name = MP_QSTR_module, |
Damien George | 97209d3 | 2014-01-07 15:58:30 +0000 | [diff] [blame] | 82 | .print = module_print, |
Damien George | 062478e | 2014-01-09 20:57:50 +0000 | [diff] [blame] | 83 | .load_attr = module_load_attr, |
| 84 | .store_attr = module_store_attr, |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | mp_obj_t mp_obj_new_module(qstr module_name) { |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 88 | 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 Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 89 | // We could error out if module already exists, but let C extensions |
| 90 | // add new members to existing modules. |
| 91 | if (el->value != MP_OBJ_NULL) { |
| 92 | return el->value; |
| 93 | } |
| 94 | |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 95 | // create new module object |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 96 | mp_obj_module_t *o = m_new_obj(mp_obj_module_t); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 97 | o->base.type = &mp_type_module; |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 98 | o->name = module_name; |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 99 | o->globals = mp_obj_new_dict(1); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 100 | |
| 101 | // store __name__ entry in the module |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 102 | mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name)); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 103 | |
| 104 | // store the new module into the slot in the global dict holding all modules |
| 105 | el->value = o; |
| 106 | |
| 107 | // return the new module |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 108 | return o; |
| 109 | } |
| 110 | |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 111 | mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in) { |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 112 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module)); |
| 113 | mp_obj_module_t *self = self_in; |
| 114 | return self->globals; |
| 115 | } |
| 116 | |
| 117 | /******************************************************************************/ |
| 118 | // Global module table and related functions |
| 119 | |
| 120 | void mp_module_init(void) { |
| 121 | mp_map_init(&mp_loaded_modules_map, 3); |
| 122 | } |
| 123 | |
| 124 | void mp_module_deinit(void) { |
| 125 | mp_map_deinit(&mp_loaded_modules_map); |
| 126 | } |
| 127 | |
| 128 | // returns MP_OBJ_NULL if not found |
| 129 | mp_obj_t mp_module_get(qstr module_name) { |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 130 | // lookup module |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 131 | mp_map_elem_t *el = mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 132 | |
Damien George | 7efc5b3 | 2014-04-05 22:36:42 +0100 | [diff] [blame] | 133 | if (el == NULL) { |
| 134 | // module not found, look for builtin module names |
| 135 | el = mp_map_lookup((mp_map_t*)&mp_builtin_module_dict_obj.map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP); |
| 136 | if (el == NULL) { |
| 137 | return MP_OBJ_NULL; |
| 138 | } |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 139 | } |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 140 | |
Damien George | 7efc5b3 | 2014-04-05 22:36:42 +0100 | [diff] [blame] | 141 | // module found, return it |
| 142 | return el->value; |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 145 | void mp_module_register(qstr qstr, mp_obj_t module) { |
| 146 | mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module; |
Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame] | 147 | } |