hax/cv_copy_flags.c.inc

filter: Statementstmt 85.4% 53 / 62 Branchbran 75.0% 24 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.9% 77 / 94 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hax/docatch.c.inc ↑ Summary hax/save_clearpadrange.c.inc »
1 /* vi: set ft=c : */
2  
3 #define padname_is_normal_lexical(pname) MY_padname_is_normal_lexical(aTHX_ pname)
4 705 static bool MY_padname_is_normal_lexical(pTHX_ PADNAME *pname)
5 {
6 /* PAD slots without names are certainly not lexicals */
7 705 ▶ if(PadnameIsNULL(pname) || !PadnameLEN(pname))
Branch: 2 branches
BranchTrueFalseTotal
if(PadnameIsNULL(pname) || !PadnameLEN(pname)) 705 0 705
if(PadnameIsNULL(pname) || !PadnameLEN(pname)) 310 395 705
8 310 return FALSE;
9  
10 /* Outer lexical captures are not lexicals */
11 395 ▶ if(PadnameOUTER(pname))
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameOUTER(pname)) 163 232 395
12 163 return FALSE;
13  
14 /* state variables are not lexicals */
15 232 ▶ if(PadnameIsSTATE(pname))
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameIsSTATE(pname)) 36 196 232
16 36 return FALSE;
17  
18 /* Protosubs for closures are not lexicals */
19 196 ▶ if(PadnamePV(pname)[0] == '&')
Branch: 1 branch
BranchTrueFalseTotal
if(PadnamePV(pname)[0] == '&') 4 192 196
20 4 return FALSE;
21  
22 /* anything left is a normal lexical */
23 192 return TRUE;
24 }
25  
26 enum {
27 CV_COPY_NULL_LEXICALS = (1<<0), /* regular lexicals end up NULL */
28 };
29  
30 #define cv_copy_flags(orig, flags) MY_cv_copy_flags(aTHX_ orig, flags)
31 93 static CV *MY_cv_copy_flags(pTHX_ CV *orig, U32 flags)
32 {
33 /* Parts of this code stolen from S_cv_clone() in pad.c
34 */
35 93 CV *new = MUTABLE_CV(newSV_type(SVt_PVCV));
36 93 CvFLAGS(new) = CvFLAGS(orig) & ~CVf_CVGV_RC;
37  
38 93 ▶ CvFILE(new) = CvDYNFILE(orig) ? savepv(CvFILE(orig)) : CvFILE(orig);
Branch: 1 branch
BranchTrueFalseTotal
CvFILE(new) = CvDYNFILE(orig) ? savepv(CvFILE(orig)) : CvFILE(orig); 0 93 93
39 #if HAVE_PERL_VERSION(5, 18, 0)
40 93 ▶ if(CvNAMED(orig)) {
Branch: 1 branch
BranchTrueFalseTotal
if(CvNAMED(orig)) { 0 93 93
41 /* Perl core uses CvNAME_HEK_set() here, but that involves a call to a
42 * non-public function unshare_hek(). The latter is only needed in the
43 * case where an old value needs to be removed, but since we've only just
44 * created the CV we know it will be empty, so we can just set the field
45 * directly
46 */
47 0 ((XPVCV*)MUTABLE_PTR(SvANY(new)))->xcv_gv_u.xcv_hek = share_hek_hek(CvNAME_HEK(orig));
48 0 CvNAMED_on(new);
49 }
50 else
51 #endif
52 93 CvGV_set(new, CvGV(orig));
53  
54 93 CvSTASH_set(new, CvSTASH(orig));
55 {
56 OP_REFCNT_LOCK;
57 93 ▶ CvROOT(new) = OpREFCNT_inc(CvROOT(orig));
Branch: 1 branch
BranchTrueFalseTotal
CvROOT(new) = OpREFCNT_inc(CvROOT(orig)); 93 0 93
58 OP_REFCNT_UNLOCK;
59 }
60 93 CvSTART(new) = CvSTART(orig);
61 93 CvOUTSIDE(new) = MUTABLE_CV(SvREFCNT_inc(CvOUTSIDE(orig)));
62 93 CvOUTSIDE_SEQ(new) = CvOUTSIDE_SEQ(orig);
63  
64 /* No need to bother with SvPV slot because that's the prototype, and it's
65 * too late for that here
66 */
67 /* TODO: Consider what to do about SvPVX */
68  
69 {
70 93 ENTER_with_name("cv_copy_flags");
71  
72 93 SAVESPTR(PL_compcv);
73 93 PL_compcv = new;
74  
75 93 SAVESPTR(PL_comppad_name);
76 93 PL_comppad_name = PadlistNAMES(CvPADLIST(orig));
77 93 CvPADLIST_set(new, pad_new(padnew_CLONE|padnew_SAVE));
78 #if HAVE_PERL_VERSION(5, 22, 0)
79 93 CvPADLIST(new)->xpadl_id = CvPADLIST(orig)->xpadl_id;
80 #endif
81  
82 93 PADNAMELIST *padnames = PadlistNAMES(CvPADLIST(orig));
83 93 const PADOFFSET fnames = PadnamelistMAX(padnames);
84 93 const PADOFFSET fpad = AvFILLp(PadlistARRAY(CvPADLIST(orig))[1]);
85 93 int depth = CvDEPTH(orig);
86 93 ▶ if(!depth)
Branch: 1 branch
BranchTrueFalseTotal
if(!depth) 0 93 93
87 0 depth = 1;
88 93 SV **origpad = AvARRAY(PadlistARRAY(CvPADLIST(orig))[depth]);
89  
90 #if !HAVE_PERL_VERSION(5, 18, 0)
91 /* Perls before 5.18.0 didn't copy the padnameslist
92 */
93 SvREFCNT_dec(PadlistNAMES(CvPADLIST(new)));
94 PadlistNAMES(CvPADLIST(new)) = (PADNAMELIST *)SvREFCNT_inc(PadlistNAMES(CvPADLIST(orig)));
95 #endif
96  
97 93 av_fill(PL_comppad, fpad);
98 93 PL_curpad = AvARRAY(PL_comppad);
99  
100 93 PADNAME **pnames = PadnamelistARRAY(padnames);
101 PADOFFSET padix;
102  
103 /* TODO: What about padix 0? */
104  
105 386 ▶ for(padix = 1; padix <= fpad; padix++) {
Branch: 1 branch
BranchTrueFalseTotal
for(padix = 1; padix <= fpad; padix++) { 293 93 386
106 293 ▶ PADNAME *pname = (padix <= fnames) ? pnames[padix] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PADNAME *pname = (padix <= fnames) ? pnames[padix] : NULL; 293 0 293
107 293 SV *newval = NULL;
108  
109 293 ▶ if(padname_is_normal_lexical(pname)) {
Branch: 1 branch
BranchTrueFalseTotal
if(padname_is_normal_lexical(pname)) { 80 213 293
110 80 ▶ if(flags & CV_COPY_NULL_LEXICALS)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CV_COPY_NULL_LEXICALS) 80 0 80
111 80 continue;
112  
113 0 switch(PadnamePV(pname)[0]) {
114 0 case '$': newval = newSV(0); break;
115 0 case '@': newval = MUTABLE_SV(newAV()); break;
116 0 case '%': newval = MUTABLE_SV(newHV()); break;
117 0 default:
118 0 croak("ARGH unsure how to handle pname=<%s> in cv_copy_flags\n",
119 PadnamePV(pname));
120 break;
121 }
122 }
123 213 ▶ else if(!origpad[padix])
Branch: 1 branch
BranchTrueFalseTotal
else if(!origpad[padix]) 3 210 213
124 3 newval = NULL;
125 210 ▶ else if(SvPADTMP(origpad[padix])) {
Branch: 1 branch
BranchTrueFalseTotal
else if(SvPADTMP(origpad[padix])) { 127 83 210
126 /* We still have to copy the value, in case it is live. Also core perl
127 * is known to set SvPADTMP on non-temporaries, like folded constants
128 * https://rt.cpan.org/Ticket/Display.html?id=142468
129 */
130 127 newval = newSVsv(origpad[padix]);
131 127 SvPADTMP_on(newval);
132 }
133 else {
134 #if !HAVE_PERL_VERSION(5, 18, 0)
135 /* Before perl 5.18.0, inner anon subs didn't find the right CvOUTSIDE
136 * at runtime, so we'll have to patch them up here
137 */
138 CV *origproto;
139 if(pname && PadnamePV(pname)[0] == '&' &&
140 CvOUTSIDE(origproto = MUTABLE_CV(origpad[padix])) == orig) {
141 /* quiet any "Variable $FOO is not available" warnings about lexicals
142 * yet to be introduced
143 */
144 ENTER_with_name("find_cv_outside");
145 SAVEINT(CvDEPTH(origproto));
146 CvDEPTH(origproto) = 1;
147  
148 CV *newproto = cv_copy_flags(origproto, flags);
149 CvPADLIST_set(newproto, CvPADLIST(origproto));
150 CvSTART(newproto) = CvSTART(origproto);
151  
152 SvREFCNT_dec(CvOUTSIDE(newproto));
153 CvOUTSIDE(newproto) = MUTABLE_CV(SvREFCNT_inc_simple_NN(new));
154  
155 LEAVE_with_name("find_cv_outside");
156  
157 newval = MUTABLE_SV(newproto);
158 }
159 else
160 #endif
161 83 ▶ if(origpad[padix])
Branch: 1 branch
BranchTrueFalseTotal
if(origpad[padix]) 83 0 83
162 83 newval = SvREFCNT_inc_NN(origpad[padix]);
163 }
164  
165 213 PL_curpad[padix] = newval;
166 }
167  
168 93 LEAVE_with_name("cv_copy_flags");
169 }
170  
171 93 return new;
172 }
« hax/docatch.c.inc ↑ Summary hax/save_clearpadrange.c.inc »