py: Initialise loaded_module map in rt_init.

STM port crashes without this re-init.  There should not be any state in
the core py/ code that relies on pre-initialised data.
This commit is contained in:
Damien George 2014-01-22 23:59:20 +00:00
parent f64086f80f
commit 0d028743aa
3 changed files with 11 additions and 8 deletions

View File

@ -17,9 +17,6 @@ typedef struct _mp_obj_module_t {
mp_map_t *globals;
} mp_obj_module_t;
// TODO: expose as sys.modules
static mp_map_t *loaded_modules;
static void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_module_t *self = self_in;
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
@ -49,10 +46,7 @@ const mp_obj_type_t module_type = {
};
mp_obj_t mp_obj_new_module(qstr module_name) {
if (loaded_modules == NULL) {
loaded_modules = mp_map_new(1);
}
mp_map_elem_t *el = mp_map_lookup(loaded_modules, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
// We could error out if module already exists, but let C extensions
// add new members to existing modules.
if (el->value != MP_OBJ_NULL) {
@ -69,7 +63,7 @@ mp_obj_t mp_obj_new_module(qstr module_name) {
}
mp_obj_t mp_obj_module_get(qstr module_name) {
mp_map_elem_t *el = mp_map_lookup(loaded_modules, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
if (el == NULL) {
return NULL;
}

View File

@ -34,6 +34,7 @@
static mp_map_t *map_locals;
static mp_map_t *map_globals;
static mp_map_t map_builtins;
static mp_map_t map_loaded_modules; // TODO: expose as sys.modules
typedef enum {
MP_CODE_NONE,
@ -83,6 +84,9 @@ void rt_init(void) {
// init built-in hash table
mp_map_init(&map_builtins, 3);
// init loaded modules table
mp_map_init(&map_loaded_modules, 3);
// built-in exceptions (TODO, make these proper classes)
mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
mp_map_add_qstr(&map_builtins, MP_QSTR_IndexError, mp_obj_new_exception(MP_QSTR_IndexError));
@ -953,6 +957,10 @@ void rt_globals_set(mp_map_t *m) {
map_globals = m;
}
mp_map_t *rt_loaded_modules_get(void) {
return &map_loaded_modules;
}
// these must correspond to the respective enum
void *const rt_fun_table[RT_F_NUMBER_OF] = {
rt_load_const_dec,

View File

@ -43,3 +43,4 @@ struct _mp_map_t *rt_locals_get(void);
void rt_locals_set(struct _mp_map_t *m);
struct _mp_map_t *rt_globals_get(void);
void rt_globals_set(struct _mp_map_t *m);
struct _mp_map_t *rt_loaded_modules_get(void);