lib/Sub/WhenBodied.xs

filter: Statementstmt 72.7% 80 / 110 Branchbran 56.9% 33 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.2% 113 / 168 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Sub/WhenBodied.pm »
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
7 #define PERL_DECIMAL_VERSION \
8 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
9 #define PERL_VERSION_GE(r,v,s) \
10 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
11  
12 #ifndef CvISXSUB
13 # define CvISXSUB(cv) (!!CvXSUB(cv))
14 #endif /* !CvISXSUB */
15  
16 #ifndef SvSTASH_set
17 # define SvSTASH_set(sv, stash) (SvSTASH(sv) = (stash))
18 #endif /* !SvSTASH_set */
19  
20 #ifndef gv_stashpvs
21 # define gv_stashpvs(name, flags) gv_stashpvn(""name"", sizeof(name)-1, flags)
22 #endif /* !gv_stashpvs */
23  
24 #ifdef PadlistARRAY
25 # define QUSE_PADLIST_STRUCT 1
26 #else /* !PadlistARRAY */
27 # define QUSE_PADLIST_STRUCT 0
28 typedef AV PADNAMELIST;
29 # define PadlistARRAY(pl) ((PAD**)AvARRAY(pl))
30 # define PadlistNAMES(pl) (PadlistARRAY(pl)[0])
31 #endif /* !PadlistARRAY */
32  
33 #define safe_av_fetch(av, key) THX_safe_av_fetch(aTHX_ av, key)
34 77 static SV *THX_safe_av_fetch(pTHX_ AV *av, I32 key)
35 {
36 77 SV **item_ptr = av_fetch(av, key, 0);
37 77 ▶ return item_ptr ? *item_ptr : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return item_ptr ? *item_ptr : &PL_sv_undef; 21 56 77
38 }
39  
40 #define sv_unbless(sv) THX_sv_unbless(aTHX_ sv)
41 0 static void THX_sv_unbless(pTHX_ SV *sv)
42 {
43 SV *oldstash;
44 0 ▶ if(!SvOBJECT(sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOBJECT(sv)) return; 0 0 0
45 0 SvOBJECT_off(sv);
46 0 ▶ if((oldstash = (SV*)SvSTASH(sv))) {
Branch: 1 branch
BranchTrueFalseTotal
if((oldstash = (SV*)SvSTASH(sv))) { 0 0 0
47 #if !PERL_VERSION_GE(5,17,10)
48 PL_sv_objcount--;
49 #endif /* <5.17.10 */
50 0 SvSTASH_set(sv, NULL);
51 0 SvREFCNT_dec(oldstash);
52 }
53 }
54  
55 /*
56 * Pending actions to apply to a sub are handled in several stages. The
57 * mechanism is quite convoluted, which is unavoidable given the lack of
58 * support from the core.
59 *
60 * Initially, when an action is to be tied to a partially-built sub, a
61 * marker object gets stored in the sub's pad. Specifically, it is
62 * added to the slot used by the @_-in-waiting. The pad and the future
63 * @_ will be created if necessary. If the pad gets thrown away, by the
64 * CV dying or being "undef"ed, the marker object also dies, and the
65 * actions are never triggered. If the partial sub content is moved
66 * from one CV to another, such as by "sub foo; sub foo { ... }", the
67 * marker moves with it. The marker doesn't know which CV it is
68 * attached to; it is the presence of the marker in a CV's pad that is
69 * significant.
70 *
71 * The actions waiting to be performed are stored in the marker object.
72 * If another action is requested, on a CV that already has a marker, it
73 * gets added to the existing marker.
74 *
75 * When a partially-built sub gets its body attached, the peephole
76 * optimiser is triggered. Code in this module is in the chain, and
77 * looks for the marker. If present, it removes the marker from the
78 * CV (actually: makes it a non-marker) and starts processing actions.
79 *
80 * While actions are being processed, the queue of pending actions is made
81 * accessible through a chain of AVs (running_actions). If another action
82 * is requested, while this is in progress, it gets added to the queue.
83 *
84 * If an action is requested on a sub that already has a body and does
85 * not have a running queue, the queueing function sets up a running
86 * queue and starts processing actions. Doing this, rather than just
87 * performing the action directly, keeps actions sequential, in case
88 * another action is requested while one is already executing.
89 */
90  
91 static void (*next_peep)(pTHX_ OP*);
92 static void my_peep(pTHX_ OP*);
93 static SV *running_actions;
94 static HV *stash_wblist;
95  
96 #define new_minimal_padlist() THX_new_minimal_padlist(aTHX)
97 0 static PADLIST *THX_new_minimal_padlist(pTHX)
98 {
99 PADLIST *padlist;
100 PAD *pad;
101 PADNAMELIST *padname;
102 0 pad = newAV();
103 0 av_store(pad, 0, &PL_sv_undef);
104 #if QUSE_PADLIST_STRUCT
105 0 Newxz(padlist, 1, PADLIST);
106 0 Newx(PadlistARRAY(padlist), 2, PAD *);
107 #else /* !QUSE_PADLIST_STRUCT */
108 padlist = newAV();
109 # if !PERL_VERSION_GE(5,15,3)
110 AvREAL_off(padlist);
111 # endif /* < 5.15.3 */
112 av_extend(padlist, 1);
113 #endif /* !QUSE_PADLIST_STRUCT */
114 #if PERL_VERSION_GE(5,21,7)
115 0 padname = newPADNAMELIST(0);
116 #else /* <5.21.7 */
117 padname = newAV();
118 # ifdef AvPAD_NAMELIST_on
119 AvPAD_NAMELIST_on(padname);
120 # endif /* AvPAD_NAMELIST_on */
121 #endif /* <5.21.7 */
122 0 PadlistARRAY(padlist)[0] = (PAD*)padname;
123 0 PadlistARRAY(padlist)[1] = pad;
124 0 return padlist;
125 }
126  
127 #define cv_find_wblist(sub) THX_cv_find_wblist(aTHX_ sub)
128 77 static AV *THX_cv_find_wblist(pTHX_ CV *sub)
129 {
130 PADLIST *padlist;
131 AV *argav;
132 I32 pos;
133 77 ▶ if(CvISXSUB(sub) || CvDEPTH(sub) != 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if(CvISXSUB(sub) || CvDEPTH(sub) != 0) return NULL; 74 3 77
if(CvISXSUB(sub) || CvDEPTH(sub) != 0) return NULL; 0 74 74
134 74 padlist = CvPADLIST(sub);
135 74 ▶ if(!padlist) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!padlist) return NULL; 0 74 74
136 74 argav = (AV*)safe_av_fetch(PadlistARRAY(padlist)[1], 0);
137 74 ▶ if(SvTYPE((SV*)argav) != SVt_PVAV) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE((SV*)argav) != SVt_PVAV) return NULL; 55 19 74
138 19 ▶ for(pos = av_len(argav); pos >= 0; pos--) {
Branch: 1 branch
BranchTrueFalseTotal
for(pos = av_len(argav); pos >= 0; pos--) { 0 19 19
139 0 SV *v = safe_av_fetch(argav, pos);
140 0 ▶ if(SvTYPE(v) == SVt_PVAV && SvOBJECT(v) &&
Branch: 2 branches
BranchTrueFalseTotal
if(SvTYPE(v) == SVt_PVAV && SvOBJECT(v) && 0 0 0
if(SvTYPE(v) == SVt_PVAV && SvOBJECT(v) && 0 0 0
141 0 ▶ SvSTASH(v) == stash_wblist)
Branch: 1 branch
BranchTrueFalseTotal
SvSTASH(v) == stash_wblist) 0 0 0
142 0 return (AV*)v;
143 }
144 19 return NULL;
145 }
146  
147 #define cv_force_wblist(sub) THX_cv_force_wblist(aTHX_ sub)
148 2 static AV *THX_cv_force_wblist(pTHX_ CV *sub)
149 {
150 PADLIST *padlist;
151 PAD *pad;
152 AV *argav, *wbl;
153 I32 pos;
154 2 padlist = CvPADLIST(sub);
155 2 ▶ if(!padlist) goto create_padlist;
Branch: 1 branch
BranchTrueFalseTotal
if(!padlist) goto create_padlist; 0 2 2
156 2 pad = PadlistARRAY(padlist)[1];
157 2 argav = (AV*)safe_av_fetch(pad, 0);
158 2 ▶ if(SvTYPE((SV*)argav) != SVt_PVAV) goto create_argav;
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE((SV*)argav) != SVt_PVAV) goto create_argav; 1 1 2
159 1 ▶ for(pos = av_len(argav); pos >= 0; pos--) {
Branch: 1 branch
BranchTrueFalseTotal
for(pos = av_len(argav); pos >= 0; pos--) { 1 0 1
160 1 SV *v = safe_av_fetch(argav, pos);
161 1 ▶ if(SvTYPE(v) == SVt_PVAV && SvOBJECT(v) &&
Branch: 2 branches
BranchTrueFalseTotal
if(SvTYPE(v) == SVt_PVAV && SvOBJECT(v) && 1 0 1
if(SvTYPE(v) == SVt_PVAV && SvOBJECT(v) && 1 0 1
162 1 ▶ SvSTASH(v) == stash_wblist)
Branch: 1 branch
BranchTrueFalseTotal
SvSTASH(v) == stash_wblist) 1 0 1
163 1 return (AV*)v;
164 }
165 0 goto create_wblist;
166 0 create_padlist:
167 0 CvPADLIST(sub) = padlist = new_minimal_padlist();
168 0 pad = PadlistARRAY(padlist)[1];
169 1 create_argav:
170 1 argav = newAV();
171 1 av_extend(argav, 0);
172 1 av_store(pad, 0, (SV*)argav);
173 1 create_wblist:
174 1 wbl = newAV();
175 1 sv_bless(sv_2mortal(newRV_inc((SV*)wbl)), stash_wblist);
176 1 av_push(argav, (SV*)wbl);
177 1 ▶ if(!next_peep) {
Branch: 1 branch
BranchTrueFalseTotal
if(!next_peep) { 1 0 1
178 1 next_peep = PL_peepp;
179 1 PL_peepp = my_peep;
180 }
181 1 return wbl;
182 }
183  
184 #define find_running_wblist(sub) THX_find_running_wblist(aTHX_ sub)
185 22 static AV *THX_find_running_wblist(pTHX_ CV *sub)
186 {
187 22 AV *runav = (AV*)running_actions;
188 24 ▶ while(SvTYPE((SV*)runav) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
while(SvTYPE((SV*)runav) == SVt_PVAV) { 12 12 24
189 12 CV *runsubject = (CV*)*av_fetch(runav, 0, 0);
190 12 ▶ if(runsubject == sub) return (AV*)*av_fetch(runav, 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
if(runsubject == sub) return (AV*)*av_fetch(runav, 1, 0); 10 2 12
191 2 runav = (AV*)*av_fetch(runav, 2, 0);
192 }
193 12 return NULL;
194 }
195  
196 #define setup_wblist_to_run(sub, wbl) THX_setup_wblist_to_run(aTHX_ sub, wbl)
197 12 static void THX_setup_wblist_to_run(pTHX_ CV *sub, AV *wbl)
198 {
199 12 AV *runav = newAV();
200 12 av_extend(runav, 2);
201 12 av_store(runav, 0, SvREFCNT_inc((SV*)sub));
202 12 av_store(runav, 1, SvREFCNT_inc((SV*)wbl));
203 12 av_store(runav, 2, SvREFCNT_inc(running_actions));
204 12 SAVEGENERICSV(running_actions);
205 12 running_actions = (SV*)runav;
206 12 }
207  
208 #define run_actions(sub, wbl) \
209 THX_run_actions(aTHX_ sub, wbl)
210 12 static void THX_run_actions(pTHX_ CV *sub, AV *wbl)
211 {
212 12 SV *subject_ref = sv_2mortal(newRV_inc((SV*)sub));
213 34 ▶ while(av_len(wbl) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
while(av_len(wbl) != -1) { 22 12 34
214 22 dSP;
215 22 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 22 22
216 22 ▶ XPUSHs(subject_ref);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(subject_ref); 0 22 22
217 22 PUTBACK;
218 22 call_sv(sv_2mortal(av_shift(wbl)), G_VOID|G_DISCARD);
219 }
220 12 }
221  
222 55 static void my_peep(pTHX_ OP*o)
223 {
224 55 CV *sub = PL_compcv;
225 55 AV *wbl = cv_find_wblist(PL_compcv);
226 55 ▶ if(!wbl || find_running_wblist(sub)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!wbl || find_running_wblist(sub)) { 0 55 55
if(!wbl || find_running_wblist(sub)) { 0 0 0
227 55 next_peep(aTHX_ o);
228 55 return;
229 }
230 0 ENTER;
231 0 setup_wblist_to_run(sub, wbl);
232 0 sv_unbless((SV*)wbl);
233 0 next_peep(aTHX_ o);
234 0 run_actions(sub, wbl);
235 0 LEAVE;
236 }
237  
238 MODULE = Sub::WhenBodied PACKAGE = Sub::WhenBodied
239  
240 PROTOTYPES: DISABLE
241  
242 BOOT:
243 2 stash_wblist = gv_stashpvs("Sub::WhenBodied::__WBLIST__", 1);
244 2 running_actions = &PL_sv_no;
245  
246 void
247 when_sub_bodied(CV *sub, CV *action)
248 PROTOTYPE: $$
249 PREINIT:
250 AV *wbl;
251 CODE:
252 24 ▶ if(!CvISXSUB(sub) && !CvROOT(sub)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!CvISXSUB(sub) && !CvROOT(sub)) { 21 3 24
if(!CvISXSUB(sub) && !CvROOT(sub)) { 2 19 21
253 2 wbl = cv_force_wblist(sub);
254 2 av_push(wbl, SvREFCNT_inc((SV*)action));
255 22 ▶ } else if((wbl = cv_find_wblist(sub))) {
Branch: 1 branch
BranchTrueFalseTotal
} else if((wbl = cv_find_wblist(sub))) { 0 22 22
256 0 av_push(wbl, SvREFCNT_inc((SV*)action));
257 22 ▶ } else if((wbl = find_running_wblist(sub))) {
Branch: 1 branch
BranchTrueFalseTotal
} else if((wbl = find_running_wblist(sub))) { 10 12 22
258 10 av_push(wbl, SvREFCNT_inc((SV*)action));
259 } else {
260 12 wbl = newAV();
261 12 av_push(wbl, SvREFCNT_inc((SV*)action));
262 12 ENTER;
263 12 setup_wblist_to_run(sub, wbl);
264 12 SvREFCNT_dec(wbl);
265 12 run_actions(sub, wbl);
266 12 LEAVE;
267 }
↑ Summary blib/lib/Sub/WhenBodied.pm »