mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* object.c (rb_obj_clone): call initialize_clone hook method to
call initialize_copy. * object.c (rb_obj_dup): call initialize_dup hook. * lib/delegate.rb (Delegator#initialize_clone): use new hook to implement deep copy. [ruby-dev:40242] * lib/delegate.rb (Delegator#initialize_dup): ditto. * test/test_delegate.rb (TestDelegateClass#test_copy_frozen): add a test to ensure #clone copies frozen status. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
26051e1d06
commit
a7926befe0
4 changed files with 39 additions and 6 deletions
15
ChangeLog
15
ChangeLog
|
@ -7,6 +7,21 @@ Mon Feb 8 16:27:57 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
|||
* tool/eval.rb, win32/{mkexports,resource}.rb: miniruby no longer
|
||||
contains "." in $:.
|
||||
|
||||
Mon Feb 8 15:15:07 2010 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* object.c (rb_obj_clone): call initialize_clone hook method to
|
||||
call initialize_copy.
|
||||
|
||||
* object.c (rb_obj_dup): call initialize_dup hook.
|
||||
|
||||
* lib/delegate.rb (Delegator#initialize_clone): use new hook to
|
||||
implement deep copy. [ruby-dev:40242]
|
||||
|
||||
* lib/delegate.rb (Delegator#initialize_dup): ditto.
|
||||
|
||||
* test/test_delegate.rb (TestDelegateClass#test_copy_frozen): add
|
||||
a test to ensure #clone copies frozen status.
|
||||
|
||||
Mon Feb 8 10:28:58 2010 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* configure.in: check pthread_attr_getstack to prevent obsolete
|
||||
|
|
|
@ -205,13 +205,14 @@ class Delegator < BasicObject
|
|||
end
|
||||
|
||||
# :nodoc:
|
||||
def dup
|
||||
self.class.new(__getobj__.dup)
|
||||
def initialize_clone(obj)
|
||||
self.__setobj__(obj.__getobj__.clone)
|
||||
end
|
||||
# :nodoc:
|
||||
def clone
|
||||
self.class.new(__getobj__.clone)
|
||||
def initialize_dup(obj)
|
||||
self.__setobj__(obj.__getobj__.dup)
|
||||
end
|
||||
private :initialize_clone, :initialize_dup
|
||||
|
||||
# Freeze self and target at once.
|
||||
def freeze
|
||||
|
|
18
object.c
18
object.c
|
@ -31,7 +31,8 @@ VALUE rb_cNilClass;
|
|||
VALUE rb_cTrueClass;
|
||||
VALUE rb_cFalseClass;
|
||||
|
||||
static ID id_eq, id_eql, id_match, id_inspect, id_init_copy;
|
||||
static ID id_eq, id_eql, id_match, id_inspect;
|
||||
static ID id_init_copy, id_init_clone, id_init_dup;
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
|
@ -204,7 +205,6 @@ init_copy(VALUE dest, VALUE obj)
|
|||
}
|
||||
break;
|
||||
}
|
||||
rb_funcall(dest, id_init_copy, 1, obj);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -243,6 +243,7 @@ rb_obj_clone(VALUE obj)
|
|||
RBASIC(clone)->klass = rb_singleton_class_clone(obj);
|
||||
RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT) | FL_TEST(clone, FL_UNTRUSTED)) & ~(FL_FREEZE|FL_FINALIZE);
|
||||
init_copy(clone, obj);
|
||||
rb_funcall(clone, id_init_clone, 1, obj);
|
||||
RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
|
||||
|
||||
return clone;
|
||||
|
@ -276,6 +277,7 @@ rb_obj_dup(VALUE obj)
|
|||
}
|
||||
dup = rb_obj_alloc(rb_obj_class(obj));
|
||||
init_copy(dup, obj);
|
||||
rb_funcall(dup, id_init_dup, 1, obj);
|
||||
|
||||
return dup;
|
||||
}
|
||||
|
@ -292,6 +294,14 @@ rb_obj_init_copy(VALUE obj, VALUE orig)
|
|||
return obj;
|
||||
}
|
||||
|
||||
/* :nodoc: */
|
||||
VALUE
|
||||
rb_obj_init_dup_clone(VALUE obj, VALUE orig)
|
||||
{
|
||||
rb_funcall(obj, id_init_copy, 1, orig);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* obj.to_s => string
|
||||
|
@ -2578,6 +2588,8 @@ Init_Object(void)
|
|||
rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
|
||||
rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
|
||||
rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
|
||||
rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
|
||||
rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
|
||||
|
||||
rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
|
||||
rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
|
||||
|
@ -2712,6 +2724,8 @@ Init_Object(void)
|
|||
id_match = rb_intern("=~");
|
||||
id_inspect = rb_intern("inspect");
|
||||
id_init_copy = rb_intern("initialize_copy");
|
||||
id_init_clone = rb_intern("initialize_clone");
|
||||
id_init_dup = rb_intern("initialize_dup");
|
||||
|
||||
for (i=0; conv_method_names[i].method; i++) {
|
||||
conv_method_names[i].id = rb_intern(conv_method_names[i].method);
|
||||
|
|
|
@ -92,6 +92,9 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
d = SimpleDelegator.new(a)
|
||||
assert_nothing_raised(bug2679) {d.dup[0] += 1}
|
||||
assert_raise(RuntimeError) {d.clone[0] += 1}
|
||||
d.freeze
|
||||
assert(d.clone.frozen?)
|
||||
assert(!d.dup.frozen?)
|
||||
end
|
||||
|
||||
def test_frozen
|
||||
|
|
Loading…
Reference in a new issue