Upper.xs

filter: Statementstmt 94.2% 926 / 983 Branchbran 70.5% 439 / 622 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.0% 1365 / 1605 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xsh/mem.h ↑ Summary xsh/threads.h »
1 /* This file is part of the Scope::Upper Perl module.
2 * See http://search.cpan.org/dist/Scope-Upper/ */
3  
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 /* --- XS helpers ---------------------------------------------------------- */
10  
11 #define XSH_PACKAGE "Scope::Upper"
12  
13 #include "xsh/caps.h"
14 #include "xsh/util.h"
15 #include "xsh/debug.h"
16  
17 /* --- Compatibility ------------------------------------------------------- */
18  
19 /* perl 5.23.8 onwards has a revamped context system */
20 #define SU_HAS_NEW_CXT XSH_HAS_PERL(5, 23, 8)
21  
22 /* given/when was added in 5.10.0 and removed in 5.41.3 */
23 #define SU_HAS_GIVEN (XSH_HAS_PERL(5, 10, 0) && !XSH_HAS_PERL(5, 41, 3))
24  
25 #ifndef dVAR
26 # define dVAR dNOOP
27 #endif
28  
29 #ifndef MUTABLE_SV
30 # define MUTABLE_SV(S) ((SV *) (S))
31 #endif
32  
33 #ifndef MUTABLE_AV
34 # define MUTABLE_AV(A) ((AV *) (A))
35 #endif
36  
37 #ifndef MUTABLE_CV
38 # define MUTABLE_CV(C) ((CV *) (C))
39 #endif
40  
41 #ifndef PERL_UNUSED_VAR
42 # define PERL_UNUSED_VAR(V)
43 #endif
44  
45 #ifndef Newx
46 # define Newx(v, n, c) New(0, v, n, c)
47 #endif
48  
49 #ifdef DEBUGGING
50 # ifdef PoisonNew
51 # define SU_POISON(D, N, T) PoisonNew((D), (N), T)
52 # elif defined(Poison)
53 # define SU_POISON(D, N, T) Poison((D), (N), T)
54 # endif
55 #endif
56 #ifndef SU_POISON
57 # define SU_POISON(D, N, T) NOOP
58 #endif
59  
60 #ifndef newSV_type
61 static SV *su_newSV_type(pTHX_ svtype t) {
62 SV *sv = newSV(0);
63 SvUPGRADE(sv, t);
64 return sv;
65 }
66 # define newSV_type(T) su_newSV_type(aTHX_ (T))
67 #endif
68  
69 #ifdef newSVpvn_flags
70 # define su_newmortal_pvn(S, L) newSVpvn_flags((S), (L), SVs_TEMP)
71 #else
72 # define su_newmortal_pvn(S, L) sv_2mortal(newSVpvn((S), (L)))
73 #endif
74 #define su_newmortal_pvs(S) su_newmortal_pvn((S), sizeof(S)-1)
75  
76 #ifndef SvPV_const
77 # define SvPV_const(S, L) SvPV(S, L)
78 #endif
79  
80 #ifndef SvPVX_const
81 # define SvPVX_const(S) SvPVX(S)
82 #endif
83  
84 #ifndef SvPV_nolen_const
85 # define SvPV_nolen_const(S) SvPV_nolen(S)
86 #endif
87  
88 #ifndef SvREFCNT_inc_simple_void
89 # define SvREFCNT_inc_simple_void(sv) ((void) SvREFCNT_inc(sv))
90 #endif
91  
92 #ifndef mPUSHi
93 # define mPUSHi(I) PUSHs(sv_2mortal(newSViv(I)))
94 #endif
95  
96 #ifndef GvCV_set
97 # define GvCV_set(G, C) (GvCV(G) = (C))
98 #endif
99  
100 #ifndef CvGV_set
101 # define CvGV_set(C, G) (CvGV(C) = (G))
102 #endif
103  
104 #ifndef CvSTASH_set
105 # define CvSTASH_set(C, S) (CvSTASH(C) = (S))
106 #endif
107  
108 #ifndef CvISXSUB
109 # define CvISXSUB(C) CvXSUB(C)
110 #endif
111  
112 #ifndef PadlistARRAY
113 # define PadlistARRAY(P) AvARRAY(P)
114 # define PadARRAY(P) AvARRAY(P)
115 #endif
116  
117 #ifndef CxHASARGS
118 # define CxHASARGS(C) ((C)->blk_sub.hasargs)
119 #endif
120  
121 #ifndef CxGIMME
122 # ifdef G_WANT
123 # define CxGIMME(C) ((C)->blk_gimme & G_WANT)
124 # else
125 # define CxGIMME(C) ((C)->blk_gimme)
126 # endif
127 #endif
128  
129 #ifndef CxOLD_OP_TYPE
130 # define CxOLD_OP_TYPE(C) (C)->blk_eval.old_op_type
131 #endif
132  
133 #ifndef OutCopFILE
134 # define OutCopFILE(C) CopFILE(C)
135 #endif
136  
137 #ifndef OutCopFILE_len
138 # define OutCopFILE_len(C) strlen(OutCopFILE(C))
139 #endif
140  
141 #ifndef CopHINTS_get
142 # define CopHINTS_get(C) ((I32) (C)->op_private & HINT_PRIVATE_MASK)
143 #endif
144  
145 #ifndef CopHINTHASH_get
146 # define CopHINTHASH_get(C) (C)->cop_hints_hash
147 #endif
148  
149 #ifndef cophh_2hv
150 # define COPHH struct refcounted_he
151 # define cophh_2hv(H, F) Perl_refcounted_he_chain_2hv(aTHX_ (H))
152 #endif
153  
154 #ifndef HvNAME_get
155 # define HvNAME_get(H) HvNAME(H)
156 #endif
157  
158 #ifndef HvNAMELEN
159 # define HvNAMELEN(H) strlen(HvNAME(H))
160 #endif
161  
162 #ifndef gv_fetchpvn_flags
163 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
164 #endif
165  
166 #ifndef hv_fetchs
167 # define hv_fetchs(H, K, L) hv_fetch((H), (K), sizeof(K)-1, (L))
168 #endif
169  
170 #ifndef OP_GIMME_REVERSE
171 static U8 su_op_gimme_reverse(U8 gimme) {
172 switch (gimme) {
173 case G_VOID:
174 return OPf_WANT_VOID;
175 case G_ARRAY:
176 return OPf_WANT_LIST;
177 default:
178 break;
179 }
180  
181 return OPf_WANT_SCALAR;
182 }
183 #define OP_GIMME_REVERSE(G) su_op_gimme_reverse(G)
184 #endif
185  
186 #ifndef OpSIBLING
187 # ifdef OP_SIBLING
188 # define OpSIBLING(O) OP_SIBLING(O)
189 # else
190 # define OpSIBLING(O) ((O)->op_sibling)
191 # endif
192 #endif
193  
194 #ifndef PERL_MAGIC_tied
195 # define PERL_MAGIC_tied 'P'
196 #endif
197  
198 #ifndef PERL_MAGIC_env
199 # define PERL_MAGIC_env 'E'
200 #endif
201  
202 #ifndef NEGATIVE_INDICES_VAR
203 # define NEGATIVE_INDICES_VAR "NEGATIVE_INDICES"
204 #endif
205  
206 /* --- Error messages ------------------------------------------------------ */
207  
208 static const char su_stack_smash[] = "Cannot target a scope outside of the current stack";
209 static const char su_no_such_target[] = "No targetable %s scope in the current stack";
210  
211 /* --- Unique context ID global storage ------------------------------------ */
212  
213 /* ... Sequence ID counter ................................................. */
214  
215 typedef struct {
216 UV *seqs;
217 STRLEN size;
218 } su_uv_array;
219  
220 static su_uv_array su_uid_seq_counter;
221  
222 #ifdef USE_ITHREADS
223  
224 static perl_mutex su_uid_seq_counter_mutex;
225  
226 #endif /* USE_ITHREADS */
227  
228 823 static UV su_uid_seq_next(pTHX_ UV depth) {
229 #define su_uid_seq_next(D) su_uid_seq_next(aTHX_ (D))
230 UV seq;
231 UV *seqs;
232  
233 XSH_LOCK(&su_uid_seq_counter_mutex);
234  
235 823 seqs = su_uid_seq_counter.seqs;
236  
237 823 ▶ if (depth >= su_uid_seq_counter.size) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= su_uid_seq_counter.size) { 27 796 823
238 UV i;
239  
240 27 seqs = PerlMemShared_realloc(seqs, (depth + 1) * sizeof(UV));
241 90 ▶ for (i = su_uid_seq_counter.size; i <= depth; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = su_uid_seq_counter.size; i <= depth; ++i) 63 27 90
242 63 seqs[i] = 0;
243  
244 27 su_uid_seq_counter.seqs = seqs;
245 27 su_uid_seq_counter.size = depth + 1;
246 }
247  
248 823 seq = ++seqs[depth];
249  
250 XSH_UNLOCK(&su_uid_seq_counter_mutex);
251  
252 823 return seq;
253 }
254  
255 /* ... UID storage ......................................................... */
256  
257 typedef struct {
258 UV seq;
259 U32 flags;
260 } su_uid;
261  
262 #define SU_UID_ACTIVE 1
263  
264 3588 static UV su_uid_depth(pTHX_ I32 cxix) {
265 #define su_uid_depth(I) su_uid_depth(aTHX_ (I))
266 const PERL_SI *si;
267 UV depth;
268  
269 3588 depth = cxix;
270 3592 ▶ for (si = PL_curstackinfo->si_prev; si; si = si->si_prev)
Branch: 1 branch
BranchTrueFalseTotal
for (si = PL_curstackinfo->si_prev; si; si = si->si_prev) 4 3588 3592
271 4 depth += si->si_cxix + 1;
272  
273 3588 return depth;
274 }
275  
276 typedef struct {
277 su_uid *map;
278 STRLEN used;
279 STRLEN alloc;
280 } su_uid_storage;
281  
282 2749 static void su_uid_storage_dup(pTHX_ su_uid_storage *new_cxt, const su_uid_storage *old_cxt, UV max_depth) {
283 #define su_uid_storage_dup(N, O, D) su_uid_storage_dup(aTHX_ (N), (O), (D))
284 2749 su_uid *old_map = old_cxt->map;
285  
286 2749 ▶ if (old_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_map) { 503 2246 2749
287 503 su_uid *new_map = new_cxt->map;
288 503 STRLEN old_used = old_cxt->used;
289 STRLEN new_used, new_alloc;
290 STRLEN i;
291  
292 503 new_used = max_depth < old_used ? max_depth : old_used;
293 503 new_cxt->used = new_used;
294  
295 503 ▶ if (new_used <= new_cxt->alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_used <= new_cxt->alloc) { 241 262 503
296 241 new_alloc = new_cxt->alloc;
297 } else {
298 262 new_alloc = new_used;
299 262 ▶ Renew(new_map, new_alloc, su_uid);
Branch: 1 branch
BranchTrueFalseTotal
Renew(new_map, new_alloc, su_uid); 0 262 262
300 262 new_cxt->map = new_map;
301 262 new_cxt->alloc = new_alloc;
302 }
303  
304 13433 ▶ for (i = 0; i < new_alloc; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < new_alloc; ++i) { 12930 503 13433
305 12930 su_uid *new_uid = new_map + i;
306  
307 12930 ▶ if (i < new_used) { /* => i < max_depth && i < old_used */
Branch: 1 branch
BranchTrueFalseTotal
if (i < new_used) { /* => i < max_depth && i < old_used */ 8604 4326 12930
308 8604 su_uid *old_uid = old_map + i;
309  
310 8604 ▶ if (old_uid && (old_uid->flags & SU_UID_ACTIVE)) {
Branch: 2 branches
BranchTrueFalseTotal
if (old_uid && (old_uid->flags & SU_UID_ACTIVE)) { 8604 0 8604
if (old_uid && (old_uid->flags & SU_UID_ACTIVE)) { 1658 6946 8604
311 1658 *new_uid = *old_uid;
312 1658 continue;
313 }
314 }
315  
316 11272 new_uid->seq = 0;
317 11272 new_uid->flags = 0;
318 }
319 }
320  
321 2749 return;
322 }
323  
324 /* --- unwind() global storage --------------------------------------------- */
325  
326 typedef struct {
327 I32 cxix;
328 I32 items;
329 SV **savesp;
330 LISTOP return_op;
331 OP proxy_op;
332 } su_unwind_storage;
333  
334 /* --- yield() global storage ---------------------------------------------- */
335  
336 typedef struct {
337 I32 cxix;
338 I32 items;
339 SV **savesp;
340 UNOP leave_op;
341 OP proxy_op;
342 } su_yield_storage;
343  
344 /* --- uplevel() data tokens and global storage ---------------------------- */
345  
346 #define SU_UPLEVEL_HIJACKS_RUNOPS XSH_HAS_PERL(5, 8, 0)
347  
348 typedef struct {
349 void *next;
350  
351 su_uid_storage tmp_uid_storage;
352 su_uid_storage old_uid_storage;
353  
354 I32 cxix;
355  
356 CV *callback;
357 CV *renamed;
358  
359 #if SU_HAS_NEW_CXT
360 U8 *cxtypes; /* array of saved context types */
361 I32 gap; /* how many contexts have temporarily CXt_NULLed out*/
362 AV* argarray; /* the PL_curpad[0] of the uplevel sub */
363 #else
364 I32 target_depth;
365 CV *target;
366 PERL_SI *si;
367 PERL_SI *old_curstackinfo;
368 AV *old_mainstack;
369 OP *old_op;
370 bool old_catch;
371 bool died;
372 #endif
373  
374 COP *old_curcop;
375  
376 #if SU_UPLEVEL_HIJACKS_RUNOPS
377 runops_proc_t old_runops;
378 #endif
379 } su_uplevel_ud;
380  
381 #if SU_HAS_NEW_CXT
382 /* used to flag a context stack entry whose type has been temporarily
383 * set to CXt_NULL. It relies on perl not using this value for real
384 * CXt_NULL entries.
385 */
386 # define CXp_SU_UPLEVEL_NULLED 0x20
387 #endif
388  
389 242 static su_uplevel_ud *su_uplevel_ud_new(pTHX) {
390 #define su_uplevel_ud_new() su_uplevel_ud_new(aTHX)
391 su_uplevel_ud *sud;
392 PERL_SI *si;
393  
394 242 Newx(sud, 1, su_uplevel_ud);
395 242 sud->next = NULL;
396  
397 242 sud->tmp_uid_storage.map = NULL;
398 242 sud->tmp_uid_storage.used = 0;
399 242 sud->tmp_uid_storage.alloc = 0;
400  
401 #if !SU_HAS_NEW_CXT
402 Newx(si, 1, PERL_SI);
403 si->si_stack = newAV();
404 AvREAL_off(si->si_stack);
405 si->si_cxstack = NULL;
406 si->si_cxmax = -1;
407  
408 sud->si = si;
409 #endif
410  
411 242 return sud;
412 }
413  
414 242 static void su_uplevel_ud_delete(pTHX_ su_uplevel_ud *sud) {
415 #define su_uplevel_ud_delete(S) su_uplevel_ud_delete(aTHX_ (S))
416  
417 #if !SU_HAS_NEW_CXT
418 PERL_SI *si = sud->si;
419  
420 Safefree(si->si_cxstack);
421 SvREFCNT_dec(si->si_stack);
422 Safefree(si);
423 #endif
424  
425 242 Safefree(sud->tmp_uid_storage.map);
426  
427 242 Safefree(sud);
428  
429 242 return;
430 }
431  
432 typedef struct {
433 su_uplevel_ud *top;
434 su_uplevel_ud *root;
435 I32 count;
436 } su_uplevel_storage;
437  
438 #ifndef SU_UPLEVEL_STORAGE_SIZE
439 # define SU_UPLEVEL_STORAGE_SIZE 4
440 #endif
441  
442 /* --- Global data --------------------------------------------------------- */
443  
444 typedef struct {
445 su_unwind_storage unwind_storage;
446 su_yield_storage yield_storage;
447 su_uplevel_storage uplevel_storage;
448 su_uid_storage uid_storage;
449 } xsh_user_cxt_t;
450  
451 #define XSH_THREADS_USER_CONTEXT 1
452 #define XSH_THREADS_USER_CLONE_NEEDS_DUP 0
453 #define XSH_THREADS_COMPILE_TIME_PROTECTION 0
454  
455 #if XSH_THREADSAFE
456  
457 static void xsh_user_clone(pTHX_ const xsh_user_cxt_t *old_cxt, xsh_user_cxt_t *new_cxt) {
458 new_cxt->uplevel_storage.top = NULL;
459 new_cxt->uplevel_storage.root = NULL;
460 new_cxt->uplevel_storage.count = 0;
461 new_cxt->uid_storage.map = NULL;
462 new_cxt->uid_storage.used = 0;
463 new_cxt->uid_storage.alloc = 0;
464  
465 su_uid_storage_dup(&new_cxt->uid_storage, &old_cxt->uid_storage,
466 old_cxt->uid_storage.used);
467  
468 return;
469 }
470  
471 #endif /* XSH_THREADSAFE */
472  
473 #include "xsh/threads.h"
474  
475 /* --- Stack manipulations ------------------------------------------------- */
476  
477 /* how many slots on the save stack various save types take up */
478  
479 #define SU_SAVE_DESTRUCTOR_SIZE 3 /* SAVEt_DESTRUCTOR_X */
480 #define SU_SAVE_SCALAR_SIZE 3 /* SAVEt_SV */
481 #define SU_SAVE_ARY_SIZE 3 /* SAVEt_AV */
482 #define SU_SAVE_AELEM_SIZE 4 /* SAVEt_AELEM */
483 #define SU_SAVE_HASH_SIZE 3 /* SAVEt_HV */
484 #define SU_SAVE_HELEM_SIZE 4 /* SAVEt_HELEM */
485 #define SU_SAVE_HDELETE_SIZE 4 /* SAVEt_DELETE */
486  
487 #define SU_SAVE_GVCV_SIZE SU_SAVE_DESTRUCTOR_SIZE
488  
489 /* the overhead of save_alloc() but not including any elements,
490 * of which there must be at least 1 */
491 #if XSH_HAS_PERL(5, 14, 0)
492 # define SU_SAVE_ALLOC_SIZE 1 /* SAVEt_ALLOC */
493 #else
494 # define SU_SAVE_ALLOC_SIZE 2 /* SAVEt_ALLOC */
495 #endif
496  
497 #ifdef SAVEADELETE
498 # define SU_SAVE_ADELETE_SIZE 3 /* SAVEt_ADELETE */
499 #else
500 # define SU_SAVE_ADELETE_SIZE SU_SAVE_DESTRUCTOR_SIZE
501 #endif
502  
503 /* (NB: it was 4 between 5.13.1 and 5.13.7) */
504 #if XSH_HAS_PERL(5, 8, 9)
505 # define SU_SAVE_GP_SIZE 3 /* SAVEt_GP */
506 # else
507 # define SU_SAVE_GP_SIZE 6 /* SAVEt_GP */
508 #endif
509  
510 /* sometimes we don't know in advance whether we're saving or deleting
511 * an array/hash element. So include enough room for a variable-sized
512 * save_alloc() to pad it to a fixed size.
513 */
514  
515 #if SU_SAVE_AELEM_SIZE < SU_SAVE_ADELETE_SIZE
516 # define SU_SAVE_AELEM_OR_ADELETE_SIZE \
517 (SU_SAVE_ADELETE_SIZE + SU_SAVE_ALLOC_SIZE + 1)
518 #elif SU_SAVE_AELEM_SIZE > SU_SAVE_ADELETE_SIZE
519 # define SU_SAVE_AELEM_OR_ADELETE_SIZE \
520 (SU_SAVE_AELEM_SIZE + SU_SAVE_ALLOC_SIZE + 1)
521 #else
522 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_AELEM_SIZE
523 #endif
524  
525 #if SU_SAVE_HELEM_SIZE < SU_SAVE_HDELETE_SIZE
526 # define SU_SAVE_HELEM_OR_HDELETE_SIZE \
527 (SU_SAVE_HDELETE_SIZE + SU_SAVE_ALLOC_SIZE + 1)
528 #elif SU_SAVE_HELEM_SIZE > SU_SAVE_HDELETE_SIZE
529 # define SU_SAVE_HELEM_OR_HDELETE_SIZE \
530 (SU_SAVE_HELEM_SIZE + SU_SAVE_ALLOC_SIZE + 1)
531 #else
532 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HELEM_SIZE
533 #endif
534  
535 #ifndef SvCANEXISTDELETE
536 # define SvCANEXISTDELETE(sv) \
537 (!SvRMAGICAL(sv) \
538 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
539 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
540 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
541 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
542 ) \
543 )
544 #endif
545  
546 /* ... Saving array elements ............................................... */
547  
548 4061 static I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
549 #define su_av_key2idx(A, K) su_av_key2idx(aTHX_ (A), (K))
550 I32 idx;
551  
552 4061 ▶ if (key >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (key >= 0) 4054 7 4061
553 4054 return key;
554  
555 /* Added by MJD in perl-5.8.1 with 6f12eb6d2a1dfaf441504d869b27d2e40ef4966a */
556 #if XSH_HAS_PERL(5, 8, 1)
557 7 ▶ if (SvRMAGICAL(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvRMAGICAL(av)) { 2 5 7
558 2 const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
559 2 ▶ if (tied_magic) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied_magic) { 2 0 2
560 2 ▶ SV * const * const negative_indices_glob = hv_fetch(
Branch: 1 branch
BranchTrueFalseTotal
SV * const * const negative_indices_glob = hv_fetch( 2 0 2
561 SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
562 NEGATIVE_INDICES_VAR, sizeof(NEGATIVE_INDICES_VAR)-1, 0
563 );
564 2 ▶ if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
Branch: 2 branches
BranchTrueFalseTotal
if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob))) 2 0 2
if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob))) 1 1 2
565 1 return key;
566 }
567 }
568 #endif
569  
570 6 idx = key + av_len(av) + 1;
571 6 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 2 4 6
572 2 return key;
573  
574 4 return idx;
575 }
576  
577 #ifndef SAVEADELETE
578  
579 typedef struct {
580 AV *av;
581 I32 idx;
582 } su_ud_adelete;
583  
584 static void su_adelete(pTHX_ void *ud_) {
585 su_ud_adelete *ud = (su_ud_adelete *) ud_;
586  
587 av_delete(ud->av, ud->idx, G_DISCARD);
588 SvREFCNT_dec(ud->av);
589  
590 Safefree(ud);
591 }
592  
593 static void su_save_adelete(pTHX_ AV *av, I32 idx) {
594 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
595 su_ud_adelete *ud;
596  
597 Newx(ud, 1, su_ud_adelete);
598 ud->av = av;
599 ud->idx = idx;
600 SvREFCNT_inc_simple_void(av);
601  
602 SAVEDESTRUCTOR_X(su_adelete, ud);
603 }
604  
605 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
606  
607 #endif /* SAVEADELETE */
608  
609 4061 static void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
610 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
611 I32 idx;
612 4061 I32 preeminent = 1;
613 SV **svp;
614 HV *stash;
615 MAGIC *mg;
616  
617 4061 idx = su_av_key2idx(av, SvIV(key));
618  
619 4061 ▶ if (SvCANEXISTDELETE(av))
Branch: 1 branch
BranchTrueFalseTotal
if (SvCANEXISTDELETE(av)) 4057 4 4061
620 4057 preeminent = av_exists(av, idx);
621  
622 4061 svp = av_fetch(av, idx, 1);
623 4061 ▶ if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx); 4059 2 4061
if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx); 0 4059 4059
624  
625 4059 ▶ if (preeminent)
Branch: 1 branch
BranchTrueFalseTotal
if (preeminent) 4053 6 4059
626 4053 save_aelem(av, idx, svp);
627 else
628 6 SAVEADELETE(av, idx);
629  
630 4059 ▶ if (val) { /* local $x[$idx] = $val; */
Branch: 1 branch
BranchTrueFalseTotal
if (val) { /* local $x[$idx] = $val; */ 2963 1096 4059
631 2963 ▶ SvSetMagicSV(*svp, val);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV(*svp, val); 2963 0 2963
SvSetMagicSV(*svp, val); 1 2962 2963
632 } else { /* local $x[$idx]; delete $x[$idx]; */
633 1096 av_delete(av, idx, G_DISCARD);
634 }
635 4059 }
636  
637 /* ... Saving hash elements ................................................ */
638  
639 2050 static void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
640 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
641 2050 I32 preeminent = 1;
642 HE *he;
643 SV **svp;
644 HV *stash;
645 MAGIC *mg;
646  
647 2050 ▶ if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
Branch: 2 branches
BranchTrueFalseTotal
if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env)) 0 2050 2050
if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env)) 0 0 0
648 2050 preeminent = hv_exists_ent(hv, keysv, 0);
649  
650 2050 he = hv_fetch_ent(hv, keysv, 1, 0);
651 2050 ▶ svp = he ? &HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
svp = he ? &HeVAL(he) : NULL; 2050 0 2050
652 2050 ▶ if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp)); 2050 0 2050
if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp)); 0 2050 2050
653  
654 2050 ▶ if (HvNAME_get(hv) && isGV(*svp)) {
Branch: 7 branches
BranchTrueFalseTotal
if (HvNAME_get(hv) && isGV(*svp)) { 169 1881 2050
if (HvNAME_get(hv) && isGV(*svp)) { 0 169 169
if (HvNAME_get(hv) && isGV(*svp)) { 0 0 0
if (HvNAME_get(hv) && isGV(*svp)) { 0 0 0
if (HvNAME_get(hv) && isGV(*svp)) { 0 0 0
if (HvNAME_get(hv) && isGV(*svp)) { 0 0 0
if (HvNAME_get(hv) && isGV(*svp)) { 0 0 0
655 0 save_gp((GV *) *svp, 0);
656 0 return;
657 }
658  
659 2050 ▶ if (preeminent) {
Branch: 1 branch
BranchTrueFalseTotal
if (preeminent) { 1494 556 2050
660 1494 save_helem(hv, keysv, svp);
661 } else {
662 STRLEN keylen;
663 556 const char * const key = SvPV_const(keysv, keylen);
664 556 ▶ SAVEDELETE(hv, savepvn(key, keylen),
Branch: 1 branch
BranchTrueFalseTotal
SAVEDELETE(hv, savepvn(key, keylen), 0 556 556
665 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
666 }
667  
668 2050 ▶ if (val) { /* local $x{$keysv} = $val; */
Branch: 1 branch
BranchTrueFalseTotal
if (val) { /* local $x{$keysv} = $val; */ 1962 88 2050
669 1962 ▶ SvSetMagicSV(*svp, val);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV(*svp, val); 1962 0 1962
SvSetMagicSV(*svp, val); 2 1960 1962
670 } else { /* local $x{$keysv}; delete $x{$keysv}; */
671 88 (void)hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
672 }
673 }
674  
675 /* ... Saving code slots from a glob ....................................... */
676  
677 #if !XSH_HAS_PERL(5, 10, 0) && !defined(mro_method_changed_in)
678 # define mro_method_changed_in(G) PL_sub_generation++
679 #endif
680  
681 typedef struct {
682 GV *gv;
683 CV *old_cv;
684 } su_save_gvcv_ud;
685  
686 13 static void su_restore_gvcv(pTHX_ void *ud_) {
687 13 su_save_gvcv_ud *ud = ud_;
688 13 GV *gv = ud->gv;
689  
690 13 GvCV_set(gv, ud->old_cv);
691 13 GvCVGEN(gv) = 0;
692 13 mro_method_changed_in(GvSTASH(gv));
693  
694 13 Safefree(ud);
695 13 }
696  
697 13 static void su_save_gvcv(pTHX_ GV *gv) {
698 #define su_save_gvcv(G) su_save_gvcv(aTHX_ (G))
699 su_save_gvcv_ud *ud;
700  
701 13 Newx(ud, 1, su_save_gvcv_ud);
702 13 ud->gv = gv;
703 13 ud->old_cv = GvCV(gv);
704  
705 13 GvCV_set(gv, NULL);
706 13 GvCVGEN(gv) = 0;
707 13 mro_method_changed_in(GvSTASH(gv));
708  
709 13 SAVEDESTRUCTOR_X(su_restore_gvcv, ud);
710 13 }
711  
712 /* --- Actions ------------------------------------------------------------- */
713  
714 typedef struct {
715 I32 orig_ix; /* original savestack_ix */
716 I32 offset; /* how much we bumped this savestack index */
717 } su_ud_origin_elem;
718  
719 typedef struct {
720 U8 type;
721 U8 private;
722 /* spare */
723 I32 depth;
724 su_ud_origin_elem *origin;
725 } su_ud_common;
726  
727 #define SU_UD_TYPE(U) (((su_ud_common *) (U))->type)
728 #define SU_UD_PRIVATE(U) (((su_ud_common *) (U))->private)
729 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
730 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
731  
732 #define SU_UD_TYPE_REAP 0
733 #define SU_UD_TYPE_LOCALIZE 1
734 #define SU_UD_TYPE_UID 2
735  
736 #define SU_UD_FREE(U) STMT_START { \
737 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
738 Safefree(U); \
739 } STMT_END
740  
741 /* ... Reap ................................................................ */
742  
743 #define SU_SAVE_LAST_CX (!XSH_HAS_PERL(5, 8, 4) || (XSH_HAS_PERL(5, 9, 5) && !XSH_HAS_PERL(5, 14, 0)) || XSH_HAS_PERL(5, 15, 0))
744  
745 typedef struct {
746 su_ud_common ci;
747 SV *cb;
748 } su_ud_reap;
749  
750 #define SU_UD_REAP_CB(U) (((su_ud_reap *) (U))->cb)
751  
752 3360 static void su_call(pTHX_ SV *cb) {
753 #if SU_SAVE_LAST_CX
754 I32 cxix;
755 PERL_CONTEXT saved_cx;
756 #endif /* SU_SAVE_LAST_CX */
757  
758 3360 dSP;
759  
760 XSH_D(xsh_debug_log("@@@ call scope_ix=%2d save_ix=%2d\n",
761 PL_scopestack_ix, PL_savestack_ix));
762  
763 3360 ENTER;
764 3360 SAVETMPS;
765  
766 3360 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3360 3360
767 3360 PUTBACK;
768  
769 #if SU_SAVE_LAST_CX
770 /* If the recently popped context isn't saved there, it will be overwritten by
771 * the sub scope from call_sv, although it's still needed in our caller. */
772 3360 ▶ cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX); 3360 0 3360
773 3360 saved_cx = cxstack[cxix];
774 #endif /* SU_SAVE_LAST_CX */
775  
776 3360 call_sv(cb, G_VOID);
777  
778 #if SU_SAVE_LAST_CX
779 3358 cxstack[cxix] = saved_cx;
780 #endif /* SU_SAVE_LAST_CX */
781  
782 3358 PUTBACK;
783  
784 3358 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 3358 3358
785 3358 LEAVE;
786  
787 3358 SvREFCNT_dec(cb);
788  
789 3358 return;
790 }
791  
792 /* ... Localize & localize array/hash element .............................. */
793  
794 typedef struct {
795 su_ud_common ci;
796 SV *sv;
797 SV *val;
798 SV *elem;
799 } su_ud_localize;
800  
801 #define SU_UD_LOCALIZE_SV(U) (((su_ud_localize *) (U))->sv)
802 #define SU_UD_LOCALIZE_VAL(U) (((su_ud_localize *) (U))->val)
803 #define SU_UD_LOCALIZE_ELEM(U) (((su_ud_localize *) (U))->elem)
804  
805 #define SU_UD_LOCALIZE_FREE(U) STMT_START { \
806 SvREFCNT_dec(SU_UD_LOCALIZE_ELEM(U)); \
807 SvREFCNT_dec(SU_UD_LOCALIZE_VAL(U)); \
808 SvREFCNT_dec(SU_UD_LOCALIZE_SV(U)); \
809 SU_UD_FREE(U); \
810 } STMT_END
811  
812 9137 static I32 su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) {
813 #define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E))
814 9137 int take_ref = 0;
815 9137 svtype t = SVt_NULL;
816 I32 size;
817  
818 9137 ▶ SvREFCNT_inc_simple_void(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(sv); 9137 0 9137
819  
820 9137 ▶ if (SvTYPE(sv) >= SVt_PVGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) >= SVt_PVGV) { 1013 8124 9137
821 1013 ▶ if (SvFAKE(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvFAKE(sv)) { 2 1011 1013
822 2 sv_force_normal(sv);
823 2 goto string_spec;
824 }
825  
826 1011 ▶ if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */
Branch: 2 branches
BranchTrueFalseTotal
if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */ 1010 1 1011
if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */ 1001 9 1010
827 1002 t = SVt_PVGV;
828 } else { /* local *x = \$val; */
829 9 t = SvTYPE(SvRV(val));
830 }
831 8124 ▶ } else if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(sv)) { 12 8112 8124
832 12 croak("Invalid %s reference as the localization target",
833 sv_reftype(SvRV(sv), 0));
834 } else {
835 STRLEN len, l;
836 const char *p, *s;
837 8114 string_spec:
838 8114 p = SvPV_const(sv, len);
839 8115 ▶ for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
Branch: 2 branches
BranchTrueFalseTotal
for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { } 8114 1 8115
for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { } 1 8113 8114
840 8114 ▶ if (!l) {
Branch: 1 branch
BranchTrueFalseTotal
if (!l) { 1 8113 8114
841 1 l = len;
842 1 s = p;
843 }
844 8114 switch (*s) {
845 1983 case '$': t = SVt_PV; break;
846 4062 case '@': t = SVt_PVAV; break;
847 2051 case '%': t = SVt_PVHV; break;
848 8 case '&': t = SVt_PVCV; break;
849 3 case '*': t = SVt_PVGV; break;
850 }
851 8114 ▶ if (t != SVt_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (t != SVt_NULL) { 8107 7 8114
852 8107 ++s;
853 8107 --l;
854 8107 ▶ if (t == SVt_PV)
Branch: 1 branch
BranchTrueFalseTotal
if (t == SVt_PV) 1983 6124 8107
855 1983 take_ref = 1;
856 7 ▶ } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
Branch: 1 branch
BranchTrueFalseTotal
} else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */ 7 0 7
857 7 ▶ if (SvROK(val) && !sv_isobject(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && !sv_isobject(val)) { 5 2 7
if (SvROK(val) && !sv_isobject(val)) { 4 1 5
858 4 t = SvTYPE(SvRV(val));
859 } else {
860 3 t = SvTYPE(val);
861 3 take_ref = 1;
862 }
863 }
864  
865 8114 SvREFCNT_dec(sv);
866 8114 sv = newSVpvn(s, l);
867 }
868  
869 9125 switch (t) {
870 4064 case SVt_PVAV:
871 4064 size = elem ? SU_SAVE_AELEM_OR_ADELETE_SIZE
872 4064 ▶ : SU_SAVE_ARY_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
: SU_SAVE_ARY_SIZE; 4061 3 4064
873 4064 break;
874 2053 case SVt_PVHV:
875 2053 size = elem ? SU_SAVE_HELEM_OR_HDELETE_SIZE
876 2053 ▶ : SU_SAVE_HASH_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
: SU_SAVE_HASH_SIZE; 2050 3 2053
877 2053 break;
878 1005 case SVt_PVGV:
879 1005 size = SU_SAVE_GP_SIZE;
880 1005 break;
881 14 case SVt_PVCV:
882 14 size = SU_SAVE_GVCV_SIZE;
883 14 break;
884 1989 default:
885 1989 size = SU_SAVE_SCALAR_SIZE;
886 1989 break;
887 }
888  
889 9125 SU_UD_PRIVATE(ud) = t;
890  
891 9125 ud->sv = sv;
892 9125 ▶ if (val) {
Branch: 1 branch
BranchTrueFalseTotal
if (val) { 7932 1193 9125
893 7932 val = newSVsv(val);
894 7932 ▶ ud->val = take_ref ? newRV_noinc(val) : val;
Branch: 1 branch
BranchTrueFalseTotal
ud->val = take_ref ? newRV_noinc(val) : val; 1984 5948 7932
895 } else {
896 1193 ud->val = NULL;
897 }
898 9125 ud->elem = SvREFCNT_inc(elem);
899  
900 9125 return size;
901 }
902  
903 9122 static void su_localize(pTHX_ void *ud_) {
904 #define su_localize(U) su_localize(aTHX_ (U))
905 9122 su_ud_localize *ud = (su_ud_localize *) ud_;
906 9122 SV *sv = ud->sv;
907 9122 SV *val = ud->val;
908 9122 SV *elem = ud->elem;
909 9122 svtype t = SU_UD_PRIVATE(ud);
910 GV *gv;
911  
912 9122 ▶ if (SvTYPE(sv) >= SVt_PVGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) >= SVt_PVGV) { 1011 8111 9122
913 1011 gv = (GV *) sv;
914 } else {
915 /* new perl context implementation frees savestack *before* restoring
916 * PL_curcop. Temporarily restore it prematurely to make gv_fetch*
917 * looks up unqualified var names in the caller's package */
918 #if SU_HAS_NEW_CXT
919 8111 COP *old_cop = PL_curcop;
920 8111 PL_curcop = CX_CUR()->blk_oldcop;
921 #endif
922  
923 #ifdef gv_fetchsv
924 8111 gv = gv_fetchsv(sv, GV_ADDMULTI, t);
925 #else
926 {
927 STRLEN len;
928 const char *name = SvPV_const(sv, len);
929 gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t);
930 }
931 #endif
932  
933 #if SU_HAS_NEW_CXT
934 8111 CX_CUR()->blk_oldcop = old_cop;
935 #endif
936 }
937  
938 XSH_D({
939 SV *z = newSV(0);
940 SvUPGRADE(z, t);
941 xsh_debug_log("%p: === localize a %s\n", ud, sv_reftype(z, 0));
942 xsh_debug_log("%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
943 ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
944 SvREFCNT_dec(z);
945 });
946  
947 /* Inspired from Alias.pm */
948 9122 switch (t) {
949 4064 case SVt_PVAV:
950 4064 ▶ if (elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem) { 4061 3 4064
951 4061 su_save_aelem(GvAV(gv), elem, val);
952 4059 return;
953 } else {
954 3 save_ary(gv);
955 }
956 3 break;
957 2053 case SVt_PVHV:
958 2053 ▶ if (elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem) { 2050 3 2053
959 2050 su_save_helem(GvHV(gv), elem, val);
960 2050 return;
961 } else {
962 3 save_hash(gv);
963 }
964 3 break;
965 1004 case SVt_PVGV:
966 1004 save_gp(gv, 1); /* hide previous entry in symtab */
967 1004 break;
968 13 case SVt_PVCV:
969 13 su_save_gvcv(gv);
970 13 break;
971 1988 default:
972 1988 save_scalar(gv);
973 1988 break;
974 }
975  
976 3011 ▶ if (val)
Branch: 1 branch
BranchTrueFalseTotal
if (val) 3003 8 3011
977 3003 ▶ SvSetMagicSV((SV *) gv, val);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV((SV *) gv, val); 3003 0 3003
SvSetMagicSV((SV *) gv, val); 0 3003 3003
978  
979 3011 return;
980 }
981  
982 /* ... Unique context ID ................................................... */
983  
984 /* We must pass the index because XSH_CXT.uid_storage might be reallocated
985 * between the UID fetch and the invalidation at the end of scope. */
986  
987 typedef struct {
988 su_ud_common ci;
989 I32 idx;
990 } su_ud_uid;
991  
992 823 static void su_uid_drop(pTHX_ void *ud_) {
993 823 su_ud_uid *ud = ud_;
994 dXSH_CXT;
995  
996 823 XSH_CXT.uid_storage.map[ud->idx].flags &= ~SU_UID_ACTIVE;
997  
998 823 ▶ SU_UD_FREE(ud);
Branch: 1 branch
BranchTrueFalseTotal
SU_UD_FREE(ud); 823 0 823
999  
1000 823 return;
1001 }
1002  
1003 /* --- Pop a context back -------------------------------------------------- */
1004  
1005 #ifdef DEBUGGING
1006 # define SU_CX_TYPENAME(T) PL_block_type[(T)]
1007 #else
1008 # if XSH_HAS_PERL(5, 23, 8)
1009 static const char *su_block_type[] = {
1010 "NULL",
1011 "WHEN",
1012 "BLOCK",
1013 "GIVEN",
1014 "LOOP_ARY",
1015 "LOOP_LAZYSV",
1016 "LOOP_LAZYIV",
1017 "LOOP_LIST",
1018 "LOOP_PLAIN",
1019 "SUB",
1020 "FORMAT",
1021 "EVAL",
1022 "SUBST"
1023 };
1024 # elif XSH_HAS_PERL(5, 11, 0)
1025 static const char *su_block_type[] = {
1026 "NULL",
1027 "WHEN",
1028 "BLOCK",
1029 "GIVEN",
1030 "LOOP_FOR",
1031 "LOOP_PLAIN",
1032 "LOOP_LAZYSV",
1033 "LOOP_LAZYIV",
1034 "SUB",
1035 "FORMAT",
1036 "EVAL",
1037 "SUBST"
1038 };
1039 # elif XSH_HAS_PERL(5, 10, 0)
1040 static const char *su_block_type[] = {
1041 "NULL",
1042 "SUB",
1043 "EVAL",
1044 "LOOP",
1045 "SUBST",
1046 "BLOCK",
1047 "FORMAT"
1048 "WHEN",
1049 "GIVEN"
1050 };
1051 # else
1052 static const char *su_block_type[] = {
1053 "NULL",
1054 "SUB",
1055 "EVAL",
1056 "LOOP",
1057 "SUBST",
1058 "BLOCK",
1059 "FORMAT"
1060 };
1061 # endif
1062 # define SU_CX_TYPENAME(T) su_block_type[(T)]
1063 #endif
1064  
1065 #define SU_CXNAME(C) SU_CX_TYPENAME(CxTYPE(C))
1066  
1067 #if XSH_DEBUG
1068  
1069 /* for debugging. These indicate how many ENTERs each context type
1070 * does before the PUSHBLOCK */
1071  
1072 static const int su_cxt_enter_count[] = {
1073 # if XSH_HAS_PERL(5, 23, 8)
1074 0 /* context pushes no longer do ENTERs */
1075 # elif XSH_HAS_PERL(5, 11, 0)
1076 /* NULL WHEN BLOCK GIVEN LOOP_FOR LOOP_PLAIN LOOP_LAZYSV
1077 * LOOP_LAZYIV SUB FORMAT EVAL SUBST */
1078 0, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 0
1079 # elif XSH_HAS_PERL(5, 10, 0)
1080 /* NULL SUB EVAL LOOP SUBST BLOCK FORMAT WHEN GIVEN */
1081 0, 1, 1, 2, 0, 1, 1, 1, 1
1082 # else
1083 /* NULL SUB EVAL LOOP SUBST BLOCK FORMAT */
1084 0, 1, 1, 2, 0, 1, 1
1085 # endif
1086 };
1087  
1088 #endif /* XSH_DEBUG */
1089  
1090 /* push at least 'size' slots worth of padding onto the savestack */
1091  
1092 34281 static void su_ss_push_padding(pTHX_ void *ud, I32 size) {
1093 #define su_ss_push_padding(U, S) su_ss_push_padding(aTHX_ (U), (S))
1094 34281 ▶ if (size <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 0) 14236 20045 34281
1095 14236 return;
1096  
1097 20045 ▶ if (size < SU_SAVE_ALLOC_SIZE + 1) /* minimum possible SAVEt_ALLOC */
Branch: 1 branch
BranchTrueFalseTotal
if (size < SU_SAVE_ALLOC_SIZE + 1) /* minimum possible SAVEt_ALLOC */ 12342 7703 20045
1098 12342 size = SU_SAVE_ALLOC_SIZE + 1;
1099  
1100 XSH_D(xsh_debug_log(
1101 "%p: push %2d padding at save_ix=%d\n",
1102 ud, size, PL_savestack_ix));
1103  
1104 20045 save_alloc((size - SU_SAVE_ALLOC_SIZE) * sizeof(*PL_savestack), 0);
1105  
1106 20045 return;
1107 }
1108  
1109 static void su_pop(pTHX_ void *ud);
1110  
1111 /* push an su_pop destructor onto the savestack with suitable padding.
1112 * first indicates that this is the first push of a destructor */
1113  
1114 30222 static void su_ss_push_destructor(pTHX_ void *ud, I32 depth, bool first) {
1115 #define su_ss_push_destructor(U, D, F) su_ss_push_destructor(aTHX_ (U), (D), (F))
1116 30222 su_ud_origin_elem *origin = SU_UD_ORIGIN(ud);
1117  
1118 assert(first || origin[depth+1].orig_ix == PL_savestack_ix);
1119  
1120 30222 su_ss_push_padding(ud,
1121 (origin[depth].orig_ix + origin[depth].offset) - PL_savestack_ix);
1122  
1123 XSH_D(xsh_debug_log(
1124 "%p: push destructor at save_ix=%d depth=%d scope_ix=%d\n",
1125 ud, PL_savestack_ix, depth, PL_scopestack_ix));
1126  
1127 30222 SAVEDESTRUCTOR_X(su_pop, ud);
1128  
1129 assert(first ||
1130 PL_savestack_ix <= origin[depth+1].orig_ix + origin[depth+1].offset);
1131  
1132 30222 return;
1133 }
1134  
1135 /* this is called during each leave_scope() via SAVEDESTRUCTOR_X */
1136  
1137 30222 static void su_pop(pTHX_ void *ud) {
1138 #define su_pop(U) su_pop(aTHX_ (U))
1139 I32 depth, base, mark;
1140 su_ud_origin_elem *origin;
1141  
1142 30222 depth = SU_UD_DEPTH(ud);
1143 30222 origin = SU_UD_ORIGIN(ud);
1144  
1145 XSH_D(xsh_debug_log("%p: ### su_pop: depth=%d\n", ud, depth));
1146  
1147 30222 depth--;
1148 30222 mark = PL_savestack_ix;
1149 30222 base = origin[depth].orig_ix;
1150  
1151 XSH_D(xsh_debug_log("%p: residual savestack frame is %d(+%d)..%d\n",
1152 ud, base, origin[depth].offset, mark));
1153  
1154 30222 ▶ if (base < mark) {
Branch: 1 branch
BranchTrueFalseTotal
if (base < mark) { 30222 0 30222
1155 XSH_D(xsh_debug_log("%p: clear leftovers at %d..%d\n", ud, base, mark));
1156 30222 leave_scope(base);
1157 }
1158 assert(PL_savestack_ix == base);
1159  
1160 30222 SU_UD_DEPTH(ud) = depth;
1161  
1162 30222 ▶ if (depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth > 0) { 16917 13305 30222
1163 16917 su_ss_push_destructor(ud, depth-1, 0);
1164 } else {
1165 13305 I32 offset = origin[0].offset; /* grab value before origin is freed */
1166 13305 switch (SU_UD_TYPE(ud)) {
1167 3360 case SU_UD_TYPE_REAP: {
1168 XSH_D(
1169 xsh_debug_log("%p: === reap\n%p: depth=%d scope_ix=%d save_ix=%d\n",
1170 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix)
1171 );
1172 3360 SAVEDESTRUCTOR_X(su_call, SU_UD_REAP_CB(ud));
1173 3360 ▶ SU_UD_FREE(ud);
Branch: 1 branch
BranchTrueFalseTotal
SU_UD_FREE(ud); 3360 0 3360
1174 3360 break;
1175 }
1176 9122 case SU_UD_TYPE_LOCALIZE:
1177 9122 su_localize(ud);
1178 9120 ▶ SU_UD_LOCALIZE_FREE(ud);
Branch: 1 branch
BranchTrueFalseTotal
SU_UD_LOCALIZE_FREE(ud); 9120 0 9120
1179 9120 break;
1180 823 case SU_UD_TYPE_UID:
1181 823 SAVEDESTRUCTOR_X(su_uid_drop, ud);
1182 823 break;
1183 }
1184 /* perl 5.23.8 onwards is very fussy about the return from leave_scope()
1185 * leaving PL_savestack_ix where it expects it to be */
1186 13303 ▶ if (PL_savestack_ix < base + offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_savestack_ix < base + offset) { 4059 9244 13303
1187 4059 I32 gap = (base + offset) - PL_savestack_ix;
1188 assert(gap >= SU_SAVE_ALLOC_SIZE + 1);
1189 4059 su_ss_push_padding(ud, gap);
1190 }
1191 assert(PL_savestack_ix == base + offset);
1192 }
1193  
1194 XSH_D(xsh_debug_log("%p: end pop: ss_ix=%d\n", ud, PL_savestack_ix));
1195 30220 }
1196  
1197 /* --- Initialize the stack and the action userdata ------------------------ */
1198  
1199 13305 static void su_init(pTHX_ void *ud, I32 cxix, I32 size) {
1200 #define su_init(U, C, S) su_init(aTHX_ (U), (C), (S))
1201 su_ud_origin_elem *origin;
1202 I32 i, depth;
1203 I32 cur_cx_ix, cur_scope_ix;
1204  
1205 XSH_D(xsh_debug_log("%p: ### su_init(cxix=%d, size=%d)\n", ud, cxix, size));
1206  
1207 13305 depth = PL_scopestack_ix - cxstack[cxix].blk_oldscopesp;
1208 #if SU_HAS_NEW_CXT
1209 13305 depth += (cxstack_ix - cxix); /* each context frame holds 1 scope */
1210 #endif
1211 XSH_D(xsh_debug_log(
1212 "%p: going down by depth=%d with scope_ix=%d save_ix=%d\n",
1213 ud, depth, PL_scopestack_ix, PL_savestack_ix));
1214  
1215 /* Artificially increase the position of each savestack frame boundary
1216 * to make space to squeeze in a 'size' sized entry (first one) or a
1217 * SU_SAVE_DESTRUCTOR_SIZE sized entry (higher ones). In addition, make
1218 * sure that each boundary is higher than the previous, so that *every*
1219 * scope exit triggers a call to leave_scope(). Each scope exit will call
1220 * the su_pop() destructor, which is responsible for: freeing any
1221 * savestack entries below the artificially raised floor; then pushing a
1222 * new destructor in that space. On the final pop, the "real" savestack
1223 * action is pushed rather than another destructor.
1224 *
1225 * On older perls, savestack frame boundaries are specified by a range of
1226 * scopestack entries (one per ENTER). Each scope entry typically does
1227 * one or two ENTERs followed by a PUSHBLOCK. Thus the
1228 * cx->blku_oldscopesp field set by the PUSHBLOCK points to the next free
1229 * slot, which is one above the last of the ENTERs. In the debugging
1230 * output we indicate that by bracketing the ENTERs directly preceding
1231 * that context push with dashes, e.g.:
1232 *
1233 * 13b98d8: ------------------
1234 * 13b98d8: ENTER origin[0] scope[3] savestack=3+3
1235 * 13b98d8: ENTER origin[1] scope[4] savestack=9+3
1236 * 13b98d8: cx=1 LOOP_LAZYIV
1237 * 13b98d8: ------------------
1238 *
1239 * In addition to context stack pushes, other activities can push ENTERs
1240 * too, such as grep expr and XS sub calls.
1241 *
1242 * For newer perls (SU_HAS_NEW_CXT), a context push no longer does any
1243 * ENTERs; instead the old savestack position is stored in the new
1244 * cx->blk_oldsaveix field; thus this field specifies an additional
1245 * savestack frame boundary point in addition to the scopestack entries,
1246 * and will also need adjusting.
1247 *
1248 * We record the original and modified position of each boundary in the
1249 * origin array.
1250 *
1251 * The passed cxix argument represents the scope we wish to inject into;
1252 * we have to adjust all the savestack frame boundaries above (but not
1253 * including) that context.
1254 */
1255  
1256 13305 Newx(origin, depth, su_ud_origin_elem);
1257  
1258 13305 cur_cx_ix = cxix;
1259 13305 cur_scope_ix = cxstack[cxix].blk_oldscopesp;
1260  
1261 #if SU_HAS_NEW_CXT
1262 XSH_D(xsh_debug_log("%p: cx=%-2d %-11s\n",
1263 ud, cur_cx_ix, SU_CXNAME(cxstack+cur_cx_ix)));
1264 13305 cur_cx_ix++;
1265 #endif
1266  
1267 43527 ▶ for (i = 0; cur_scope_ix < PL_scopestack_ix; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; cur_scope_ix < PL_scopestack_ix; i++) { 30222 13305 43527
1268 I32 *ixp;
1269 I32 offset;
1270  
1271 #if SU_HAS_NEW_CXT
1272  
1273 30222 ▶ if (cur_cx_ix <= cxstack_ix
Branch: 1 branch
BranchTrueFalseTotal
if (cur_cx_ix <= cxstack_ix 16917 13305 30222
1274 16917 ▶ && cur_scope_ix == cxstack[cur_cx_ix].blk_oldscopesp)
Branch: 1 branch
BranchTrueFalseTotal
&& cur_scope_ix == cxstack[cur_cx_ix].blk_oldscopesp) 16913 4 16917
1275 16913 ixp = &(cxstack[cur_cx_ix++].blk_oldsaveix);
1276 else
1277 13309 ixp = &PL_scopestack[cur_scope_ix++]; /* an ENTER pushed after cur context */
1278  
1279 #else
1280  
1281 XSH_D({
1282 if (cur_cx_ix <= cxstack_ix) {
1283 if (cur_scope_ix == cxstack[cur_cx_ix].blk_oldscopesp) {
1284 xsh_debug_log("%p: cx=%-2d %s\n%p: ------------------\n",
1285 ud, cur_cx_ix, SU_CXNAME(cxstack+cur_cx_ix), ud);
1286 cur_cx_ix++;
1287 }
1288 else if (cur_scope_ix + su_cxt_enter_count[CxTYPE(cxstack+cur_cx_ix)]
1289 == cxstack[cur_cx_ix].blk_oldscopesp)
1290 xsh_debug_log("%p: ------------------\n", ud);
1291 }
1292 });
1293  
1294 ixp = &PL_scopestack[cur_scope_ix++];
1295  
1296 #endif
1297  
1298 30222 ▶ if (i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) { 13305 16917 30222
1299 13305 offset = size;
1300 } else {
1301 /* we have three constraints to satisfy:
1302 * 1) Each adjusted offset must be at least SU_SAVE_DESTRUCTOR_SIZE
1303 * above its unadjusted boundary, so that there is space to inject a
1304 * destructor into the outer scope.
1305 * 2) Each adjusted boundary must be at least SU_SAVE_DESTRUCTOR_SIZE
1306 * higher than the previous adjusted boundary, so that a new
1307 * destructor can be added below the Nth adjusted frame boundary,
1308 * but be within the (N-1)th adjusted frame and so be triggered on
1309 * the next scope exit;
1310 * 3) If the adjustment needs to be greater than SU_SAVE_DESTRUCTOR_SIZE,
1311 * then it should be greater by an amount of at least the minimum
1312 * pad side, so a destructor and padding can be pushed.
1313 */
1314 I32 pad;
1315 16917 offset = SU_SAVE_DESTRUCTOR_SIZE; /* rule 1 */
1316 16917 pad = (origin[i-1].orig_ix + origin[i-1].offset) + offset
1317 16917 - (*ixp + offset);
1318 16917 ▶ if (pad > 0) { /* rule 2 */
Branch: 1 branch
BranchTrueFalseTotal
if (pad > 0) { /* rule 2 */ 2681 14236 16917
1319 2681 ▶ if (pad < SU_SAVE_ALLOC_SIZE + 1) /* rule 3 */
Branch: 1 branch
BranchTrueFalseTotal
if (pad < SU_SAVE_ALLOC_SIZE + 1) /* rule 3 */ 26 2655 2681
1320 26 pad = SU_SAVE_ALLOC_SIZE + 1;
1321 2681 offset += pad;
1322 }
1323 }
1324  
1325 30222 origin[i].offset = offset;
1326 30222 origin[i].orig_ix = *ixp;
1327 30222 *ixp += offset;
1328  
1329 #if SU_HAS_NEW_CXT
1330 XSH_D({
1331 if (ixp == &PL_scopestack[cur_scope_ix-1])
1332 xsh_debug_log(
1333 "%p: ENTER origin[%d] scope[%d] savestack=%d+%d\n",
1334 ud, i, cur_scope_ix, origin[i].orig_ix, origin[i].offset);
1335 else
1336 xsh_debug_log(
1337 "%p: cx=%-2d %-11s origin[%d] scope[%d] savestack=%d+%d\n",
1338 ud, cur_cx_ix-1, SU_CXNAME(cxstack+cur_cx_ix-1),
1339 i, cur_scope_ix, origin[i].orig_ix, origin[i].offset);
1340 });
1341 #else
1342 XSH_D(xsh_debug_log(
1343 "%p: ENTER origin[%d] scope[%d] savestack=%d+%d\n",
1344 ud, i, cur_scope_ix, origin[i].orig_ix, origin[i].offset));
1345 #endif
1346  
1347 }
1348  
1349 assert(i == depth);
1350  
1351 13305 SU_UD_DEPTH(ud) = depth;
1352 13305 SU_UD_ORIGIN(ud) = origin;
1353  
1354 13305 su_ss_push_destructor(ud, depth-1, 1);
1355 13305 }
1356  
1357 /* --- Unwind stack -------------------------------------------------------- */
1358  
1359 5231 static void su_unwind(pTHX_ void *ud_) {
1360 dXSH_CXT;
1361 5231 I32 cxix = XSH_CXT.unwind_storage.cxix;
1362 5231 I32 items = XSH_CXT.unwind_storage.items;
1363 I32 mark;
1364  
1365 PERL_UNUSED_VAR(ud_);
1366  
1367 5231 PL_stack_sp = XSH_CXT.unwind_storage.savesp;
1368 #if XSH_HAS_PERL(5, 19, 4)
1369 {
1370 I32 i;
1371 5231 SV **sp = PL_stack_sp;
1372 10566 ▶ for (i = -items + 1; i <= 0; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = -items + 1; i <= 0; ++i) 5335 5231 10566
1373 5335 ▶ if (!SvTEMP(sp[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTEMP(sp[i])) 5321 14 5335
1374 5321 sv_2mortal(SvREFCNT_inc(sp[i]));
1375 }
1376 #endif
1377  
1378 5231 ▶ if (cxstack_ix > cxix)
Branch: 1 branch
BranchTrueFalseTotal
if (cxstack_ix > cxix) 3912 1319 5231
1379 3912 dounwind(cxix);
1380  
1381 5231 mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
1382 5231 ▶ PUSHMARK(PL_stack_sp - items);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(PL_stack_sp - items); 0 5231 5231
1383  
1384 XSH_D({
1385 I32 gimme = GIMME_V;
1386 xsh_debug_log("%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
1387 &XSH_CXT, cxix,
1388 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
1389 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
1390 });
1391  
1392 5231 PL_op = (OP *) &(XSH_CXT.unwind_storage.return_op);
1393 5231 PL_op = PL_op->op_ppaddr(aTHX);
1394  
1395 5231 *PL_markstack_ptr = mark;
1396  
1397 5231 XSH_CXT.unwind_storage.proxy_op.op_next = PL_op;
1398 5231 PL_op = &(XSH_CXT.unwind_storage.proxy_op);
1399 5231 }
1400  
1401 /* --- Yield --------------------------------------------------------------- */
1402  
1403 #if XSH_HAS_PERL(5, 10, 0)
1404 # define SU_RETOP_SUB(C) ((C)->blk_sub.retop)
1405 # define SU_RETOP_EVAL(C) ((C)->blk_eval.retop)
1406 # define SU_RETOP_LOOP(C) ((C)->blk_loop.my_op->op_lastop->op_next)
1407 # if SU_HAS_GIVEN
1408 # define SU_RETOP_GIVEN(C) ((C)->blk_givwhen.leave_op->op_next)
1409 # endif
1410 #else
1411 # define SU_RETOP_SUB(C) ((C)->blk_oldretsp > 0 ? PL_retstack[(C)->blk_oldretsp - 1] : NULL)
1412 # define SU_RETOP_EVAL(C) SU_RETOP_SUB(C)
1413 # define SU_RETOP_LOOP(C) ((C)->blk_loop.last_op->op_next)
1414 #endif
1415  
1416 41526 static void su_yield(pTHX_ void *ud_) {
1417 dXSH_CXT;
1418 PERL_CONTEXT *cx;
1419 41526 const char *which = ud_;
1420 41526 I32 cxix = XSH_CXT.yield_storage.cxix;
1421 41526 I32 items = XSH_CXT.yield_storage.items;
1422 41526 opcode type = OP_NULL;
1423 41526 U8 flags = 0;
1424 OP *next;
1425  
1426 41526 cx = cxstack + cxix;
1427 41526 switch (CxTYPE(cx)) {
1428 15584 case CXt_BLOCK: {
1429 15584 I32 i, cur = cxstack_ix, n = 1;
1430 15584 OP *o = NULL;
1431 /* Is this actually a given/when block? This may occur only when yield was
1432 * called with HERE (or nothing) as the context. */
1433 #if SU_HAS_GIVEN
1434 if (cxix > 0) {
1435 PERL_CONTEXT *prev = cx - 1;
1436 U8 prev_type = CxTYPE(prev);
1437 if ((prev_type == CXt_GIVEN || prev_type == CXt_WHEN)
1438 && (prev->blk_oldcop == cx->blk_oldcop)) {
1439 cxix--;
1440 cx = prev;
1441 if (prev_type == CXt_GIVEN)
1442 goto cxt_given;
1443 else
1444 goto cxt_when;
1445 }
1446 }
1447 #endif
1448 15584 type = OP_LEAVE;
1449 15584 next = NULL;
1450 /* Bare blocks (that appear as do { ... } blocks, map { ... } blocks or
1451 * constant folded blcoks) don't need to save the op to return to anywhere
1452 * since 'last' isn't supposed to work inside them. So we climb higher in
1453 * the context stack until we reach a context that has a return op (i.e. a
1454 * sub, an eval, a format or a real loop), recording how many blocks we
1455 * crossed. Then we follow the op_next chain until we get to the leave op
1456 * that closes the original block, which we are assured to reach since
1457 * everything is static (the blocks we have crossed cannot be evals or
1458 * subroutine calls). */
1459 15586 ▶ for (i = cxix + 1; i <= cur; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxix + 1; i <= cur; ++i) { 15567 19 15586
1460 15567 PERL_CONTEXT *cx2 = cxstack + i;
1461 15567 switch (CxTYPE(cx2)) {
1462 2 case CXt_BLOCK:
1463 2 ++n;
1464 2 break;
1465 15552 case CXt_SUB:
1466 case CXt_FORMAT:
1467 15552 o = SU_RETOP_SUB(cx2);
1468 15552 break;
1469 7 case CXt_EVAL:
1470 7 o = SU_RETOP_EVAL(cx2);
1471 7 break;
1472 #if XSH_HAS_PERL(5, 11, 0)
1473 # if XSH_HAS_PERL(5, 23, 8)
1474 6 case CXt_LOOP_ARY:
1475 case CXt_LOOP_LIST:
1476 # else
1477 case CXt_LOOP_FOR:
1478 # endif
1479 case CXt_LOOP_PLAIN:
1480 case CXt_LOOP_LAZYSV:
1481 case CXt_LOOP_LAZYIV:
1482 #else
1483 case CXt_LOOP:
1484 #endif
1485 6 o = SU_RETOP_LOOP(cx2);
1486 6 break;
1487 }
1488 15567 ▶ if (o)
Branch: 1 branch
BranchTrueFalseTotal
if (o) 15565 2 15567
1489 15565 break;
1490 }
1491 15584 ▶ if (!o)
Branch: 1 branch
BranchTrueFalseTotal
if (!o) 19 15565 15584
1492 19 o = PL_op;
1493 39065 ▶ while (n && o) {
Branch: 2 branches
BranchTrueFalseTotal
while (n && o) { 39065 0 39065
while (n && o) { 39065 0 39065
1494 /* We may find other enter/leave blocks on our way to the matching leave.
1495 * Make sure the depth is incremented/decremented appropriately. */
1496 39065 ▶ if (o->op_type == OP_ENTER) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_type == OP_ENTER) { 2 39063 39065
1497 2 ++n;
1498 39063 ▶ } else if (o->op_type == OP_LEAVE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (o->op_type == OP_LEAVE) { 15588 23475 39063
1499 15588 --n;
1500 15588 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 15584 4 15588
1501 15584 next = o->op_next;
1502 15584 break;
1503 }
1504 }
1505 23481 o = o->op_next;
1506 }
1507 15584 break;
1508 }
1509 18158 case CXt_SUB:
1510 case CXt_FORMAT:
1511 18158 type = OP_LEAVESUB;
1512 18158 next = SU_RETOP_SUB(cx);
1513 18158 break;
1514 7778 case CXt_EVAL:
1515 7778 ▶ type = CxTRYBLOCK(cx) ? OP_LEAVETRY : OP_LEAVEEVAL;
Branch: 1 branch
BranchTrueFalseTotal
type = CxTRYBLOCK(cx) ? OP_LEAVETRY : OP_LEAVEEVAL; 7777 1 7778
1516 7778 next = SU_RETOP_EVAL(cx);
1517 7778 break;
1518 #if XSH_HAS_PERL(5, 11, 0)
1519 # if XSH_HAS_PERL(5, 23, 8)
1520 4 case CXt_LOOP_ARY:
1521 case CXt_LOOP_LIST:
1522 # else
1523 case CXt_LOOP_FOR:
1524 # endif
1525 case CXt_LOOP_PLAIN:
1526 case CXt_LOOP_LAZYSV:
1527 case CXt_LOOP_LAZYIV:
1528 #else
1529 case CXt_LOOP:
1530 #endif
1531 4 type = OP_LEAVELOOP;
1532 4 next = SU_RETOP_LOOP(cx);
1533 4 break;
1534 #if SU_HAS_GIVEN
1535 case CXt_GIVEN:
1536 cxt_given:
1537 type = OP_LEAVEGIVEN;
1538 next = SU_RETOP_GIVEN(cx);
1539 break;
1540 case CXt_WHEN:
1541 cxt_when:
1542 #if XSH_HAS_PERL(5, 15, 1)
1543 type = OP_LEAVEWHEN;
1544 #else
1545 type = OP_BREAK;
1546 flags |= OPf_SPECIAL;
1547 #endif
1548 next = NULL;
1549 break;
1550 #endif
1551 2 case CXt_SUBST:
1552 2 croak("%s() can't target a substitution context", which);
1553 break;
1554 0 default:
1555 0 croak("%s() doesn't know how to leave a %s context",
1556 which, SU_CXNAME(cxstack + cxix));
1557 break;
1558 }
1559  
1560 41524 PL_stack_sp = XSH_CXT.yield_storage.savesp;
1561 #if XSH_HAS_PERL(5, 19, 4)
1562 {
1563 I32 i;
1564 41524 SV **sp = PL_stack_sp;
1565 83115 ▶ for (i = -items + 1; i <= 0; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = -items + 1; i <= 0; ++i) 41591 41524 83115
1566 41591 ▶ if (!SvTEMP(sp[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTEMP(sp[i])) 41589 2 41591
1567 41589 sv_2mortal(SvREFCNT_inc(sp[i]));
1568 }
1569 #endif
1570  
1571 41524 ▶ if (cxstack_ix > cxix)
Branch: 1 branch
BranchTrueFalseTotal
if (cxstack_ix > cxix) 31120 10404 41524
1572 31120 dounwind(cxix);
1573  
1574 /* Copy the arguments passed to yield() where the leave op expects to find
1575 * them. */
1576 41524 ▶ if (items)
Branch: 1 branch
BranchTrueFalseTotal
if (items) 27690 13834 41524
1577 27690 Move(PL_stack_sp - items + 1, PL_stack_base + cx->blk_oldsp + 1, items, SV *);
1578 41524 PL_stack_sp = PL_stack_base + cx->blk_oldsp + items;
1579  
1580 41524 flags |= OP_GIMME_REVERSE(cx->blk_gimme);
1581  
1582 41524 XSH_CXT.yield_storage.leave_op.op_type = type;
1583 41524 XSH_CXT.yield_storage.leave_op.op_ppaddr = PL_ppaddr[type];
1584 41524 XSH_CXT.yield_storage.leave_op.op_flags = flags;
1585 41524 XSH_CXT.yield_storage.leave_op.op_next = next;
1586  
1587 41524 PL_op = (OP *) &(XSH_CXT.yield_storage.leave_op);
1588 41524 PL_op = PL_op->op_ppaddr(aTHX);
1589  
1590 41524 XSH_CXT.yield_storage.proxy_op.op_next = PL_op;
1591 41524 PL_op = &(XSH_CXT.yield_storage.proxy_op);
1592 41524 }
1593  
1594 /* --- Uplevel ------------------------------------------------------------- */
1595  
1596 #define SU_UPLEVEL_SAVE(f, t) STMT_START { sud->old_##f = PL_##f; PL_##f = (t); } STMT_END
1597 #define SU_UPLEVEL_RESTORE(f) STMT_START { PL_##f = sud->old_##f; } STMT_END
1598  
1599 2749 static su_uplevel_ud *su_uplevel_storage_new(pTHX_ I32 cxix) {
1600 #define su_uplevel_storage_new(I) su_uplevel_storage_new(aTHX_ (I))
1601 su_uplevel_ud *sud;
1602 UV depth;
1603 dXSH_CXT;
1604  
1605 2749 sud = XSH_CXT.uplevel_storage.root;
1606 2749 ▶ if (sud) {
Branch: 1 branch
BranchTrueFalseTotal
if (sud) { 2507 242 2749
1607 2507 XSH_CXT.uplevel_storage.root = sud->next;
1608 2507 XSH_CXT.uplevel_storage.count--;
1609 } else {
1610 242 sud = su_uplevel_ud_new();
1611 }
1612  
1613 2749 sud->next = XSH_CXT.uplevel_storage.top;
1614 2749 XSH_CXT.uplevel_storage.top = sud;
1615  
1616 2749 depth = su_uid_depth(cxix);
1617 2749 su_uid_storage_dup(&sud->tmp_uid_storage, &XSH_CXT.uid_storage, depth);
1618 2749 sud->old_uid_storage = XSH_CXT.uid_storage;
1619 2749 XSH_CXT.uid_storage = sud->tmp_uid_storage;
1620  
1621 2749 return sud;
1622 }
1623  
1624 #if XSH_HAS_PERL(5, 13, 7)
1625  
1626 2749 static void su_uplevel_storage_delete(pTHX_ su_uplevel_ud *sud) {
1627 #define su_uplevel_storage_delete(S) su_uplevel_storage_delete(aTHX_ (S))
1628 dXSH_CXT;
1629  
1630 2749 sud->tmp_uid_storage = XSH_CXT.uid_storage;
1631 2749 XSH_CXT.uid_storage = sud->old_uid_storage;
1632 {
1633 su_uid *map;
1634 STRLEN i, alloc;
1635 2749 map = sud->tmp_uid_storage.map;
1636 2749 alloc = sud->tmp_uid_storage.alloc;
1637 16485 ▶ for (i = 0; i < alloc; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < alloc; ++i) 13736 2749 16485
1638 13736 map[i].flags &= ~SU_UID_ACTIVE;
1639 }
1640 2749 XSH_CXT.uplevel_storage.top = sud->next;
1641  
1642 2749 ▶ if (XSH_CXT.uplevel_storage.count >= SU_UPLEVEL_STORAGE_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (XSH_CXT.uplevel_storage.count >= SU_UPLEVEL_STORAGE_SIZE) { 224 2525 2749
1643 224 su_uplevel_ud_delete(sud);
1644 } else {
1645 2525 sud->next = XSH_CXT.uplevel_storage.root;
1646 2525 XSH_CXT.uplevel_storage.root = sud;
1647 2525 XSH_CXT.uplevel_storage.count++;
1648 }
1649 2749 }
1650  
1651 #endif
1652  
1653 0 static int su_uplevel_goto_static(const OP *o) {
1654 0 ▶ for (; o; o = OpSIBLING(o)) {
Branch: 2 branches
BranchTrueFalseTotal
for (; o; o = OpSIBLING(o)) { 0 0 0
for (; o; o = OpSIBLING(o)) { 0 0 0
1655 /* goto ops are unops with kids. */
1656 0 ▶ if (!(o->op_flags & OPf_KIDS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(o->op_flags & OPf_KIDS)) 0 0 0
1657 0 continue;
1658  
1659 0 switch (o->op_type) {
1660 0 case OP_LEAVEEVAL:
1661 case OP_LEAVETRY:
1662 /* Don't care about gotos inside eval, as they are forbidden at run time. */
1663 0 break;
1664 0 case OP_GOTO:
1665 0 return 1;
1666 0 default:
1667 0 ▶ if (su_uplevel_goto_static(((const UNOP *) o)->op_first))
Branch: 1 branch
BranchTrueFalseTotal
if (su_uplevel_goto_static(((const UNOP *) o)->op_first)) 0 0 0
1668 0 return 1;
1669 0 break;
1670 }
1671 }
1672  
1673 0 return 0;
1674 }
1675  
1676 #if !SU_HAS_NEW_CXT && SU_UPLEVEL_HIJACKS_RUNOPS
1677  
1678 static int su_uplevel_goto_runops(pTHX) {
1679 #define su_uplevel_goto_runops() su_uplevel_goto_runops(aTHX)
1680 register OP *op;
1681 dVAR;
1682  
1683 op = PL_op;
1684 do {
1685 if (op->op_type == OP_GOTO) {
1686 AV *argarray = NULL;
1687 I32 cxix;
1688  
1689 for (cxix = cxstack_ix; cxix >= 0; --cxix) {
1690 const PERL_CONTEXT *cx = cxstack + cxix;
1691  
1692 switch (CxTYPE(cx)) {
1693 case CXt_SUB:
1694 if (CxHASARGS(cx)) {
1695 argarray = cx->blk_sub.argarray;
1696 goto done;
1697 }
1698 break;
1699 case CXt_EVAL:
1700 case CXt_FORMAT:
1701 goto done;
1702 default:
1703 break;
1704 }
1705 }
1706  
1707 done:
1708 if (argarray) {
1709 dXSH_CXT;
1710  
1711 if (XSH_CXT.uplevel_storage.top->cxix == cxix) {
1712 AV *args = GvAV(PL_defgv);
1713 I32 items = AvFILLp(args);
1714  
1715 av_extend(argarray, items);
1716 Copy(AvARRAY(args), AvARRAY(argarray), items + 1, SV *);
1717 AvFILLp(argarray) = items;
1718 }
1719 }
1720 }
1721  
1722 PL_op = op = op->op_ppaddr(aTHX);
1723  
1724 #if !XSH_HAS_PERL(5, 13, 0)
1725 PERL_ASYNC_CHECK();
1726 #endif
1727 } while (op);
1728  
1729 TAINT_NOT;
1730  
1731 return 0;
1732 }
1733  
1734 #endif /* SU_UPLEVEL_HIJACKS_RUNOPS */
1735  
1736 #define su_at_underscore(C) PadARRAY(PadlistARRAY(CvPADLIST(C))[CvDEPTH(C)])[0]
1737  
1738 #if SU_HAS_NEW_CXT
1739  
1740 2749 static void su_uplevel_restore_new(pTHX_ void *sus_) {
1741 2749 su_uplevel_ud *sud = sus_;
1742 PERL_CONTEXT *cx;
1743 I32 i;
1744 2749 U8 *saved_cxtypes = sud->cxtypes;
1745  
1746 38844 ▶ for (i = 0; i < sud->gap; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sud->gap; i++) { 36095 2749 38844
1747 36095 PERL_CONTEXT *cx = cxstack + sud->cxix + i;
1748 XSH_D(xsh_debug_log("su_uplevel_restore: i=%d cxix=%d type %s => %s\n",
1749 i, cx-cxstack, SU_CX_TYPENAME(CxTYPE(cx)),
1750 SU_CX_TYPENAME(saved_cxtypes[i] & CXTYPEMASK)));
1751 36095 cx->cx_type = saved_cxtypes[i];
1752 }
1753 2749 Safefree(saved_cxtypes);
1754  
1755 /* renamed is a copy of callback, but they share the same CvPADLIST.
1756 * At this point any calls to renamed should have exited so that its
1757 * depth is back to that of of callback. At this point its safe to free
1758 * renamed, then undo the extra ref count that was ensuring that callback
1759 * remains alive
1760 */
1761 assert(sud->renamed);
1762 assert(sud->callback);
1763  
1764 2749 CvDEPTH(sud->callback)--;
1765 assert(CvDEPTH(sud->callback) == CvDEPTH(sud->renamed));
1766 2749 ▶ if (!CvISXSUB(sud->renamed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!CvISXSUB(sud->renamed)) { 2744 5 2749
1767 2744 CvDEPTH(sud->renamed) = 0;
1768 2744 CvPADLIST(sud->renamed) = NULL;
1769 }
1770 2749 SvREFCNT_dec(sud->renamed);
1771 2749 SvREFCNT_dec(sud->callback);
1772  
1773 2749 SU_UPLEVEL_RESTORE(curcop);
1774  
1775 2749 su_uplevel_storage_delete(sud);
1776  
1777 2749 return;
1778 }
1779  
1780 #else
1781  
1782 /* 5.23.7 and earlier */
1783  
1784 static void su_uplevel_restore_old(pTHX_ void *sus_) {
1785 su_uplevel_ud *sud = sus_;
1786 PERL_SI *cur = sud->old_curstackinfo;
1787 PERL_SI *si = sud->si;
1788  
1789 #if SU_UPLEVEL_HIJACKS_RUNOPS
1790 if (PL_runops == su_uplevel_goto_runops)
1791 PL_runops = sud->old_runops;
1792 #endif
1793  
1794 if (sud->callback) {
1795 PERL_CONTEXT *cx = cxstack + sud->cxix;
1796 AV *argarray = MUTABLE_AV(su_at_underscore(sud->callback));
1797  
1798 /* We have to fix the pad entry for @_ in the original callback because it
1799 * may have been reified. */
1800 if (AvREAL(argarray)) {
1801 const I32 fill = AvFILLp(argarray);
1802 SvREFCNT_dec(argarray);
1803 argarray = newAV();
1804 AvREAL_off(argarray);
1805 AvREIFY_on(argarray);
1806 av_extend(argarray, fill);
1807 su_at_underscore(sud->callback) = MUTABLE_SV(argarray);
1808 } else {
1809 CLEAR_ARGARRAY(argarray);
1810 }
1811  
1812 /* If the old cv member is our renamed CV, it means that this place has been
1813 * reached without a goto() happening, and the old argarray member is
1814 * actually our fake argarray. Destroy it properly in that case. */
1815 if (cx->blk_sub.cv == sud->renamed) {
1816 SvREFCNT_dec(cx->blk_sub.argarray);
1817 cx->blk_sub.argarray = argarray;
1818 }
1819  
1820 CvDEPTH(sud->callback)--;
1821 SvREFCNT_dec(sud->callback);
1822 }
1823  
1824 /* Free the renamed CV. We must do it ourselves so that we can force the
1825 * depth to be 0, or perl would complain about it being "still in use".
1826 * But we *know* that it cannot be so. */
1827 if (sud->renamed) {
1828 if (!CvISXSUB(sud->renamed)) {
1829 CvDEPTH(sud->renamed) = 0;
1830 CvPADLIST(sud->renamed) = NULL;
1831 }
1832 SvREFCNT_dec(sud->renamed);
1833 }
1834  
1835 CATCH_SET(sud->old_catch);
1836  
1837 SU_UPLEVEL_RESTORE(op);
1838  
1839 /* stack_grow() wants PL_curstack so restore the old stack first */
1840 if (PL_curstackinfo == si) {
1841 PL_curstack = cur->si_stack;
1842 if (sud->old_mainstack)
1843 SU_UPLEVEL_RESTORE(mainstack);
1844 SU_UPLEVEL_RESTORE(curstackinfo);
1845  
1846 if (sud->died) {
1847 CV *target = sud->target;
1848 I32 levels = 0, i;
1849  
1850 /* When we die, the depth of the target CV is not updated because of the
1851 * stack switcheroo. So we have to look at all the frames between the
1852 * uplevel call and the catch block to count how many call frames to the
1853 * target CV were skipped. */
1854 for (i = cur->si_cxix; i > sud->cxix; i--) {
1855 register const PERL_CONTEXT *cx = cxstack + i;
1856  
1857 if (CxTYPE(cx) == CXt_SUB) {
1858 if (cx->blk_sub.cv == target)
1859 ++levels;
1860 }
1861 }
1862  
1863 /* If we died, the replacement stack was already unwinded to the first
1864 * eval frame, and all the contexts down there were popped. We don't have
1865 * to pop manually any context of the original stack, because they must
1866 * have been in the replacement stack as well (since the second was copied
1867 * from the first). Thus we only have to make sure the original stack index
1868 * points to the context just below the first eval scope under the target
1869 * frame. */
1870 for (; i >= 0; i--) {
1871 register const PERL_CONTEXT *cx = cxstack + i;
1872  
1873 switch (CxTYPE(cx)) {
1874 case CXt_SUB:
1875 if (cx->blk_sub.cv == target)
1876 ++levels;
1877 break;
1878 case CXt_EVAL:
1879 goto found_it;
1880 break;
1881 default:
1882 break;
1883 }
1884 }
1885  
1886 found_it:
1887 CvDEPTH(target) = sud->target_depth - levels;
1888 PL_curstackinfo->si_cxix = i - 1;
1889  
1890 #if !XSH_HAS_PERL(5, 13, 1)
1891 /* Since $@ was maybe localized between the target frame and the uplevel
1892 * call, we forcefully flush the save stack to get rid of it and then
1893 * reset $@ to its proper value. Note that the the call to
1894 * su_uplevel_restore() must happen before the "reset $@" item of the save
1895 * stack is processed, as uplevel was called after the localization.
1896 * Andrew's changes to how $@ was handled, which were mainly integrated
1897 * between perl 5.13.0 and 5.13.1, fixed this. */
1898 if (ERRSV && SvTRUE(ERRSV)) {
1899 register const PERL_CONTEXT *cx = cxstack + i; /* This is the eval scope */
1900 SV *errsv = SvREFCNT_inc(ERRSV);
1901 PL_scopestack_ix = cx->blk_oldscopesp;
1902 leave_scope(PL_scopestack[PL_scopestack_ix]);
1903 sv_setsv(ERRSV, errsv);
1904 SvREFCNT_dec(errsv);
1905 }
1906 #endif
1907 }
1908 }
1909  
1910 SU_UPLEVEL_RESTORE(curcop);
1911  
1912 SvREFCNT_dec(sud->target);
1913  
1914 PL_stack_base = AvARRAY(cur->si_stack);
1915 PL_stack_sp = PL_stack_base + AvFILLp(cur->si_stack);
1916 PL_stack_max = PL_stack_base + AvMAX(cur->si_stack);
1917  
1918 /* When an exception is thrown from the uplevel'd subroutine,
1919 * su_uplevel_restore() may be called by the LEAVE in die_unwind() (renamed
1920 * die_where() in more recent perls), which has the sad habit of keeping a
1921 * pointer to the current context frame across this call. This means that we
1922 * can't free the temporary context stack we used for the uplevel call right
1923 * now, or that pointer upwards would point to garbage. */
1924 #if XSH_HAS_PERL(5, 13, 7)
1925 /* This issue has been fixed in perl with commit 8f89e5a9, which was made
1926 * public in perl 5.13.7. */
1927 su_uplevel_storage_delete(sud);
1928 #else
1929 /* Otherwise, we just enqueue it back in the global storage list. */
1930 {
1931 dXSH_CXT;
1932  
1933 sud->tmp_uid_storage = XSH_CXT.uid_storage;
1934 XSH_CXT.uid_storage = sud->old_uid_storage;
1935  
1936 XSH_CXT.uplevel_storage.top = sud->next;
1937 sud->next = XSH_CXT.uplevel_storage.root;
1938 XSH_CXT.uplevel_storage.root = sud;
1939 XSH_CXT.uplevel_storage.count++;
1940 }
1941 #endif
1942  
1943 return;
1944 }
1945  
1946 #endif
1947  
1948 2749 static CV *su_cv_clone(pTHX_ CV *proto, GV *gv) {
1949 #define su_cv_clone(P, G) su_cv_clone(aTHX_ (P), (G))
1950 dVAR;
1951 CV *cv;
1952  
1953 2749 cv = MUTABLE_CV(newSV_type(SvTYPE(proto)));
1954  
1955 2749 CvFLAGS(cv) = CvFLAGS(proto);
1956 #ifdef CVf_CVGV_RC
1957 2749 CvFLAGS(cv) &= ~CVf_CVGV_RC;
1958 #endif
1959 2749 CvDEPTH(cv) = CvDEPTH(proto);
1960 #ifdef USE_ITHREADS
1961 CvFILE(cv) = CvISXSUB(proto) ? CvFILE(proto) : savepv(CvFILE(proto));
1962 #else
1963 2749 CvFILE(cv) = CvFILE(proto);
1964 #endif
1965  
1966 2749 CvGV_set(cv, gv);
1967 #if SU_RELEASE && XSH_HAS_PERL_EXACT(5, 21, 4)
1968 CvNAMED_off(cv);
1969 #endif
1970 2749 CvSTASH_set(cv, CvSTASH(proto));
1971 /* Commit 4c74a7df, publicized with perl 5.13.3, began to add backrefs to
1972 * stashes. CvSTASH_set() started to do it as well with commit c68d95645
1973 * (which was part of perl 5.13.7). */
1974 #if XSH_HAS_PERL(5, 13, 3) && !XSH_HAS_PERL(5, 13, 7)
1975 if (CvSTASH(proto))
1976 Perl_sv_add_backref(aTHX_ CvSTASH(proto), MUTABLE_SV(cv));
1977 #endif
1978  
1979 2749 ▶ if (CvISXSUB(proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (CvISXSUB(proto)) { 5 2744 2749
1980 5 CvXSUB(cv) = CvXSUB(proto);
1981 5 CvXSUBANY(cv) = CvXSUBANY(proto);
1982 } else {
1983 OP_REFCNT_LOCK;
1984 2744 ▶ CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
Branch: 1 branch
BranchTrueFalseTotal
CvROOT(cv) = OpREFCNT_inc(CvROOT(proto)); 2744 0 2744
1985 OP_REFCNT_UNLOCK;
1986 2744 CvSTART(cv) = CvSTART(proto);
1987 2744 CvPADLIST(cv) = CvPADLIST(proto);
1988 }
1989 2749 CvOUTSIDE(cv) = CvOUTSIDE(proto);
1990 #ifdef CVf_WEAKOUTSIDE
1991 2749 ▶ if (!(CvFLAGS(proto) & CVf_WEAKOUTSIDE))
Branch: 1 branch
BranchTrueFalseTotal
if (!(CvFLAGS(proto) & CVf_WEAKOUTSIDE)) 2749 0 2749
1992 #endif
1993 2749 ▶ SvREFCNT_inc_simple_void(CvOUTSIDE(cv));
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(CvOUTSIDE(cv)); 2744 5 2749
1994 #ifdef CvOUTSIDE_SEQ
1995 2749 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
1996 #endif
1997  
1998 2749 ▶ if (SvPOK(proto))
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(proto)) 5 2744 2749
1999 5 sv_setpvn(MUTABLE_SV(cv), SvPVX_const(proto), SvCUR(proto));
2000  
2001 #ifdef CvCONST
2002 2749 ▶ if (CvCONST(cv))
Branch: 1 branch
BranchTrueFalseTotal
if (CvCONST(cv)) 0 2749 2749
2003 0 CvCONST_off(cv);
2004 #endif
2005  
2006 2749 return cv;
2007 }
2008  
2009 #if SU_HAS_NEW_CXT
2010  
2011 /* this one-shot runops "loop" is designed to be called just before
2012 * execution of the first op following an uplevel()'s entersub. It gets a
2013 * chance to fix up the args as seen by caller(), before immediately
2014 * falling through to the previous runops loop. Note that pp_entersub is
2015 * called directly by call_sv() rather than being called from a runops
2016 * loop.
2017 */
2018  
2019 2744 static int su_uplevel_runops_hook_entersub(pTHX) {
2020 2744 OP *op = PL_op;
2021 dXSH_CXT;
2022 2744 su_uplevel_ud *sud = XSH_CXT.uplevel_storage.top;
2023  
2024 /* Create a new array containing a copy of the original sub's call args,
2025 * then stick it in PL_curpad[0] of the current running sub so that
2026 * thay will be seen by caller().
2027 */
2028 assert(sud);
2029 2744 ▶ if (sud->argarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (sud->argarray) { 2744 0 2744
2030 I32 fill;
2031 2744 AV *av = newAV();
2032 2744 AvREAL_off(av);
2033 2744 AvREIFY_on(av);
2034  
2035 2744 fill = AvFILLp(sud->argarray);
2036 2744 ▶ if (fill >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fill >= 0) { 2565 179 2744
2037 2565 av_extend(av, fill);
2038 2565 Copy(AvARRAY(sud->argarray), AvARRAY(av), fill + 1, SV *);
2039 2565 AvFILLp(av) = fill;
2040 }
2041  
2042 #if !XSH_HAS_PERL(5, 37, 10)
2043 /* should be referenced by PL_curpad[0] and *_ */
2044 assert(SvREFCNT(PL_curpad[0]) > 1);
2045 SvREFCNT_dec(PL_curpad[0]);
2046 #endif
2047  
2048 2744 PL_curpad[0] = (SV *) av;
2049 }
2050  
2051 /* undo the temporary runops hook and fall through to a real runops loop. */
2052 assert(sud->old_runops != su_uplevel_runops_hook_entersub);
2053 2744 PL_runops = sud->old_runops;
2054  
2055 2744 CALLRUNOPS(aTHX);
2056  
2057 1732 return 0;
2058 }
2059  
2060 2749 static I32 su_uplevel_new(pTHX_ CV *callback, I32 cxix, I32 args) {
2061 #define su_uplevel_new(CB, CX, A) su_uplevel_new(aTHX_ (CB), (CX), (A))
2062 su_uplevel_ud *sud;
2063 U8 *saved_cxtypes;
2064 I32 i, ret;
2065 I32 gimme;
2066 2749 CV *base_cv = cxstack[cxix].blk_sub.cv;
2067 2749 dSP;
2068  
2069 assert(CxTYPE(&cxstack[cxix]) == CXt_SUB);
2070  
2071 2749 ENTER;
2072  
2073 2749 gimme = GIMME_V;
2074  
2075 /* At this point SP points to the top arg.
2076 * Shuffle the args down by one, eliminating the CV slot */
2077 2749 Move(SP - args + 1, SP - args, args, SV *);
2078 2749 SP--;
2079 2749 ▶ PUSHMARK(SP - args);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP - args); 0 2749 2749
2080 2749 PUTBACK;
2081  
2082 2749 sud = su_uplevel_storage_new(cxix);
2083  
2084 2749 sud->cxix = cxix;
2085 2749 sud->callback = (CV *) SvREFCNT_inc_simple(callback);
2086 2749 sud->renamed = NULL;
2087 2749 sud->gap = cxstack_ix - cxix + 1;
2088 2749 sud->argarray = NULL;
2089  
2090 2749 Newx(saved_cxtypes, sud->gap, U8);
2091 2749 sud->cxtypes = saved_cxtypes;
2092  
2093 2749 SAVEDESTRUCTOR_X(su_uplevel_restore_new, sud);
2094 2749 SU_UPLEVEL_SAVE(curcop, cxstack[cxix].blk_oldcop);
2095  
2096 /* temporarily change the type of any contexts to NULL, so they're
2097 * invisible to caller() etc. */
2098 38844 ▶ for (i = 0; i < sud->gap; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sud->gap; i++) { 36095 2749 38844
2099 36095 PERL_CONTEXT *cx = cxstack + cxix + i;
2100 36095 saved_cxtypes[i] = cx->cx_type; /* save type and flags */
2101 XSH_D(xsh_debug_log("su_uplevel: i=%d cxix=%d type %-11s => %s\n",
2102 i, cx-cxstack, SU_CX_TYPENAME(CxTYPE(cx)), SU_CX_TYPENAME(CXt_NULL)));
2103 36095 cx->cx_type = (CXt_NULL | CXp_SU_UPLEVEL_NULLED);
2104 }
2105  
2106 /* create a copy of the callback with a doctored name (as seen by
2107 * caller). It shares the padlist with callback */
2108 2749 sud->renamed = su_cv_clone(callback, CvGV(base_cv));
2109 2749 sud->old_runops = PL_runops;
2110  
2111 2749 ▶ if (!CvISXSUB(sud->renamed) && CxHASARGS(&cxstack[cxix])) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CvISXSUB(sud->renamed) && CxHASARGS(&cxstack[cxix])) { 2744 5 2749
if (!CvISXSUB(sud->renamed) && CxHASARGS(&cxstack[cxix])) { 2744 0 2744
2112 2744 sud->argarray = (AV *) su_at_underscore(base_cv);
2113 assert(PL_runops != su_uplevel_runops_hook_entersub);
2114 /* set up a one-shot runops hook so that we can fake up the
2115 * args as seen by caller() on return from pp_entersub */
2116 2744 PL_runops = su_uplevel_runops_hook_entersub;
2117 }
2118  
2119 2749 CvDEPTH(callback)++; /* match what CvDEPTH(sud->renamed) is about to become */
2120  
2121 2749 ret = call_sv((SV *) sud->renamed, gimme);
2122  
2123 1736 LEAVE;
2124  
2125 1736 return ret;
2126 }
2127  
2128 #else
2129  
2130 static I32 su_uplevel_old(pTHX_ CV *callback, I32 cxix, I32 args) {
2131 #define su_uplevel_old(CB, CX, A) su_uplevel_old(aTHX_ (CB), (CX), (A))
2132 su_uplevel_ud *sud;
2133 const PERL_CONTEXT *cx = cxstack + cxix;
2134 PERL_SI *si;
2135 PERL_SI *cur = PL_curstackinfo;
2136 SV **old_stack_sp;
2137 CV *target;
2138 CV *renamed;
2139 UNOP sub_op;
2140 I32 gimme;
2141 I32 old_mark, new_mark;
2142 I32 ret;
2143 dSP;
2144  
2145 ENTER;
2146  
2147 gimme = GIMME_V;
2148 /* Make PL_stack_sp point just before the CV. */
2149 PL_stack_sp -= args + 1;
2150 old_mark = AvFILLp(PL_curstack) = PL_stack_sp - PL_stack_base;
2151 SPAGAIN;
2152  
2153 sud = su_uplevel_storage_new(cxix);
2154  
2155 sud->cxix = cxix;
2156 sud->died = 1;
2157 sud->callback = NULL;
2158 sud->renamed = NULL;
2159 SAVEDESTRUCTOR_X(su_uplevel_restore_old, sud);
2160  
2161 si = sud->si;
2162  
2163 si->si_type = cur->si_type;
2164 si->si_next = NULL;
2165 si->si_prev = cur->si_prev;
2166 #ifdef DEBUGGING
2167 si->si_markoff = cx->blk_oldmarksp;
2168 #endif
2169  
2170 /* Allocate enough space for all the elements of the original stack up to the
2171 * target context, plus the forthcoming arguments. */
2172 new_mark = cx->blk_oldsp;
2173 av_extend(si->si_stack, new_mark + 1 + args + 1);
2174 Copy(AvARRAY(PL_curstack), AvARRAY(si->si_stack), new_mark + 1, SV *);
2175 AvFILLp(si->si_stack) = new_mark;
2176 SU_POISON(AvARRAY(si->si_stack) + new_mark + 1, args + 1, SV *);
2177  
2178 /* Specialized SWITCHSTACK() */
2179 PL_stack_base = AvARRAY(si->si_stack);
2180 old_stack_sp = PL_stack_sp;
2181 PL_stack_sp = PL_stack_base + AvFILLp(si->si_stack);
2182 PL_stack_max = PL_stack_base + AvMAX(si->si_stack);
2183 SPAGAIN;
2184  
2185 /* Copy the context stack up to the context just below the target. */
2186 si->si_cxix = (cxix < 0) ? -1 : (cxix - 1);
2187 if (si->si_cxmax < cxix) {
2188 /* The max size must be at least two so that GROW(max) = (max*3)/2 > max */
2189 si->si_cxmax = (cxix < 4) ? 4 : cxix;
2190 Renew(si->si_cxstack, si->si_cxmax + 1, PERL_CONTEXT);
2191 }
2192 Copy(cur->si_cxstack, si->si_cxstack, cxix, PERL_CONTEXT);
2193 SU_POISON(si->si_cxstack + cxix, si->si_cxmax + 1 - cxix, PERL_CONTEXT);
2194  
2195 target = cx->blk_sub.cv;
2196 sud->target = (CV *) SvREFCNT_inc(target);
2197 sud->target_depth = CvDEPTH(target);
2198  
2199 /* blk_oldcop is essentially needed for caller() and stack traces. It has no
2200 * run-time implication, since PL_curcop will be overwritten as soon as we
2201 * enter a sub (a sub starts by a nextstate/dbstate). Hence it's safe to just
2202 * make it point to the blk_oldcop for the target frame, so that caller()
2203 * reports the right file name, line number and lexical hints. */
2204 SU_UPLEVEL_SAVE(curcop, cx->blk_oldcop);
2205 /* Don't reset PL_markstack_ptr, or we would overwrite the mark stack below
2206 * this point. Don't reset PL_curpm either, we want the most recent matches. */
2207  
2208 SU_UPLEVEL_SAVE(curstackinfo, si);
2209 /* If those two are equal, we need to fool POPSTACK_TO() */
2210 if (PL_mainstack == PL_curstack)
2211 SU_UPLEVEL_SAVE(mainstack, si->si_stack);
2212 else
2213 sud->old_mainstack = NULL;
2214 PL_curstack = si->si_stack;
2215  
2216 renamed = su_cv_clone(callback, CvGV(target));
2217 sud->renamed = renamed;
2218  
2219 PUSHMARK(SP);
2220 /* Both SP and old_stack_sp point just before the CV. */
2221 Copy(old_stack_sp + 2, SP + 1, args, SV *);
2222 SP += args;
2223 PUSHs((SV *) renamed);
2224 PUTBACK;
2225  
2226 Zero(&sub_op, 1, UNOP);
2227 sub_op.op_type = OP_ENTERSUB;
2228 sub_op.op_next = NULL;
2229 sub_op.op_flags = OP_GIMME_REVERSE(gimme) | OPf_STACKED;
2230 if (PL_DBsub)
2231 sub_op.op_flags |= OPpENTERSUB_DB;
2232  
2233 SU_UPLEVEL_SAVE(op, (OP *) &sub_op);
2234  
2235 #if SU_UPLEVEL_HIJACKS_RUNOPS
2236 sud->old_runops = PL_runops;
2237 #endif
2238  
2239 sud->old_catch = CATCH_GET;
2240 CATCH_SET(TRUE);
2241  
2242 if ((PL_op = PL_ppaddr[OP_ENTERSUB](aTHX))) {
2243 PERL_CONTEXT *sub_cx = cxstack + cxstack_ix;
2244 AV *argarray = cx->blk_sub.argarray;
2245  
2246 /* If pp_entersub() returns a non-null OP, it means that the callback is not
2247 * an XSUB. */
2248  
2249 sud->callback = MUTABLE_CV(SvREFCNT_inc(callback));
2250 CvDEPTH(callback)++;
2251  
2252 if (CxHASARGS(cx) && argarray) {
2253 /* The call to pp_entersub() has saved the current @_ (in XS terms,
2254 * GvAV(PL_defgv)) in the savearray member, and has created a new argarray
2255 * with what we put on the stack. But we want to fake up the same arguments
2256 * as the ones in use at the context we uplevel to, so we replace the
2257 * argarray with an unreal copy of the original @_. */
2258 AV *av = newAV();
2259 AvREAL_off(av);
2260 AvREIFY_on(av);
2261 av_extend(av, AvMAX(argarray));
2262 AvFILLp(av) = AvFILLp(argarray);
2263 Copy(AvARRAY(argarray), AvARRAY(av), AvFILLp(av) + 1, SV *);
2264 sub_cx->blk_sub.argarray = av;
2265 } else {
2266 SvREFCNT_inc_simple_void(sub_cx->blk_sub.argarray);
2267 }
2268  
2269 if (su_uplevel_goto_static(CvROOT(renamed))) {
2270 #if SU_UPLEVEL_HIJACKS_RUNOPS
2271 if (PL_runops != PL_runops_std) {
2272 if (PL_runops == PL_runops_dbg) {
2273 if (PL_debug)
2274 croak("uplevel() can't execute code that calls goto when debugging flags are set");
2275 } else if (PL_runops != su_uplevel_goto_runops)
2276 croak("uplevel() can't execute code that calls goto with a custom runloop");
2277 }
2278  
2279 PL_runops = su_uplevel_goto_runops;
2280 #else /* SU_UPLEVEL_HIJACKS_RUNOPS */
2281 croak("uplevel() can't execute code that calls goto before perl 5.8");
2282 #endif /* !SU_UPLEVEL_HIJACKS_RUNOPS */
2283 }
2284  
2285 CALLRUNOPS(aTHX);
2286 }
2287  
2288 sud->died = 0;
2289  
2290 ret = PL_stack_sp - (PL_stack_base + new_mark);
2291 if (ret > 0) {
2292 AV *old_stack = sud->old_curstackinfo->si_stack;
2293  
2294 if (old_mark + ret > AvMAX(old_stack)) {
2295 /* Specialized EXTEND(old_sp, ret) */
2296 av_extend(old_stack, old_mark + ret + 1);
2297 old_stack_sp = AvARRAY(old_stack) + old_mark;
2298 }
2299  
2300 Copy(PL_stack_sp - ret + 1, old_stack_sp + 1, ret, SV *);
2301 PL_stack_sp += ret;
2302 AvFILLp(old_stack) += ret;
2303 }
2304  
2305 LEAVE;
2306  
2307 return ret;
2308 }
2309  
2310 #endif
2311  
2312 /* --- Unique context ID --------------------------------------------------- */
2313  
2314 839 static su_uid *su_uid_storage_fetch(pTHX_ UV depth) {
2315 #define su_uid_storage_fetch(D) su_uid_storage_fetch(aTHX_ (D))
2316 su_uid *map;
2317 STRLEN alloc;
2318 dXSH_CXT;
2319  
2320 839 map = XSH_CXT.uid_storage.map;
2321 839 alloc = XSH_CXT.uid_storage.alloc;
2322  
2323 839 ▶ if (depth >= alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= alloc) { 230 609 839
2324 STRLEN i;
2325  
2326 230 ▶ Renew(map, depth + 1, su_uid);
Branch: 1 branch
BranchTrueFalseTotal
Renew(map, depth + 1, su_uid); 0 230 230
2327 1087 ▶ for (i = alloc; i <= depth; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = alloc; i <= depth; ++i) { 857 230 1087
2328 857 map[i].seq = 0;
2329 857 map[i].flags = 0;
2330 }
2331  
2332 230 XSH_CXT.uid_storage.map = map;
2333 230 XSH_CXT.uid_storage.alloc = depth + 1;
2334 }
2335  
2336 839 ▶ if (depth >= XSH_CXT.uid_storage.used)
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= XSH_CXT.uid_storage.used) 419 420 839
2337 419 XSH_CXT.uid_storage.used = depth + 1;
2338  
2339 839 return map + depth;
2340 }
2341  
2342 858 static int su_uid_storage_check(pTHX_ UV depth, UV seq) {
2343 #define su_uid_storage_check(D, S) su_uid_storage_check(aTHX_ (D), (S))
2344 su_uid *uid;
2345 dXSH_CXT;
2346  
2347 858 ▶ if (depth >= XSH_CXT.uid_storage.used)
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= XSH_CXT.uid_storage.used) 454 404 858
2348 454 return 0;
2349  
2350 404 uid = XSH_CXT.uid_storage.map + depth;
2351  
2352 404 ▶ return (uid->seq == seq) && (uid->flags & SU_UID_ACTIVE);
Branch: 2 branches
BranchTrueFalseTotal
return (uid->seq == seq) && (uid->flags & SU_UID_ACTIVE); 388 16 404
return (uid->seq == seq) && (uid->flags & SU_UID_ACTIVE); 379 9 388
2353 }
2354  
2355 839 static SV *su_uid_get(pTHX_ I32 cxix) {
2356 #define su_uid_get(I) su_uid_get(aTHX_ (I))
2357 su_uid *uid;
2358 SV *uid_sv;
2359 UV depth;
2360  
2361 839 depth = su_uid_depth(cxix);
2362 839 uid = su_uid_storage_fetch(depth);
2363  
2364 839 ▶ if (!(uid->flags & SU_UID_ACTIVE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(uid->flags & SU_UID_ACTIVE)) { 823 16 839
2365 su_ud_uid *ud;
2366  
2367 823 uid->seq = su_uid_seq_next(depth);
2368 823 uid->flags |= SU_UID_ACTIVE;
2369  
2370 823 Newx(ud, 1, su_ud_uid);
2371 823 SU_UD_TYPE(ud) = SU_UD_TYPE_UID;
2372 823 ud->idx = depth;
2373 823 su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE);
2374 }
2375  
2376 839 uid_sv = sv_newmortal();
2377 839 sv_setpvf(uid_sv, "%"UVuf"-%"UVuf, depth, uid->seq);
2378  
2379 839 return uid_sv;
2380 }
2381  
2382 #ifdef grok_number
2383  
2384 #define su_grok_number(S, L, VP) grok_number((S), (L), (VP))
2385  
2386 #else /* grok_number */
2387  
2388 #define IS_NUMBER_IN_UV 0x1
2389  
2390 static int su_grok_number(pTHX_ const char *s, STRLEN len, UV *valuep) {
2391 #define su_grok_number(S, L, VP) su_grok_number(aTHX_ (S), (L), (VP))
2392 STRLEN i;
2393 SV *tmpsv;
2394  
2395 /* This crude check should be good enough for a fallback implementation.
2396 * Better be too strict than too lax. */
2397 for (i = 0; i < len; ++i) {
2398 if (!isDIGIT(s[i]))
2399 return 0;
2400 }
2401  
2402 tmpsv = sv_newmortal();
2403 sv_setpvn(tmpsv, s, len);
2404 *valuep = sv_2uv(tmpsv);
2405  
2406 return IS_NUMBER_IN_UV;
2407 }
2408  
2409 #endif /* !grok_number */
2410  
2411 863 static int su_uid_validate(pTHX_ SV *uid) {
2412 #define su_uid_validate(U) su_uid_validate(aTHX_ (U))
2413 const char *s;
2414 863 STRLEN len, p = 0;
2415 UV depth, seq;
2416 int type;
2417  
2418 863 s = SvPV_const(uid, len);
2419  
2420 2342 ▶ while (p < len && s[p] != '-')
Branch: 2 branches
BranchTrueFalseTotal
while (p < len && s[p] != '-') 2341 1 2342
while (p < len && s[p] != '-') 1479 862 2341
2421 1479 ++p;
2422 863 ▶ if (p >= len)
Branch: 1 branch
BranchTrueFalseTotal
if (p >= len) 1 862 863
2423 1 croak("UID contains only one part");
2424  
2425 862 type = su_grok_number(s, p, &depth);
2426 862 ▶ if (type != IS_NUMBER_IN_UV)
Branch: 1 branch
BranchTrueFalseTotal
if (type != IS_NUMBER_IN_UV) 2 860 862
2427 2 croak("First UID part is not an unsigned integer");
2428  
2429 860 ++p; /* Skip '-'. As we used to have p < len, len - (p + 1) >= 0. */
2430  
2431 860 type = su_grok_number(s + p, len - p, &seq);
2432 860 ▶ if (type != IS_NUMBER_IN_UV)
Branch: 1 branch
BranchTrueFalseTotal
if (type != IS_NUMBER_IN_UV) 2 858 860
2433 2 croak("Second UID part is not an unsigned integer");
2434  
2435 858 return su_uid_storage_check(depth, seq);
2436 }
2437  
2438 /* --- Context operations -------------------------------------------------- */
2439  
2440 /* Remove sequences of BLOCKs having DB for stash, followed by a SUB context
2441 * for the debugger callback. */
2442  
2443 275717 static I32 su_context_skip_db(pTHX_ I32 cxix) {
2444 #define su_context_skip_db(C) su_context_skip_db(aTHX_ (C))
2445 I32 i;
2446  
2447 275717 ▶ if (!PL_DBsub)
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_DBsub) 0 275717 275717
2448 0 return cxix;
2449  
2450 275717 ▶ for (i = cxix; i > 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxix; i > 0; --i) { 275682 35 275717
2451 275682 PERL_CONTEXT *cx = cxstack + i;
2452  
2453 275682 switch (CxTYPE(cx)) {
2454 #if XSH_HAS_PERL(5, 17, 1)
2455 88725 case CXt_LOOP_PLAIN:
2456 #endif
2457 case CXt_BLOCK:
2458 88725 ▶ 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)) 88725 0 88725
if (cx->blk_oldcop && CopSTASH(cx->blk_oldcop) == GvSTASH(PL_DBgv)) 0 88725 88725
2459 0 continue;
2460 88725 break;
2461 121711 case CXt_SUB:
2462 121711 ▶ if (cx->blk_sub.cv == GvCV(PL_DBsub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_sub.cv == GvCV(PL_DBsub)) { 0 121711 121711
2463 0 cxix = i - 1;
2464 0 continue;
2465 }
2466 121711 break;
2467 65246 default:
2468 65246 break;
2469 }
2470  
2471 275682 break;
2472 }
2473  
2474 275717 return cxix;
2475 }
2476  
2477 #if SU_HAS_NEW_CXT
2478  
2479 /* convert a physical context stack index into the logical equivalent:
2480 * one that ignores all the context frames hidden by uplevel().
2481 * Perl-level functions use logical args (e.g. UP takes an optional logical
2482 * value and returns a logical value), while we use and store *real*
2483 * values internally.
2484 */
2485  
2486 246685 static I32 su_context_real2logical(pTHX_ I32 cxix) {
2487 # define su_context_real2logical(C) su_context_real2logical(aTHX_ (C))
2488 PERL_CONTEXT *cx;
2489 246685 I32 i, gaps = 0;
2490  
2491 3356052 ▶ for (i = 0; i <= cxix; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= cxix; i++) { 3109367 246685 3356052
2492 3109367 cx = cxstack + i;
2493 3109367 ▶ if (cx->cx_type == (CXt_NULL | CXp_SU_UPLEVEL_NULLED))
Branch: 1 branch
BranchTrueFalseTotal
if (cx->cx_type == (CXt_NULL | CXp_SU_UPLEVEL_NULLED)) 22154 3087213 3109367
2494 22154 gaps++;
2495 }
2496  
2497 XSH_D(xsh_debug_log("su_context_real2logical: %d => %d\n", cxix, cxix - gaps));
2498  
2499 246685 return cxix - gaps;
2500 }
2501  
2502 /* convert a logical context stack index (one that ignores all the context
2503 * frames hidden by uplevel) into the physical equivalent
2504 */
2505  
2506 76452 static I32 su_context_logical2real(pTHX_ I32 cxix) {
2507 # define su_context_logical2real(C) su_context_logical2real(aTHX_ (C))
2508 PERL_CONTEXT *cx;
2509 76452 I32 i, seen = -1;
2510  
2511 1134327 ▶ for (i = 0; i <= cxstack_ix; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= cxstack_ix; i++) { 1134325 2 1134327
2512 1134325 PERL_CONTEXT *cx = cxstack + i;
2513 1134325 ▶ if (cx->cx_type != (CXt_NULL | CXp_SU_UPLEVEL_NULLED))
Branch: 1 branch
BranchTrueFalseTotal
if (cx->cx_type != (CXt_NULL | CXp_SU_UPLEVEL_NULLED)) 1133616 709 1134325
2514 1133616 seen++;
2515 1134325 ▶ if (seen >= cxix)
Branch: 1 branch
BranchTrueFalseTotal
if (seen >= cxix) 76450 1057875 1134325
2516 76450 break;
2517 }
2518  
2519 XSH_D(xsh_debug_log("su_context_logical2real: %d => %d\n", cxix, i));
2520  
2521 76452 ▶ if (i > cxstack_ix)
Branch: 1 branch
BranchTrueFalseTotal
if (i > cxstack_ix) 2 76450 76452
2522 2 i = cxstack_ix;
2523  
2524 76452 return i;
2525 }
2526  
2527 #else
2528 # define su_context_real2logical(C) (C)
2529 # define su_context_logical2real(C) (C)
2530 #endif
2531  
2532 275713 static I32 su_context_normalize_up(pTHX_ I32 cxix) {
2533 #define su_context_normalize_up(C) su_context_normalize_up(aTHX_ (C))
2534 PERL_CONTEXT *cx;
2535  
2536 275713 ▶ if (cxix <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cxix <= 0) 31 275682 275713
2537 31 return 0;
2538  
2539 275682 cx = cxstack + cxix;
2540 275682 ▶ if (CxTYPE(cx) == CXt_BLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (CxTYPE(cx) == CXt_BLOCK) { 81229 194453 275682
2541 81229 PERL_CONTEXT *prev = cx - 1;
2542  
2543 81229 switch (CxTYPE(prev)) {
2544 #if SU_HAS_GIVEN
2545 case CXt_GIVEN:
2546 case CXt_WHEN:
2547 #endif
2548 #if XSH_HAS_PERL(5, 11, 0)
2549 /* That's the only subcategory that can cause an extra BLOCK context */
2550 407 case CXt_LOOP_PLAIN:
2551 #else
2552 case CXt_LOOP:
2553 #endif
2554 407 ▶ if (cx->blk_oldcop == prev->blk_oldcop)
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_oldcop == prev->blk_oldcop) 2 405 407
2555 2 return cxix - 1;
2556 405 break;
2557 6 case CXt_SUBST:
2558 6 ▶ if (cx->blk_oldcop && OpSIBLING(cx->blk_oldcop)
Branch: 3 branches
BranchTrueFalseTotal
if (cx->blk_oldcop && OpSIBLING(cx->blk_oldcop) 6 0 6
if (cx->blk_oldcop && OpSIBLING(cx->blk_oldcop) 6 0 6
if (cx->blk_oldcop && OpSIBLING(cx->blk_oldcop) 6 0 6
2559 6 ▶ && OpSIBLING(cx->blk_oldcop)->op_type == OP_SUBST)
Branch: 2 branches
BranchTrueFalseTotal
&& OpSIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 6 0 6
&& OpSIBLING(cx->blk_oldcop)->op_type == OP_SUBST) 6 0 6
2560 6 return cxix - 1;
2561 0 break;
2562 }
2563 }
2564  
2565 275674 return cxix;
2566 }
2567  
2568 12497 static I32 su_context_normalize_down(pTHX_ I32 cxix) {
2569 #define su_context_normalize_down(C) su_context_normalize_down(aTHX_ (C))
2570 PERL_CONTEXT *next;
2571  
2572 12497 ▶ if (cxix >= cxstack_ix)
Branch: 1 branch
BranchTrueFalseTotal
if (cxix >= cxstack_ix) 725 11772 12497
2573 725 return cxstack_ix;
2574  
2575 11772 next = cxstack + cxix + 1;
2576 11772 ▶ if (CxTYPE(next) == CXt_BLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (CxTYPE(next) == CXt_BLOCK) { 1248 10524 11772
2577 1248 PERL_CONTEXT *cx = next - 1;
2578  
2579 1248 switch (CxTYPE(cx)) {
2580 #if SU_HAS_GIVEN
2581 case CXt_GIVEN:
2582 case CXt_WHEN:
2583 #endif
2584 #if XSH_HAS_PERL(5, 11, 0)
2585 /* That's the only subcategory that can cause an extra BLOCK context */
2586 192 case CXt_LOOP_PLAIN:
2587 #else
2588 case CXt_LOOP:
2589 #endif
2590 192 ▶ if (cx->blk_oldcop == next->blk_oldcop)
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_oldcop == next->blk_oldcop) 0 192 192
2591 0 return cxix + 1;
2592 192 break;
2593 0 case CXt_SUBST:
2594 0 ▶ if (next->blk_oldcop && OpSIBLING(next->blk_oldcop)
Branch: 3 branches
BranchTrueFalseTotal
if (next->blk_oldcop && OpSIBLING(next->blk_oldcop) 0 0 0
if (next->blk_oldcop && OpSIBLING(next->blk_oldcop) 0 0 0
if (next->blk_oldcop && OpSIBLING(next->blk_oldcop) 0 0 0
2595 0 ▶ && OpSIBLING(next->blk_oldcop)->op_type == OP_SUBST)
Branch: 2 branches
BranchTrueFalseTotal
&& OpSIBLING(next->blk_oldcop)->op_type == OP_SUBST) 0 0 0
&& OpSIBLING(next->blk_oldcop)->op_type == OP_SUBST) 0 0 0
2596 0 return cxix + 1;
2597 0 break;
2598 }
2599 }
2600  
2601 11772 return cxix;
2602 }
2603  
2604 #define su_context_here() su_context_normalize_up(su_context_skip_db(cxstack_ix))
2605  
2606 24 static I32 su_context_gimme(pTHX_ I32 cxix) {
2607 #define su_context_gimme(C) su_context_gimme(aTHX_ (C))
2608 I32 i;
2609  
2610 29 ▶ for (i = cxix; i >= 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxix; i >= 0; --i) { 29 0 29
2611 29 PERL_CONTEXT *cx = cxstack + i;
2612  
2613 29 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 6 23 29
2614 /* gimme is always G_ARRAY for loop contexts. */
2615 #if XSH_HAS_PERL(5, 11, 0)
2616 # if XSH_HAS_PERL(5, 23, 8)
2617 6 case CXt_LOOP_ARY:
2618 case CXt_LOOP_LIST:
2619 # else
2620 case CXt_LOOP_FOR:
2621 # endif
2622 case CXt_LOOP_PLAIN:
2623 case CXt_LOOP_LAZYSV:
2624 case CXt_LOOP_LAZYIV:
2625 #else
2626 case CXt_LOOP:
2627 #endif
2628 case CXt_SUBST: {
2629 6 const COP *cop = cx->blk_oldcop;
2630 6 ▶ if (cop && OpSIBLING(cop)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cop && OpSIBLING(cop)) { 6 0 6
if (cop && OpSIBLING(cop)) { 6 0 6
if (cop && OpSIBLING(cop)) { 6 0 6
2631 6 ▶ switch (OpSIBLING(cop)->op_flags & OPf_WANT) {
Branch: 1 branch
BranchTrueFalseTotal
switch (OpSIBLING(cop)->op_flags & OPf_WANT) { 6 0 6
2632 1 case OPf_WANT_VOID:
2633 1 return G_VOID;
2634 0 case OPf_WANT_SCALAR:
2635 0 return G_SCALAR;
2636 0 case OPf_WANT_LIST:
2637 0 return G_ARRAY;
2638 5 default:
2639 5 break;
2640 }
2641 }
2642 5 break;
2643 }
2644 23 default:
2645 23 return CxGIMME(cx);
2646 break;
2647 }
2648 }
2649  
2650 0 return G_VOID;
2651 }
2652  
2653 /* --- Module setup/teardown ----------------------------------------------- */
2654  
2655 49 static void xsh_user_global_setup(pTHX) {
2656 HV *stash;
2657  
2658 MUTEX_INIT(&su_uid_seq_counter_mutex);
2659  
2660 XSH_LOCK(&su_uid_seq_counter_mutex);
2661 49 su_uid_seq_counter.seqs = NULL;
2662 49 su_uid_seq_counter.size = 0;
2663 XSH_UNLOCK(&su_uid_seq_counter_mutex);
2664  
2665 49 stash = gv_stashpv(XSH_PACKAGE, 1);
2666 49 newCONSTSUB(stash, "TOP", newSViv(0));
2667 49 newCONSTSUB(stash, "SU_THREADSAFE", newSVuv(XSH_THREADSAFE));
2668  
2669 49 return;
2670 }
2671  
2672 49 static void xsh_user_local_setup(pTHX_ xsh_user_cxt_t *cxt) {
2673  
2674 /* NewOp() calls calloc() which just zeroes the memory with memset(). */
2675 49 Zero(&(cxt->unwind_storage.return_op), 1, LISTOP);
2676 49 cxt->unwind_storage.return_op.op_type = OP_RETURN;
2677 49 cxt->unwind_storage.return_op.op_ppaddr = PL_ppaddr[OP_RETURN];
2678  
2679 49 Zero(&(cxt->unwind_storage.proxy_op), 1, OP);
2680 49 cxt->unwind_storage.proxy_op.op_type = OP_STUB;
2681 49 cxt->unwind_storage.proxy_op.op_ppaddr = NULL;
2682  
2683 49 Zero(&(cxt->yield_storage.leave_op), 1, UNOP);
2684 49 cxt->yield_storage.leave_op.op_type = OP_STUB;
2685 49 cxt->yield_storage.leave_op.op_ppaddr = NULL;
2686  
2687 49 Zero(&(cxt->yield_storage.proxy_op), 1, OP);
2688 49 cxt->yield_storage.proxy_op.op_type = OP_STUB;
2689 49 cxt->yield_storage.proxy_op.op_ppaddr = NULL;
2690  
2691 49 cxt->uplevel_storage.top = NULL;
2692 49 cxt->uplevel_storage.root = NULL;
2693 49 cxt->uplevel_storage.count = 0;
2694  
2695 49 cxt->uid_storage.map = NULL;
2696 49 cxt->uid_storage.used = 0;
2697 49 cxt->uid_storage.alloc = 0;
2698  
2699 49 return;
2700 }
2701  
2702 49 static void xsh_user_local_teardown(pTHX_ xsh_user_cxt_t *cxt) {
2703 su_uplevel_ud *cur;
2704  
2705 49 Safefree(cxt->uid_storage.map);
2706  
2707 49 cur = cxt->uplevel_storage.root;
2708 49 ▶ if (cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur) { 11 38 49
2709 su_uplevel_ud *prev;
2710 do {
2711 18 prev = cur;
2712 18 cur = prev->next;
2713 18 su_uplevel_ud_delete(prev);
2714 18 ▶ } while (cur);
Branch: 1 branch
BranchTrueFalseTotal
} while (cur); 7 11 18
2715 }
2716  
2717 49 return;
2718 }
2719  
2720 49 static void xsh_user_global_teardown(pTHX) {
2721 XSH_LOCK(&su_uid_seq_counter_mutex);
2722 49 PerlMemShared_free(su_uid_seq_counter.seqs);
2723 49 su_uid_seq_counter.size = 0;
2724 XSH_UNLOCK(&su_uid_seq_counter_mutex);
2725  
2726 MUTEX_DESTROY(&su_uid_seq_counter_mutex);
2727  
2728 49 return;
2729 }
2730  
2731 /* --- XS ------------------------------------------------------------------ */
2732  
2733 /* D is real; B is logical. Returns real. */
2734  
2735 #define SU_GET_CONTEXT(A, B, D) \
2736 STMT_START { \
2737 if (items > A) { \
2738 SV *csv = ST(B); \
2739 if (!SvOK(csv)) \
2740 goto default_cx; \
2741 cxix = SvIV(csv); \
2742 if (cxix < 0) \
2743 cxix = 0; \
2744 else if (cxix > cxstack_ix) \
2745 goto default_cx; \
2746 cxix = su_context_logical2real(cxix); \
2747 } else { \
2748 default_cx: \
2749 cxix = (D); \
2750 } \
2751 } STMT_END
2752  
2753 #define SU_GET_LEVEL(A, B) \
2754 STMT_START { \
2755 level = 0; \
2756 if (items > 0) { \
2757 SV *lsv = ST(B); \
2758 if (SvOK(lsv)) { \
2759 level = SvIV(lsv); \
2760 if (level < 0) \
2761 level = 0; \
2762 } \
2763 } \
2764 } STMT_END
2765  
2766 #if XSH_HAS_PERL(5, 10, 0)
2767 # define SU_INFO_COUNT 11
2768 #else
2769 # define SU_INFO_COUNT 10
2770 #endif
2771  
2772 5233 XS(XS_Scope__Upper_unwind) {
2773 #ifdef dVAR
2774 5233 dVAR; dXSARGS;
2775 #else
2776 dXSARGS;
2777 #endif
2778 dXSH_CXT;
2779 I32 cxix;
2780  
2781 PERL_UNUSED_VAR(cv); /* -W */
2782 PERL_UNUSED_VAR(ax); /* -Wall */
2783  
2784 5233 ▶ SU_GET_CONTEXT(0, items - 1, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, items - 1, cxstack_ix); 5232 1 5233
SU_GET_CONTEXT(0, items - 1, cxstack_ix); 0 5232 5232
SU_GET_CONTEXT(0, items - 1, cxstack_ix); 1 5231 5232
SU_GET_CONTEXT(0, items - 1, cxstack_ix); 0 5231 5231
2785 do {
2786 5239 PERL_CONTEXT *cx = cxstack + cxix;
2787 5239 switch (CxTYPE(cx)) {
2788 3283 case CXt_SUB:
2789 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
2790 0 continue;
2791 case CXt_EVAL:
2792 case CXt_FORMAT:
2793 5231 XSH_CXT.unwind_storage.cxix = cxix;
2794 5231 XSH_CXT.unwind_storage.items = items;
2795 5231 XSH_CXT.unwind_storage.savesp = PL_stack_sp;
2796 5231 ▶ if (items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 0) { 5230 1 5231
2797 5230 XSH_CXT.unwind_storage.items--;
2798 5230 XSH_CXT.unwind_storage.savesp--;
2799 }
2800 /* pp_entersub will want to sanitize the stack after returning from there
2801 * Screw that, we're insane!
2802 * dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
2803 5231 ▶ if (GIMME_V == G_SCALAR)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) 1740 3491 5231
2804 1740 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
2805 5231 SAVEDESTRUCTOR_X(su_unwind, NULL);
2806 5231 return;
2807 8 default:
2808 8 break;
2809 }
2810 8 ▶ } while (--cxix >= 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (--cxix >= 0); 6 2 8
2811 2 croak("Can't return outside a subroutine");
2812 }
2813  
2814 static const char su_yield_name[] = "yield";
2815  
2816 41523 XS(XS_Scope__Upper_yield) {
2817 #ifdef dVAR
2818 41523 dVAR; dXSARGS;
2819 #else
2820 dXSARGS;
2821 #endif
2822 dXSH_CXT;
2823 I32 cxix;
2824  
2825 PERL_UNUSED_VAR(cv); /* -W */
2826 PERL_UNUSED_VAR(ax); /* -Wall */
2827  
2828 41523 ▶ SU_GET_CONTEXT(0, items - 1, su_context_here());
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, items - 1, su_context_here()); 41513 10 41523
SU_GET_CONTEXT(0, items - 1, su_context_here()); 0 41513 41513
SU_GET_CONTEXT(0, items - 1, su_context_here()); 0 41513 41513
SU_GET_CONTEXT(0, items - 1, su_context_here()); 0 41513 41513
2829 41523 XSH_CXT.yield_storage.cxix = cxix;
2830 41523 XSH_CXT.yield_storage.items = items;
2831 41523 XSH_CXT.yield_storage.savesp = PL_stack_sp;
2832 41523 ▶ if (items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 0) { 41513 10 41523
2833 41513 XSH_CXT.yield_storage.items--;
2834 41513 XSH_CXT.yield_storage.savesp--;
2835 }
2836 /* See XS_Scope__Upper_unwind */
2837 41523 ▶ if (GIMME_V == G_SCALAR)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) 13836 27687 41523
2838 13836 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
2839 41523 SAVEDESTRUCTOR_X(su_yield, su_yield_name);
2840 41523 return;
2841 }
2842  
2843 static const char su_leave_name[] = "leave";
2844  
2845 3 XS(XS_Scope__Upper_leave) {
2846 #ifdef dVAR
2847 3 dVAR; dXSARGS;
2848 #else
2849 dXSARGS;
2850 #endif
2851 dXSH_CXT;
2852  
2853 PERL_UNUSED_VAR(cv); /* -W */
2854 PERL_UNUSED_VAR(ax); /* -Wall */
2855  
2856 3 XSH_CXT.yield_storage.cxix = su_context_here();
2857 3 XSH_CXT.yield_storage.items = items;
2858 3 XSH_CXT.yield_storage.savesp = PL_stack_sp;
2859 /* See XS_Scope__Upper_unwind */
2860 3 ▶ if (GIMME_V == G_SCALAR)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) 0 3 3
2861 0 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
2862  
2863 3 SAVEDESTRUCTOR_X(su_yield, su_leave_name);
2864  
2865 3 return;
2866 }
2867  
2868 MODULE = Scope::Upper PACKAGE = Scope::Upper
2869  
2870 PROTOTYPES: ENABLE
2871  
2872 BOOT:
2873 {
2874 49 xsh_setup();
2875 49 newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
2876 49 newXSproto("Scope::Upper::yield", XS_Scope__Upper_yield, file, NULL);
2877 49 newXSproto("Scope::Upper::leave", XS_Scope__Upper_leave, file, NULL);
2878 }
2879  
2880 #if XSH_THREADSAFE
2881  
2882 void
2883 CLONE(...)
2884 PROTOTYPE: DISABLE
2885 PPCODE:
2886 xsh_clone();
2887 XSRETURN(0);
2888  
2889 #endif /* XSH_THREADSAFE */
2890  
2891 void
2892 HERE()
2893 PROTOTYPE:
2894 PREINIT:
2895 I32 cxix;
2896 PPCODE:
2897 165279 cxix = su_context_real2logical(su_context_here());
2898 165279 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 165279 165279
2899 165279 mPUSHi(cxix);
2900 165279 XSRETURN(1);
2901  
2902 void
2903 UP(...)
2904 PROTOTYPE: ;$
2905 PREINIT:
2906 I32 cxix;
2907 PPCODE:
2908 21827 ▶ SU_GET_CONTEXT(0, 0, su_context_here());
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, 0, su_context_here()); 9986 11841 21827
SU_GET_CONTEXT(0, 0, su_context_here()); 0 9986 9986
SU_GET_CONTEXT(0, 0, su_context_here()); 0 9986 9986
SU_GET_CONTEXT(0, 0, su_context_here()); 0 9986 9986
2909 21827 ▶ if (cxix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cxix > 0) { 21826 1 21827
2910 21826 --cxix;
2911 21826 cxix = su_context_skip_db(cxix);
2912 21826 cxix = su_context_normalize_up(cxix);
2913 21826 cxix = su_context_real2logical(cxix);
2914 } else {
2915 1 warn(su_stack_smash);
2916 }
2917 21827 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 21827 21827
2918 21827 mPUSHi(cxix);
2919 21827 XSRETURN(1);
2920  
2921 void
2922 SUB(...)
2923 PROTOTYPE: ;$
2924 PREINIT:
2925 I32 cxix;
2926 PPCODE:
2927 5917 ▶ SU_GET_CONTEXT(0, 0, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, 0, cxstack_ix); 4850 1067 5917
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 4850 4850
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 4850 4850
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 4850 4850
2928 5917 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 5917 5917
2929 21523 ▶ for (; cxix >= 0; --cxix) {
Branch: 1 branch
BranchTrueFalseTotal
for (; cxix >= 0; --cxix) { 21505 18 21523
2930 21505 PERL_CONTEXT *cx = cxstack + cxix;
2931 21505 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 15606 5899 21505
2932 15606 default:
2933 15606 continue;
2934 5899 case CXt_SUB:
2935 5899 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 5899 0 5899
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 5899 5899
2936 0 continue;
2937 5899 cxix = su_context_real2logical(cxix);
2938 5899 mPUSHi(cxix);
2939 5899 XSRETURN(1);
2940 }
2941 }
2942 18 warn(su_no_such_target, "subroutine");
2943 18 XSRETURN_UNDEF;
2944  
2945 void
2946 EVAL(...)
2947 PROTOTYPE: ;$
2948 PREINIT:
2949 I32 cxix;
2950 PPCODE:
2951 19 ▶ SU_GET_CONTEXT(0, 0, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 19 19
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 0 0
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 0 0
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 0 0
2952 19 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 19 19
2953 55 ▶ for (; cxix >= 0; --cxix) {
Branch: 1 branch
BranchTrueFalseTotal
for (; cxix >= 0; --cxix) { 38 17 55
2954 38 PERL_CONTEXT *cx = cxstack + cxix;
2955 38 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 36 2 38
2956 36 default:
2957 36 continue;
2958 2 case CXt_EVAL:
2959 2 cxix = su_context_real2logical(cxix);
2960 2 mPUSHi(cxix);
2961 2 XSRETURN(1);
2962 }
2963 }
2964 17 warn(su_no_such_target, "eval");
2965 17 XSRETURN_UNDEF;
2966  
2967 void
2968 SCOPE(...)
2969 PROTOTYPE: ;$
2970 PREINIT:
2971 I32 cxix, level;
2972 PPCODE:
2973 23376 ▶ SU_GET_LEVEL(0, 0);
Branch: 3 branches
BranchTrueFalseTotal
SU_GET_LEVEL(0, 0); 23372 4 23376
SU_GET_LEVEL(0, 0); 23372 0 23372
SU_GET_LEVEL(0, 0); 0 23372 23372
2974 23376 cxix = su_context_here();
2975 75911 ▶ while (--level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (--level >= 0) { 52536 23375 75911
2976 52536 ▶ if (cxix <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cxix <= 0) { 1 52535 52536
2977 1 warn(su_stack_smash);
2978 1 break;
2979 }
2980 52535 --cxix;
2981 52535 cxix = su_context_skip_db(cxix);
2982 52535 cxix = su_context_normalize_up(cxix);
2983 52535 cxix = su_context_real2logical(cxix);
2984 }
2985 23376 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 23376 23376
2986 23376 mPUSHi(cxix);
2987 23376 XSRETURN(1);
2988  
2989 void
2990 CALLER(...)
2991 PROTOTYPE: ;$
2992 PREINIT:
2993 I32 cxix, level;
2994 PPCODE:
2995 1144 ▶ SU_GET_LEVEL(0, 0);
Branch: 3 branches
BranchTrueFalseTotal
SU_GET_LEVEL(0, 0); 1140 4 1144
SU_GET_LEVEL(0, 0); 1140 0 1140
SU_GET_LEVEL(0, 0); 0 1140 1140
2996 13273 ▶ for (cxix = cxstack_ix; cxix > 0; --cxix) {
Branch: 1 branch
BranchTrueFalseTotal
for (cxix = cxstack_ix; cxix > 0; --cxix) { 13267 6 13273
2997 13267 PERL_CONTEXT *cx = cxstack + cxix;
2998 13267 switch (CxTYPE(cx)) {
2999 6447 case CXt_SUB:
3000 6447 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 6447 0 6447
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 6447 6447
3001 0 continue;
3002 case CXt_EVAL:
3003 case CXt_FORMAT:
3004 6460 ▶ if (--level < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--level < 0) 1138 5322 6460
3005 1138 goto done;
3006 5322 break;
3007 }
3008 }
3009 6 done:
3010 1144 ▶ if (level >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (level >= 0) 6 1138 1144
3011 6 warn(su_stack_smash);
3012 1144 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 1144 1144
3013 1144 cxix = su_context_real2logical(cxix);
3014 1144 mPUSHi(cxix);
3015 1144 XSRETURN(1);
3016  
3017 void
3018 want_at(...)
3019 PROTOTYPE: ;$
3020 PREINIT:
3021 I32 cxix;
3022 PPCODE:
3023 18 ▶ SU_GET_CONTEXT(0, 0, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, 0, cxstack_ix); 11 7 18
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 11 11
SU_GET_CONTEXT(0, 0, cxstack_ix); 1 10 11
SU_GET_CONTEXT(0, 0, cxstack_ix); 0 10 10
3024 18 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 18 18
3025 26 ▶ while (cxix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (cxix > 0) { 23 3 26
3026 23 PERL_CONTEXT *cx = cxstack + cxix--;
3027 23 switch (CxTYPE(cx)) {
3028 11 case CXt_SUB:
3029 11 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 11 0 11
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 11 11
3030 0 continue;
3031 case CXt_EVAL:
3032 case CXt_FORMAT: {
3033 15 I32 gimme = cx->blk_gimme;
3034 15 switch (gimme) {
3035 1 case G_VOID: XSRETURN_UNDEF; break;
3036 3 case G_SCALAR: XSRETURN_NO; break;
3037 11 case G_ARRAY: XSRETURN_YES; break;
3038 }
3039 0 break;
3040 }
3041 }
3042 }
3043 3 XSRETURN_UNDEF;
3044  
3045 void
3046 context_info(...)
3047 PROTOTYPE: ;$
3048 PREINIT:
3049 I32 cxix;
3050 const PERL_CONTEXT *cx, *dbcx;
3051 COP *cop;
3052 PPCODE:
3053 24 ▶ SU_GET_CONTEXT(0, 0, su_context_skip_db(cxstack_ix));
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, 0, su_context_skip_db(cxstack_ix)); 17 7 24
SU_GET_CONTEXT(0, 0, su_context_skip_db(cxstack_ix)); 0 17 17
SU_GET_CONTEXT(0, 0, su_context_skip_db(cxstack_ix)); 1 16 17
SU_GET_CONTEXT(0, 0, su_context_skip_db(cxstack_ix)); 0 16 16
3054 24 cxix = su_context_normalize_up(cxix);
3055 24 cx = cxstack + cxix;
3056 24 dbcx = cx;
3057 24 ▶ if (PL_DBsub && cxix && (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)) {
Branch: 4 branches
BranchTrueFalseTotal
if (PL_DBsub && cxix && (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)) { 24 0 24
if (PL_DBsub && cxix && (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)) { 21 3 24
if (PL_DBsub && cxix && (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)) { 12 9 21
if (PL_DBsub && cxix && (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)) { 0 12 12
3058 9 I32 i = su_context_skip_db(cxix - 1) + 1;
3059 9 ▶ if (i < cxix && CxTYPE(cxstack + i) == CXt_SUB)
Branch: 2 branches
BranchTrueFalseTotal
if (i < cxix && CxTYPE(cxstack + i) == CXt_SUB) 0 9 9
if (i < cxix && CxTYPE(cxstack + i) == CXt_SUB) 0 0 0
3060 0 cx = cxstack + i;
3061 }
3062 24 cop = cx->blk_oldcop;
3063 24 ▶ EXTEND(SP, SU_INFO_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, SU_INFO_COUNT); 0 24 24
3064 /* stash (0) */
3065 {
3066 24 HV *stash = CopSTASH(cop);
3067 24 ▶ if (stash)
Branch: 1 branch
BranchTrueFalseTotal
if (stash) 24 0 24
3068 24 ▶ PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash)));
Branch: 12 branches
BranchTrueFalseTotal
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 24 0 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 24 0 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 0 24 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 0 0 0
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 24 0 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 0 24 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 24 0 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 24 0 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 0 24 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 0 0 0
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 24 0 24
PUSHs(su_newmortal_pvn(HvNAME(stash), HvNAMELEN(stash))); 0 24 24
3069 else
3070 0 PUSHs(&PL_sv_undef);
3071 }
3072 /* file (1) */
3073 24 ▶ PUSHs(su_newmortal_pvn(OutCopFILE(cop), OutCopFILE_len(cop)));
Branch: 2 branches
BranchTrueFalseTotal
PUSHs(su_newmortal_pvn(OutCopFILE(cop), OutCopFILE_len(cop))); 24 0 24
PUSHs(su_newmortal_pvn(OutCopFILE(cop), OutCopFILE_len(cop))); 24 0 24
3074 /* line (2) */
3075 24 mPUSHi(CopLINE(cop));
3076 /* subroutine (3) and has_args (4) */
3077 24 switch (CxTYPE(cx)) {
3078 9 case CXt_SUB:
3079 case CXt_FORMAT: {
3080 9 GV *cvgv = CvGV(dbcx->blk_sub.cv);
3081 9 ▶ if (cvgv && isGV(cvgv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cvgv && isGV(cvgv)) { 9 0 9
if (cvgv && isGV(cvgv)) { 9 0 9
3082 9 SV *sv = sv_newmortal();
3083 9 gv_efullname3(sv, cvgv, NULL);
3084 9 PUSHs(sv);
3085 } else {
3086 0 PUSHs(su_newmortal_pvs("(unknown)"));
3087 }
3088 9 ▶ if (CxHASARGS(cx))
Branch: 1 branch
BranchTrueFalseTotal
if (CxHASARGS(cx)) 9 0 9
3089 9 PUSHs(&PL_sv_yes);
3090 else
3091 0 PUSHs(&PL_sv_no);
3092 9 break;
3093 }
3094 5 case CXt_EVAL:
3095 5 PUSHs(su_newmortal_pvs("(eval)"));
3096 5 mPUSHi(0);
3097 5 break;
3098 10 default:
3099 10 PUSHs(&PL_sv_undef);
3100 10 PUSHs(&PL_sv_undef);
3101 }
3102 /* gimme (5) */
3103 24 switch (su_context_gimme(cxix)) {
3104 11 case G_ARRAY:
3105 11 PUSHs(&PL_sv_yes);
3106 11 break;
3107 3 case G_SCALAR:
3108 3 PUSHs(&PL_sv_no);
3109 3 break;
3110 10 default: /* G_VOID */
3111 10 PUSHs(&PL_sv_undef);
3112 10 break;
3113 }
3114 /* eval text (6) and is_require (7) */
3115 24 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 5 19 24
3116 5 case CXt_EVAL:
3117 5 ▶ if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) { 2 3 5
3118 /* eval STRING */
3119 #if XSH_HAS_PERL(5, 17, 4)
3120 2 PUSHs(newSVpvn_flags(SvPVX(cx->blk_eval.cur_text),
3121 SvCUR(cx->blk_eval.cur_text)-2,
3122 SvUTF8(cx->blk_eval.cur_text)|SVs_TEMP));
3123 #else
3124 PUSHs(cx->blk_eval.cur_text);
3125 #endif
3126 2 PUSHs(&PL_sv_no);
3127 2 break;
3128 3 ▶ } else if (cx->blk_eval.old_namesv) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cx->blk_eval.old_namesv) { 0 3 3
3129 /* require */
3130 0 PUSHs(sv_mortalcopy(cx->blk_eval.old_namesv));
3131 0 PUSHs(&PL_sv_yes);
3132 0 break;
3133 }
3134 /* FALLTHROUGH */
3135 default:
3136 /* Anything else including eval BLOCK */
3137 22 PUSHs(&PL_sv_undef);
3138 22 PUSHs(&PL_sv_undef);
3139 22 break;
3140 }
3141 /* hints (8) */
3142 24 mPUSHi(CopHINTS_get(cop));
3143 /* warnings (9) */
3144 {
3145 24 SV *mask = NULL;
3146 #if XSH_HAS_PERL(5, 37, 6)
3147 24 char *old_warnings = cop->cop_warnings;
3148 #elif XSH_HAS_PERL(5, 9, 4)
3149 STRLEN *old_warnings = cop->cop_warnings;
3150 #else
3151 SV *old_warnings = cop->cop_warnings;
3152 #endif
3153 24 ▶ if (old_warnings == pWARN_STD) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_warnings == pWARN_STD) { 3 21 24
3154 3 ▶ if (PL_dowarn & G_WARN_ON)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dowarn & G_WARN_ON) 0 3 3
3155 0 goto context_info_warnings_on;
3156 else
3157 #if XSH_HAS_PERL(5, 17, 4)
3158 3 mask = &PL_sv_undef;
3159 #else
3160 goto context_info_warnings_off;
3161 #endif
3162 21 ▶ } else if (old_warnings == pWARN_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (old_warnings == pWARN_NONE) { 0 21 21
3163 #if !XSH_HAS_PERL(5, 17, 4)
3164 context_info_warnings_off:
3165 #endif
3166 0 mask = su_newmortal_pvn(WARN_NONEstring, WARNsize);
3167 21 ▶ } else if (old_warnings == pWARN_ALL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (old_warnings == pWARN_ALL) { 21 0 21
3168 HV *bits;
3169 21 context_info_warnings_on:
3170 #if XSH_HAS_PERL(5, 8, 7)
3171 21 bits = get_hv("warnings::Bits", 0);
3172 21 ▶ if (bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits) { 21 0 21
3173 21 SV **bits_all = hv_fetchs(bits, "all", FALSE);
3174 21 ▶ if (bits_all)
Branch: 1 branch
BranchTrueFalseTotal
if (bits_all) 21 0 21
3175 21 mask = sv_mortalcopy(*bits_all);
3176 }
3177 #endif
3178 21 ▶ if (!mask)
Branch: 1 branch
BranchTrueFalseTotal
if (!mask) 0 21 21
3179 0 mask = su_newmortal_pvn(WARN_ALLstring, WARNsize);
3180 } else {
3181 #if XSH_HAS_PERL(5, 37, 6)
3182 0 mask = su_newmortal_pvn((char *) old_warnings, RCPV_LEN(old_warnings));
3183 #elif XSH_HAS_PERL(5, 9, 4)
3184 mask = su_newmortal_pvn((char *) (old_warnings + 1), old_warnings[0]);
3185 #else
3186 mask = sv_mortalcopy(old_warnings);
3187 #endif
3188 }
3189 24 PUSHs(mask);
3190 }
3191 #if XSH_HAS_PERL(5, 10, 0)
3192 /* hints hash (10) */
3193 {
3194 24 COPHH *hints_hash = CopHINTHASH_get(cop);
3195 24 ▶ if (hints_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (hints_hash) { 0 24 24
3196 0 SV *rhv = sv_2mortal(newRV_noinc((SV *) cophh_2hv(hints_hash, 0)));
3197 0 PUSHs(rhv);
3198 } else {
3199 24 PUSHs(&PL_sv_undef);
3200 }
3201 }
3202 #endif
3203 24 XSRETURN(SU_INFO_COUNT);
3204  
3205 void
3206 reap(SV *hook, ...)
3207 PROTOTYPE: &;$
3208 PREINIT:
3209 I32 cxix;
3210 su_ud_reap *ud;
3211 CODE:
3212 3360 ▶ SU_GET_CONTEXT(1, 1, su_context_skip_db(cxstack_ix));
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(1, 1, su_context_skip_db(cxstack_ix)); 3357 3 3360
SU_GET_CONTEXT(1, 1, su_context_skip_db(cxstack_ix)); 0 3357 3357
SU_GET_CONTEXT(1, 1, su_context_skip_db(cxstack_ix)); 0 3357 3357
SU_GET_CONTEXT(1, 1, su_context_skip_db(cxstack_ix)); 0 3357 3357
3213 3360 cxix = su_context_normalize_down(cxix);
3214 3360 Newx(ud, 1, su_ud_reap);
3215 3360 SU_UD_TYPE(ud) = SU_UD_TYPE_REAP;
3216 3360 ▶ ud->cb = (SvROK(hook) && SvTYPE(SvRV(hook)) >= SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
ud->cb = (SvROK(hook) && SvTYPE(SvRV(hook)) >= SVt_PVCV) 3360 0 3360
3217 6720 ▶ ? SvRV(hook) : hook;
Branch: 1 branch
BranchTrueFalseTotal
? SvRV(hook) : hook; 3360 0 3360
3218 3360 ▶ SvREFCNT_inc_simple_void(ud->cb);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(ud->cb); 3360 0 3360
3219 3360 su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE);
3220  
3221 void
3222 localize(SV *sv, SV *val, ...)
3223 PROTOTYPE: $$;$
3224 PREINIT:
3225 I32 cxix;
3226 I32 size;
3227 su_ud_localize *ud;
3228 CODE:
3229 3007 ▶ SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix));
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 3007 0 3007
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 0 3007 3007
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 0 3007 3007
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 0 3007 3007
3230 3007 cxix = su_context_normalize_down(cxix);
3231 3007 Newx(ud, 1, su_ud_localize);
3232 3007 SU_UD_TYPE(ud) = SU_UD_TYPE_LOCALIZE;
3233 3007 size = su_ud_localize_init(ud, sv, val, NULL);
3234 3003 su_init(ud, cxix, size);
3235  
3236 void
3237 localize_elem(SV *sv, SV *elem, SV *val, ...)
3238 PROTOTYPE: $$$;$
3239 PREINIT:
3240 I32 cxix;
3241 I32 size;
3242 su_ud_localize *ud;
3243 CODE:
3244 4938 ▶ if (SvTYPE(sv) >= SVt_PVGV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) >= SVt_PVGV) 5 4933 4938
3245 5 croak("Can't infer the element localization type from a glob and the value");
3246 4933 ▶ SU_GET_CONTEXT(3, 3, su_context_skip_db(cxstack_ix));
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(3, 3, su_context_skip_db(cxstack_ix)); 4928 5 4933
SU_GET_CONTEXT(3, 3, su_context_skip_db(cxstack_ix)); 0 4928 4928
SU_GET_CONTEXT(3, 3, su_context_skip_db(cxstack_ix)); 0 4928 4928
SU_GET_CONTEXT(3, 3, su_context_skip_db(cxstack_ix)); 0 4928 4928
3247 4933 cxix = su_context_normalize_down(cxix);
3248 4933 Newx(ud, 1, su_ud_localize);
3249 /* Initialize SU_UD_ORIGIN(ud) in case SU_UD_LOCALIZE_FREE(ud) needs it */
3250 4933 SU_UD_ORIGIN(ud) = NULL;
3251 4933 SU_UD_TYPE(ud) = SU_UD_TYPE_LOCALIZE;
3252 4933 size = su_ud_localize_init(ud, sv, val, elem);
3253 4929 ▶ if (SU_UD_PRIVATE(ud) != SVt_PVAV && SU_UD_PRIVATE(ud) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SU_UD_PRIVATE(ud) != SVt_PVAV && SU_UD_PRIVATE(ud) != SVt_PVHV) { 1965 2964 4929
if (SU_UD_PRIVATE(ud) != SVt_PVAV && SU_UD_PRIVATE(ud) != SVt_PVHV) { 3 1962 1965
3254 3 ▶ SU_UD_LOCALIZE_FREE(ud);
Branch: 1 branch
BranchTrueFalseTotal
SU_UD_LOCALIZE_FREE(ud); 0 3 3
3255 3 croak("Can't localize an element of something that isn't an array or a hash");
3256 }
3257 4926 su_init(ud, cxix, size);
3258  
3259 void
3260 localize_delete(SV *sv, SV *elem, ...)
3261 PROTOTYPE: $$;$
3262 PREINIT:
3263 I32 cxix;
3264 I32 size;
3265 su_ud_localize *ud;
3266 CODE:
3267 1197 ▶ SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix));
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 1193 4 1197
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 0 1193 1193
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 0 1193 1193
SU_GET_CONTEXT(2, 2, su_context_skip_db(cxstack_ix)); 0 1193 1193
3268 1197 cxix = su_context_normalize_down(cxix);
3269 1197 Newx(ud, 1, su_ud_localize);
3270 1197 SU_UD_TYPE(ud) = SU_UD_TYPE_LOCALIZE;
3271 1197 size = su_ud_localize_init(ud, sv, NULL, elem);
3272 1193 su_init(ud, cxix, size);
3273  
3274 void
3275 uplevel(SV *code, ...)
3276 PROTOTYPE: &@
3277 PREINIT:
3278 2754 ▶ I32 cxix, ret, args = 0;
Branch: 1 branch
BranchTrueFalseTotal
I32 cxix, ret, args = 0; 2753 1 2754
3279 PPCODE:
3280 2754 ▶ if (SvROK(code))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(code)) 2753 1 2754
3281 2753 code = SvRV(code);
3282 2754 ▶ if (SvTYPE(code) < SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(code) < SVt_PVCV) 2 2752 2754
3283 2 croak("First argument to uplevel must be a code reference");
3284 2752 ▶ SU_GET_CONTEXT(1, items - 1, cxstack_ix);
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(1, items - 1, cxstack_ix); 2338 414 2752
SU_GET_CONTEXT(1, items - 1, cxstack_ix); 0 2338 2338
SU_GET_CONTEXT(1, items - 1, cxstack_ix); 0 2338 2338
SU_GET_CONTEXT(1, items - 1, cxstack_ix); 0 2338 2338
3285 do {
3286 3527 PERL_CONTEXT *cx = cxstack + cxix;
3287 3527 switch (CxTYPE(cx)) {
3288 2 case CXt_EVAL:
3289 2 croak("Can't uplevel to an eval frame");
3290 0 case CXt_FORMAT:
3291 0 croak("Can't uplevel to a format frame");
3292 2749 case CXt_SUB:
3293 2749 ▶ if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 2749 0 2749
if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub)) 0 2749 2749
3294 0 continue;
3295 2749 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 2336 413 2749
3296 2336 PL_stack_sp--;
3297 2336 args = items - 2;
3298 }
3299 /* su_uplevel() takes care of extending the stack if needed. */
3300 #if SU_HAS_NEW_CXT
3301 2749 ret = su_uplevel_new((CV *) code, cxix, args);
3302 #else
3303 ret = su_uplevel_old((CV *) code, cxix, args);
3304 #endif
3305 1736 XSRETURN(ret);
3306 776 default:
3307 776 break;
3308 }
3309 776 ▶ } while (--cxix >= 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (--cxix >= 0); 775 1 776
3310 1 croak("Can't uplevel outside a subroutine");
3311  
3312 void
3313 uid(...)
3314 PROTOTYPE: ;$
3315 PREINIT:
3316 I32 cxix;
3317 SV *uid;
3318 PPCODE:
3319 839 ▶ SU_GET_CONTEXT(0, 0, su_context_here());
Branch: 4 branches
BranchTrueFalseTotal
SU_GET_CONTEXT(0, 0, su_context_here()); 20 819 839
SU_GET_CONTEXT(0, 0, su_context_here()); 0 20 20
SU_GET_CONTEXT(0, 0, su_context_here()); 0 20 20
SU_GET_CONTEXT(0, 0, su_context_here()); 0 20 20
3320 839 uid = su_uid_get(cxix);
3321 839 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 839 839
3322 839 PUSHs(uid);
3323 839 XSRETURN(1);
3324  
3325 void
3326 validate_uid(SV *uid)
3327 PROTOTYPE: $
3328 PREINIT:
3329 SV *ret;
3330 PPCODE:
3331 863 ▶ ret = su_uid_validate(uid) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ret = su_uid_validate(uid) ? &PL_sv_yes : &PL_sv_no; 379 479 858
3332 858 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 858 858
3333 858 PUSHs(ret);
3334 858 XSRETURN(1);
« xsh/mem.h ↑ Summary xsh/threads.h »