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 ------------------------------------------------------- */
30 # define MUTABLE_SV(S) ((SV *) (S))
34 # define MUTABLE_AV(A) ((AV *) (A))
38 # define MUTABLE_CV(C) ((CV *) (C))
41 #ifndef PERL_UNUSED_VAR
42 # define PERL_UNUSED_VAR(V)
46 # define STMT_START do
50 # define STMT_END while (0)
54 # define SU_D(X) STMT_START X STMT_END
60 # define Newx(v, n, c) New(0, v, n, c)
65 # define SU_POISON(D, N, T) PoisonNew((D), (N), T)
66 # elif defined(Poison)
67 # define SU_POISON(D, N, T) Poison((D), (N), T)
71 # define SU_POISON(D, N, T) NOOP
75 STATIC SV *su_newSV_type(pTHX_ svtype t) {
80 # define newSV_type(T) su_newSV_type(aTHX_ (T))
84 # define SvPV_const(S, L) SvPV(S, L)
88 # define SvPVX_const(S) SvPVX(S)
91 #ifndef SvPV_nolen_const
92 # define SvPV_nolen_const(S) SvPV_nolen(S)
95 #ifndef SvREFCNT_inc_simple_void
96 # define SvREFCNT_inc_simple_void(sv) ((void) SvREFCNT_inc(sv))
100 # define mPUSHi(I) PUSHs(sv_2mortal(newSViv(I)))
104 # define GvCV_set(G, C) (GvCV(G) = (C))
108 # define CvGV_set(C, G) (CvGV(C) = (G))
112 # define CvSTASH_set(C, S) (CvSTASH(C) = (S))
116 # define CvISXSUB(C) CvXSUB(C)
120 # define CxHASARGS(C) ((C)->blk_sub.hasargs)
124 # define HvNAME_get(H) HvNAME(H)
127 #ifndef gv_fetchpvn_flags
128 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
131 #ifndef PERL_MAGIC_tied
132 # define PERL_MAGIC_tied 'P'
135 #ifndef PERL_MAGIC_env
136 # define PERL_MAGIC_env 'E'
139 #ifndef NEGATIVE_INDICES_VAR
140 # define NEGATIVE_INDICES_VAR "NEGATIVE_INDICES"
143 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
144 #define SU_HAS_PERL_EXACT(R, V, S) ((PERL_REVISION == (R)) && (PERL_VERSION == (V)) && (PERL_SUBVERSION == (S)))
146 /* --- Threads and multiplicity -------------------------------------------- */
148 #ifndef SU_MULTIPLICITY
149 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
150 # define SU_MULTIPLICITY 1
152 # define SU_MULTIPLICITY 0
155 #if SU_MULTIPLICITY && !defined(tTHX)
156 # define tTHX PerlInterpreter*
159 #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))
160 # define SU_THREADSAFE 1
161 # ifndef MY_CXT_CLONE
162 # define MY_CXT_CLONE \
164 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
165 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
166 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
169 # define SU_THREADSAFE 0
171 # define dMY_CXT dNOOP
173 # define MY_CXT su_globaldata
175 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
177 # define MY_CXT_INIT NOOP
179 # define MY_CXT_CLONE NOOP
182 /* --- unwind() global storage --------------------------------------------- */
192 /* --- uplevel() data tokens and global storage ---------------------------- */
194 #define SU_UPLEVEL_HIJACKS_RUNOPS SU_HAS_PERL(5, 8, 0)
209 PERL_SI *old_curstackinfo;
214 #if SU_UPLEVEL_HIJACKS_RUNOPS
215 runops_proc_t old_runops;
221 STATIC su_uplevel_ud *su_uplevel_ud_new(pTHX) {
222 #define su_uplevel_ud_new() su_uplevel_ud_new(aTHX)
226 Newx(sud, 1, su_uplevel_ud);
229 Newx(si, 1, PERL_SI);
230 si->si_stack = newAV();
231 AvREAL_off(si->si_stack);
232 si->si_cxstack = NULL;
240 STATIC void su_uplevel_ud_delete(pTHX_ su_uplevel_ud *sud) {
241 #define su_uplevel_ud_delete(S) su_uplevel_ud_delete(aTHX_ (S))
242 PERL_SI *si = sud->si;
244 Safefree(si->si_cxstack);
245 SvREFCNT_dec(si->si_stack);
256 } su_uplevel_storage;
258 #ifndef SU_UPLEVEL_STORAGE_SIZE
259 # define SU_UPLEVEL_STORAGE_SIZE 4
262 /* --- Global data --------------------------------------------------------- */
264 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
267 char *stack_placeholder;
268 su_unwind_storage unwind_storage;
269 su_uplevel_storage uplevel_storage;
274 /* --- Stack manipulations ------------------------------------------------- */
276 #define SU_SAVE_PLACEHOLDER() save_pptr(&MY_CXT.stack_placeholder)
278 #define SU_SAVE_DESTRUCTOR_SIZE 3
279 #define SU_SAVE_PLACEHOLDER_SIZE 3
281 #define SU_SAVE_SCALAR_SIZE 3
283 #define SU_SAVE_ARY_SIZE 3
284 #define SU_SAVE_AELEM_SIZE 4
286 # define SU_SAVE_ADELETE_SIZE 3
288 # define SU_SAVE_ADELETE_SIZE SU_SAVE_DESTRUCTOR_SIZE
290 #if SU_SAVE_AELEM_SIZE < SU_SAVE_ADELETE_SIZE
291 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_ADELETE_SIZE
293 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_AELEM_SIZE
296 #define SU_SAVE_HASH_SIZE 3
297 #define SU_SAVE_HELEM_SIZE 4
298 #define SU_SAVE_HDELETE_SIZE 4
299 #if SU_SAVE_HELEM_SIZE < SU_SAVE_HDELETE_SIZE
300 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HDELETE_SIZE
302 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HELEM_SIZE
305 #define SU_SAVE_GVCV_SIZE SU_SAVE_DESTRUCTOR_SIZE
307 #if !SU_HAS_PERL(5, 8, 9)
308 # define SU_SAVE_GP_SIZE 6
309 #elif !SU_HAS_PERL(5, 13, 0) || (SU_RELEASE && SU_HAS_PERL_EXACT(5, 13, 0))
310 # define SU_SAVE_GP_SIZE 3
311 #elif !SU_HAS_PERL(5, 13, 8)
312 # define SU_SAVE_GP_SIZE 4
314 # define SU_SAVE_GP_SIZE 3
317 #ifndef SvCANEXISTDELETE
318 # define SvCANEXISTDELETE(sv) \
320 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
321 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
322 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
323 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
328 /* ... Saving array elements ............................................... */
330 STATIC I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
331 #define su_av_key2idx(A, K) su_av_key2idx(aTHX_ (A), (K))
337 /* Added by MJD in perl-5.8.1 with 6f12eb6d2a1dfaf441504d869b27d2e40ef4966a */
338 #if SU_HAS_PERL(5, 8, 1)
339 if (SvRMAGICAL(av)) {
340 const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
342 SV * const * const negative_indices_glob =
343 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
344 NEGATIVE_INDICES_VAR, 16, 0);
345 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
351 idx = key + av_len(av) + 1;
365 STATIC void su_adelete(pTHX_ void *ud_) {
366 su_ud_adelete *ud = (su_ud_adelete *) ud_;
368 av_delete(ud->av, ud->idx, G_DISCARD);
369 SvREFCNT_dec(ud->av);
374 STATIC void su_save_adelete(pTHX_ AV *av, I32 idx) {
375 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
378 Newx(ud, 1, su_ud_adelete);
381 SvREFCNT_inc_simple_void(av);
383 SAVEDESTRUCTOR_X(su_adelete, ud);
386 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
388 #endif /* SAVEADELETE */
390 STATIC void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
391 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
398 idx = su_av_key2idx(av, SvIV(key));
400 if (SvCANEXISTDELETE(av))
401 preeminent = av_exists(av, idx);
403 svp = av_fetch(av, idx, 1);
404 if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
407 save_aelem(av, idx, svp);
409 SAVEADELETE(av, idx);
411 if (val) { /* local $x[$idx] = $val; */
412 SvSetMagicSV(*svp, val);
413 } else { /* local $x[$idx]; delete $x[$idx]; */
414 av_delete(av, idx, G_DISCARD);
418 /* ... Saving hash elements ................................................ */
420 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
421 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
428 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
429 preeminent = hv_exists_ent(hv, keysv, 0);
431 he = hv_fetch_ent(hv, keysv, 1, 0);
432 svp = he ? &HeVAL(he) : NULL;
433 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
435 if (HvNAME_get(hv) && isGV(*svp)) {
436 save_gp((GV *) *svp, 0);
441 save_helem(hv, keysv, svp);
444 const char * const key = SvPV_const(keysv, keylen);
445 SAVEDELETE(hv, savepvn(key, keylen),
446 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
449 if (val) { /* local $x{$keysv} = $val; */
450 SvSetMagicSV(*svp, val);
451 } else { /* local $x{$keysv}; delete $x{$keysv}; */
452 (void)hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
456 /* ... Saving code slots from a glob ....................................... */
458 #if !SU_HAS_PERL(5, 10, 0) && !defined(mro_method_changed_in)
459 # define mro_method_changed_in(G) PL_sub_generation++
467 STATIC void su_restore_gvcv(pTHX_ void *ud_) {
468 su_save_gvcv_ud *ud = ud_;
471 GvCV_set(gv, ud->old_cv);
473 mro_method_changed_in(GvSTASH(gv));
478 STATIC void su_save_gvcv(pTHX_ GV *gv) {
479 #define su_save_gvcv(G) su_save_gvcv(aTHX_ (G))
482 Newx(ud, 1, su_save_gvcv_ud);
484 ud->old_cv = GvCV(gv);
488 mro_method_changed_in(GvSTASH(gv));
490 SAVEDESTRUCTOR_X(su_restore_gvcv, ud);
493 /* --- Actions ------------------------------------------------------------- */
499 void (*handler)(pTHX_ void *);
502 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
503 #define SU_UD_PAD(U) (((su_ud_common *) (U))->pad)
504 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
505 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
507 #define SU_UD_FREE(U) STMT_START { \
508 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
512 /* ... Reap ................................................................ */
519 STATIC void su_call(pTHX_ void *ud_) {
520 su_ud_reap *ud = (su_ud_reap *) ud_;
521 #if SU_HAS_PERL(5, 9, 5)
522 PERL_CONTEXT saved_cx;
529 PerlIO_printf(Perl_debug_log,
530 "%p: @@@ call\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
531 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
540 /* If the recently popped context isn't saved there, it will be overwritten by
541 * the sub scope from call_sv, although it's still needed in our caller. */
543 #if SU_HAS_PERL(5, 9, 5)
544 if (cxstack_ix < cxstack_max)
545 cxix = cxstack_ix + 1;
547 cxix = Perl_cxinc(aTHX);
548 saved_cx = cxstack[cxix];
551 call_sv(ud->cb, G_VOID);
553 #if SU_HAS_PERL(5, 9, 5)
554 cxstack[cxix] = saved_cx;
562 SvREFCNT_dec(ud->cb);
566 STATIC void su_reap(pTHX_ void *ud) {
567 #define su_reap(U) su_reap(aTHX_ (U))
569 PerlIO_printf(Perl_debug_log,
570 "%p: === reap\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
571 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
574 SAVEDESTRUCTOR_X(su_call, ud);
577 /* ... Localize & localize array/hash element .............................. */
587 #define SU_UD_LOCALIZE_FREE(U) STMT_START { \
588 SvREFCNT_dec((U)->elem); \
589 SvREFCNT_dec((U)->val); \
590 SvREFCNT_dec((U)->sv); \
594 STATIC I32 su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) {
595 #define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E))
600 SvREFCNT_inc_simple_void(sv);
602 if (SvTYPE(sv) >= SVt_PVGV) {
603 if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */
605 } else { /* local *x = \$val; */
606 t = SvTYPE(SvRV(val));
609 } else if (SvROK(sv)) {
610 croak("Invalid %s reference as the localization target",
611 sv_reftype(SvRV(sv), 0));
614 const char *p = SvPV_const(sv, len), *s;
615 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
621 case '$': t = SVt_PV; break;
622 case '@': t = SVt_PVAV; break;
623 case '%': t = SVt_PVHV; break;
624 case '&': t = SVt_PVCV; break;
625 case '*': t = SVt_PVGV; break;
630 } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
631 if (SvROK(val) && !sv_isobject(val)) {
632 t = SvTYPE(SvRV(val));
644 size = elem ? SU_SAVE_AELEM_OR_ADELETE_SIZE
649 size = elem ? SU_SAVE_HELEM_OR_HDELETE_SIZE
654 size = SU_SAVE_GP_SIZE;
658 size = SU_SAVE_GVCV_SIZE;
662 size = SU_SAVE_SCALAR_SIZE;
665 /* When deref is set, val isn't NULL */
668 ud->val = val ? newSVsv(deref ? SvRV(val) : val) : NULL;
669 ud->elem = SvREFCNT_inc(elem);
675 STATIC void su_localize(pTHX_ void *ud_) {
676 #define su_localize(U) su_localize(aTHX_ (U))
677 su_ud_localize *ud = (su_ud_localize *) ud_;
684 if (SvTYPE(sv) >= SVt_PVGV) {
688 gv = gv_fetchsv(sv, GV_ADDMULTI, t);
691 const char *name = SvPV_const(sv, len);
692 gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t);
699 PerlIO_printf(Perl_debug_log, "%p: === localize a %s\n",ud, sv_reftype(z, 0));
700 PerlIO_printf(Perl_debug_log,
701 "%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
702 ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
706 /* Inspired from Alias.pm */
710 su_save_aelem(GvAV(gv), elem, val);
717 su_save_helem(GvHV(gv), elem, val);
723 save_gp(gv, 1); /* hide previous entry in symtab */
729 gv = (GV *) save_scalar(gv);
734 SvSetMagicSV((SV *) gv, val);
737 SU_UD_LOCALIZE_FREE(ud);
740 /* --- Pop a context back -------------------------------------------------- */
744 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
746 # define SU_CXNAME "XXX"
750 STATIC void su_pop(pTHX_ void *ud) {
751 #define su_pop(U) su_pop(aTHX_ (U))
752 I32 depth, base, mark, *origin;
753 depth = SU_UD_DEPTH(ud);
756 PerlIO_printf(Perl_debug_log,
758 "%p: leave scope at depth=%2d scope_ix=%2d cur_top=%2d cur_base=%2d\n",
760 ud, depth, PL_scopestack_ix,PL_savestack_ix,PL_scopestack[PL_scopestack_ix])
763 origin = SU_UD_ORIGIN(ud);
764 mark = origin[depth];
765 base = origin[depth - 1];
767 SU_D(PerlIO_printf(Perl_debug_log,
768 "%p: original scope was %*c top=%2d base=%2d\n",
769 ud, 24, ' ', mark, base));
772 SU_D(PerlIO_printf(Perl_debug_log, "%p: clear leftovers\n", ud));
773 PL_savestack_ix = mark;
776 PL_savestack_ix = base;
778 SU_UD_DEPTH(ud) = --depth;
783 if ((pad = SU_UD_PAD(ud))) {
786 SU_D(PerlIO_printf(Perl_debug_log,
787 "%p: push a pad slot at depth=%2d scope_ix=%2d save_ix=%2d\n",
788 ud, depth, PL_scopestack_ix, PL_savestack_ix));
789 SU_SAVE_PLACEHOLDER();
793 SU_D(PerlIO_printf(Perl_debug_log,
794 "%p: push destructor at depth=%2d scope_ix=%2d save_ix=%2d\n",
795 ud, depth, PL_scopestack_ix, PL_savestack_ix));
796 SAVEDESTRUCTOR_X(su_pop, ud);
798 SU_UD_HANDLER(ud)(aTHX_ ud);
801 SU_D(PerlIO_printf(Perl_debug_log,
802 "%p: --- end pop: cur_top=%2d == cur_base=%2d\n",
803 ud, PL_savestack_ix, PL_scopestack[PL_scopestack_ix]));
806 /* --- Initialize the stack and the action userdata ------------------------ */
808 STATIC I32 su_init(pTHX_ void *ud, I32 cxix, I32 size) {
809 #define su_init(U, C, S) su_init(aTHX_ (U), (C), (S))
810 I32 i, depth = 1, pad, offset, *origin;
812 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
814 if (size <= SU_SAVE_DESTRUCTOR_SIZE)
817 I32 extra = size - SU_SAVE_DESTRUCTOR_SIZE;
818 pad = extra / SU_SAVE_PLACEHOLDER_SIZE;
819 if (extra % SU_SAVE_PLACEHOLDER_SIZE)
822 offset = SU_SAVE_DESTRUCTOR_SIZE + SU_SAVE_PLACEHOLDER_SIZE * pad;
824 SU_D(PerlIO_printf(Perl_debug_log, "%p: size=%d pad=%d offset=%d\n",
825 ud, size, pad, offset));
827 for (i = cxstack_ix; i > cxix; --i) {
828 PERL_CONTEXT *cx = cxstack + i;
829 switch (CxTYPE(cx)) {
830 #if SU_HAS_PERL(5, 10, 0)
832 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is block\n", ud, i));
833 /* Given and when blocks are actually followed by a simple block, so skip
835 if (cxix > 0) { /* Implies i > 0 */
836 PERL_CONTEXT *next = cx - 1;
837 if (CxTYPE(next) == CXt_GIVEN || CxTYPE(next) == CXt_WHEN)
843 #if SU_HAS_PERL(5, 11, 0)
846 case CXt_LOOP_LAZYSV:
847 case CXt_LOOP_LAZYIV:
851 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
855 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is other\n", ud, i));
860 SU_D(PerlIO_printf(Perl_debug_log, "%p: going down to depth %d\n", ud, depth));
862 Newx(origin, depth + 1, I32);
863 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
864 PL_scopestack[PL_scopestack_ix - depth] += size;
865 for (i = depth - 1; i >= 1; --i) {
866 I32 j = PL_scopestack_ix - i;
867 origin[depth - i] = PL_scopestack[j];
868 PL_scopestack[j] += offset;
870 origin[depth] = PL_savestack_ix;
872 SU_UD_ORIGIN(ud) = origin;
873 SU_UD_DEPTH(ud) = depth;
876 /* Make sure the first destructor fires by pushing enough fake slots on the
878 if (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
879 <= PL_scopestack[PL_scopestack_ix - 1]) {
882 SU_D(PerlIO_printf(Perl_debug_log,
883 "%p: push a fake slot at scope_ix=%2d save_ix=%2d\n",
884 ud, PL_scopestack_ix, PL_savestack_ix));
885 SU_SAVE_PLACEHOLDER();
886 } while (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
887 <= PL_scopestack[PL_scopestack_ix - 1]);
889 SU_D(PerlIO_printf(Perl_debug_log,
890 "%p: push first destructor at scope_ix=%2d save_ix=%2d\n",
891 ud, PL_scopestack_ix, PL_savestack_ix));
892 SAVEDESTRUCTOR_X(su_pop, ud);
895 for (i = 0; i <= depth; ++i) {
896 I32 j = PL_scopestack_ix - i;
897 PerlIO_printf(Perl_debug_log,
898 "%p: depth=%2d scope_ix=%2d saved_floor=%2d new_floor=%2d\n",
899 ud, i, j, origin[depth - i],
900 i == 0 ? PL_savestack_ix : PL_scopestack[j]);
907 /* --- Unwind stack -------------------------------------------------------- */
909 STATIC void su_unwind(pTHX_ void *ud_) {
911 I32 cxix = MY_CXT.unwind_storage.cxix;
912 I32 items = MY_CXT.unwind_storage.items - 1;
913 SV **savesp = MY_CXT.unwind_storage.savesp;
916 PERL_UNUSED_VAR(ud_);
919 PL_stack_sp = savesp;
921 if (cxstack_ix > cxix)
928 mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
929 *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
933 PerlIO_printf(Perl_debug_log,
934 "%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
936 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
937 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
940 PL_op = (OP *) &(MY_CXT.unwind_storage.return_op);
941 PL_op = PL_op->op_ppaddr(aTHX);
943 *PL_markstack_ptr = mark;
945 MY_CXT.unwind_storage.proxy_op.op_next = PL_op;
946 PL_op = &(MY_CXT.unwind_storage.proxy_op);
949 /* --- Uplevel ------------------------------------------------------------- */
951 #ifndef OP_GIMME_REVERSE
952 STATIC U8 su_op_gimme_reverse(U8 gimme) {
955 return OPf_WANT_VOID;
957 return OPf_WANT_LIST;
962 return OPf_WANT_SCALAR;
964 #define OP_GIMME_REVERSE(G) su_op_gimme_reverse(G)
967 #define SU_UPLEVEL_SAVE(f, t) STMT_START { sud->old_##f = PL_##f; PL_##f = (t); } STMT_END
968 #define SU_UPLEVEL_RESTORE(f) STMT_START { PL_##f = sud->old_##f; } STMT_END
970 STATIC su_uplevel_ud *su_uplevel_storage_new(pTHX) {
971 #define su_uplevel_storage_new() su_uplevel_storage_new(aTHX)
975 sud = MY_CXT.uplevel_storage.root;
977 MY_CXT.uplevel_storage.root = sud->next;
978 MY_CXT.uplevel_storage.count--;
980 sud = su_uplevel_ud_new();
983 sud->next = MY_CXT.uplevel_storage.top;
984 MY_CXT.uplevel_storage.top = sud;
989 STATIC void su_uplevel_storage_delete(pTHX_ su_uplevel_ud *sud) {
990 #define su_uplevel_storage_delete(S) su_uplevel_storage_delete(aTHX_ (S))
993 MY_CXT.uplevel_storage.top = sud->next;
995 if (MY_CXT.uplevel_storage.count >= SU_UPLEVEL_STORAGE_SIZE) {
996 su_uplevel_ud_delete(sud);
998 sud->next = MY_CXT.uplevel_storage.root;
999 MY_CXT.uplevel_storage.root = sud;
1000 MY_CXT.uplevel_storage.count++;
1004 STATIC int su_uplevel_goto_static(const OP *o) {
1005 for (; o; o = o->op_sibling) {
1006 /* goto ops are unops with kids. */
1007 if (!(o->op_flags & OPf_KIDS))
1010 switch (o->op_type) {
1013 /* Don't care about gotos inside eval, as they are forbidden at run time. */
1018 if (su_uplevel_goto_static(cUNOPo->op_first))
1027 #if SU_UPLEVEL_HIJACKS_RUNOPS
1029 STATIC int su_uplevel_goto_runops(pTHX) {
1030 #define su_uplevel_goto_runops() su_uplevel_goto_runops(aTHX)
1036 if (op->op_type == OP_GOTO) {
1037 AV *argarray = NULL;
1040 for (cxix = cxstack_ix; cxix >= 0; --cxix) {
1041 const PERL_CONTEXT *cx = cxstack + cxix;
1043 switch (CxTYPE(cx)) {
1045 if (CxHASARGS(cx)) {
1046 argarray = cx->blk_sub.argarray;
1062 if (MY_CXT.uplevel_storage.top->cxix == cxix) {
1063 AV *args = GvAV(PL_defgv);
1064 I32 items = AvFILLp(args);
1066 av_extend(argarray, items);
1067 Copy(AvARRAY(args), AvARRAY(argarray), items + 1, SV *);
1068 AvFILLp(argarray) = items;
1073 PL_op = op = op->op_ppaddr(aTHX);
1075 #if !SU_HAS_PERL(5, 13, 0)
1085 #endif /* SU_UPLEVEL_HIJACKS_RUNOPS */
1087 #define su_at_underscore(C) AvARRAY(AvARRAY(CvPADLIST(C))[CvDEPTH(C)])[0]
1089 STATIC void su_uplevel_restore(pTHX_ void *sus_) {
1090 su_uplevel_ud *sud = sus_;
1091 PERL_SI *cur = sud->old_curstackinfo;
1092 PERL_SI *si = sud->si;
1094 #if SU_UPLEVEL_HIJACKS_RUNOPS
1095 if (PL_runops == su_uplevel_goto_runops)
1096 PL_runops = sud->old_runops;
1099 if (sud->callback) {
1100 PERL_CONTEXT *cx = cxstack + sud->cxix;
1101 AV *argarray = MUTABLE_AV(su_at_underscore(sud->callback));
1103 /* We have to fix the pad entry for @_ in the original callback because it
1104 * may have been reified. */
1105 if (AvREAL(argarray)) {
1106 const I32 fill = AvFILLp(argarray);
1107 SvREFCNT_dec(argarray);
1109 AvREAL_off(argarray);
1110 AvREIFY_on(argarray);
1111 av_extend(argarray, fill);
1112 su_at_underscore(sud->callback) = MUTABLE_SV(argarray);
1114 CLEAR_ARGARRAY(argarray);
1117 /* If the old cv member is our renamed CV, it means that this place has been
1118 * reached without a goto() happening, and the old argarray member is
1119 * actually our fake argarray. Destroy it properly in that case. */
1120 if (cx->blk_sub.cv == sud->renamed) {
1121 SvREFCNT_dec(cx->blk_sub.argarray);
1122 cx->blk_sub.argarray = argarray;
1125 CvDEPTH(sud->callback)--;
1126 SvREFCNT_dec(sud->callback);
1129 /* Free the renamed CV. We must do it ourselves so that we can force the
1130 * depth to be 0, or perl would complain about it being "still in use".
1131 * But we *know* that it cannot be so. */
1133 CvDEPTH(sud->renamed) = 0;
1134 CvPADLIST(sud->renamed) = NULL;
1135 SvREFCNT_dec(sud->renamed);
1138 CATCH_SET(sud->old_catch);
1140 SU_UPLEVEL_RESTORE(op);
1142 /* stack_grow() wants PL_curstack so restore the old stack first */
1143 if (PL_curstackinfo == si) {
1144 PL_curstack = cur->si_stack;
1145 if (sud->old_mainstack)
1146 SU_UPLEVEL_RESTORE(mainstack);
1147 SU_UPLEVEL_RESTORE(curstackinfo);
1150 CV *target = sud->target;
1153 /* When we die, the depth of the target CV is not updated because of the
1154 * stack switcheroo. So we have to look at all the frames between the
1155 * uplevel call and the catch block to count how many call frames to the
1156 * target CV were skipped. */
1157 for (i = cur->si_cxix; i > sud->cxix; i--) {
1158 register const PERL_CONTEXT *cx = cxstack + i;
1160 if (CxTYPE(cx) == CXt_SUB) {
1161 if (cx->blk_sub.cv == target)
1166 /* If we died, the replacement stack was already unwinded to the first
1167 * eval frame, and all the contexts down there were popped. We don't have
1168 * to pop manually any context of the original stack, because they must
1169 * have been in the replacement stack as well (since the second was copied
1170 * from the first). Thus we only have to make sure the original stack index
1171 * points to the context just below the first eval scope under the target
1173 for (; i >= 0; i--) {
1174 register const PERL_CONTEXT *cx = cxstack + i;
1176 switch (CxTYPE(cx)) {
1178 if (cx->blk_sub.cv == target)
1190 CvDEPTH(target) = sud->target_depth - levels;
1191 PL_curstackinfo->si_cxix = i - 1;
1193 #if !SU_HAS_PERL(5, 13, 1)
1194 /* Since $@ was maybe localized between the target frame and the uplevel
1195 * call, we forcefully flush the save stack to get rid of it and then
1196 * reset $@ to its proper value. Note that the the call to
1197 * su_uplevel_restore() must happen before the "reset $@" item of the save
1198 * stack is processed, as uplevel was called after the localization.
1199 * Andrew's changes to how $@ was handled, which were mainly integrated
1200 * between perl 5.13.0 and 5.13.1, fixed this. */
1201 if (ERRSV && SvTRUE(ERRSV)) {
1202 register const PERL_CONTEXT *cx = cxstack + i; /* This is the eval scope */
1203 SV *errsv = SvREFCNT_inc(ERRSV);
1204 PL_scopestack_ix = cx->blk_oldscopesp;
1205 leave_scope(PL_scopestack[PL_scopestack_ix]);
1206 sv_setsv(ERRSV, errsv);
1207 SvREFCNT_dec(errsv);
1213 SU_UPLEVEL_RESTORE(curcop);
1215 SvREFCNT_dec(sud->target);
1217 PL_stack_base = AvARRAY(cur->si_stack);
1218 PL_stack_sp = PL_stack_base + AvFILLp(cur->si_stack);
1219 PL_stack_max = PL_stack_base + AvMAX(cur->si_stack);
1221 /* When an exception is thrown from the uplevel'd subroutine,
1222 * su_uplevel_restore() may be called by the LEAVE in die_unwind() (renamed
1223 * die_where() in more recent perls), which has the sad habit of keeping a
1224 * pointer to the current context frame across this call. This means that we
1225 * can't free the temporary context stack we used for the uplevel call right
1226 * now, or that pointer upwards would point to garbage. */
1227 #if SU_HAS_PERL(5, 13, 7)
1228 /* This issue has been fixed in perl with commit 8f89e5a9, which was made
1229 * public in perl 5.13.7. */
1230 su_uplevel_storage_delete(sud);
1232 /* Otherwise, we just enqueue it back in the global storage list. */
1236 MY_CXT.uplevel_storage.top = sud->next;
1237 sud->next = MY_CXT.uplevel_storage.root;
1238 MY_CXT.uplevel_storage.root = sud;
1239 MY_CXT.uplevel_storage.count++;
1246 STATIC CV *su_cv_clone(pTHX_ CV *proto, GV *gv) {
1247 #define su_cv_clone(P, G) su_cv_clone(aTHX_ (P), (G))
1251 cv = MUTABLE_CV(newSV_type(SvTYPE(proto)));
1253 CvFLAGS(cv) = CvFLAGS(proto);
1255 CvFLAGS(cv) &= ~CVf_CVGV_RC;
1257 CvDEPTH(cv) = CvDEPTH(proto);
1259 CvFILE(cv) = CvISXSUB(proto) ? CvFILE(proto) : savepv(CvFILE(proto));
1261 CvFILE(cv) = CvFILE(proto);
1265 CvSTASH_set(cv, CvSTASH(proto));
1266 /* Commit 4c74a7df, publicized with perl 5.13.3, began to add backrefs to
1267 * stashes. CvSTASH_set() started to do it as well with commit c68d95645
1268 * (which was part of perl 5.13.7). */
1269 #if SU_HAS_PERL(5, 13, 3) && !SU_HAS_PERL(5, 13, 7)
1271 Perl_sv_add_backref(aTHX_ CvSTASH(proto), MUTABLE_SV(cv));
1274 if (CvISXSUB(proto)) {
1275 CvXSUB(cv) = CvXSUB(proto);
1276 CvXSUBANY(cv) = CvXSUBANY(proto);
1279 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
1281 CvSTART(cv) = CvSTART(proto);
1283 CvOUTSIDE(cv) = CvOUTSIDE(proto);
1284 #ifdef CVf_WEAKOUTSIDE
1285 if (!(CvFLAGS(proto) & CVf_WEAKOUTSIDE))
1287 SvREFCNT_inc_simple_void(CvOUTSIDE(cv));
1288 CvPADLIST(cv) = CvPADLIST(proto);
1289 #ifdef CvOUTSIDE_SEQ
1290 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
1294 sv_setpvn(MUTABLE_SV(cv), SvPVX_const(proto), SvCUR(proto));
1304 STATIC I32 su_uplevel(pTHX_ CV *callback, I32 cxix, I32 args) {
1305 #define su_uplevel(C, I, A) su_uplevel(aTHX_ (C), (I), (A))
1307 const PERL_CONTEXT *cx = cxstack + cxix;
1309 PERL_SI *cur = PL_curstackinfo;
1315 I32 old_mark, new_mark;
1322 /* Make PL_stack_sp point just before the CV. */
1323 PL_stack_sp -= args + 1;
1324 old_mark = AvFILLp(PL_curstack) = PL_stack_sp - PL_stack_base;
1327 sud = su_uplevel_storage_new();
1331 sud->callback = NULL;
1332 sud->renamed = NULL;
1333 SAVEDESTRUCTOR_X(su_uplevel_restore, sud);
1337 si->si_type = cur->si_type;
1339 si->si_prev = cur->si_prev;
1341 si->si_markoff = cx->blk_oldmarksp;
1344 /* Allocate enough space for all the elements of the original stack up to the
1345 * target context, plus the forthcoming arguments. */
1346 new_mark = cx->blk_oldsp;
1347 av_extend(si->si_stack, new_mark + 1 + args + 1);
1348 Copy(PL_curstack, AvARRAY(si->si_stack), new_mark + 1, SV *);
1349 AvFILLp(si->si_stack) = new_mark;
1350 SU_POISON(AvARRAY(si->si_stack) + new_mark + 1, args + 1, SV *);
1352 /* Specialized SWITCHSTACK() */
1353 PL_stack_base = AvARRAY(si->si_stack);
1354 old_stack_sp = PL_stack_sp;
1355 PL_stack_sp = PL_stack_base + AvFILLp(si->si_stack);
1356 PL_stack_max = PL_stack_base + AvMAX(si->si_stack);
1359 /* Copy the context stack up to the context just below the target. */
1360 si->si_cxix = (cxix < 0) ? -1 : (cxix - 1);
1361 if (si->si_cxmax < cxix) {
1362 /* The max size must be at least two so that GROW(max) = (max*3)/2 > max */
1363 si->si_cxmax = (cxix < 4) ? 4 : cxix;
1364 Renew(si->si_cxstack, si->si_cxmax + 1, PERL_CONTEXT);
1366 Copy(cur->si_cxstack, si->si_cxstack, cxix, PERL_CONTEXT);
1367 SU_POISON(si->si_cxstack + cxix, si->si_cxmax + 1 - cxix, PERL_CONTEXT);
1369 target = cx->blk_sub.cv;
1370 sud->target = (CV *) SvREFCNT_inc(target);
1371 sud->target_depth = CvDEPTH(target);
1373 /* blk_oldcop is essentially needed for caller() and stack traces. It has no
1374 * run-time implication, since PL_curcop will be overwritten as soon as we
1375 * enter a sub (a sub starts by a nextstate/dbstate). Hence it's safe to just
1376 * make it point to the blk_oldcop for the target frame, so that caller()
1377 * reports the right file name, line number and lexical hints. */
1378 SU_UPLEVEL_SAVE(curcop, cx->blk_oldcop);
1379 /* Don't reset PL_markstack_ptr, or we would overwrite the mark stack below
1380 * this point. Don't reset PL_curpm either, we want the most recent matches. */
1382 SU_UPLEVEL_SAVE(curstackinfo, si);
1383 /* If those two are equal, we need to fool POPSTACK_TO() */
1384 if (PL_mainstack == PL_curstack)
1385 SU_UPLEVEL_SAVE(mainstack, si->si_stack);
1387 sud->old_mainstack = NULL;
1388 PL_curstack = si->si_stack;
1390 renamed = su_cv_clone(callback, CvGV(target));
1391 sud->renamed = renamed;
1394 /* Both SP and old_stack_sp point just before the CV. */
1395 Copy(old_stack_sp + 2, SP + 1, args, SV *);
1397 PUSHs((SV *) renamed);
1400 Zero(&sub_op, 1, UNOP);
1401 sub_op.op_type = OP_ENTERSUB;
1402 sub_op.op_next = NULL;
1403 sub_op.op_flags = OP_GIMME_REVERSE(gimme) | OPf_STACKED;
1405 sub_op.op_flags |= OPpENTERSUB_DB;
1407 SU_UPLEVEL_SAVE(op, (OP *) &sub_op);
1409 #if SU_UPLEVEL_HIJACKS_RUNOPS
1410 sud->old_runops = PL_runops;
1413 sud->old_catch = CATCH_GET;
1416 if ((PL_op = PL_ppaddr[OP_ENTERSUB](aTHX))) {
1417 PERL_CONTEXT *sub_cx = cxstack + cxstack_ix;
1419 /* If pp_entersub() returns a non-null OP, it means that the callback is not
1422 sud->callback = MUTABLE_CV(SvREFCNT_inc(callback));
1423 CvDEPTH(callback)++;
1425 if (CxHASARGS(cx) && cx->blk_sub.argarray) {
1426 /* The call to pp_entersub() has saved the current @_ (in XS terms,
1427 * GvAV(PL_defgv)) in the savearray member, and has created a new argarray
1428 * with what we put on the stack. But we want to fake up the same arguments
1429 * as the ones in use at the context we uplevel to, so we replace the
1430 * argarray with an unreal copy of the original @_. */
1434 av_extend(av, AvMAX(cx->blk_sub.argarray));
1435 AvFILLp(av) = AvFILLp(cx->blk_sub.argarray);
1436 Copy(AvARRAY(cx->blk_sub.argarray), AvARRAY(av), AvFILLp(av) + 1, SV *);
1437 sub_cx->blk_sub.argarray = av;
1439 SvREFCNT_inc_simple_void(sub_cx->blk_sub.argarray);
1442 if (su_uplevel_goto_static(CvROOT(renamed))) {
1443 #if SU_UPLEVEL_HIJACKS_RUNOPS
1444 if (PL_runops != PL_runops_std) {
1445 if (PL_runops == PL_runops_dbg) {
1447 croak("uplevel() can't execute code that calls goto when debugging flags are set");
1448 } else if (PL_runops != su_uplevel_goto_runops)
1449 croak("uplevel() can't execute code that calls goto with a custom runloop");
1452 PL_runops = su_uplevel_goto_runops;
1453 #else /* SU_UPLEVEL_HIJACKS_RUNOPS */
1454 croak("uplevel() can't execute code that calls goto before perl 5.8");
1455 #endif /* !SU_UPLEVEL_HIJACKS_RUNOPS */
1463 ret = PL_stack_sp - (PL_stack_base + new_mark);
1465 AV *old_stack = sud->old_curstackinfo->si_stack;
1467 if (old_mark + ret > AvMAX(old_stack)) {
1468 /* Specialized EXTEND(old_sp, ret) */
1469 av_extend(old_stack, old_mark + ret + 1);
1470 old_stack_sp = AvARRAY(old_stack) + old_mark;
1473 Copy(PL_stack_sp - ret + 1, old_stack_sp + 1, ret, SV *);
1475 AvFILLp(old_stack) += ret;
1483 /* --- Interpreter setup/teardown ------------------------------------------ */
1485 STATIC void su_teardown(pTHX_ void *param) {
1489 cur = MY_CXT.uplevel_storage.root;
1491 su_uplevel_ud *prev;
1495 su_uplevel_ud_delete(prev);
1502 STATIC void su_setup(pTHX) {
1503 #define su_setup() su_setup(aTHX)
1506 MY_CXT.stack_placeholder = NULL;
1508 /* NewOp() calls calloc() which just zeroes the memory with memset(). */
1509 Zero(&(MY_CXT.unwind_storage.return_op), 1, LISTOP);
1510 MY_CXT.unwind_storage.return_op.op_type = OP_RETURN;
1511 MY_CXT.unwind_storage.return_op.op_ppaddr = PL_ppaddr[OP_RETURN];
1513 Zero(&(MY_CXT.unwind_storage.proxy_op), 1, OP);
1514 MY_CXT.unwind_storage.proxy_op.op_type = OP_STUB;
1515 MY_CXT.unwind_storage.proxy_op.op_ppaddr = NULL;
1517 MY_CXT.uplevel_storage.top = NULL;
1518 MY_CXT.uplevel_storage.root = NULL;
1519 MY_CXT.uplevel_storage.count = 0;
1521 call_atexit(su_teardown, NULL);
1526 /* --- XS ------------------------------------------------------------------ */
1528 #if SU_HAS_PERL(5, 8, 9)
1529 # define SU_SKIP_DB_MAX 2
1531 # define SU_SKIP_DB_MAX 3
1534 /* Skip context sequences of 1 to SU_SKIP_DB_MAX (included) block contexts
1535 * followed by a DB sub */
1537 #define SU_SKIP_DB(C) \
1540 PERL_CONTEXT *base = cxstack; \
1541 PERL_CONTEXT *cx = base + (C); \
1542 while (cx >= base && (C) > skipped && CxTYPE(cx) == CXt_BLOCK) \
1544 if (cx >= base && (C) > skipped) { \
1545 switch (CxTYPE(cx)) { \
1547 if (skipped <= SU_SKIP_DB_MAX && cx->blk_sub.cv == GvCV(PL_DBsub)) \
1548 (C) -= skipped + 1; \
1556 #define SU_GET_CONTEXT(A, B) \
1565 else if (cxix > cxstack_ix) \
1566 cxix = cxstack_ix; \
1569 cxix = cxstack_ix; \
1575 #define SU_GET_LEVEL(A, B) \
1581 level = SvIV(lsv); \
1588 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
1590 XS(XS_Scope__Upper_unwind) {
1599 PERL_UNUSED_VAR(cv); /* -W */
1600 PERL_UNUSED_VAR(ax); /* -Wall */
1602 SU_GET_CONTEXT(0, items - 1);
1604 PERL_CONTEXT *cx = cxstack + cxix;
1605 switch (CxTYPE(cx)) {
1607 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1611 MY_CXT.unwind_storage.cxix = cxix;
1612 MY_CXT.unwind_storage.items = items;
1613 /* pp_entersub will want to sanitize the stack after returning from there
1614 * Screw that, we're insane */
1615 if (GIMME_V == G_SCALAR) {
1616 MY_CXT.unwind_storage.savesp = PL_stack_sp;
1617 /* dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
1618 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
1620 MY_CXT.unwind_storage.savesp = NULL;
1622 SAVEDESTRUCTOR_X(su_unwind, NULL);
1627 } while (--cxix >= 0);
1628 croak("Can't return outside a subroutine");
1631 MODULE = Scope::Upper PACKAGE = Scope::Upper
1639 stash = gv_stashpv(__PACKAGE__, 1);
1640 newCONSTSUB(stash, "TOP", newSViv(0));
1641 newCONSTSUB(stash, "SU_THREADSAFE", newSVuv(SU_THREADSAFE));
1643 newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
1656 MY_CXT.uplevel_storage.top = NULL;
1657 MY_CXT.uplevel_storage.root = NULL;
1658 MY_CXT.uplevel_storage.count = 0;
1662 #endif /* SU_THREADSAFE */
1668 I32 cxix = cxstack_ix;
1682 SU_GET_CONTEXT(0, 0);
1697 SU_GET_CONTEXT(0, 0);
1699 for (; cxix >= 0; --cxix) {
1700 PERL_CONTEXT *cx = cxstack + cxix;
1701 switch (CxTYPE(cx)) {
1705 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1719 SU_GET_CONTEXT(0, 0);
1721 for (; cxix >= 0; --cxix) {
1722 PERL_CONTEXT *cx = cxstack + cxix;
1723 switch (CxTYPE(cx)) {
1765 for (cxix = cxstack_ix; cxix > 0; --cxix) {
1766 PERL_CONTEXT *cx = cxstack + cxix;
1767 switch (CxTYPE(cx)) {
1769 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1789 SU_GET_CONTEXT(0, 0);
1792 PERL_CONTEXT *cx = cxstack + cxix--;
1793 switch (CxTYPE(cx)) {
1797 I32 gimme = cx->blk_gimme;
1799 case G_VOID: XSRETURN_UNDEF; break;
1800 case G_SCALAR: XSRETURN_NO; break;
1801 case G_ARRAY: XSRETURN_YES; break;
1816 SU_GET_CONTEXT(1, 1);
1817 Newx(ud, 1, su_ud_reap);
1818 SU_UD_ORIGIN(ud) = NULL;
1819 SU_UD_HANDLER(ud) = su_reap;
1820 ud->cb = newSVsv(hook);
1821 su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE);
1824 localize(SV *sv, SV *val, ...)
1831 SU_GET_CONTEXT(2, 2);
1832 Newx(ud, 1, su_ud_localize);
1833 SU_UD_ORIGIN(ud) = NULL;
1834 SU_UD_HANDLER(ud) = su_localize;
1835 size = su_ud_localize_init(ud, sv, val, NULL);
1836 su_init(ud, cxix, size);
1839 localize_elem(SV *sv, SV *elem, SV *val, ...)
1846 if (SvTYPE(sv) >= SVt_PVGV)
1847 croak("Can't infer the element localization type from a glob and the value");
1848 SU_GET_CONTEXT(3, 3);
1849 Newx(ud, 1, su_ud_localize);
1850 SU_UD_ORIGIN(ud) = NULL;
1851 SU_UD_HANDLER(ud) = su_localize;
1852 size = su_ud_localize_init(ud, sv, val, elem);
1853 if (ud->type != SVt_PVAV && ud->type != SVt_PVHV) {
1854 SU_UD_LOCALIZE_FREE(ud);
1855 croak("Can't localize an element of something that isn't an array or a hash");
1857 su_init(ud, cxix, size);
1860 localize_delete(SV *sv, SV *elem, ...)
1867 SU_GET_CONTEXT(2, 2);
1868 Newx(ud, 1, su_ud_localize);
1869 SU_UD_ORIGIN(ud) = NULL;
1870 SU_UD_HANDLER(ud) = su_localize;
1871 size = su_ud_localize_init(ud, sv, NULL, elem);
1872 su_init(ud, cxix, size);
1875 uplevel(SV *code, ...)
1878 I32 cxix, ret, args = 0;
1882 if (SvTYPE(code) < SVt_PVCV)
1883 croak("First argument to uplevel must be a code reference");
1884 SU_GET_CONTEXT(1, items - 1);
1886 PERL_CONTEXT *cx = cxstack + cxix;
1887 switch (CxTYPE(cx)) {
1889 croak("Can't uplevel to an eval frame");
1891 croak("Can't uplevel to a format frame");
1893 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1899 /* su_uplevel() takes care of extending the stack if needed. */
1900 ret = su_uplevel((CV *) code, cxix, args);
1905 } while (--cxix >= 0);
1906 croak("Can't uplevel outside a subroutine");