diff --git a/include/mruby/internal.h b/include/mruby/internal.h index a05a1a521..a9e43848f 100644 --- a/include/mruby/internal.h +++ b/include/mruby/internal.h @@ -178,7 +178,6 @@ mrb_value mrb_vm_cv_get(mrb_state*, mrb_sym); void mrb_vm_cv_set(mrb_state*, mrb_sym, mrb_value); mrb_value mrb_vm_const_get(mrb_state*, mrb_sym); size_t mrb_obj_iv_tbl_memsize(mrb_value); -mrb_value mrb_obj_iv_inspect(mrb_state*, struct RObject*); void mrb_obj_iv_set_force(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v); mrb_value mrb_mod_constants(mrb_state *mrb, mrb_value mod); mrb_value mrb_mod_const_at(mrb_state *mrb, struct RClass *c, mrb_value ary); diff --git a/src/kernel.c b/src/kernel.c index b39d31109..1886013a3 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -42,6 +42,50 @@ mrb_obj_basic_to_s_p(mrb_state *mrb, mrb_value obj) return mrb_func_basic_p(mrb, obj, MRB_SYM(to_s), mrb_any_to_s); } +struct inspect_i { + mrb_value obj, str; +}; + +static int +inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) +{ + struct inspect_i *a = (struct inspect_i*)p; + if (mrb_nil_p(a->str)) { + const char *cn = mrb_obj_classname(mrb, a->obj); + a->str = mrb_str_new_capa(mrb, 30); + + mrb_str_cat_lit(mrb, a->str, "-<"); + mrb_str_cat_cstr(mrb, a->str, cn); + mrb_str_cat_lit(mrb, a->str, ":"); + mrb_str_cat_str(mrb, a->str, mrb_ptr_to_str(mrb, mrb_obj_ptr(a->obj))); + + if (MRB_RECURSIVE_UNARY_P(mrb, MRB_SYM(inspect), a->obj)) { + mrb_str_cat_lit(mrb, a->str, " ..."); + return 1; + } + } + + const char *s; + mrb_int len; + mrb_value ins; + char *sp = RSTRING_PTR(a->str); + + /* need not to show internal data */ + if (sp[0] == '-') { /* first element */ + sp[0] = '#'; + mrb_str_cat_lit(mrb, a->str, " "); + } + else { + mrb_str_cat_lit(mrb, a->str, ", "); + } + s = mrb_sym_name_len(mrb, sym, &len); + mrb_str_cat(mrb, a->str, s, len); + mrb_str_cat_lit(mrb, a->str, "="); + ins = mrb_inspect(mrb, v); + mrb_str_cat_str(mrb, a->str, ins); + return 0; +} + /* 15.3.1.3.17 */ /* * call-seq: @@ -60,7 +104,13 @@ MRB_API mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value obj) { if (mrb_object_p(obj) && mrb_obj_basic_to_s_p(mrb, obj)) { - return mrb_obj_iv_inspect(mrb, mrb_obj_ptr(obj)); + struct inspect_i a = { obj, mrb_nil_value() }; + mrb_iv_foreach(mrb, obj, inspect_i, &a); + if (!mrb_nil_p(a.str)) { + mrb_assert(mrb_string_p(a.str)); + mrb_str_cat_lit(mrb, a.str, ">"); + return a.str; + } } return mrb_any_to_s(mrb, obj); } diff --git a/src/variable.c b/src/variable.c index 70d27c626..e40ede909 100644 --- a/src/variable.c +++ b/src/variable.c @@ -596,57 +596,6 @@ mrb_iv_copy(mrb_state *mrb, mrb_value dest, mrb_value src) } } -static int -inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) -{ - mrb_value str = *(mrb_value*)p; - const char *s; - mrb_int len; - mrb_value ins; - char *sp = RSTRING_PTR(str); - - /* need not to show internal data */ - if (sp[0] == '-') { /* first element */ - sp[0] = '#'; - mrb_str_cat_lit(mrb, str, " "); - } - else { - mrb_str_cat_lit(mrb, str, ", "); - } - s = mrb_sym_name_len(mrb, sym, &len); - mrb_str_cat(mrb, str, s, len); - mrb_str_cat_lit(mrb, str, "="); - ins = mrb_inspect(mrb, v); - mrb_str_cat_str(mrb, str, ins); - return 0; -} - -mrb_value -mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj) -{ - iv_tbl *t = obj->iv; - size_t len = iv_size(mrb, t); - - if (len > 0) { - const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj)); - mrb_value str = mrb_str_new_capa(mrb, 30); - - mrb_str_cat_lit(mrb, str, "-<"); - mrb_str_cat_cstr(mrb, str, cn); - mrb_str_cat_lit(mrb, str, ":"); - mrb_str_cat_str(mrb, str, mrb_ptr_to_str(mrb, obj)); - - if (MRB_RECURSIVE_UNARY_P(mrb, MRB_SYM(inspect), mrb_obj_value(obj))) { - mrb_str_cat_lit(mrb, str, " ...>"); - return str; - } - iv_foreach(mrb, t, inspect_i, &str); - mrb_str_cat_lit(mrb, str, ">"); - return str; - } - return mrb_any_to_s(mrb, mrb_obj_value(obj)); -} - /* * Removes an instance variable from an object. *