Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

String#dup, Array#dup and Hash#dup should copy instance variables. fi…

…x a #1262. (Thanks, @alloy, @takaokouji_en and @ZaZooBred)
  • Loading branch information...
commit 19514ab3772bb743960deaaf18d57dc721fe8dce 1 parent ec7dd18
@Watson1978 Watson1978 authored
Showing with 16 additions and 29 deletions.
  1. +3 −7 array.c
  2. +2 −13 hash.c
  3. +3 −0  macruby_internal.h
  4. +6 −0 object.c
  5. +2 −9 string.c
View
10 array.c
@@ -1203,14 +1203,10 @@ rary_dup(VALUE ary, SEL sel)
}
assert(rb_klass_is_rary(klass));
- VALUE dup = rary_copy(ary, klass);
+ VALUE dup = rary_alloc(klass, 0);
+ rb_obj_invoke_initialize_copy(dup, ary);
- if (OBJ_TAINTED(ary)) {
- OBJ_TAINT(dup);
- }
- if (OBJ_UNTRUSTED(ary)) {
- OBJ_UNTRUST(dup);
- }
+ OBJ_INFECT(dup, ary);
return dup;
}
View
15 hash.c
@@ -145,18 +145,6 @@ rhash_alloc(VALUE klass, SEL sel)
return (VALUE)hash;
}
-static VALUE
-rhash_copy(VALUE rcv, VALUE klass)
-{
- NEWOBJ(dup, rb_hash_t);
- dup->basic.flags = 0;
- dup->basic.klass = klass;
- GC_WB(&dup->tbl, st_copy(RHASH(rcv)->tbl));
- GC_WB(&dup->ifnone, RHASH(rcv)->ifnone);
- dup->has_proc_default = RHASH(rcv)->has_proc_default;
- return (VALUE)dup;
-}
-
VALUE
rhash_dup(VALUE rcv, SEL sel)
{
@@ -166,7 +154,8 @@ rhash_dup(VALUE rcv, SEL sel)
}
assert(rb_klass_is_rhash(klass));
- VALUE dup = rhash_copy(rcv, klass);
+ VALUE dup = rhash_alloc(klass, 0);
+ rb_obj_invoke_initialize_copy(dup, rcv);
OBJ_INFECT(dup, rcv);
return dup;
View
3  macruby_internal.h
@@ -91,6 +91,9 @@ rb_objc_release(void *addr)
// MacRubyIntern.h
+/* object.c */
+void rb_obj_invoke_initialize_copy(VALUE dest, VALUE obj);
+
/* enumerator.c */
VALUE rb_enumeratorize(VALUE, SEL, int, VALUE *);
#define RETURN_ENUMERATOR(obj, argc, argv) \
View
6 object.c
@@ -221,6 +221,12 @@ init_copy(VALUE dest, VALUE obj)
rb_vm_call(dest, selInitializeCopy, 1, &obj);
}
+void
+rb_obj_invoke_initialize_copy(VALUE dest, VALUE obj)
+{
+ init_copy(dest, obj);
+}
+
/*
* call-seq:
* obj.clone -> an_object
View
11 string.c
@@ -1831,14 +1831,6 @@ rstr_initialize(VALUE self, SEL sel, int argc, VALUE *argv)
}
static VALUE
-rstr_copy(VALUE rcv, VALUE klass)
-{
- VALUE dup = rstr_alloc(klass, 0);
- rstr_replace(dup, 0, rcv);
- return dup;
-}
-
-static VALUE
rstr_dup(VALUE str, SEL sel)
{
VALUE klass = CLASS_OF(str);
@@ -1847,7 +1839,8 @@ rstr_dup(VALUE str, SEL sel)
}
assert(rb_klass_is_rstr(klass));
- VALUE dup = rstr_copy(str, klass);
+ VALUE dup = rstr_alloc(klass, 0);
+ rb_obj_invoke_initialize_copy(dup, str);
OBJ_INFECT(dup, str);
return dup;

2 comments on commit 19514ab

@alloy
Owner

High five!

@Watson1978
Owner

Please sign in to comment.
Something went wrong with that request. Please try again.