diff options
author | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-08-01 05:55:35 +0000 |
---|---|---|
committer | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-08-01 05:55:35 +0000 |
commit | 320ae01c5fb091eab0926c186f304a9caeda1ace (patch) | |
tree | 3cae4cb5ecf2fe06f3f063e3caf457b28202ae0b | |
parent | ac44784146cfe48eb09be56c629023bc47426651 (diff) | |
download | ruby-320ae01c5fb091eab0926c186f304a9caeda1ace.tar.gz |
Object#clone with freeze: false [Feature #12300]
* object.c (rb_obj_clone2): Allow Object#clone to take freeze:
false keyword argument to not freeze the clone.
[ruby-core:75017][Feature #12300]
* test/ruby/test_object.rb (TestObject): test for it.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55786 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | object.c | 36 | ||||
-rw-r--r-- | test/ruby/test_object.rb | 16 |
3 files changed, 54 insertions, 6 deletions
@@ -1,3 +1,11 @@ +Mon Aug 1 14:50:06 2016 Jeremy Evans <code@jeremyevans.net> + + * object.c (rb_obj_clone2): Allow Object#clone to take freeze: + false keyword argument to not freeze the clone. + [ruby-core:75017][Feature #12300] + + * test/ruby/test_object.rb (TestObject): test for it. + Mon Aug 1 12:16:19 2016 SHIBATA Hiroshi <hsbt@ruby-lang.org> * ext/json/*, test/json/json_parser_test.rb: Update json-2.0.2. @@ -299,11 +299,12 @@ init_copy(VALUE dest, VALUE obj) /* * call-seq: - * obj.clone -> an_object + * obj.clone(freeze: true) -> an_object * * Produces a shallow copy of <i>obj</i>---the instance variables of * <i>obj</i> are copied, but not the objects they reference. - * <code>clone</code> copies the frozen and tainted state of <i>obj</i>. + * <code>clone</code> copies the frozen (unless :freeze keyword argument + * is given with a false value) and tainted state of <i>obj</i>. * See also the discussion under <code>Object#dup</code>. * * class Klass @@ -321,11 +322,25 @@ init_copy(VALUE dest, VALUE obj) * the class. */ -VALUE -rb_obj_clone(VALUE obj) +static VALUE +rb_obj_clone2(int argc, VALUE *argv, VALUE obj) { + static ID keyword_ids[1]; + VALUE opt; + VALUE kwargs[1]; VALUE clone; VALUE singleton; + VALUE kwfreeze = Qtrue; + int n; + + if (!keyword_ids[0]) { + CONST_ID(keyword_ids[0], "freeze"); + } + n = rb_scan_args(argc, argv, "0:", &opt); + if (!NIL_P(opt)) { + rb_get_kwargs(opt, keyword_ids, 0, 1, kwargs); + kwfreeze = kwargs[0]; + } if (rb_special_const_p(obj)) { rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj)); @@ -342,11 +357,20 @@ rb_obj_clone(VALUE obj) init_copy(clone, obj); rb_funcall(clone, id_init_clone, 1, obj); - RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE; + + if (Qfalse != kwfreeze) { + RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE; + } return clone; } +VALUE +rb_obj_clone(VALUE obj) +{ + return rb_obj_clone2(0, NULL, obj); +} + /* * call-seq: * obj.dup -> an_object @@ -3424,7 +3448,7 @@ InitVM_Object(void) rb_define_method(rb_mKernel, "class", rb_obj_class, 0); rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0); - rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0); + rb_define_method(rb_mKernel, "clone", rb_obj_clone2, -1); rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0); rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0); rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1); diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index 62390ad5b5..599b2ae140 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -28,6 +28,22 @@ class TestObject < Test::Unit::TestCase end end + def test_clone + a = Object.new + def a.b; 2 end + + a.freeze + c = a.clone + assert_equal(true, c.frozen?) + assert_equal(2, c.b) + + d = a.clone(freeze: false) + def d.e; 3; end + assert_equal(false, d.frozen?) + assert_equal(2, d.b) + assert_equal(3, d.e) + end + def test_init_dupclone cls = Class.new do def initialize_clone(orig); throw :initialize_clone; end |