ruby-changes:4712
From: ko1@a...
Date: Sat, 26 Apr 2008 19:53:11 +0900 (JST)
Subject: [ruby-changes:4712] knu - Ruby:r16206 (ruby_1_8): * class.c (struct clone_method_data): Add cref.
knu 2008-04-26 19:52:56 +0900 (Sat, 26 Apr 2008) New Revision: 16206 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/class.c branches/ruby_1_8/eval.c branches/ruby_1_8/intern.h Log: * class.c (struct clone_method_data): Add cref. (clone_method): Properly handle NODE_BMETHOD and NODE_DMETHOD. (rb_singleton_class_clone, singleton_class_clone_int): Set a proper value to klass and propagate cref. [ruby-core:16238] * eval.c (rb_block_dup, rb_method_dup), intern.h: Add duplicator methods for use from class.c#clone_method(). http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/intern.h?r1=16206&r2=16205&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16206&r2=16205&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/class.c?r1=16206&r2=16205&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/eval.c?r1=16206&r2=16205&diff_format=u Index: ruby_1_8/intern.h =================================================================== --- ruby_1_8/intern.h (revision 16205) +++ ruby_1_8/intern.h (revision 16206) @@ -193,6 +193,8 @@ void rb_obj_call_init _((VALUE, int, VALUE*)); VALUE rb_class_new_instance _((int, VALUE*, VALUE)); VALUE rb_block_proc _((void)); +VALUE rb_block_dup _((VALUE, VALUE, VALUE)); +VALUE rb_method_dup _((VALUE, VALUE, VALUE)); VALUE rb_f_lambda _((void)); VALUE rb_proc_call _((VALUE, VALUE)); VALUE rb_obj_method _((VALUE, VALUE)); Index: ruby_1_8/class.c =================================================================== --- ruby_1_8/class.c (revision 16205) +++ ruby_1_8/class.c (revision 16206) @@ -51,6 +51,7 @@ struct clone_method_data { st_table *tbl; VALUE klass; + VALUE cref; }; static int @@ -61,16 +62,33 @@ { NODE *fbody = body->nd_body; - if (fbody && nd_type(fbody) == NODE_SCOPE) { - VALUE cref = data->klass ? - (VALUE)NEW_NODE(NODE_CREF,data->klass,0,fbody->nd_rval) : - fbody->nd_rval; - fbody = NEW_NODE(NODE_SCOPE, fbody->nd_tbl, cref, fbody->nd_next); + if (fbody && data->cref) { + VALUE body; + + switch (nd_type(fbody)) { + case NODE_SCOPE: + if (fbody->nd_rval) { + NODE *tmp = NEW_NODE(nd_type(fbody->u2.node), data->cref, + fbody->u2.node->u2.node, fbody->u2.node->u3.node); + fbody = NEW_NODE(nd_type(fbody), fbody->u1.node, tmp, fbody->u3.node); + } + break; + case NODE_BMETHOD: + body = rb_block_dup(fbody->nd_cval, data->klass, data->cref); + fbody = NEW_BMETHOD(body); + break; + case NODE_DMETHOD: + body = rb_method_dup(fbody->nd_cval, data->klass, data->cref); + fbody = NEW_DMETHOD(body); + break; + } } st_insert(data->tbl, mid, (st_data_t)NEW_METHOD(fbody, body->nd_noex)); return ST_CONTINUE; } +static VALUE singleton_class_clone_int _((VALUE, VALUE)); + /* :nodoc: */ VALUE rb_mod_init_copy(clone, orig) @@ -78,8 +96,7 @@ { rb_obj_init_copy(clone, orig); if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) { - RBASIC(clone)->klass = RBASIC(orig)->klass; - RBASIC(clone)->klass = rb_singleton_class_clone(clone); + RBASIC(clone)->klass = singleton_class_clone_int(orig, clone); } RCLASS(clone)->super = RCLASS(orig)->super; if (RCLASS(orig)->iv_tbl) { @@ -94,9 +111,10 @@ if (RCLASS(orig)->m_tbl) { struct clone_method_data data; - data.tbl = RCLASS(clone)->m_tbl = st_init_numtable(); - data.klass = (VALUE)clone; - + RCLASS(clone)->m_tbl = st_init_numtable(); + data.tbl = RCLASS(clone)->m_tbl; + data.klass = clone; + data.cref = clone; st_foreach(RCLASS(orig)->m_tbl, clone_method, (st_data_t)&data); } @@ -117,15 +135,16 @@ return rb_mod_init_copy(clone, orig); } -VALUE -rb_singleton_class_clone(obj) - VALUE obj; +static VALUE +singleton_class_clone_int(obj, cref) + VALUE obj, cref; { VALUE klass = RBASIC(obj)->klass; if (!FL_TEST(klass, FL_SINGLETON)) return klass; else { + struct clone_method_data data; /* copy singleton(unnamed) class */ NEWOBJ(clone, struct RClass); OBJSETUP(clone, 0, RBASIC(klass)->flags); @@ -143,28 +162,24 @@ if (RCLASS(klass)->iv_tbl) { clone->iv_tbl = st_copy(RCLASS(klass)->iv_tbl); } - { - struct clone_method_data data; - - data.tbl = clone->m_tbl = st_init_numtable(); - switch (TYPE(obj)) { - case T_CLASS: - case T_MODULE: - data.klass = obj; - break; - default: - data.klass = 0; - break; - } - - st_foreach(RCLASS(klass)->m_tbl, clone_method, (st_data_t)&data); - } + clone->m_tbl = st_init_numtable(); + data.tbl = clone->m_tbl; + data.klass = (VALUE)clone; + data.cref = cref; + st_foreach(RCLASS(klass)->m_tbl, clone_method, (st_data_t)&data); rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone); FL_SET(clone, FL_SINGLETON); return (VALUE)clone; } } +VALUE +rb_singleton_class_clone(obj) + VALUE obj; +{ + return singleton_class_clone_int(obj, 0); +} + void rb_singleton_class_attached(klass, obj) VALUE klass, obj; Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 16205) +++ ruby_1_8/ChangeLog (revision 16206) @@ -1,3 +1,13 @@ +Sat Apr 26 19:40:34 2008 Guy Decoux <decoux@m...> + + * class.c (struct clone_method_data): Add cref. + (clone_method): Properly handle NODE_BMETHOD and NODE_DMETHOD. + (rb_singleton_class_clone, singleton_class_clone_int): Set a + proper value to klass and propagate cref. [ruby-core:16238] + + * eval.c (rb_block_dup, rb_method_dup), intern.h: Add duplicator + methods for use from class.c#clone_method(). + Sat Apr 26 19:33:56 2008 Akinori MUSHA <knu@i...> * eval.c (rb_yield_0, proc_invoke, proc_arity): allow passing a block Index: ruby_1_8/eval.c =================================================================== --- ruby_1_8/eval.c (revision 16205) +++ ruby_1_8/eval.c (revision 16206) @@ -8430,6 +8430,19 @@ return bind; } +VALUE +rb_block_dup(self, klass, cref) + VALUE self, klass, cref; +{ + struct BLOCK *block; + VALUE obj = proc_dup(self); + Data_Get_Struct(obj, struct BLOCK, block); + block->klass = klass; + block->cref = NEW_NODE(nd_type(block->cref), cref, block->cref->u2.node, + block->cref->u3.node); + return obj; +} + /* * call-seq: * binding -> a_binding @@ -9363,6 +9376,29 @@ return clone; } +VALUE +rb_method_dup(self, klass, cref) + VALUE self; + VALUE klass; + VALUE cref; +{ + VALUE clone; + struct METHOD *orig, *data; + + Data_Get_Struct(self, struct METHOD, orig); + clone = Data_Make_Struct(CLASS_OF(self),struct METHOD, bm_mark, free, data); + *data = *orig; + data->rklass = klass; + if (data->body->nd_rval) { + NODE *tmp = NEW_NODE(nd_type(data->body->u2.node), cref, + data->body->u2.node->u2.node, + data->body->u2.node->u3.node); + data->body = NEW_NODE(nd_type(data->body), data->body->u1.node, tmp, + data->body->u3.node); + } + return clone; +} + /* * call-seq: * meth.call(args, ...) => obj -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/