diff --git a/py/builtin.c b/py/builtin.c index 4564d15756..ff248f87c3 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -148,6 +148,36 @@ static mp_obj_t mp_builtin_chr(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr); +static mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) { + // TODO make this function more general and less of a hack + + mp_map_t *map; + if (n_args == 0) { + // make a list of names in the local name space + map = rt_locals_get(); + } else { // n_args == 1 + // make a list of names in the given object + mp_obj_type_t *type = mp_obj_get_type(args[0]); + if (type == &module_type) { + map = mp_obj_module_get_globals(args[0]); + } else if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)) { + map = mp_obj_dict_get_map(type->locals_dict); + } else { + return mp_obj_new_list(0, NULL); + } + } + + mp_obj_t dir = mp_obj_new_list(0, NULL); + for (uint i = 0; i < map->alloc; i++) { + if (map->table[i].key != MP_OBJ_NULL) { + mp_obj_list_append(dir, map->table[i].key); + } + } + return dir; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir); + static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) { mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in); diff --git a/py/builtin.h b/py/builtin.h index c6e453f339..f0a1dd938a 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -8,6 +8,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_bytes_obj); // Temporary hack MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); diff --git a/py/objtype.c b/py/objtype.c index 67d4f5869b..24d7af6010 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -35,7 +35,7 @@ static mp_obj_t mp_obj_class_lookup(const mp_obj_type_t *type, qstr attr) { // search locals_dict (the dynamically created set of methods/attributes) assert(MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)); // Micro Python restriction, for now - mp_map_t *locals_map = ((void*)type->locals_dict + sizeof(mp_obj_base_t)); // XXX hack to get map object from dict object + mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict); mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); if (elem != NULL) { return elem->value; diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 8bf3936977..592b849076 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -51,6 +51,7 @@ Q(callable) Q(chr) Q(complex) Q(dict) +Q(dir) Q(divmod) Q(enumerate) Q(eval) diff --git a/py/runtime.c b/py/runtime.c index 1ba0211806..7a6a44475f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -141,6 +141,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);