hax/cv_copy_flags.c.inc

filter: Statementstmt 87.1% 54 / 62 Branchbran 65.6% 21 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.7% 75 / 94 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/field.c ↑ Summary lib/Object/mop-field.xsi »
1 /* vi: set ft=c : */
2  
3 #define padname_is_normal_lexical(pname) MY_padname_is_normal_lexical(aTHX_ pname)
4 235 static bool MY_padname_is_normal_lexical(pTHX_ PADNAME *pname)
5 {
6 /* PAD slots without names are certainly not lexicals */
7 235 ▶ if(PadnameIsNULL(pname) || !PadnameLEN(pname))
Branch: 2 branches
BranchTrueFalseTotal
if(PadnameIsNULL(pname) || !PadnameLEN(pname)) 235 0 235
if(PadnameIsNULL(pname) || !PadnameLEN(pname)) 63 172 235
8 63 return FALSE;
9  
10 /* Outer lexical captures are not lexicals */
11 172 ▶ if(PadnameOUTER(pname))
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameOUTER(pname)) 3 169 172
12 3 return FALSE;
13  
14 /* state variables are not lexicals */
15 169 ▶ if(PadnameIsSTATE(pname))
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameIsSTATE(pname)) 0 169 169
16 0 return FALSE;
17  
18 /* Protosubs for closures are not lexicals */
19 169 ▶ if(PadnamePV(pname)[0] == '&')
Branch: 1 branch
BranchTrueFalseTotal
if(PadnamePV(pname)[0] == '&') 0 169 169
20 0 return FALSE;
21  
22 /* anything left is a normal lexical */
23 169 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 59 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 59 CV *new = MUTABLE_CV(newSV_type(SVt_PVCV));
36 59 CvFLAGS(new) = CvFLAGS(orig) & ~CVf_CVGV_RC;
37  
38 59 ▶ CvFILE(new) = CvDYNFILE(orig) ? savepv(CvFILE(orig)) : CvFILE(orig);
Branch: 1 branch
BranchTrueFalseTotal
CvFILE(new) = CvDYNFILE(orig) ? savepv(CvFILE(orig)) : CvFILE(orig); 0 59 59
39 59 ▶ if(CvNAMED(orig)) {
Branch: 1 branch
BranchTrueFalseTotal
if(CvNAMED(orig)) { 0 59 59
40 /* Perl core uses CvNAME_HEK_set() here, but that involves a call to a
41 * non-public function unshare_hek(). The latter is only needed in the
42 * case where an old value needs to be removed, but since we've only just
43 * created the CV we know it will be empty, so we can just set the field
44 * directly
45 */
46 0 ((XPVCV*)MUTABLE_PTR(SvANY(new)))->xcv_gv_u.xcv_hek = share_hek_hek(CvNAME_HEK(orig));
47 0 CvNAMED_on(new);
48 }
49 else
50 59 CvGV_set(new, CvGV(orig));
51  
52 59 CvSTASH_set(new, CvSTASH(orig));
53 {
54 OP_REFCNT_LOCK;
55 59 ▶ CvROOT(new) = OpREFCNT_inc(CvROOT(orig));
Branch: 1 branch
BranchTrueFalseTotal
CvROOT(new) = OpREFCNT_inc(CvROOT(orig)); 59 0 59
56 OP_REFCNT_UNLOCK;
57 }
58 59 CvSTART(new) = CvSTART(orig);
59 59 CvOUTSIDE(new) = MUTABLE_CV(SvREFCNT_inc(CvOUTSIDE(orig)));
60 59 CvOUTSIDE_SEQ(new) = CvOUTSIDE_SEQ(orig);
61  
62 /* No need to bother with SvPV slot because that's the prototype, and it's
63 * too late for that here
64 */
65 /* TODO: Consider what to do about SvPVX */
66  
67 {
68 59 ENTER_with_name("cv_copy_flags");
69  
70 59 SAVESPTR(PL_compcv);
71 59 PL_compcv = new;
72  
73 59 SAVESPTR(PL_comppad_name);
74 59 PL_comppad_name = PadlistNAMES(CvPADLIST(orig));
75 59 CvPADLIST_set(new, pad_new(padnew_CLONE|padnew_SAVE));
76 59 CvPADLIST(new)->xpadl_id = CvPADLIST(orig)->xpadl_id;
77  
78 59 PADNAMELIST *padnames = PadlistNAMES(CvPADLIST(orig));
79 59 const PADOFFSET fnames = PadnamelistMAX(padnames);
80 59 const PADOFFSET fpad = AvFILLp(PadlistARRAY(CvPADLIST(orig))[1]);
81 59 int depth = CvDEPTH(orig);
82 59 ▶ if(!depth)
Branch: 1 branch
BranchTrueFalseTotal
if(!depth) 59 0 59
83 59 depth = 1;
84 59 SV **origpad = AvARRAY(PadlistARRAY(CvPADLIST(orig))[depth]);
85  
86  
87 59 av_fill(PL_comppad, fpad);
88 59 PL_curpad = AvARRAY(PL_comppad);
89  
90 59 PADNAME **pnames = PadnamelistARRAY(padnames);
91 PADOFFSET padix;
92  
93 /* TODO: What about padix 0? */
94  
95 294 ▶ for(padix = 1; padix <= fpad; padix++) {
Branch: 1 branch
BranchTrueFalseTotal
for(padix = 1; padix <= fpad; padix++) { 235 59 294
96 235 ▶ PADNAME *pname = (padix <= fnames) ? pnames[padix] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PADNAME *pname = (padix <= fnames) ? pnames[padix] : NULL; 235 0 235
97 235 SV *newval = NULL;
98  
99 235 ▶ if(padname_is_normal_lexical(pname)) {
Branch: 1 branch
BranchTrueFalseTotal
if(padname_is_normal_lexical(pname)) { 169 66 235
100 169 ▶ if(flags & CV_COPY_NULL_LEXICALS)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CV_COPY_NULL_LEXICALS) 0 169 169
101 0 continue;
102  
103 169 switch(PadnamePV(pname)[0]) {
104 72 case '$': newval = newSV(0); break;
105 59 case '@': newval = MUTABLE_SV(newAV()); break;
106 38 case '%': newval = MUTABLE_SV(newHV()); break;
107 0 default:
108 0 croak("ARGH unsure how to handle pname=<%s> in cv_copy_flags\n",
109 PadnamePV(pname));
110 break;
111 }
112 }
113 66 ▶ else if(!origpad[padix])
Branch: 1 branch
BranchTrueFalseTotal
else if(!origpad[padix]) 0 66 66
114 0 newval = NULL;
115 66 ▶ else if(SvPADTMP(origpad[padix])) {
Branch: 1 branch
BranchTrueFalseTotal
else if(SvPADTMP(origpad[padix])) { 4 62 66
116 /* We still have to copy the value, in case it is live. Also core perl
117 * is known to set SvPADTMP on non-temporaries, like folded constants
118 * https://rt.cpan.org/Ticket/Display.html?id=142468
119 */
120 4 newval = newSVsv(origpad[padix]);
121 4 SvPADTMP_on(newval);
122 }
123 else {
124 62 ▶ if(origpad[padix])
Branch: 1 branch
BranchTrueFalseTotal
if(origpad[padix]) 62 0 62
125 62 newval = SvREFCNT_inc_NN(origpad[padix]);
126 }
127  
128 235 PL_curpad[padix] = newval;
129 }
130  
131 59 LEAVE_with_name("cv_copy_flags");
132 }
133  
134 59 return new;
135 }
« src/field.c ↑ Summary lib/Object/mop-field.xsi »