ruby_deep_clone 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.5'
5
5
 
6
6
  s.name = 'ruby_deep_clone'
7
- s.version = '0.2.0'
7
+ s.version = '0.3.0'
8
8
  s.date = '2013-04-03'
9
9
 
10
10
  s.summary = "Ruby native deep clone"
@@ -1,4 +1,29 @@
1
1
  #include "deep_clone.h"
2
+ int ident = 0;
3
+
4
+ void
5
+ inspect(VALUE val) {
6
+ #ifdef DC_DEBUG
7
+ int i;
8
+ for(i = 0; i <= ident-1; i++) {
9
+ printf("\t");
10
+ }
11
+ printf("BEFORE %d ", BUILTIN_TYPE(val));
12
+ printf("INSPECT: %s\n", RSTRING_PTR(rb_any_to_s(val)));
13
+ #endif
14
+ }
15
+
16
+ void
17
+ inspect_kvp(ID key, VALUE val) {
18
+ #ifdef DC_DEBUG
19
+ int i;
20
+ for(i = 0; i <= ident-1; i++) {
21
+ printf("\t");
22
+ }
23
+ printf("BEFORE %s %d %d", RSTRING_PTR(rb_inspect(ID2SYM(key))), val);
24
+ printf("VALUE: %s => %s\n", RSTRING_PTR(rb_inspect(ID2SYM(key))), RSTRING_PTR(rb_any_to_s(val)));
25
+ #endif
26
+ }
2
27
 
3
28
  void Init_deep_clone()
4
29
  {
@@ -8,8 +33,9 @@ void Init_deep_clone()
8
33
 
9
34
  static int clone_variable(st_data_t key, st_data_t index, struct dump_call_arg *arg)
10
35
  {
11
- VALUE val = ROBJECT_IVPTR(arg->src)[(long)index];
12
- rb_ivar_set(arg->obj, key, clone_object(val,arg->tracker));
36
+ VALUE val = rb_ivar_get(arg->obj, (ID)key);// ROBJECT_IVPTR(arg->src)[(long)index];
37
+ inspect_kvp((ID)key, val);
38
+ rb_ivar_set(arg->obj, (ID)key, clone_object(val,arg->tracker));
13
39
  return ST_CONTINUE;
14
40
  }
15
41
 
@@ -24,16 +50,20 @@ static VALUE clone_object(VALUE object, VALUE tracker)
24
50
  if(rb_special_const_p(object))
25
51
  return object;
26
52
 
53
+ inspect(object);
54
+
27
55
  VALUE new_obj;
28
56
  VALUE id = rb_obj_id(object);
29
57
 
30
58
  if(st_lookup(RHASH_TBL(tracker), id, 0)) {
31
59
  new_obj = rb_hash_aref(tracker,id);
32
60
  } else {
61
+ ident++;
33
62
  switch (BUILTIN_TYPE(object)) {
34
63
  case T_ARRAY:
35
64
  new_obj = rb_ary_new2(RARRAY_LEN(object));
36
65
  long len = RARRAY_LEN(object);
66
+ if(len == 0) break;
37
67
  rb_hash_aset(tracker,id,new_obj);
38
68
  VALUE *ptr = RARRAY_PTR(object);
39
69
  while (len--) {
@@ -54,20 +84,35 @@ static VALUE clone_object(VALUE object, VALUE tracker)
54
84
  break;
55
85
  case T_CLASS:
56
86
  case T_MODULE:
87
+ case T_FLOAT:
57
88
  case T_REGEXP:
89
+ case T_BIGNUM:
90
+ case T_NIL:
91
+ case T_TRUE:
92
+ case T_FALSE:
93
+ case T_FIXNUM:
94
+ case T_STRUCT:
95
+ case T_FILE:
58
96
  new_obj = object;
59
97
  rb_hash_aset(tracker,id,new_obj);
60
98
  break;
61
99
  default:
62
100
  new_obj = rb_obj_clone(object);
101
+
102
+ // Unfreeze the new object
103
+ OBJ_UNFREEZE(new_obj);
104
+
63
105
  rb_hash_aset(tracker,id,new_obj);
64
106
  st_table *tbl = ROBJECT_IV_INDEX_TBL(object);
65
107
  if(tbl) {
66
108
  struct dump_call_arg arg = {new_obj,tracker, object};
67
109
  TABLE_FOREACH(tbl, clone_variable, (st_data_t)&arg);
68
110
  }
111
+
112
+ if(OBJ_FROZEN(object)) OBJ_FREEZE(new_obj);
69
113
  break;
70
114
  }
115
+ ident--;
71
116
  }
72
117
  return new_obj;
73
118
  }
@@ -10,6 +10,10 @@ struct dump_call_arg {
10
10
  VALUE src;
11
11
  };
12
12
 
13
+ // #define DC_DEBUG 0
14
+
15
+ #define OBJ_UNFREEZE(x) FL_UNSET((x), FL_FREEZE)
16
+
13
17
  #ifdef SA_EMPTY
14
18
 
15
19
  // Gotta do this because of 1.9.3's falcon patch
@@ -25,15 +29,9 @@ struct dump_call_arg {
25
29
  #define RCLASS_IV_INDEX_TBL(c) (&RCLASS_EXT(c)->iv_index_tbl)
26
30
 
27
31
  #else
28
-
29
32
  // Make it work with vanilla ruby (including 2.0)
30
- #ifndef TABLE_FOREACH
31
- #define TABLE_FOREACH st_foreach
32
- #endif
33
-
34
- #ifndef RCLASS_IV_INDEX_TBL
35
33
  #define RCLASS_IV_INDEX_TBL(c) (RCLASS(c)->iv_index_tbl)
36
- #endif
34
+ #define TABLE_FOREACH st_foreach
37
35
 
38
36
  #endif
39
37
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_deep_clone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2013-04-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: !ruby/object:Gem::Requirement
17
+ requirement: &18286920 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,15 +22,10 @@ dependencies:
22
22
  version: 2.13.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ~>
29
- - !ruby/object:Gem::Version
30
- version: 2.13.0
25
+ version_requirements: *18286920
31
26
  - !ruby/object:Gem::Dependency
32
27
  name: rake-compiler
33
- requirement: !ruby/object:Gem::Requirement
28
+ requirement: &18285940 !ruby/object:Gem::Requirement
34
29
  none: false
35
30
  requirements:
36
31
  - - ~>
@@ -38,15 +33,10 @@ dependencies:
38
33
  version: 0.8.3
39
34
  type: :development
40
35
  prerelease: false
41
- version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ~>
45
- - !ruby/object:Gem::Version
46
- version: 0.8.3
36
+ version_requirements: *18285940
47
37
  - !ruby/object:Gem::Dependency
48
38
  name: rake
49
- requirement: !ruby/object:Gem::Requirement
39
+ requirement: &18285120 !ruby/object:Gem::Requirement
50
40
  none: false
51
41
  requirements:
52
42
  - - ~>
@@ -54,12 +44,7 @@ dependencies:
54
44
  version: 10.0.4
55
45
  type: :development
56
46
  prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ~>
61
- - !ruby/object:Gem::Version
62
- version: 10.0.4
47
+ version_requirements: *18285120
63
48
  description: Native implementation to create deep clones of Ruby objects
64
49
  email:
65
50
  - balmma@sysinf.ch
@@ -95,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
80
  version: '0'
96
81
  requirements: []
97
82
  rubyforge_project:
98
- rubygems_version: 1.8.24
83
+ rubygems_version: 1.8.11
99
84
  signing_key:
100
85
  specification_version: 2
101
86
  summary: Ruby native deep clone