Skip to content

Commit

Permalink
object.c: rb_class_search_ancestor
Browse files Browse the repository at this point in the history
* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45584 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Apr 14, 2014
1 parent c3b82fc commit c2a7a09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ rb_float_new_inline(double d)

/* object.c */
VALUE rb_obj_equal(VALUE obj1, VALUE obj2);
VALUE rb_class_search_ancestor(VALUE klass, VALUE super);

struct RBasicRaw {
VALUE flags;
Expand Down
32 changes: 21 additions & 11 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ class_or_module_required(VALUE c)
return c;
}

static VALUE class_search_ancestor(VALUE cl, VALUE c);

/*
* call-seq:
* obj.instance_of?(class) -> true or false
Expand Down Expand Up @@ -647,15 +649,27 @@ rb_obj_is_kind_of(VALUE obj, VALUE c)
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:
Expand Down Expand Up @@ -1554,16 +1568,12 @@ rb_class_inherited_p(VALUE mod, VALUE arg)
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;
}
Expand Down

0 comments on commit c2a7a09

Please sign in to comment.