1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16206 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-04-26 10:52:56 +00:00
parent f2323f8869
commit 5f909db83b
4 changed files with 92 additions and 29 deletions

View file

@ -1,3 +1,13 @@
Sat Apr 26 19:40:34 2008 Guy Decoux <decoux@moulon.inra.fr>
* 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@iDaemons.org>
* eval.c (rb_yield_0, proc_invoke, proc_arity): allow passing a block

73
class.c
View file

@ -51,6 +51,7 @@ rb_class_new(super)
struct clone_method_data {
st_table *tbl;
VALUE klass;
VALUE cref;
};
static int
@ -61,16 +62,33 @@ clone_method(mid, body, data)
{
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_mod_init_copy(clone, orig)
{
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 @@ rb_mod_init_copy(clone, orig)
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 @@ rb_class_init_copy(clone, orig)
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 @@ rb_singleton_class_clone(obj)
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;

36
eval.c
View file

@ -8430,6 +8430,19 @@ proc_dup(self)
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 @@ method_clone(self)
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

View file

@ -193,6 +193,8 @@ VALUE rb_require_safe _((VALUE, int));
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));