diff --git a/py/builtin.c b/py/builtin.c
index 4564d157563524ab855a47982237b457e231b3f3..ff248f87c3ad8ceed3eb94176a48b2aacd1f0935 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 c6e453f3391a9976dd5d2b83574c7cb3ef9443bc..f0a1dd938adc882618688be5157c3db3cc31aa97 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 67d4f5869bc028610dd46da53f7b6591f723d08c..24d7af601091d428c2fb46be6c0c4afda2778ef0 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 8bf3936977d6ed9c4eaec591e965bb6ce627fed7..592b84907680498a6c4dd2926b19b35241593910 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 1ba0211806f8bd15714a98effdc3778b6a18d263..7a6a44475f692c07cd0fc5e329c403bad46e1ce7 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);