summaryrefslogtreecommitdiff
path: root/vm_insnhelper.c
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-06 13:08:41 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-06 13:08:41 +0000
commit60d6038ddaa6678ddcd7ce96ca6c947494b227a6 (patch)
tree0cc16dd0704a2c11acd4e51552565e8b427e0298 /vm_insnhelper.c
parentbd0c636211bb3063d9633d955f8807a1d9490048 (diff)
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method definition, so ci->me is set to a method entry allocated in the stack, and it causes SEGV/ILL. In this commit, a method entry with VM_METHOD_TYPE_REFINED holds the whole original method entry. Furthermore, rb_thread_mark() is changed to mark cfp->klass to avoid GC for iclasses created by copy_refinement_iclass(). * 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_me) 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. * vm.c (rb_thread_mark): mark cfp->klass for iclasses created by copy_refinement_iclass(). * vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass. * 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@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r--vm_insnhelper.c92
1 files changed, 89 insertions, 3 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 75da83e525..7d57b792b9 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,42 @@ 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 *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_me) {
+ ci->me = ci->me->def->body.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 +1921,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 +2015,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 +2023,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);
}
}