1 /* This file is part of the Scope::Upper Perl module.
2 * See http://search.cpan.org/dist/Scope-Upper/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "Scope::Upper"
15 /* --- Compatibility ------------------------------------------------------- */
17 #ifndef PERL_UNUSED_VAR
18 # define PERL_UNUSED_VAR(V)
22 # define STMT_START do
26 # define STMT_END while (0)
30 # define SU_D(X) STMT_START X STMT_END
36 # define Newx(v, n, c) New(0, v, n, c)
40 # define SvPV_const(S, L) SvPV(S, L)
43 #ifndef SvPV_nolen_const
44 # define SvPV_nolen_const(S) SvPV_nolen(S)
47 #ifndef SvREFCNT_inc_simple_void
48 # define SvREFCNT_inc_simple_void(sv) SvREFCNT_inc(sv)
52 # define HvNAME_get(H) HvNAME(H)
55 #ifndef gv_fetchpvn_flags
56 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
59 #ifndef PERL_MAGIC_tied
60 # define PERL_MAGIC_tied 'P'
63 #ifndef PERL_MAGIC_env
64 # define PERL_MAGIC_env 'E'
67 #ifndef NEGATIVE_INDICES_VAR
68 # define NEGATIVE_INDICES_VAR "NEGATIVE_INDICES"
71 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
73 /* --- Threads and multiplicity -------------------------------------------- */
83 #ifndef SU_MULTIPLICITY
84 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
85 # define SU_MULTIPLICITY 1
87 # define SU_MULTIPLICITY 0
90 #if SU_MULTIPLICITY && !defined(tTHX)
91 # define tTHX PerlInterpreter*
94 #if SU_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
95 # define SU_THREADSAFE 1
97 # define MY_CXT_CLONE \
99 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
100 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
101 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
104 # define SU_THREADSAFE 0
106 # define dMY_CXT dNOOP
108 # define MY_CXT su_globaldata
110 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
112 # define MY_CXT_INIT NOOP
114 # define MY_CXT_CLONE NOOP
117 /* --- Global data --------------------------------------------------------- */
119 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
122 char *stack_placeholder;
131 /* --- Stack manipulations ------------------------------------------------- */
133 #define SU_SAVE_PLACEHOLDER() save_pptr(&MY_CXT.stack_placeholder)
135 #define SU_SAVE_DESTRUCTOR_SIZE 3
136 #define SU_SAVE_PLACEHOLDER_SIZE 3
138 #define SU_SAVE_SCALAR_SIZE 3
140 #define SU_SAVE_ARY_SIZE 3
141 #define SU_SAVE_AELEM_SIZE 4
143 # define SU_SAVE_ADELETE_SIZE 3
145 # define SU_SAVE_ADELETE_SIZE SU_SAVE_DESTRUCTOR_SIZE
147 #if SU_SAVE_AELEM_SIZE < SU_SAVE_ADELETE_SIZE
148 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_ADELETE_SIZE
150 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_AELEM_SIZE
153 #define SU_SAVE_HASH_SIZE 3
154 #define SU_SAVE_HELEM_SIZE 4
155 #define SU_SAVE_HDELETE_SIZE 4
156 #if SU_SAVE_HELEM_SIZE < SU_SAVE_HDELETE_SIZE
157 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HDELETE_SIZE
159 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HELEM_SIZE
162 #define SU_SAVE_SPTR_SIZE 3
164 #if !SU_HAS_PERL(5, 8, 9)
165 # define SU_SAVE_GP_SIZE 6
166 #elif !SU_HAS_PERL(5, 13, 0)
167 # define SU_SAVE_GP_SIZE 3
169 # define SU_SAVE_GP_SIZE 4
172 #ifndef SvCANEXISTDELETE
173 # define SvCANEXISTDELETE(sv) \
175 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
176 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
177 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
178 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
183 /* ... Saving array elements ............................................... */
185 STATIC I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
186 #define su_av_key2idx(A, K) su_av_key2idx(aTHX_ (A), (K))
192 /* Added by MJD in perl-5.8.1 with 6f12eb6d2a1dfaf441504d869b27d2e40ef4966a */
193 #if SU_HAS_PERL(5, 8, 1)
194 if (SvRMAGICAL(av)) {
195 const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
197 SV * const * const negative_indices_glob =
198 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
199 NEGATIVE_INDICES_VAR, 16, 0);
200 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
206 idx = key + av_len(av) + 1;
220 STATIC void su_adelete(pTHX_ void *ud_) {
221 su_ud_adelete *ud = (su_ud_adelete *) ud_;
223 av_delete(ud->av, ud->idx, G_DISCARD);
224 SvREFCNT_dec(ud->av);
229 STATIC void su_save_adelete(pTHX_ AV *av, I32 idx) {
230 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
233 Newx(ud, 1, su_ud_adelete);
236 SvREFCNT_inc_simple_void(av);
238 SAVEDESTRUCTOR_X(su_adelete, ud);
241 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
243 #endif /* SAVEADELETE */
245 STATIC void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
246 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
253 idx = su_av_key2idx(av, SvIV(key));
255 if (SvCANEXISTDELETE(av))
256 preeminent = av_exists(av, idx);
258 svp = av_fetch(av, idx, 1);
259 if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
262 save_aelem(av, idx, svp);
264 SAVEADELETE(av, idx);
266 if (val) { /* local $x[$idx] = $val; */
267 SvSetMagicSV(*svp, val);
268 } else { /* local $x[$idx]; delete $x[$idx]; */
269 av_delete(av, idx, G_DISCARD);
273 /* ... Saving hash elements ................................................ */
275 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
276 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
283 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
284 preeminent = hv_exists_ent(hv, keysv, 0);
286 he = hv_fetch_ent(hv, keysv, 1, 0);
287 svp = he ? &HeVAL(he) : NULL;
288 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
290 if (HvNAME_get(hv) && isGV(*svp)) {
291 save_gp((GV *) *svp, 0);
296 save_helem(hv, keysv, svp);
299 const char * const key = SvPV_const(keysv, keylen);
300 SAVEDELETE(hv, savepvn(key, keylen),
301 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
304 if (val) { /* local $x{$keysv} = $val; */
305 SvSetMagicSV(*svp, val);
306 } else { /* local $x{$keysv}; delete $x{$keysv}; */
307 (void)hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
311 /* --- Actions ------------------------------------------------------------- */
317 void (*handler)(pTHX_ void *);
320 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
321 #define SU_UD_PAD(U) (((su_ud_common *) (U))->pad)
322 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
323 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
325 #define SU_UD_FREE(U) STMT_START { \
326 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
330 /* ... Reap ................................................................ */
337 STATIC void su_call(pTHX_ void *ud_) {
338 su_ud_reap *ud = (su_ud_reap *) ud_;
339 #if SU_HAS_PERL(5, 9, 5)
340 PERL_CONTEXT saved_cx;
347 PerlIO_printf(Perl_debug_log,
348 "%p: @@@ call\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
349 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
358 /* If the recently popped context isn't saved there, it will be overwritten by
359 * the sub scope from call_sv, although it's still needed in our caller. */
361 #if SU_HAS_PERL(5, 9, 5)
362 if (cxstack_ix < cxstack_max)
363 cxix = cxstack_ix + 1;
365 cxix = Perl_cxinc(aTHX);
366 saved_cx = cxstack[cxix];
369 call_sv(ud->cb, G_VOID);
371 #if SU_HAS_PERL(5, 9, 5)
372 cxstack[cxix] = saved_cx;
380 SvREFCNT_dec(ud->cb);
384 STATIC void su_reap(pTHX_ void *ud) {
385 #define su_reap(U) su_reap(aTHX_ (U))
387 PerlIO_printf(Perl_debug_log,
388 "%p: === reap\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
389 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
392 SAVEDESTRUCTOR_X(su_call, ud);
395 /* ... Localize & localize array/hash element .............................. */
405 #define SU_UD_LOCALIZE_FREE(U) STMT_START { \
406 SvREFCNT_dec((U)->elem); \
407 SvREFCNT_dec((U)->val); \
408 SvREFCNT_dec((U)->sv); \
412 STATIC I32 su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) {
413 #define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E))
418 SvREFCNT_inc_simple_void(sv);
420 if (SvTYPE(sv) >= SVt_PVGV) {
421 if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */
423 } else { /* local *x = \$val; */
424 t = SvTYPE(SvRV(val));
427 } else if (SvROK(sv)) {
428 croak("Invalid %s reference as the localization target",
429 sv_reftype(SvRV(sv), 0));
432 const char *p = SvPV_const(sv, len), *s;
433 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
439 case '$': t = SVt_PV; break;
440 case '@': t = SVt_PVAV; break;
441 case '%': t = SVt_PVHV; break;
442 case '&': t = SVt_PVCV; break;
443 case '*': t = SVt_PVGV; break;
448 } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
449 if (SvROK(val) && !sv_isobject(val)) {
450 t = SvTYPE(SvRV(val));
462 size = elem ? SU_SAVE_AELEM_OR_ADELETE_SIZE
467 size = elem ? SU_SAVE_HELEM_OR_HDELETE_SIZE
472 size = SU_SAVE_GP_SIZE;
476 size = SU_SAVE_SPTR_SIZE;
480 size = SU_SAVE_SCALAR_SIZE;
483 /* When deref is set, val isn't NULL */
486 ud->val = val ? newSVsv(deref ? SvRV(val) : val) : NULL;
487 ud->elem = SvREFCNT_inc(elem);
493 STATIC void su_localize(pTHX_ void *ud_) {
494 #define su_localize(U) su_localize(aTHX_ (U))
495 su_ud_localize *ud = (su_ud_localize *) ud_;
502 if (SvTYPE(sv) >= SVt_PVGV) {
506 gv = gv_fetchsv(sv, GV_ADDMULTI, t);
509 const char *name = SvPV_const(sv, len);
510 gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t);
517 PerlIO_printf(Perl_debug_log, "%p: === localize a %s\n",ud, sv_reftype(z, 0));
518 PerlIO_printf(Perl_debug_log,
519 "%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
520 ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
524 /* Inspired from Alias.pm */
528 su_save_aelem(GvAV(gv), elem, val);
535 su_save_helem(GvHV(gv), elem, val);
541 save_gp(gv, 1); /* hide previous entry in symtab */
548 gv = (GV *) save_scalar(gv);
553 SvSetMagicSV((SV *) gv, val);
556 SU_UD_LOCALIZE_FREE(ud);
559 /* --- Pop a context back -------------------------------------------------- */
563 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
565 # define SU_CXNAME "XXX"
569 STATIC void su_pop(pTHX_ void *ud) {
570 #define su_pop(U) su_pop(aTHX_ (U))
571 I32 depth, base, mark, *origin;
572 depth = SU_UD_DEPTH(ud);
575 PerlIO_printf(Perl_debug_log,
577 "%p: leave scope at depth=%2d scope_ix=%2d cur_top=%2d cur_base=%2d\n",
579 ud, depth, PL_scopestack_ix,PL_savestack_ix,PL_scopestack[PL_scopestack_ix])
582 origin = SU_UD_ORIGIN(ud);
583 mark = origin[depth];
584 base = origin[depth - 1];
586 SU_D(PerlIO_printf(Perl_debug_log,
587 "%p: original scope was %*c top=%2d base=%2d\n",
588 ud, 24, ' ', mark, base));
591 SU_D(PerlIO_printf(Perl_debug_log, "%p: clear leftovers\n", ud));
592 PL_savestack_ix = mark;
595 PL_savestack_ix = base;
597 SU_UD_DEPTH(ud) = --depth;
602 if ((pad = SU_UD_PAD(ud))) {
605 SU_D(PerlIO_printf(Perl_debug_log,
606 "%p: push a pad slot at depth=%2d scope_ix=%2d save_ix=%2d\n",
607 ud, depth, PL_scopestack_ix, PL_savestack_ix));
608 SU_SAVE_PLACEHOLDER();
612 SU_D(PerlIO_printf(Perl_debug_log,
613 "%p: push destructor at depth=%2d scope_ix=%2d save_ix=%2d\n",
614 ud, depth, PL_scopestack_ix, PL_savestack_ix));
615 SAVEDESTRUCTOR_X(su_pop, ud);
617 SU_UD_HANDLER(ud)(aTHX_ ud);
620 SU_D(PerlIO_printf(Perl_debug_log,
621 "%p: --- end pop: cur_top=%2d == cur_base=%2d\n",
622 ud, PL_savestack_ix, PL_scopestack[PL_scopestack_ix]));
625 /* --- Initialize the stack and the action userdata ------------------------ */
627 STATIC I32 su_init(pTHX_ void *ud, I32 cxix, I32 size) {
628 #define su_init(U, C, S) su_init(aTHX_ (U), (C), (S))
629 I32 i, depth = 1, pad, offset, *origin;
631 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
633 if (size <= SU_SAVE_DESTRUCTOR_SIZE)
636 I32 extra = size - SU_SAVE_DESTRUCTOR_SIZE;
637 pad = extra / SU_SAVE_PLACEHOLDER_SIZE;
638 if (extra % SU_SAVE_PLACEHOLDER_SIZE)
641 offset = SU_SAVE_DESTRUCTOR_SIZE + SU_SAVE_PLACEHOLDER_SIZE * pad;
643 SU_D(PerlIO_printf(Perl_debug_log, "%p: size=%d pad=%d offset=%d\n",
644 ud, size, pad, offset));
646 for (i = cxstack_ix; i > cxix; --i) {
647 PERL_CONTEXT *cx = cxstack + i;
648 switch (CxTYPE(cx)) {
649 #if SU_HAS_PERL(5, 10, 0)
651 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is block\n", ud, i));
652 /* Given and when blocks are actually followed by a simple block, so skip
654 if (cxix > 0) { /* Implies i > 0 */
655 PERL_CONTEXT *next = cx - 1;
656 if (CxTYPE(next) == CXt_GIVEN || CxTYPE(next) == CXt_WHEN)
662 #if SU_HAS_PERL(5, 11, 0)
665 case CXt_LOOP_LAZYSV:
666 case CXt_LOOP_LAZYIV:
670 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
674 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is other\n", ud, i));
679 SU_D(PerlIO_printf(Perl_debug_log, "%p: going down to depth %d\n", ud, depth));
681 Newx(origin, depth + 1, I32);
682 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
683 PL_scopestack[PL_scopestack_ix - depth] += size;
684 for (i = depth - 1; i >= 1; --i) {
685 I32 j = PL_scopestack_ix - i;
686 origin[depth - i] = PL_scopestack[j];
687 PL_scopestack[j] += offset;
689 origin[depth] = PL_savestack_ix;
691 SU_UD_ORIGIN(ud) = origin;
692 SU_UD_DEPTH(ud) = depth;
695 /* Make sure the first destructor fires by pushing enough fake slots on the
697 if (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
698 <= PL_scopestack[PL_scopestack_ix - 1]) {
701 SU_D(PerlIO_printf(Perl_debug_log,
702 "%p: push a fake slot at scope_ix=%2d save_ix=%2d\n",
703 ud, PL_scopestack_ix, PL_savestack_ix));
704 SU_SAVE_PLACEHOLDER();
705 } while (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
706 <= PL_scopestack[PL_scopestack_ix - 1]);
708 SU_D(PerlIO_printf(Perl_debug_log,
709 "%p: push first destructor at scope_ix=%2d save_ix=%2d\n",
710 ud, PL_scopestack_ix, PL_savestack_ix));
711 SAVEDESTRUCTOR_X(su_pop, ud);
714 for (i = 0; i <= depth; ++i) {
715 I32 j = PL_scopestack_ix - i;
716 PerlIO_printf(Perl_debug_log,
717 "%p: depth=%2d scope_ix=%2d saved_floor=%2d new_floor=%2d\n",
718 ud, i, j, origin[depth - i],
719 i == 0 ? PL_savestack_ix : PL_scopestack[j]);
726 /* --- Unwind stack -------------------------------------------------------- */
728 STATIC void su_unwind(pTHX_ void *ud_) {
730 I32 cxix = MY_CXT.cxix;
731 I32 items = MY_CXT.items - 1;
732 SV **savesp = MY_CXT.savesp;
735 PERL_UNUSED_VAR(ud_);
738 PL_stack_sp = savesp;
740 if (cxstack_ix > cxix)
747 mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
748 *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
752 PerlIO_printf(Perl_debug_log,
753 "%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
755 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
756 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
759 PL_op = PL_ppaddr[OP_RETURN](aTHX);
760 *PL_markstack_ptr = mark;
762 MY_CXT.fakeop.op_next = PL_op;
763 PL_op = &(MY_CXT.fakeop);
766 /* --- XS ------------------------------------------------------------------ */
768 #if SU_HAS_PERL(5, 8, 9)
769 # define SU_SKIP_DB_MAX 2
771 # define SU_SKIP_DB_MAX 3
774 /* Skip context sequences of 1 to SU_SKIP_DB_MAX (included) block contexts
775 * followed by a DB sub */
777 #define SU_SKIP_DB(C) \
780 PERL_CONTEXT *base = cxstack; \
781 PERL_CONTEXT *cx = base + (C); \
782 while (cx >= base && (C) > skipped && CxTYPE(cx) == CXt_BLOCK) \
784 if (cx >= base && (C) > skipped) { \
785 switch (CxTYPE(cx)) { \
787 if (skipped <= SU_SKIP_DB_MAX && cx->blk_sub.cv == GvCV(PL_DBsub)) \
788 (C) -= skipped + 1; \
796 #define SU_GET_CONTEXT(A, B) \
805 else if (cxix > cxstack_ix) \
815 #define SU_GET_LEVEL(A, B) \
828 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
830 XS(XS_Scope__Upper_unwind) {
839 PERL_UNUSED_VAR(cv); /* -W */
840 PERL_UNUSED_VAR(ax); /* -Wall */
842 SU_GET_CONTEXT(0, items - 1);
844 PERL_CONTEXT *cx = cxstack + cxix;
845 switch (CxTYPE(cx)) {
847 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
852 MY_CXT.items = items;
853 /* pp_entersub will want to sanitize the stack after returning from there
854 * Screw that, we're insane */
855 if (GIMME_V == G_SCALAR) {
856 MY_CXT.savesp = PL_stack_sp;
857 /* dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
858 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
860 MY_CXT.savesp = NULL;
862 SAVEDESTRUCTOR_X(su_unwind, NULL);
867 } while (--cxix >= 0);
868 croak("Can't return outside a subroutine");
871 MODULE = Scope::Upper PACKAGE = Scope::Upper
880 MY_CXT.stack_placeholder = NULL;
882 stash = gv_stashpv(__PACKAGE__, 1);
883 newCONSTSUB(stash, "TOP", newSViv(0));
884 newCONSTSUB(stash, "SU_THREADSAFE", newSVuv(SU_THREADSAFE));
886 newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
900 #endif /* SU_THREADSAFE */
906 I32 cxix = cxstack_ix;
910 RETVAL = newSViv(cxix);
920 SU_GET_CONTEXT(0, 0);
925 RETVAL = newSViv(cxix);
935 SU_GET_CONTEXT(0, 0);
936 for (; cxix >= 0; --cxix) {
937 PERL_CONTEXT *cx = cxstack + cxix;
938 switch (CxTYPE(cx)) {
942 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
944 ST(0) = sv_2mortal(newSViv(cxix));
956 SU_GET_CONTEXT(0, 0);
957 for (; cxix >= 0; --cxix) {
958 PERL_CONTEXT *cx = cxstack + cxix;
959 switch (CxTYPE(cx)) {
963 ST(0) = sv_2mortal(newSViv(cxix));
990 ST(0) = sv_2mortal(newSViv(cxix));
1000 for (cxix = cxstack_ix; cxix > 0; --cxix) {
1001 PERL_CONTEXT *cx = cxstack + cxix;
1002 switch (CxTYPE(cx)) {
1004 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1014 ST(0) = sv_2mortal(newSViv(cxix));
1023 SU_GET_CONTEXT(0, 0);
1025 PERL_CONTEXT *cx = cxstack + cxix--;
1026 switch (CxTYPE(cx)) {
1030 I32 gimme = cx->blk_gimme;
1032 case G_VOID: XSRETURN_UNDEF; break;
1033 case G_SCALAR: XSRETURN_NO; break;
1034 case G_ARRAY: XSRETURN_YES; break;
1049 SU_GET_CONTEXT(1, 1);
1050 Newx(ud, 1, su_ud_reap);
1051 SU_UD_ORIGIN(ud) = NULL;
1052 SU_UD_HANDLER(ud) = su_reap;
1053 ud->cb = newSVsv(hook);
1054 su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE);
1057 localize(SV *sv, SV *val, ...)
1064 SU_GET_CONTEXT(2, 2);
1065 Newx(ud, 1, su_ud_localize);
1066 SU_UD_ORIGIN(ud) = NULL;
1067 SU_UD_HANDLER(ud) = su_localize;
1068 size = su_ud_localize_init(ud, sv, val, NULL);
1069 su_init(ud, cxix, size);
1072 localize_elem(SV *sv, SV *elem, SV *val, ...)
1079 if (SvTYPE(sv) >= SVt_PVGV)
1080 croak("Can't infer the element localization type from a glob and the value");
1081 SU_GET_CONTEXT(3, 3);
1082 Newx(ud, 1, su_ud_localize);
1083 SU_UD_ORIGIN(ud) = NULL;
1084 SU_UD_HANDLER(ud) = su_localize;
1085 size = su_ud_localize_init(ud, sv, val, elem);
1086 if (ud->type != SVt_PVAV && ud->type != SVt_PVHV) {
1087 SU_UD_LOCALIZE_FREE(ud);
1088 croak("Can't localize an element of something that isn't an array or a hash");
1090 su_init(ud, cxix, size);
1093 localize_delete(SV *sv, SV *elem, ...)
1100 SU_GET_CONTEXT(2, 2);
1101 Newx(ud, 1, su_ud_localize);
1102 SU_UD_ORIGIN(ud) = NULL;
1103 SU_UD_HANDLER(ud) = su_localize;
1104 size = su_ud_localize_init(ud, sv, NULL, elem);
1105 su_init(ud, cxix, size);