diff options
author | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-06 13:08:41 +0000 |
---|---|---|
committer | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-06 13:08:41 +0000 |
commit | 60d6038ddaa6678ddcd7ce96ca6c947494b227a6 (patch) | |
tree | 0cc16dd0704a2c11acd4e51552565e8b427e0298 /vm_method.c | |
parent | bd0c636211bb3063d9633d955f8807a1d9490048 (diff) | |
download | ruby-60d6038ddaa6678ddcd7ce96ca6c947494b227a6.tar.gz |
* 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_method.c')
-rw-r--r-- | vm_method.c | 333 |
1 files changed, 186 insertions, 147 deletions
diff --git a/vm_method.c b/vm_method.c index 4b4be139a7..2db6000691 100644 --- a/vm_method.c +++ b/vm_method.c @@ -4,7 +4,7 @@ #define CACHE_SIZE 0x800 #define CACHE_MASK 0x7ff -#define EXPR1(c,o,m) ((((c)>>3)^((o)>>3)^(m))&CACHE_MASK) +#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK) #define NOEX_NOREDEF 0 #ifndef NOEX_NOREDEF @@ -21,7 +21,6 @@ struct cache_entry { /* method hash table. */ VALUE filled_version; /* filled state version */ ID mid; /* method's id */ VALUE klass; /* receiver's class */ - VALUE refinements; /* refinements */ rb_method_entry_t *me; VALUE defined_class; }; @@ -148,26 +147,78 @@ rb_sweep_method_entry(void *pvm) } } -void -rb_free_method_entry(rb_method_entry_t *me) +static void +release_method_definition(rb_method_definition_t *def) { - rb_method_definition_t *def = me->def; - - if (def) { - if (def->alias_count == 0) { - xfree(def); - } - else if (def->alias_count > 0) { - def->alias_count--; + if (def == 0) + return; + if (def->alias_count == 0) { + if (def->type == VM_METHOD_TYPE_REFINED && + def->body.orig_me) { + release_method_definition(def->body.orig_me->def); + xfree(def->body.orig_me); } - me->def = 0; + xfree(def); } + else if (def->alias_count > 0) { + def->alias_count--; + } +} + +void +rb_free_method_entry(rb_method_entry_t *me) +{ + release_method_definition(me->def); xfree(me); } static int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2); -void rb_redefine_opt_method(VALUE, ID); +static inline rb_method_entry_t * +lookup_method_table(VALUE klass, ID id) +{ + st_data_t body; + st_table *m_tbl = RCLASS_M_TBL(klass); + if (st_lookup(m_tbl, id, &body)) { + return (rb_method_entry_t *) body; + } + else { + return 0; + } +} + +static void +make_method_entry_refined(rb_method_entry_t *me) +{ + rb_method_definition_t *new_def; + + if (me->def && me->def->type == VM_METHOD_TYPE_REFINED) + return; + + new_def = ALLOC(rb_method_definition_t); + new_def->type = VM_METHOD_TYPE_REFINED; + new_def->original_id = me->called_id; + new_def->alias_count = 0; + new_def->body.orig_me = ALLOC(rb_method_entry_t); + *new_def->body.orig_me = *me; + rb_vm_check_redefinition_opt_method(me, me->klass); + if (me->def) me->def->alias_count++; + me->def = new_def; +} + +void +rb_add_refined_method_entry(VALUE refined_class, ID mid) +{ + rb_method_entry_t *me = lookup_method_table(refined_class, mid); + + if (me) { + make_method_entry_refined(me); + } + else { + rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, + NOEX_PUBLIC); + } +} static rb_method_entry_t * rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, @@ -179,6 +230,7 @@ rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, #endif st_table *mtbl; st_data_t data; + int make_refined = 0; if (NIL_P(klass)) { klass = rb_cObject; @@ -201,14 +253,19 @@ rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, rklass = klass; #endif if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) { - ID id_refined_class; - VALUE refined_class; + VALUE refined_class = + rb_refinement_module_get_refined_class(klass); - CONST_ID(id_refined_class, "__refined_class__"); - refined_class = rb_ivar_get(klass, id_refined_class); - rb_redefine_opt_method(refined_class, mid); + rb_add_refined_method_entry(refined_class, mid); + } + if (type == VM_METHOD_TYPE_REFINED) { + rb_method_entry_t *old_me = + lookup_method_table(RCLASS_ORIGIN(klass), mid); + if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass); + } + else { + klass = RCLASS_ORIGIN(klass); } - klass = RCLASS_ORIGIN(klass); mtbl = RCLASS_M_TBL(klass); /* check re-definition */ @@ -224,6 +281,8 @@ rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, } #endif rb_vm_check_redefinition_opt_method(old_me, klass); + if (old_def->type == VM_METHOD_TYPE_REFINED) + make_refined = 1; if (RTEST(ruby_verbose) && type != VM_METHOD_TYPE_UNDEF && @@ -276,6 +335,10 @@ rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, } } + if (make_refined) { + make_method_entry_refined(me); + } + st_insert(mtbl, mid, (st_data_t) me); return me; @@ -343,7 +406,12 @@ rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_ int line; rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, 0, noex); rb_method_definition_t *def = ALLOC(rb_method_definition_t); - me->def = def; + if (me->def && me->def->type == VM_METHOD_TYPE_REFINED) { + me->def->body.orig_me->def = def; + } + else { + me->def = def; + } def->type = type; def->original_id = mid; def->alias_count = 0; @@ -381,10 +449,13 @@ rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_ case VM_METHOD_TYPE_ZSUPER: case VM_METHOD_TYPE_UNDEF: break; + case VM_METHOD_TYPE_REFINED: + def->body.orig_me = (rb_method_entry_t *) opts; + break; default: rb_bug("rb_add_method: unsupported method type (%d)\n", type); } - if (type != VM_METHOD_TYPE_UNDEF) { + if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) { method_added(klass, mid); } return me; @@ -427,86 +498,18 @@ rb_get_alloc_func(VALUE klass) return 0; } -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 inline int -lookup_method_table(VALUE klass, ID id, st_data_t *body) -{ - st_table *m_tbl = RCLASS_M_TBL(klass); - if (!m_tbl) { - m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(RBASIC(klass)->klass)); - } - return st_lookup(m_tbl, id, body); -} - static inline rb_method_entry_t* -search_method_with_refinements(VALUE klass, ID id, VALUE refinements, - VALUE *defined_class_ptr) +search_method(VALUE klass, ID id, VALUE *defined_class_ptr) { - st_data_t body; - VALUE iclass, skipped_class = Qnil; - - for (body = 0; klass; klass = RCLASS_SUPER(klass)) { - if (klass != skipped_class) { - iclass = rb_hash_lookup(refinements, klass); - if (NIL_P(iclass) && BUILTIN_TYPE(klass) == T_ICLASS) { - iclass = rb_hash_lookup(refinements, RBASIC(klass)->klass); - if (!NIL_P(iclass)) - iclass = copy_refinement_iclass(iclass, klass); - } - if (!NIL_P(iclass)) { - skipped_class = klass; - klass = iclass; - } - } - if (lookup_method_table(klass, id, &body)) break; - } - - if (defined_class_ptr) - *defined_class_ptr = klass; - return (rb_method_entry_t *)body; -} - -static inline rb_method_entry_t* -search_method_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr) -{ - st_data_t body; + rb_method_entry_t *me; - for (body = 0; klass; klass = RCLASS_SUPER(klass)) { - if (lookup_method_table(klass, id, &body)) break; + for (me = 0; klass; klass = RCLASS_SUPER(klass)) { + if ((me = lookup_method_table(klass, id)) != 0) break; } if (defined_class_ptr) *defined_class_ptr = klass; - return (rb_method_entry_t *)body; -} - -static rb_method_entry_t* -search_method(VALUE klass, ID id, VALUE refinements, VALUE *defined_class_ptr) -{ - if (NIL_P(refinements)) { - return search_method_without_refinements(klass, id, defined_class_ptr); - } - else { - return search_method_with_refinements(klass, id, refinements, - defined_class_ptr); - } + return me; } /* @@ -516,19 +519,17 @@ search_method(VALUE klass, ID id, VALUE refinements, VALUE *defined_class_ptr) * rb_method_entry() simply. */ rb_method_entry_t * -rb_method_entry_get_without_cache(VALUE klass, VALUE refinements, ID id, +rb_method_entry_get_without_cache(VALUE klass, ID id, VALUE *defined_class_ptr) { VALUE defined_class; - rb_method_entry_t *me = search_method(klass, id, refinements, - &defined_class); + rb_method_entry_t *me = search_method(klass, id, &defined_class); if (ruby_running) { struct cache_entry *ent; - ent = cache + EXPR1(klass, refinements, id); + ent = cache + EXPR1(klass, id); ent->filled_version = GET_VM_STATE_VERSION(); ent->klass = klass; - ent->refinements = refinements; ent->defined_class = defined_class; if (UNDEFINED_METHOD_ENTRY_P(me)) { @@ -548,37 +549,84 @@ rb_method_entry_get_without_cache(VALUE klass, VALUE refinements, ID id, } rb_method_entry_t * -rb_method_entry_get_with_refinements(VALUE refinements, VALUE klass, ID id, - VALUE *defined_class_ptr) +rb_method_entry(VALUE klass, ID id, VALUE *defined_class_ptr) { #if OPT_GLOBAL_METHOD_CACHE struct cache_entry *ent; - ent = cache + EXPR1(klass, refinements, id); + ent = cache + EXPR1(klass, id); if (ent->filled_version == GET_VM_STATE_VERSION() && - ent->mid == id && ent->klass == klass && - ent->refinements == refinements) { + ent->mid == id && ent->klass == klass) { if (defined_class_ptr) *defined_class_ptr = ent->defined_class; return ent->me; } #endif - return rb_method_entry_get_without_cache(klass, refinements, id, + return rb_method_entry_get_without_cache(klass, id, defined_class_ptr); +} + +static rb_method_entry_t * +get_original_method_entry(VALUE refinements, + rb_method_entry_t *me, + VALUE *defined_class_ptr) +{ + if (me->def->body.orig_me) { + return me->def->body.orig_me; + } + else { + rb_method_entry_t *tmp_me; + tmp_me = rb_method_entry(RCLASS_SUPER(me->klass), me->called_id, + defined_class_ptr); + return rb_resolve_refined_method(refinements, tmp_me, + defined_class_ptr); + } +} + +rb_method_entry_t * +rb_resolve_refined_method(VALUE refinements, rb_method_entry_t *me, + VALUE *defined_class_ptr) +{ + if (me && me->def->type == VM_METHOD_TYPE_REFINED) { + VALUE refinement; + rb_method_entry_t *tmp_me; + + refinement = find_refinement(refinements, me->klass); + if (NIL_P(refinement)) { + return get_original_method_entry(refinements, me, + defined_class_ptr); + } + tmp_me = rb_method_entry(refinement, me->called_id, + defined_class_ptr); + if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) { + return tmp_me; + } + else { + return get_original_method_entry(refinements, me, defined_class_ptr); + } + } + else { + return me; + } } rb_method_entry_t * -rb_method_entry(VALUE klass, ID id, VALUE *defined_class_ptr) +rb_method_entry_with_refinements(VALUE klass, ID id, + VALUE *defined_class_ptr) { - NODE *cref = rb_vm_cref(); - VALUE refinements = Qnil; + VALUE defined_class; + rb_method_entry_t *me = rb_method_entry(klass, id, &defined_class); + + if (me && me->def->type == VM_METHOD_TYPE_REFINED) { + NODE *cref = rb_vm_cref(); + VALUE refinements = cref ? cref->nd_refinements : Qnil; - if (cref && !NIL_P(cref->nd_refinements)) { - refinements = cref->nd_refinements; + me = rb_resolve_refined_method(refinements, me, &defined_class); } - return rb_method_entry_get_with_refinements(refinements, klass, id, - defined_class_ptr); + if (defined_class_ptr) + *defined_class_ptr = defined_class; + return me; } static void @@ -677,9 +725,9 @@ rb_export_method(VALUE klass, ID name, rb_method_flag_t noex) rb_secure(4); } - me = search_method(klass, name, Qnil, &defined_class); + me = search_method(klass, name, &defined_class); if (!me && RB_TYPE_P(klass, T_MODULE)) { - me = search_method(rb_cObject, name, Qnil, &defined_class); + me = search_method(rb_cObject, name, &defined_class); } if (UNDEFINED_METHOD_ENTRY_P(me)) { @@ -691,6 +739,9 @@ rb_export_method(VALUE klass, ID name, rb_method_flag_t noex) if (klass == defined_class) { me->flag = noex; + if (me->def->type == VM_METHOD_TYPE_REFINED) { + me->def->body.orig_me->flag = noex; + } } else { rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, noex); @@ -701,7 +752,8 @@ rb_export_method(VALUE klass, ID name, rb_method_flag_t noex) int rb_method_boundp(VALUE klass, ID id, int ex) { - rb_method_entry_t *me = rb_method_entry(klass, id, 0); + rb_method_entry_t *me = + rb_method_entry_with_refinements(klass, id, 0); if (me != 0) { if ((ex & ~NOEX_RESPONDS) && @@ -767,9 +819,6 @@ void rb_undef(VALUE klass, ID id) { rb_method_entry_t *me; - NODE *cref = rb_vm_cref(); - VALUE refinements = Qnil; - void rb_using_refinement(NODE *cref, VALUE klass, VALUE module); if (NIL_P(klass)) { rb_raise(rb_eTypeError, "no class to undef method"); @@ -785,10 +834,7 @@ rb_undef(VALUE klass, ID id) rb_warn("undefining `%s' may cause serious problems", rb_id2name(id)); } - if (cref && !NIL_P(cref->nd_refinements)) { - refinements = cref->nd_refinements; - } - me = search_method(klass, id, refinements, 0); + me = search_method(klass, id, 0); if (UNDEFINED_METHOD_ENTRY_P(me)) { const char *s0 = " class"; @@ -809,11 +855,6 @@ rb_undef(VALUE klass, ID id) rb_id2name(id), s0, rb_class2name(c)); } - if (!RTEST(rb_class_inherited_p(klass, me->klass))) { - VALUE mod = rb_module_new(); - rb_using_refinement(cref, klass, mod); - klass = mod; - } rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, NOEX_PUBLIC); CALL_METHOD_HOOK(klass, undefined, id); @@ -1035,6 +1076,10 @@ rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2) static int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2) { + if (d1 && d1->type == VM_METHOD_TYPE_REFINED) + d1 = d1->body.orig_me->def; + if (d2 && d2->type == VM_METHOD_TYPE_REFINED) + d2 = d2->body.orig_me->def; if (d1 == d2) return 1; if (!d1 || !d2) return 0; if (d1->type != d2->type) { @@ -1069,6 +1114,7 @@ rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_defini static st_index_t rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def) { + again: hash = rb_hash_uint(hash, def->type); switch (def->type) { case VM_METHOD_TYPE_ISEQ: @@ -1089,6 +1135,14 @@ rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def) return hash; case VM_METHOD_TYPE_OPTIMIZED: return rb_hash_uint(hash, def->body.optimize_type); + case VM_METHOD_TYPE_REFINED: + if (def->body.orig_me) { + def = def->body.orig_me->def; + goto again; + } + else { + return hash; + } default: rb_bug("rb_hash_method_definition: unsupported method type (%d)\n", def->type); } @@ -1118,11 +1172,11 @@ rb_alias(VALUE klass, ID name, ID def) } again: - orig_me = search_method(klass, def, Qnil, 0); + orig_me = search_method(klass, def, 0); if (UNDEFINED_METHOD_ENTRY_P(orig_me)) { if ((!RB_TYPE_P(klass, T_MODULE)) || - (orig_me = search_method(rb_cObject, def, Qnil, 0), + (orig_me = search_method(rb_cObject, def, 0), UNDEFINED_METHOD_ENTRY_P(orig_me))) { rb_print_undef(klass, def, 0); } @@ -1398,9 +1452,9 @@ rb_mod_modfunc(int argc, VALUE *argv, VALUE module) id = rb_to_id(argv[i]); for (;;) { - me = search_method(m, id, Qnil, 0); + me = search_method(m, id, 0); if (me == 0) { - me = search_method(rb_cObject, id, Qnil, 0); + me = search_method(rb_cObject, id, 0); } if (UNDEFINED_METHOD_ENTRY_P(me)) { rb_print_undef(module, id, 0); @@ -1519,21 +1573,6 @@ obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv) } void -rb_redefine_opt_method(VALUE klass, ID mid) -{ - st_data_t data; - rb_method_entry_t *me = 0; - VALUE origin = RCLASS_ORIGIN(klass); - - if (!st_lookup(RCLASS_M_TBL(origin), mid, &data) || - !(me = (rb_method_entry_t *)data) || - (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF)) { - return; - } - rb_vm_check_redefinition_opt_method(me, origin); -} - -void Init_eval_method(void) { #undef rb_intern |