From 4acb2452b30f6a8bbcf8fe46b05e56bb470a7026 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Sun, 2 Feb 2014 22:07:44 +0000
Subject: [PATCH] py: Add very basic implementation of dir() builtin.

Only works on modules and class instances.
---
 py/builtin.c  | 30 ++++++++++++++++++++++++++++++
 py/builtin.h  |  1 +
 py/objtype.c  |  2 +-
 py/qstrdefs.h |  1 +
 py/runtime.c  |  1 +
 5 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/py/builtin.c b/py/builtin.c
index 4564d1575..ff248f87c 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 c6e453f33..f0a1dd938 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 67d4f5869..24d7af601 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 8bf393697..592b84907 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 1ba021180..7a6a44475 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);
-- 
GitLab