diff options
author | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-29 12:50:10 +0000 |
---|---|---|
committer | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-29 12:50:10 +0000 |
commit | 421314cf4e532cd4b840924b73f73fe33f25647b (patch) | |
tree | 0be26d7e4bdb9b4ef264f4c0ca3260c390b29c69 /vm_insnhelper.c | |
parent | bda0aca2afa263022876432c5a91259193192a25 (diff) | |
download | ruby-421314cf4e532cd4b840924b73f73fe33f25647b.tar.gz |
* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_def) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.
* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.
* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.
* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.
* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.
* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.
* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.
* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37993 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r-- | vm_insnhelper.c | 94 |
1 files changed, 91 insertions, 3 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 223986f19e..241313a301 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1694,6 +1694,47 @@ vm_call_method_missing(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_inf return vm_call_method(th, reg_cfp, &ci_entry); } +static VALUE +copy_refinement_iclass(VALUE iclass, VALUE superclass) +{ + VALUE result, c; + + Check_Type(iclass, T_ICLASS); + c = result = rb_include_class_new(RBASIC(iclass)->klass, superclass); + RCLASS_REFINED_CLASS(c) = RCLASS_REFINED_CLASS(iclass); + iclass = RCLASS_SUPER(iclass); + while (iclass && BUILTIN_TYPE(iclass) == T_ICLASS) { + c = RCLASS_SUPER(c) = rb_include_class_new(RBASIC(iclass)->klass, + RCLASS_SUPER(c)); + RCLASS_REFINED_CLASS(c) = RCLASS_REFINED_CLASS(iclass); + iclass = RCLASS_SUPER(iclass); + } + return result; +} + +static VALUE +find_refinement(VALUE refinements, VALUE klass) +{ + VALUE refinement; + + if (NIL_P(refinements)) { + return Qnil; + } + refinement = rb_hash_lookup(refinements, klass); + if (NIL_P(refinement) && + BUILTIN_TYPE(klass) == T_ICLASS) { + refinement = rb_hash_lookup(refinements, + RBASIC(klass)->klass); + if (!NIL_P(refinement)) { + refinement = copy_refinement_iclass(refinement, klass); + } + } + return refinement; +} + +static int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2); +static VALUE vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci); + static inline VALUE vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) { @@ -1735,7 +1776,10 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) return vm_call_bmethod(th, cfp, ci); } case VM_METHOD_TYPE_ZSUPER:{ - VALUE klass = RCLASS_SUPER(ci->me->klass); + VALUE klass; + + zsuper_method_dispatch: + klass = RCLASS_SUPER(ci->me->klass); ci_temp = *ci; ci = &ci_temp; @@ -1778,6 +1822,44 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) break; case VM_METHOD_TYPE_UNDEF: break; + case VM_METHOD_TYPE_REFINED:{ + NODE *cref = rb_vm_get_cref(cfp->iseq, cfp->ep); + VALUE refinements = cref ? cref->nd_refinements : Qnil; + VALUE refinement, defined_class; + rb_method_entry_t orig_me, *me; + ci_temp = *ci; + ci = &ci_temp; + + refinement = find_refinement(refinements, + ci->defined_class); + if (NIL_P(refinement)) { + goto no_refinement_dispatch; + } + me = rb_method_entry(refinement, ci->mid, &defined_class); + if (me) { + if (ci->call == vm_call_super_method && + cfp->me && + rb_method_definition_eq(me->def, cfp->me->def)) { + goto no_refinement_dispatch; + } + ci->me = me; + ci->defined_class = defined_class; + if (me->def->type != VM_METHOD_TYPE_REFINED) { + goto normal_method_dispatch; + } + } + + no_refinement_dispatch: + if (ci->me->def->body.orig_def) { + orig_me = *ci->me; + orig_me.def = ci->me->def->body.orig_def; + ci->me = &orig_me; + goto normal_method_dispatch; + } + else { + goto zsuper_method_dispatch; + } + } } rb_bug("vm_call_method: unsupported method type (%d)", ci->me->def->type); } @@ -1841,6 +1923,12 @@ vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci return vm_call_method(th, reg_cfp, ci); } +static VALUE +vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) +{ + return vm_call_method(th, reg_cfp, ci); +} + /* super */ static inline VALUE @@ -1929,7 +2017,7 @@ vm_search_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_inf /* TODO: use inline cache */ ci->me = rb_method_entry(ci->klass, ci->mid, &ci->defined_class); - ci->call = vm_call_general; + ci->call = vm_call_super_method; while (iseq && !iseq->klass) { iseq = iseq->parent_iseq; @@ -1937,7 +2025,7 @@ vm_search_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_inf if (ci->me && ci->me->def->type == VM_METHOD_TYPE_ISEQ && ci->me->def->body.iseq == iseq) { ci->klass = RCLASS_SUPER(ci->defined_class); - ci->me = rb_method_entry_get_with_refinements(Qnil, ci->klass, ci->mid, &ci->defined_class); + ci->me = rb_method_entry(ci->klass, ci->mid, &ci->defined_class); } } |