Damien George | 2870862 | 2014-01-02 21:30:26 +0000 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
| 9 | #include "obj.h" |
| 10 | #include "runtime.h" |
| 11 | #include "map.h" |
| 12 | |
| 13 | typedef struct _mp_obj_module_t { |
| 14 | mp_obj_base_t base; |
| 15 | qstr name; |
| 16 | mp_map_t *globals; |
| 17 | } mp_obj_module_t; |
| 18 | |
| 19 | void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { |
| 20 | mp_obj_module_t *self = self_in; |
| 21 | print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name)); |
| 22 | } |
| 23 | |
| 24 | const mp_obj_type_t module_type = { |
| 25 | { &mp_const_type }, |
| 26 | "module", |
| 27 | module_print, // print |
| 28 | NULL, // call_n |
| 29 | NULL, // unary_op |
| 30 | NULL, // binary_op |
| 31 | NULL, // getiter |
| 32 | NULL, // iternext |
| 33 | {{NULL, NULL},}, // method list |
| 34 | }; |
| 35 | |
| 36 | mp_obj_t mp_obj_new_module(qstr module_name) { |
| 37 | mp_obj_module_t *o = m_new_obj(mp_obj_module_t); |
| 38 | o->base.type = &module_type; |
| 39 | o->name = module_name; |
| 40 | o->globals = mp_map_new(MP_MAP_QSTR, 0); |
| 41 | return o; |
| 42 | } |
| 43 | |
| 44 | mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in) { |
| 45 | assert(MP_OBJ_IS_TYPE(self_in, &module_type)); |
| 46 | mp_obj_module_t *self = self_in; |
| 47 | return self->globals; |
| 48 | } |