From 75bed271f0719642f1866bdbbfa3c9940aad9abb Mon Sep 17 00:00:00 2001 From: shugo Date: Wed, 12 Dec 2012 09:35:50 +0000 Subject: * class.c (rb_prepend_module): move refined methods from the origin of a class to the class, because refinements should have priority over prepended modules. * test/ruby/test_refinement.rb: related test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38344 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- class.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'class.c') diff --git a/class.c b/class.c index ca9bdef41e..cb85580d92 100644 --- a/class.c +++ b/class.c @@ -731,6 +731,33 @@ include_modules_at(VALUE klass, VALUE c, VALUE module) return changed; } +static int +move_refined_method(st_data_t key, st_data_t value, st_data_t data) +{ + rb_method_entry_t *me = (rb_method_entry_t *) value; + st_table *tbl = (st_table *) data; + + if (me->def->type == VM_METHOD_TYPE_REFINED) { + if (me->def->body.orig_me) { + rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me; + me->def->body.orig_me = NULL; + new_me = ALLOC(rb_method_entry_t); + *new_me = *me; + st_add_direct(tbl, key, (st_data_t) new_me); + *me = *orig_me; + xfree(orig_me); + return ST_CONTINUE; + } + else { + st_add_direct(tbl, key, (st_data_t) me); + return ST_DELETE; + } + } + else { + return ST_CONTINUE; + } +} + void rb_prepend_module(VALUE klass, VALUE module) { @@ -754,6 +781,8 @@ rb_prepend_module(VALUE klass, VALUE module) RCLASS_ORIGIN(klass) = origin; RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass); RCLASS_M_TBL(klass) = st_init_numtable(); + st_foreach(RCLASS_M_TBL(origin), move_refined_method, + (st_data_t) RCLASS_M_TBL(klass)); } changed = include_modules_at(klass, klass, module); if (changed < 0) -- cgit v1.2.3