* 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
This commit is contained in:
shugo 2012-12-06 13:08:41 +00:00
parent bd0c636211
commit 60d6038dda
14 changed files with 445 additions and 181 deletions

View file

@ -179,7 +179,14 @@ vm_call0_body(rb_thread_t* th, rb_call_info_t *ci, const VALUE *argv)
case VM_METHOD_TYPE_BMETHOD:
return vm_call_bmethod_body(th, ci, argv);
case VM_METHOD_TYPE_ZSUPER:
case VM_METHOD_TYPE_REFINED:
{
if (ci->me->def->type == VM_METHOD_TYPE_REFINED &&
ci->me->def->body.orig_me) {
ci->me = ci->me->def->body.orig_me;
goto again;
}
ci->defined_class = RCLASS_SUPER(ci->defined_class);
if (!ci->defined_class || !(ci->me = rb_method_entry(ci->defined_class, ci->mid, &ci->defined_class))) {
@ -274,8 +281,7 @@ stack_check(void)
}
static inline rb_method_entry_t *
rb_search_method_entry(VALUE refinements, VALUE recv, ID mid,
VALUE *defined_class_ptr);
rb_search_method_entry(VALUE recv, ID mid, VALUE *defined_class_ptr);
static inline int rb_method_call_status(rb_thread_t *th, const rb_method_entry_t *me, call_type scope, VALUE self);
#define NOEX_OK NOEX_NOSUPER
@ -296,11 +302,11 @@ static inline int rb_method_call_status(rb_thread_t *th, const rb_method_entry_t
*/
static inline VALUE
rb_call0(VALUE recv, ID mid, int argc, const VALUE *argv,
call_type scope, VALUE self, VALUE refinements)
call_type scope, VALUE self)
{
VALUE defined_class;
rb_method_entry_t *me =
rb_search_method_entry(refinements, recv, mid, &defined_class);
rb_search_method_entry(recv, mid, &defined_class);
rb_thread_t *th = GET_THREAD();
int call_status = rb_method_call_status(th, me, scope, self);
@ -364,7 +370,7 @@ check_funcall(VALUE recv, ID mid, int argc, VALUE *argv)
}
}
me = rb_search_method_entry(Qnil, recv, mid, &defined_class);
me = rb_search_method_entry(recv, mid, &defined_class);
call_status = rb_method_call_status(th, me, CALL_FCALL, th->cfp->self);
if (call_status != NOEX_OK) {
if (rb_method_basic_definition_p(klass, idMethodMissing)) {
@ -429,8 +435,7 @@ rb_type_str(enum ruby_value_type type)
}
static inline rb_method_entry_t *
rb_search_method_entry(VALUE refinements, VALUE recv, ID mid,
VALUE *defined_class_ptr)
rb_search_method_entry(VALUE recv, ID mid, VALUE *defined_class_ptr)
{
VALUE klass = CLASS_OF(recv);
@ -469,8 +474,7 @@ rb_search_method_entry(VALUE refinements, VALUE recv, ID mid,
rb_id2name(mid), type, (void *)recv, flags, klass);
}
}
return rb_method_entry_get_with_refinements(refinements, klass, mid,
defined_class_ptr);
return rb_method_entry(klass, mid, defined_class_ptr);
}
static inline int
@ -532,7 +536,7 @@ static inline VALUE
rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
{
rb_thread_t *th = GET_THREAD();
return rb_call0(recv, mid, argc, argv, scope, th->cfp->self, Qnil);
return rb_call0(recv, mid, argc, argv, scope, th->cfp->self);
}
NORETURN(static void raise_method_missing(rb_thread_t *th, int argc, const VALUE *argv,
@ -785,11 +789,23 @@ rb_funcall_passing_block_with_refinements(VALUE recv, ID mid, int argc,
const VALUE *argv,
VALUE refinements)
{
rb_thread_t *th = GET_THREAD();
VALUE defined_class;
rb_method_entry_t *me =
rb_search_method_entry(recv, mid, &defined_class);
rb_thread_t *th;
int call_status;
PASS_PASSED_BLOCK_TH(th);
return rb_call0(recv, mid, argc, argv, CALL_PUBLIC, th->cfp->self,
refinements);
if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
me = rb_resolve_refined_method(refinements, me, &defined_class);
}
PASS_PASSED_BLOCK_TH(GET_THREAD());
th = GET_THREAD();
call_status = rb_method_call_status(th, me, CALL_PUBLIC, th->cfp->self);
if (call_status != NOEX_OK) {
return method_missing(recv, mid, argc, argv, call_status);
}
stack_check();
return vm_call0(th, recv, mid, argc, argv, me, defined_class);
}
static VALUE
@ -823,7 +839,7 @@ send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope)
id = rb_to_id(vid);
}
PASS_PASSED_BLOCK_TH(th);
return rb_call0(recv, id, argc, argv, scope, self, Qnil);
return rb_call0(recv, id, argc, argv, scope, self);
}
/*