mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
object.c: singleton class clone
* object.c (rb_obj_clone): attach clone to its singleton class during cloning singleton class so that singleton_method_added will be called on it. based on the patch by shiba (satoshi shiba)[Bug #5283] in [ruby-dev:44477]. [Bug #5283] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38648 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1180302200
commit
ad6f06aed8
5 changed files with 32 additions and 1 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Sat Dec 29 11:37:36 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* object.c (rb_obj_clone): attach clone to its singleton class during
|
||||||
|
cloning singleton class so that singleton_method_added will be called
|
||||||
|
on it. based on the patch by shiba (satoshi shiba)[Bug #5283] in
|
||||||
|
[ruby-dev:44477]. [Bug #5283]
|
||||||
|
|
||||||
Sat Dec 29 10:10:39 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Dec 29 10:10:39 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* configure.in (crt_externs.h): use standard macro AC_CHECK_HEADERS.
|
* configure.in (crt_externs.h): use standard macro AC_CHECK_HEADERS.
|
||||||
|
|
9
class.c
9
class.c
|
@ -239,6 +239,12 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_singleton_class_clone(VALUE obj)
|
rb_singleton_class_clone(VALUE obj)
|
||||||
|
{
|
||||||
|
return rb_singleton_class_clone_and_attach(obj, Qundef);
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
|
||||||
{
|
{
|
||||||
VALUE klass = RBASIC(obj)->klass;
|
VALUE klass = RBASIC(obj)->klass;
|
||||||
|
|
||||||
|
@ -264,6 +270,9 @@ rb_singleton_class_clone(VALUE obj)
|
||||||
RCLASS_CONST_TBL(clone) = st_init_numtable();
|
RCLASS_CONST_TBL(clone) = st_init_numtable();
|
||||||
st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
|
st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
|
||||||
}
|
}
|
||||||
|
if (attach != Qundef) {
|
||||||
|
rb_singleton_class_attached(clone, attach);
|
||||||
|
}
|
||||||
RCLASS_M_TBL(clone) = st_init_numtable();
|
RCLASS_M_TBL(clone) = st_init_numtable();
|
||||||
st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
|
st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
|
||||||
rb_singleton_class_attached(RBASIC(clone)->klass, clone);
|
rb_singleton_class_attached(RBASIC(clone)->klass, clone);
|
||||||
|
|
|
@ -63,6 +63,7 @@ VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj);
|
||||||
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj);
|
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj);
|
||||||
int rb_obj_basic_to_s_p(VALUE);
|
int rb_obj_basic_to_s_p(VALUE);
|
||||||
VALUE rb_special_singleton_class(VALUE);
|
VALUE rb_special_singleton_class(VALUE);
|
||||||
|
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
|
||||||
void Init_class_hierarchy(void);
|
void Init_class_hierarchy(void);
|
||||||
|
|
||||||
/* compile.c */
|
/* compile.c */
|
||||||
|
|
2
object.c
2
object.c
|
@ -298,7 +298,7 @@ rb_obj_clone(VALUE obj)
|
||||||
rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
|
rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
|
||||||
}
|
}
|
||||||
clone = rb_obj_alloc(rb_obj_class(obj));
|
clone = rb_obj_alloc(rb_obj_class(obj));
|
||||||
singleton = rb_singleton_class_clone(obj);
|
singleton = rb_singleton_class_clone_and_attach(obj, clone);
|
||||||
RBASIC(clone)->klass = singleton;
|
RBASIC(clone)->klass = singleton;
|
||||||
if (FL_TEST(singleton, FL_SINGLETON)) {
|
if (FL_TEST(singleton, FL_SINGLETON)) {
|
||||||
rb_singleton_class_attached(singleton, clone);
|
rb_singleton_class_attached(singleton, clone);
|
||||||
|
|
|
@ -336,4 +336,18 @@ class TestClass < Test::Unit::TestCase
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_cloned_singleton_method_added
|
||||||
|
bug5283 = '[ruby-dev:44477]'
|
||||||
|
added = []
|
||||||
|
c = Class.new
|
||||||
|
c.singleton_class.class_eval do
|
||||||
|
define_method(:singleton_method_added) {|mid| added << [self, mid]}
|
||||||
|
def foo; :foo; end
|
||||||
|
end
|
||||||
|
added.clear
|
||||||
|
d = c.clone
|
||||||
|
assert_empty(added.grep(->(k) {c == k[0]}), bug5283)
|
||||||
|
assert_equal(:foo, d.foo)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue