Unwind.xs

filter: Statementstmt 93.4% 129 / 138 Branchbran 65.8% 83 / 126 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.3% 212 / 264 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Scope/Unwind.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #define MY_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
9  
10 #define SU_GET_CONTEXT(B, D) \
11 STMT_START { \
12 if (items > 0) { \
13 SV *csv = ST(B); \
14 if (!SvOK(csv)) \
15 goto default_cx; \
16 cxix = SvIV(csv); \
17 if (cxix < 0) \
18 cxix = 0; \
19 else if (cxix > cxstack_ix) \
20 goto default_cx; \
21 } else { \
22 default_cx: \
23 cxix = (D); \
24 } \
25 } STMT_END
26  
27 #define SU_GET_LEVEL() \
28 STMT_START { \
29 level = 0; \
30 if (items > 0) { \
31 SV *lsv = ST(0); \
32 if (SvOK(lsv)) { \
33 level = SvIV(lsv); \
34 if (level < 0) \
35 level = 0; \
36 } \
37 } \
38 } STMT_END
39  
40 #define MY_CXT_KEY "Scope::Unwind-" XS_VERSION
41 typedef struct {
42 I32 cxix;
43 I32 items;
44 SV **savesp;
45 LISTOP return_op;
46 OP proxy_op;
47 } my_cxt_t;
48  
49 START_MY_CXT
50  
51 5231 static void su_unwind(pTHX_ void *ud_) {
52 dMY_CXT;
53 5231 I32 cxix = MY_CXT.cxix;
54 5231 I32 items = MY_CXT.items;
55 I32 mark;
56  
57 PERL_UNUSED_VAR(ud_);
58  
59 5231 PL_stack_sp = MY_CXT.savesp;
60 #if MY_HAS_PERL(5, 19, 4)
61 {
62 I32 i;
63 5231 SV **sp = PL_stack_sp;
64 10566 ▶ for (i = -items + 1; i <= 0; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = -items + 1; i <= 0; ++i) 5335 5231 10566
65 5335 ▶ if (!SvTEMP(sp[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTEMP(sp[i])) 5321 14 5335
66 5321 sv_2mortal(SvREFCNT_inc(sp[i]));
67 }
68 #endif
69  
70 5231 ▶ if (cxstack_ix > cxix)
Branch: 1 branch
BranchTrueFalseTotal
if (cxstack_ix > cxix) 3912 1319 5231
71 3912 dounwind(cxix);
72  
73 5231 mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
74 5231 *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
75  
76 /*
77 MY_D({
78 I32 gimme = GIMME_V;
79 su_debug_log("%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
80 &MY_CXT, cxix,
81 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
82 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
83 });*/
84  
85 5231 PL_op = (OP *) &(MY_CXT.return_op);
86 5231 PL_op = PL_op->op_ppaddr(aTHX);
87  
88 5231 *PL_markstack_ptr = mark;
89  
90 5231 MY_CXT.proxy_op.op_next = PL_op;
91 5231 PL_op = &(MY_CXT.proxy_op);
92 5231 }
93  
94 #ifndef OP_SIBLING
95 # define OP_SIBLING(O) ((O)->op_sibling)
96 #endif
97  
98 26695 static I32 su_context_normalize_up(pTHX_ I32 cxix) {
99 #define su_context_normalize_up(C) su_context_normalize_up(aTHX_ (C))
100 PERL_CONTEXT *cx;
101  
102 26695 ▶ if (cxix <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cxix <= 0) 26 26669 26695
103 26 return 0;
104  
105 26669 cx = cxstack + cxix;
106 26669 ▶ if (CxTYPE(cx) == CXt_BLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (CxTYPE(cx) == CXt_BLOCK) { 25 26644 26669
107 25 PERL_CONTEXT *prev = cx - 1;
108  
109 25 switch (CxTYPE(prev)) {
110 #if MY_HAS_PERL(5, 10, 0)
111 10 case CXt_GIVEN:
112 case CXt_WHEN:
113 #endif
114 #if MY_HAS_PERL(5, 11, 0)
115 /* That's the only subcategory that can cause an extra BLOCK context */
116 case CXt_LOOP_PLAIN:
117 #else
118 case CXt_LOOP:
119 #endif
120 10 ▶ if (cx->blk_oldcop == prev->blk_oldcop)
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_oldcop == prev->blk_oldcop) 10 0 10
121 10 return cxix - 1;
122 0 break;
123 4 case CXt_SUBST:
124 4 ▶ if (cx->blk_oldcop && OP_SIBLING(cx->blk_oldcop) && OP_SIBLING(cx->blk_oldcop)->op_type == OP_SUBST)
Branch: 5 branches
BranchTrueFalseTotal
if (cx->blk_oldcop && OP_SIBLING(cx->blk_oldcop) && OP_SIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 4 0 4
if (cx->blk_oldcop && OP_SIBLING(cx->blk_oldcop) && OP_SIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 4 0 4
if (cx->blk_oldcop && OP_SIBLING(cx->blk_oldcop) && OP_SIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 4 0 4
if (cx->blk_oldcop && OP_SIBLING(cx->blk_oldcop) && OP_SIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 4 0 4
if (cx->blk_oldcop && OP_SIBLING(cx->blk_oldcop) && OP_SIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 4 0 4
125 4 return cxix - 1;
126 0 break;
127 }
128 }
129  
130 26655 return cxix;
131 }
132  
133 26695 static I32 su_context_skip_db(pTHX_ I32 cxix) {
134 #define su_context_skip_db(C) su_context_skip_db(aTHX_ (C))
135 I32 i;
136  
137 26695 ▶ if (!PL_DBsub)
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_DBsub) 0 26695 26695
138 0 return cxix;
139  
140 26695 ▶ for (i = cxix; i > 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxix; i > 0; --i) { 26669 26 26695
141 26669 PERL_CONTEXT *cx = cxstack + i;
142  
143 26669 switch (CxTYPE(cx)) {
144 #if MY_HAS_PERL(5, 17, 1)
145 38 case CXt_LOOP_PLAIN:
146 #endif
147 case CXt_BLOCK:
148 38 ▶ if (cx->blk_oldcop && CopSTASH(cx->blk_oldcop) == GvSTASH(PL_DBgv))
Branch: 2 branches
BranchTrueFalseTotal
if (cx->blk_oldcop && CopSTASH(cx->blk_oldcop) == GvSTASH(PL_DBgv)) 38 0 38
if (cx->blk_oldcop && CopSTASH(cx->blk_oldcop) == GvSTASH(PL_DBgv)) 0 38 38
149 0 continue;
150 38 break;
151 16873 case CXt_SUB:
152 16873 ▶ if (cx->blk_sub.cv == GvCV(PL_DBsub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_sub.cv == GvCV(PL_DBsub)) { 0 16873 16873
153 0 cxix = i - 1;
154 0 continue;
155 }
156 16873 break;
157 9758 default:
158 9758 break;
159 }
160 26669 break;
161 }
162  
163 26695 return cxix;
164 }
165  
166 #define su_context_here() su_context_normalize_up(su_context_skip_db(cxstack_ix))
167  
168 static const char su_stack_smash[] = "Cannot target a scope outside of the current stack";
169 static const char su_no_such_target[] = "No targetable %s scope in the current stack";
170  
171 MODULE = Scope::Unwind PACKAGE = Scope::Unwind
172  
173 PROTOTYPES: DISABLE
174  
175 BOOT:
176 MY_CXT_INIT;
177  
178 /* NewOp() calls calloc() which just zeroes the memory with memset(). */
179 6 Zero(&MY_CXT.return_op, 1, LISTOP);
180 6 MY_CXT.return_op.op_type = OP_RETURN;
181 6 MY_CXT.return_op.op_ppaddr = PL_ppaddr[OP_RETURN];
182  
183 6 Zero(&MY_CXT.proxy_op, 1, OP);
184 6 MY_CXT.proxy_op.op_type = OP_STUB;
185 6 MY_CXT.proxy_op.op_ppaddr = NULL;
186  
187 6 newCONSTSUB(gv_stashpvs("Scope::Unwind", 1), "TOP", newSViv(0));
188  
189 IV
190 HERE()
191 PREINIT:
192 I32 cxix;
193 CODE:
194 18159 ▶ RETVAL = su_context_here();
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = su_context_here(); 10358 7801 18159
195 OUTPUT:
196 RETVAL
197  
198 void
199 SUB(...)
200 PREINIT:
201 I32 cxix;
202 PPCODE:
203 59 ▶ SU_GET_CONTEXT(0, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, cxstack_ix); 0 59 59
SU_GET_CONTEXT(0, cxstack_ix); 0 0 0
SU_GET_CONTEXT(0, cxstack_ix); 0 0 0
SU_GET_CONTEXT(0, cxstack_ix); 0 0 0
204 59 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 59 59
205 136 ▶ for (; cxix >= 0; --cxix) {
Branch: 1 branch
BranchTrueFalseTotal
for (; cxix >= 0; --cxix) { 114 22 136
206 114 PERL_CONTEXT *cx = cxstack + cxix;
207 114 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 77 37 114
208 77 default:
209 77 continue;
210 37 case CXt_SUB:
211 37 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 37 0 37
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 37 37
212 0 continue;
213 37 mPUSHi(cxix);
214 37 XSRETURN(1);
215 }
216 }
217 22 warn(su_no_such_target, "subroutine");
218 22 XSRETURN_UNDEF;
219  
220 int
221 UP(...)
222 PREINIT:
223 I32 cxix;
224 CODE:
225 27 ▶ SU_GET_CONTEXT(0, su_context_here());
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, su_context_here()); 1 26 27
SU_GET_CONTEXT(0, su_context_here()); 0 1 1
SU_GET_CONTEXT(0, su_context_here()); 0 1 1
SU_GET_CONTEXT(0, su_context_here()); 0 1 1
226 27 ▶ if (cxix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cxix > 0) { 26 1 27
227 26 --cxix;
228 26 cxix = su_context_skip_db(cxix);
229 26 cxix = su_context_normalize_up(cxix);
230 } else {
231 1 warn(su_stack_smash);
232 }
233 27 ▶ RETVAL = cxix;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = cxix; 0 27 27
234 OUTPUT:
235 RETVAL
236  
237 void
238 EVAL(...)
239 PREINIT:
240 I32 cxix;
241 PPCODE:
242 23 ▶ SU_GET_CONTEXT(0, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, cxstack_ix); 0 23 23
SU_GET_CONTEXT(0, cxstack_ix); 0 0 0
SU_GET_CONTEXT(0, cxstack_ix); 0 0 0
SU_GET_CONTEXT(0, cxstack_ix); 0 0 0
243 23 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 23 23
244 72 ▶ for (; cxix >= 0; --cxix) {
Branch: 1 branch
BranchTrueFalseTotal
for (; cxix >= 0; --cxix) { 55 17 72
245 55 PERL_CONTEXT *cx = cxstack + cxix;
246 55 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 49 6 55
247 49 default:
248 49 continue;
249 6 case CXt_EVAL:
250 6 mPUSHi(cxix);
251 6 XSRETURN(1);
252 }
253 }
254 17 warn(su_no_such_target, "eval");
255 17 XSRETURN_UNDEF;
256  
257 int
258 SCOPE(...)
259 PREINIT:
260 I32 cxix, level;
261 CODE:
262 2619 ▶ SU_GET_LEVEL();
Branch: 3 branches
BranchTrueFalseTotal
SU_GET_LEVEL(); 2615 4 2619
SU_GET_LEVEL(); 2615 0 2615
SU_GET_LEVEL(); 0 2615 2615
263 2619 cxix = su_context_here();
264 8484 ▶ while (--level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (--level >= 0) { 5866 2618 8484
265 5866 ▶ if (cxix <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cxix <= 0) { 1 5865 5866
266 1 warn(su_stack_smash);
267 1 break;
268 }
269 5865 --cxix;
270 5865 cxix = su_context_skip_db(cxix);
271 5865 cxix = su_context_normalize_up(cxix);
272 }
273 2619 ▶ RETVAL = cxix;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = cxix; 2591 28 2619
274 OUTPUT:
275 RETVAL
276  
277 int
278 CALLER(...)
279 PREINIT:
280 I32 cxix, level;
281 CODE:
282 15 ▶ SU_GET_LEVEL();
Branch: 3 branches
BranchTrueFalseTotal
SU_GET_LEVEL(); 11 4 15
SU_GET_LEVEL(); 11 0 11
SU_GET_LEVEL(); 0 11 11
283 33 ▶ for (cxix = cxstack_ix; cxix > 0; --cxix) {
Branch: 1 branch
BranchTrueFalseTotal
for (cxix = cxstack_ix; cxix > 0; --cxix) { 27 6 33
284 27 PERL_CONTEXT *cx = cxstack + cxix;
285 27 switch (CxTYPE(cx)) {
286 12 case CXt_SUB:
287 12 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 12 0 12
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 12 12
288 0 continue;
289 case CXt_EVAL:
290 case CXt_FORMAT:
291 16 ▶ if (--level < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--level < 0) 9 7 16
292 9 goto done;
293 7 break;
294 }
295 }
296 6 done:
297 15 ▶ if (level >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (level >= 0) 6 9 15
298 6 warn(su_stack_smash);
299 15 ▶ RETVAL = cxix;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = cxix; 0 15 15
300 OUTPUT:
301 RETVAL
302  
303 void
304 unwind(...)
305 PREINIT:
306 dMY_CXT;
307 I32 cxix;
308 CODE:
309 PERL_UNUSED_VAR(cv); /* -W */
310 PERL_UNUSED_VAR(ax); /* -Wall */
311  
312 5233 ▶ SU_GET_CONTEXT(items - 1, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(items - 1, cxstack_ix); 5232 1 5233
SU_GET_CONTEXT(items - 1, cxstack_ix); 0 5232 5232
SU_GET_CONTEXT(items - 1, cxstack_ix); 1 5231 5232
SU_GET_CONTEXT(items - 1, cxstack_ix); 0 5231 5231
313 do {
314 5239 PERL_CONTEXT *cx = cxstack + cxix;
315 5239 switch (CxTYPE(cx)) {
316 3283 case CXt_SUB:
317 3283 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 3283 0 3283
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 3283 3283
318 0 continue;
319 case CXt_EVAL:
320 case CXt_FORMAT:
321 5231 MY_CXT.cxix = cxix;
322 5231 MY_CXT.items = items;
323 5231 MY_CXT.savesp = PL_stack_sp;
324 5231 ▶ if (items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 0) { 5230 1 5231
325 5230 MY_CXT.items--;
326 5230 MY_CXT.savesp--;
327 }
328 /* pp_entersub will want to sanitize the stack after returning from there
329 * Screw that, we're insane!
330 * dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
331 5231 ▶ if (GIMME_V == G_SCALAR)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) 1740 3491 5231
332 1740 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
333 5231 SAVEDESTRUCTOR_X(su_unwind, NULL);
334 5231 return;
335 8 default:
336 8 break;
337 }
338 8 ▶ } while (--cxix >= 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (--cxix >= 0); 6 2 8
339 2 croak("Can't return outside a subroutine");
↑ Summary blib/lib/Scope/Unwind.pm »