Skip to content

Commit e2bbf75

Browse files
committed
kernel.c: prepare for new recursive inspect check
Like previous recursive `<=>` check, scan call stack to detect recursive `inspect` calls. We no longer need incomplete `_inspect` hack to pass around a hash table to record objects currently inspecting.
1 parent 75c8ffd commit e2bbf75

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎include/mruby/internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ void mrb_ary_decref(mrb_state*, mrb_shared_array*);
1212
mrb_value mrb_ary_subseq(mrb_state *mrb, mrb_value ary, mrb_int beg, mrb_int len);
1313
#endif
1414

15+
mrb_bool mrb_inspect_recursive_p(mrb_state *mrb, mrb_value self);
16+
1517
#ifdef MRUBY_CLASS_H
1618
struct RClass *mrb_vm_define_class(mrb_state*, mrb_value, mrb_value, mrb_sym);
1719
struct RClass *mrb_vm_define_module(mrb_state*, mrb_value, mrb_sym);

‎src/kernel.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ mrb_cmp_m(mrb_state *mrb, mrb_value self)
9898
return mrb_nil_value();
9999
}
100100

101+
static mrb_bool
102+
inspect_recursive_p(mrb_state *mrb, mrb_value obj, int n)
103+
{
104+
for (mrb_callinfo *ci=&mrb->c->ci[-1-n]; ci>=mrb->c->cibase; ci--) {
105+
if (ci->mid == MRB_SYM(inspect) &&
106+
mrb_obj_eq(mrb, obj, ci->stack[0])) {
107+
return TRUE;
108+
}
109+
}
110+
return FALSE;
111+
}
112+
113+
mrb_bool
114+
mrb_inspect_recursive_p(mrb_state *mrb, mrb_value obj)
115+
{
116+
return inspect_recursive_p(mrb, obj, 0);
117+
}
118+
119+
static mrb_value
120+
mrb_obj_inspect_recursive_p(mrb_state *mrb, mrb_value obj)
121+
{
122+
return mrb_bool_value(inspect_recursive_p(mrb, obj, 1));
123+
}
124+
101125
/* 15.3.1.3.3 */
102126
/* 15.3.1.3.33 */
103127
/*
@@ -571,6 +595,7 @@ mrb_init_kernel(mrb_state *mrb)
571595
mrb_define_method(mrb, krn, "__to_int", mrb_ensure_int_type, MRB_ARGS_NONE()); /* internal */
572596
mrb_define_method(mrb, krn, "__ENCODING__", mrb_encoding, MRB_ARGS_NONE());
573597
mrb_define_method(mrb, krn, "respond_to_missing?", mrb_false, MRB_ARGS_ARG(1,1));
598+
mrb_define_method(mrb, krn, "__inspect_recursive?", mrb_obj_inspect_recursive_p, MRB_ARGS_NONE());
574599

575600
mrb_include_module(mrb, mrb->object_class, mrb->kernel_module);
576601
}

0 commit comments

Comments
 (0)