@@ -148,6 +148,36 @@ static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
148148
149149MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_chr_obj , mp_builtin_chr );
150150
151+ static mp_obj_t mp_builtin_dir (uint n_args , const mp_obj_t * args ) {
152+ // TODO make this function more general and less of a hack
153+
154+ mp_map_t * map ;
155+ if (n_args == 0 ) {
156+ // make a list of names in the local name space
157+ map = rt_locals_get ();
158+ } else { // n_args == 1
159+ // make a list of names in the given object
160+ mp_obj_type_t * type = mp_obj_get_type (args [0 ]);
161+ if (type == & module_type ) {
162+ map = mp_obj_module_get_globals (args [0 ]);
163+ } else if (type -> locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE (type -> locals_dict , & dict_type )) {
164+ map = mp_obj_dict_get_map (type -> locals_dict );
165+ } else {
166+ return mp_obj_new_list (0 , NULL );
167+ }
168+ }
169+
170+ mp_obj_t dir = mp_obj_new_list (0 , NULL );
171+ for (uint i = 0 ; i < map -> alloc ; i ++ ) {
172+ if (map -> table [i ].key != MP_OBJ_NULL ) {
173+ mp_obj_list_append (dir , map -> table [i ].key );
174+ }
175+ }
176+ return dir ;
177+ }
178+
179+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_builtin_dir_obj , 0 , 1 , mp_builtin_dir );
180+
151181static mp_obj_t mp_builtin_divmod (mp_obj_t o1_in , mp_obj_t o2_in ) {
152182 if (MP_OBJ_IS_SMALL_INT (o1_in ) && MP_OBJ_IS_SMALL_INT (o2_in )) {
153183 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE (o1_in );
0 commit comments