ruby-changes:33503
From: nobu <ko1@a...>
Date: Mon, 14 Apr 2014 16:59:49 +0900 (JST)
Subject: [ruby-changes:33503] nobu:r45584 (trunk): object.c: rb_class_search_ancestor
nobu 2014-04-14 16:59:42 +0900 (Mon, 14 Apr 2014) New Revision: 45584 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45584 Log: object.c: rb_class_search_ancestor * object.c (rb_class_search_ancestor): return ancestor class or iclass if inherited. * object.c (rb_obj_is_kind_of, rb_class_inherited_p): share function to search the ancestor. Modified files: trunk/internal.h trunk/object.c Index: object.c =================================================================== --- object.c (revision 45583) +++ object.c (revision 45584) @@ -587,6 +587,8 @@ class_or_module_required(VALUE c) https://github.com/ruby/ruby/blob/trunk/object.c#L587 return c; } +static VALUE class_search_ancestor(VALUE cl, VALUE c); + /* * call-seq: * obj.instance_of?(class) -> true or false @@ -647,15 +649,27 @@ rb_obj_is_kind_of(VALUE obj, VALUE c) https://github.com/ruby/ruby/blob/trunk/object.c#L649 VALUE cl = CLASS_OF(obj); c = class_or_module_required(c); - c = RCLASS_ORIGIN(c); + return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse; +} + +static VALUE +class_search_ancestor(VALUE cl, VALUE c) +{ while (cl) { if (cl == c || RCLASS_M_TBL_WRAPPER(cl) == RCLASS_M_TBL_WRAPPER(c)) - return Qtrue; + return cl; cl = RCLASS_SUPER(cl); } - return Qfalse; + return 0; } +VALUE +rb_class_search_ancestor(VALUE cl, VALUE c) +{ + cl = class_or_module_required(cl); + c = class_or_module_required(c); + return class_search_ancestor(cl, RCLASS_ORIGIN(c)); +} /* * call-seq: @@ -1554,16 +1568,12 @@ rb_class_inherited_p(VALUE mod, VALUE ar https://github.com/ruby/ruby/blob/trunk/object.c#L1568 rb_raise(rb_eTypeError, "compared with non class/module"); } arg = RCLASS_ORIGIN(arg); - while (mod) { - if (RCLASS_M_TBL_WRAPPER(mod) == RCLASS_M_TBL_WRAPPER(arg)) - return Qtrue; - mod = RCLASS_SUPER(mod); + if (class_search_ancestor(mod, arg)) { + return Qtrue; } /* not mod < arg; check if mod > arg */ - while (arg) { - if (RCLASS_M_TBL_WRAPPER(arg) == RCLASS_M_TBL_WRAPPER(start)) - return Qfalse; - arg = RCLASS_SUPER(arg); + if (class_search_ancestor(arg, start)) { + return Qfalse; } return Qnil; } Index: internal.h =================================================================== --- internal.h (revision 45583) +++ internal.h (revision 45584) @@ -715,6 +715,7 @@ rb_float_new_inline(double d) https://github.com/ruby/ruby/blob/trunk/internal.h#L715 /* object.c */ VALUE rb_obj_equal(VALUE obj1, VALUE obj2); +VALUE rb_class_search_ancestor(VALUE klass, VALUE super); struct RBasicRaw { VALUE flags; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/