Skip to content

Commit 7b56f13

Browse files
authored
Merge pull request mruby#6581 from dearblue/iv_inspect
2 parents 9e8e816 + 91f2dca commit 7b56f13

3 files changed

Lines changed: 51 additions & 53 deletions

File tree

include/mruby/internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ mrb_value mrb_vm_cv_get(mrb_state*, mrb_sym);
178178
void mrb_vm_cv_set(mrb_state*, mrb_sym, mrb_value);
179179
mrb_value mrb_vm_const_get(mrb_state*, mrb_sym);
180180
size_t mrb_obj_iv_tbl_memsize(mrb_value);
181-
mrb_value mrb_obj_iv_inspect(mrb_state*, struct RObject*);
182181
void mrb_obj_iv_set_force(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v);
183182
mrb_value mrb_mod_constants(mrb_state *mrb, mrb_value mod);
184183
mrb_value mrb_mod_const_at(mrb_state *mrb, struct RClass *c, mrb_value ary);

src/kernel.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,50 @@ mrb_obj_basic_to_s_p(mrb_state *mrb, mrb_value obj)
4242
return mrb_func_basic_p(mrb, obj, MRB_SYM(to_s), mrb_any_to_s);
4343
}
4444

45+
struct inspect_i {
46+
mrb_value obj, str;
47+
};
48+
49+
static int
50+
inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
51+
{
52+
struct inspect_i *a = (struct inspect_i*)p;
53+
if (mrb_nil_p(a->str)) {
54+
const char *cn = mrb_obj_classname(mrb, a->obj);
55+
a->str = mrb_str_new_capa(mrb, 30);
56+
57+
mrb_str_cat_lit(mrb, a->str, "-<");
58+
mrb_str_cat_cstr(mrb, a->str, cn);
59+
mrb_str_cat_lit(mrb, a->str, ":");
60+
mrb_str_cat_str(mrb, a->str, mrb_ptr_to_str(mrb, mrb_obj_ptr(a->obj)));
61+
62+
if (MRB_RECURSIVE_UNARY_P(mrb, MRB_SYM(inspect), a->obj)) {
63+
mrb_str_cat_lit(mrb, a->str, " ...");
64+
return 1;
65+
}
66+
}
67+
68+
const char *s;
69+
mrb_int len;
70+
mrb_value ins;
71+
char *sp = RSTRING_PTR(a->str);
72+
73+
/* need not to show internal data */
74+
if (sp[0] == '-') { /* first element */
75+
sp[0] = '#';
76+
mrb_str_cat_lit(mrb, a->str, " ");
77+
}
78+
else {
79+
mrb_str_cat_lit(mrb, a->str, ", ");
80+
}
81+
s = mrb_sym_name_len(mrb, sym, &len);
82+
mrb_str_cat(mrb, a->str, s, len);
83+
mrb_str_cat_lit(mrb, a->str, "=");
84+
ins = mrb_inspect(mrb, v);
85+
mrb_str_cat_str(mrb, a->str, ins);
86+
return 0;
87+
}
88+
4589
/* 15.3.1.3.17 */
4690
/*
4791
* call-seq:
@@ -60,7 +104,13 @@ MRB_API mrb_value
60104
mrb_obj_inspect(mrb_state *mrb, mrb_value obj)
61105
{
62106
if (mrb_object_p(obj) && mrb_obj_basic_to_s_p(mrb, obj)) {
63-
return mrb_obj_iv_inspect(mrb, mrb_obj_ptr(obj));
107+
struct inspect_i a = { obj, mrb_nil_value() };
108+
mrb_iv_foreach(mrb, obj, inspect_i, &a);
109+
if (!mrb_nil_p(a.str)) {
110+
mrb_assert(mrb_string_p(a.str));
111+
mrb_str_cat_lit(mrb, a.str, ">");
112+
return a.str;
113+
}
64114
}
65115
return mrb_any_to_s(mrb, obj);
66116
}

src/variable.c

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -596,57 +596,6 @@ mrb_iv_copy(mrb_state *mrb, mrb_value dest, mrb_value src)
596596
}
597597
}
598598

599-
static int
600-
inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
601-
{
602-
mrb_value str = *(mrb_value*)p;
603-
const char *s;
604-
mrb_int len;
605-
mrb_value ins;
606-
char *sp = RSTRING_PTR(str);
607-
608-
/* need not to show internal data */
609-
if (sp[0] == '-') { /* first element */
610-
sp[0] = '#';
611-
mrb_str_cat_lit(mrb, str, " ");
612-
}
613-
else {
614-
mrb_str_cat_lit(mrb, str, ", ");
615-
}
616-
s = mrb_sym_name_len(mrb, sym, &len);
617-
mrb_str_cat(mrb, str, s, len);
618-
mrb_str_cat_lit(mrb, str, "=");
619-
ins = mrb_inspect(mrb, v);
620-
mrb_str_cat_str(mrb, str, ins);
621-
return 0;
622-
}
623-
624-
mrb_value
625-
mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj)
626-
{
627-
iv_tbl *t = obj->iv;
628-
size_t len = iv_size(mrb, t);
629-
630-
if (len > 0) {
631-
const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj));
632-
mrb_value str = mrb_str_new_capa(mrb, 30);
633-
634-
mrb_str_cat_lit(mrb, str, "-<");
635-
mrb_str_cat_cstr(mrb, str, cn);
636-
mrb_str_cat_lit(mrb, str, ":");
637-
mrb_str_cat_str(mrb, str, mrb_ptr_to_str(mrb, obj));
638-
639-
if (MRB_RECURSIVE_UNARY_P(mrb, MRB_SYM(inspect), mrb_obj_value(obj))) {
640-
mrb_str_cat_lit(mrb, str, " ...>");
641-
return str;
642-
}
643-
iv_foreach(mrb, t, inspect_i, &str);
644-
mrb_str_cat_lit(mrb, str, ">");
645-
return str;
646-
}
647-
return mrb_any_to_s(mrb, mrb_obj_value(obj));
648-
}
649-
650599
/*
651600
* Removes an instance variable from an object.
652601
*

0 commit comments

Comments
 (0)