src/xs/clone.cc

filter: Statementstmt 97.9% 97 / 99 Branchbran 66.4% 89 / 134 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.8% 186 / 233 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/xs/merge.cc ↑ Summary src/xs/compare.cc »
1 #include "clone.h"
2 #include <map>
3  
4 #ifndef gv_fetchmeth
5 #define gv_fetchmeth(stash,name,len,level,flags) gv_fetchmethod_autoload(stash,name,0)
6 #endif
7  
8 namespace xs {
9  
10 static const char* HOOK_METHOD = "HOOK_CLONE";
11 static const int HOOK_METHLEN = strlen(HOOK_METHOD);
12 static const int CLONE_MAX_DEPTH = 1000;
13  
14 static MGVTBL clone_marker;
15  
16 struct CrossData {
17 struct WeakRef {
18 SV* dest;
19 uint64_t key;
20 };
21 std::map<uint64_t, SV*> map;
22 std::vector<WeakRef> weakrefs;
23 };
24  
25 static void _clone (pTHX_ SV*, SV*, CrossData*&, I32);
26  
27 38 Sv clone (const Sv& source, int flags) {
28 dTHX;
29 38 ▶ Sv ret = Sv::create();
Branch: 1 branch
BranchTrueFalseTotal
Sv ret = Sv::create(); 38 0 38
30  
31 38 CrossData* crossdata = NULL;
32 38 ▶ if (flags & CloneFlags::TRACK_REFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & CloneFlags::TRACK_REFS) { 10 28 38
33 10 CrossData data;
34 10 crossdata = &data;
35 10 ▶ _clone(aTHX_ ret, source, crossdata, 0);
Branch: 1 branch
BranchTrueFalseTotal
_clone(aTHX_ ret, source, crossdata, 0); 10 0 10
36 10 auto end = data.map.end();
37 12 ▶ for (const auto& row : data.weakrefs) { // post process weak refs that appeared before their strong refs
Branch: 1 branch
BranchTrueFalseTotal
for (const auto& row : data.weakrefs) { // post process weak refs that appeared before their strong refs 2 10 12
38 2 ▶ auto it = data.map.find(row.key);
Branch: 1 branch
BranchTrueFalseTotal
auto it = data.map.find(row.key); 2 0 2
39 2 ▶ if (it == end) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (it == end) continue; 1 1 2
40 1 ▶ SvSetSV_nosteal(row.dest, it->second);
Branch: 2 branches
BranchTrueFalseTotal
SvSetSV_nosteal(row.dest, it->second); 1 0 1
SvSetSV_nosteal(row.dest, it->second); 1 0 1
41 1 ▶ sv_rvweaken(row.dest);
Branch: 1 branch
BranchTrueFalseTotal
sv_rvweaken(row.dest); 1 0 1
42 }
43 10 }
44 28 ▶ else _clone(aTHX_ ret, source, crossdata, 0);
Branch: 1 branch
BranchTrueFalseTotal
else _clone(aTHX_ ret, source, crossdata, 0); 26 2 28
45  
46 72 return ret;
47 2 }
48  
49 2154 static void _clone (pTHX_ SV* dest, SV* source, CrossData*& xdata, I32 depth) {
50 2154 ▶ if (depth > CLONE_MAX_DEPTH) throw std::invalid_argument(
Branch: 1 branch
BranchTrueFalseTotal
if (depth > CLONE_MAX_DEPTH) throw std::invalid_argument( 2 2152 2154
51 6 ▶ std::string("clone: max depth (") + std::to_string(CLONE_MAX_DEPTH) + ") reached, it looks like you passed a cycled structure"
Branch: 3 branches
BranchTrueFalseTotal
std::string("clone: max depth (") + std::to_string(CLONE_MAX_DEPTH) + ") reached, it looks like you passed a cycled structure" 2 0 2
std::string("clone: max depth (") + std::to_string(CLONE_MAX_DEPTH) + ") reached, it looks like you passed a cycled structure" 2 0 2
std::string("clone: max depth (") + std::to_string(CLONE_MAX_DEPTH) + ") reached, it looks like you passed a cycled structure" 2 0 2
52 6 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
); 2 0 2
53  
54 2152 ▶ if (SvROK(source)) { // reference
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(source)) { // reference 1051 1101 2152
55 1051 SV* source_val = SvRV(source);
56 1051 svtype val_type = SvTYPE(source_val);
57  
58 1051 ▶ if (val_type == SVt_PVCV || val_type == SVt_PVIO) { // CV and IO cannot be copied - just set reference to the same SV
Branch: 2 branches
BranchTrueFalseTotal
if (val_type == SVt_PVCV || val_type == SVt_PVIO) { // CV and IO cannot be copied - just set reference to the same SV 1049 2 1051
if (val_type == SVt_PVCV || val_type == SVt_PVIO) { // CV and IO cannot be copied - just set reference to the same SV 2 1047 1049
59 4 ▶ SvSetSV_nosteal(dest, source);
Branch: 2 branches
BranchTrueFalseTotal
SvSetSV_nosteal(dest, source); 4 0 4
SvSetSV_nosteal(dest, source); 4 0 4
60 4 ▶ if (SvWEAKREF(source)) sv_rvweaken(dest);
Branch: 2 branches
BranchTrueFalseTotal
if (SvWEAKREF(source)) sv_rvweaken(dest); 2 2 4
if (SvWEAKREF(source)) sv_rvweaken(dest); 2 0 2
61 4 return;
62 }
63  
64 1047 uint64_t id = PTR2UV(source_val);
65 1047 ▶ if (xdata) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdata) { 25 1022 1047
66 25 ▶ auto it = xdata->map.find(id);
Branch: 1 branch
BranchTrueFalseTotal
auto it = xdata->map.find(id); 25 0 25
67 25 ▶ if (it != xdata->map.end()) {
Branch: 1 branch
BranchTrueFalseTotal
if (it != xdata->map.end()) { 5 20 25
68 5 ▶ SvSetSV_nosteal(dest, it->second);
Branch: 2 branches
BranchTrueFalseTotal
SvSetSV_nosteal(dest, it->second); 5 0 5
SvSetSV_nosteal(dest, it->second); 5 0 5
69 5 ▶ if (SvWEAKREF(source)) sv_rvweaken(dest);
Branch: 2 branches
BranchTrueFalseTotal
if (SvWEAKREF(source)) sv_rvweaken(dest); 1 4 5
if (SvWEAKREF(source)) sv_rvweaken(dest); 1 0 1
70 7 return;
71 }
72 20 ▶ if (SvWEAKREF(source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvWEAKREF(source)) { 2 18 20
73 // we can't clone object weakref points to right now, because no strong refs for the object cloned so far, we must wait until the end
74 2 ▶ xdata->weakrefs.push_back({dest, id});
Branch: 1 branch
BranchTrueFalseTotal
xdata->weakrefs.push_back({dest, id}); 2 0 2
75 2 return;
76 }
77 }
78  
79 GV* cloneGV;
80 1040 bool is_object = SvOBJECT(source_val);
81  
82 // cloning an object with custom clone behavior
83 1040 ▶ if (is_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_object) { 1010 30 1040
84 1010 ▶ auto mg = mg_findext(source_val, PERL_MAGIC_ext, &clone_marker);
Branch: 1 branch
BranchTrueFalseTotal
auto mg = mg_findext(source_val, PERL_MAGIC_ext, &clone_marker); 1010 0 1010
85 1010 ▶ if (mg) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg) { 2 1008 1010
86 2 xdata = reinterpret_cast<CrossData*>(mg->mg_ptr); // restore top-map after recursive clone() call
87 }
88 1008 ▶ else if ((cloneGV = gv_fetchmeth(SvSTASH(source_val), HOOK_METHOD, HOOK_METHLEN, 0))) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((cloneGV = gv_fetchmeth(SvSTASH(source_val), HOOK_METHOD, HOOK_METHLEN, 0))) { 1008 0 1008
else if ((cloneGV = gv_fetchmeth(SvSTASH(source_val), HOOK_METHOD, HOOK_METHLEN, 0))) { 3 1005 1008
89 // set cloning flag into object's magic to prevent infinite loop if user calls 'clone' again from hook
90 3 ▶ sv_magicext(source_val, NULL, PERL_MAGIC_ext, &clone_marker, (const char*)xdata, 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_magicext(source_val, NULL, PERL_MAGIC_ext, &clone_marker, (const char*)xdata, 0); 3 0 3
91 3 ▶ dSP; ENTER; SAVETMPS;
Branch: 2 branches
BranchTrueFalseTotal
dSP; ENTER; SAVETMPS; 3 0 3
dSP; ENTER; SAVETMPS; 3 0 3
92 3 ▶ PUSHMARK(SP);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
PUSHMARK(SP); 0 0 0
93 3 ▶ XPUSHs(source);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(source); 0 3 3
XPUSHs(source); 0 0 0
94 3 PUTBACK;
95 3 ▶ int count = call_sv((SV*)GvCV(cloneGV), G_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
int count = call_sv((SV*)GvCV(cloneGV), G_SCALAR); 3 0 3
96 3 SPAGAIN;
97 3 SV* retval = NULL;
98 6 ▶ while (count--) retval = POPs;
Branch: 1 branch
BranchTrueFalseTotal
while (count--) retval = POPs; 3 3 6
99 3 ▶ if (retval) SvSetSV(dest, retval);
Branch: 3 branches
BranchTrueFalseTotal
if (retval) SvSetSV(dest, retval); 3 0 3
if (retval) SvSetSV(dest, retval); 3 0 3
if (retval) SvSetSV(dest, retval); 3 0 3
100 3 PUTBACK;
101 3 ▶ FREETMPS; LEAVE;
Branch: 3 branches
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
FREETMPS; LEAVE; 3 0 3
FREETMPS; LEAVE; 3 0 3
102 // remove cloning flag from object's magic
103 3 ▶ sv_unmagicext(source_val, PERL_MAGIC_ext, &clone_marker);
Branch: 1 branch
BranchTrueFalseTotal
sv_unmagicext(source_val, PERL_MAGIC_ext, &clone_marker); 3 0 3
104 3 ▶ if (xdata) xdata->map[id] = dest;
Branch: 2 branches
BranchTrueFalseTotal
if (xdata) xdata->map[id] = dest; 1 2 3
if (xdata) xdata->map[id] = dest; 1 0 1
105 3 return;
106 }
107 }
108  
109 1037 ▶ SV* refval = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
SV* refval = newSV(0); 1037 0 1037
110 1037 ▶ sv_upgrade(dest, SVt_RV);
Branch: 1 branch
BranchTrueFalseTotal
sv_upgrade(dest, SVt_RV); 1037 0 1037
111 1037 SvRV_set(dest, refval);
112 1037 SvROK_on(dest);
113  
114 1037 ▶ if (is_object) sv_bless(dest, SvSTASH(source_val)); // cloning an object without any specific clone behavior
Branch: 2 branches
BranchTrueFalseTotal
if (is_object) sv_bless(dest, SvSTASH(source_val)); // cloning an object without any specific clone behavior 1007 30 1037
if (is_object) sv_bless(dest, SvSTASH(source_val)); // cloning an object without any specific clone behavior 1007 0 1007
115 1037 ▶ if (xdata) xdata->map[id] = dest;
Branch: 2 branches
BranchTrueFalseTotal
if (xdata) xdata->map[id] = dest; 16 1021 1037
if (xdata) xdata->map[id] = dest; 16 0 16
116 1037 ▶ _clone(aTHX_ refval, source_val, xdata, depth+1);
Branch: 1 branch
BranchTrueFalseTotal
_clone(aTHX_ refval, source_val, xdata, depth+1); 35 1002 1037
117  
118 35 return;
119 }
120  
121 1101 switch (SvTYPE(source)) {
122 64 case SVt_IV: // integer
123 case SVt_NV: // long double
124 case SVt_PV: // string
125 case SVt_PVIV: // string + integer
126 case SVt_PVNV: // string + long double
127 case SVt_PVMG: // blessed scalar (doesn't really true, it's just vars or magic vars)
128 case SVt_PVGV: // typeglob
129 #if PERL_VERSION > 16
130 case SVt_REGEXP: // regexp
131 #endif
132 64 ▶ SvSetSV_nosteal(dest, source);
Branch: 1 branch
BranchTrueFalseTotal
SvSetSV_nosteal(dest, source); 64 0 64
133 64 return;
134 #if PERL_VERSION <= 16 // fix bug in SvSetSV_nosteal while copying regexp SV prior to perl 5.16.0
135 case SVt_REGEXP: // regexp
136 SvSetSV_nosteal(dest, source);
137 if (SvSTASH(dest) == NULL) SvSTASH_set(dest, gv_stashpv("Regexp",0));
138 return;
139 #endif
140 19 case SVt_PVAV: { // array
141 19 sv_upgrade(dest, SVt_PVAV);
142 19 SV** srclist = AvARRAY((AV*)source);
143 19 SSize_t srcfill = AvFILLp((AV*)source);
144 19 av_extend((AV*)dest, srcfill); // dest is an empty array. we can set directly it's SV** array for speed
145 19 AvFILLp((AV*)dest) = srcfill; // set array len
146 19 SV** dstlist = AvARRAY((AV*)dest);
147 62 ▶ for (SSize_t i = 0; i <= srcfill; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i <= srcfill; ++i) { 45 17 62
148 45 SV* srcval = *srclist++;
149 45 ▶ if (srcval != NULL) { // if not empty slot
Branch: 1 branch
BranchTrueFalseTotal
if (srcval != NULL) { // if not empty slot 45 0 45
150 45 SV* elem = newSV(0);
151 45 dstlist[i] = elem;
152 45 _clone(aTHX_ elem, srcval, xdata, depth+1);
153 }
154 }
155 17 return;
156 }
157 1018 case SVt_PVHV: { // hash
158 1018 sv_upgrade(dest, SVt_PVHV);
159 1018 STRLEN hvmax = HvMAX((HV*)source);
160 1018 HE** hvarr = HvARRAY((HV*)source);
161 1018 ▶ if (!hvarr) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hvarr) return; 0 1018 1018
162  
163 2176 ▶ for (STRLEN i = 0; i <= hvmax; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i <= hvmax; ++i) { 2156 20 2176
164 const HE* entry;
165 2192 ▶ for (entry = hvarr[i]; entry; entry = HeNEXT(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
for (entry = hvarr[i]; entry; entry = HeNEXT(entry)) { 1034 1158 2192
166 1034 HEK* hek = HeKEY_hek(entry);
167 1034 SV* elem = newSV(0);
168 1034 hv_storehek((HV*)dest, hek, elem);
169 1034 _clone(aTHX_ elem, HeVAL(entry), xdata, depth+1);
170 }
171 }
172  
173 20 return;
174 }
175 0 case SVt_NULL: // undef
176 default: // BIND, LVALUE, FORMAT - are not copied
177 0 return;
178 }
179 }
180  
181 }
« src/xs/merge.cc ↑ Summary src/xs/compare.cc »