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 ---------------------------- */
207 PERL_SI *old_curstackinfo;
212 runops_proc_t old_runops;
217 STATIC su_uplevel_ud *su_uplevel_ud_new(pTHX) {
218 #define su_uplevel_ud_new() su_uplevel_ud_new(aTHX)
222 Newx(sud, 1, su_uplevel_ud);
225 Newx(si, 1, PERL_SI);
226 si->si_stack = newAV();
227 AvREAL_off(si->si_stack);
228 si->si_cxstack = NULL;
236 STATIC void su_uplevel_ud_delete(pTHX_ su_uplevel_ud *sud) {
237 #define su_uplevel_ud_delete(S) su_uplevel_ud_delete(aTHX_ (S))
238 PERL_SI *si = sud->si;
240 Safefree(si->si_cxstack);
241 SvREFCNT_dec(si->si_stack);
252 } su_uplevel_storage;
254 #ifndef SU_UPLEVEL_STORAGE_SIZE
255 # define SU_UPLEVEL_STORAGE_SIZE 4
258 /* --- Global data --------------------------------------------------------- */
260 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
263 char *stack_placeholder;
264 su_unwind_storage unwind_storage;
265 su_uplevel_storage uplevel_storage;
270 /* --- Stack manipulations ------------------------------------------------- */
272 #define SU_SAVE_PLACEHOLDER() save_pptr(&MY_CXT.stack_placeholder)
274 #define SU_SAVE_DESTRUCTOR_SIZE 3
275 #define SU_SAVE_PLACEHOLDER_SIZE 3
277 #define SU_SAVE_SCALAR_SIZE 3
279 #define SU_SAVE_ARY_SIZE 3
280 #define SU_SAVE_AELEM_SIZE 4
282 # define SU_SAVE_ADELETE_SIZE 3
284 # define SU_SAVE_ADELETE_SIZE SU_SAVE_DESTRUCTOR_SIZE
286 #if SU_SAVE_AELEM_SIZE < SU_SAVE_ADELETE_SIZE
287 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_ADELETE_SIZE
289 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_AELEM_SIZE
292 #define SU_SAVE_HASH_SIZE 3
293 #define SU_SAVE_HELEM_SIZE 4
294 #define SU_SAVE_HDELETE_SIZE 4
295 #if SU_SAVE_HELEM_SIZE < SU_SAVE_HDELETE_SIZE
296 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HDELETE_SIZE
298 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HELEM_SIZE
301 #define SU_SAVE_GVCV_SIZE SU_SAVE_DESTRUCTOR_SIZE
303 #if !SU_HAS_PERL(5, 8, 9)
304 # define SU_SAVE_GP_SIZE 6
305 #elif !SU_HAS_PERL(5, 13, 0) || (SU_RELEASE && SU_HAS_PERL_EXACT(5, 13, 0))
306 # define SU_SAVE_GP_SIZE 3
307 #elif !SU_HAS_PERL(5, 13, 8)
308 # define SU_SAVE_GP_SIZE 4
310 # define SU_SAVE_GP_SIZE 3
313 #ifndef SvCANEXISTDELETE
314 # define SvCANEXISTDELETE(sv) \
316 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
317 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
318 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
319 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
324 /* ... Saving array elements ............................................... */
326 STATIC I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
327 #define su_av_key2idx(A, K) su_av_key2idx(aTHX_ (A), (K))
333 /* Added by MJD in perl-5.8.1 with 6f12eb6d2a1dfaf441504d869b27d2e40ef4966a */
334 #if SU_HAS_PERL(5, 8, 1)
335 if (SvRMAGICAL(av)) {
336 const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
338 SV * const * const negative_indices_glob =
339 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
340 NEGATIVE_INDICES_VAR, 16, 0);
341 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
347 idx = key + av_len(av) + 1;
361 STATIC void su_adelete(pTHX_ void *ud_) {
362 su_ud_adelete *ud = (su_ud_adelete *) ud_;
364 av_delete(ud->av, ud->idx, G_DISCARD);
365 SvREFCNT_dec(ud->av);
370 STATIC void su_save_adelete(pTHX_ AV *av, I32 idx) {
371 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
374 Newx(ud, 1, su_ud_adelete);
377 SvREFCNT_inc_simple_void(av);
379 SAVEDESTRUCTOR_X(su_adelete, ud);
382 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
384 #endif /* SAVEADELETE */
386 STATIC void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
387 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
394 idx = su_av_key2idx(av, SvIV(key));
396 if (SvCANEXISTDELETE(av))
397 preeminent = av_exists(av, idx);
399 svp = av_fetch(av, idx, 1);
400 if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
403 save_aelem(av, idx, svp);
405 SAVEADELETE(av, idx);
407 if (val) { /* local $x[$idx] = $val; */
408 SvSetMagicSV(*svp, val);
409 } else { /* local $x[$idx]; delete $x[$idx]; */
410 av_delete(av, idx, G_DISCARD);
414 /* ... Saving hash elements ................................................ */
416 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
417 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
424 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
425 preeminent = hv_exists_ent(hv, keysv, 0);
427 he = hv_fetch_ent(hv, keysv, 1, 0);
428 svp = he ? &HeVAL(he) : NULL;
429 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
431 if (HvNAME_get(hv) && isGV(*svp)) {
432 save_gp((GV *) *svp, 0);
437 save_helem(hv, keysv, svp);
440 const char * const key = SvPV_const(keysv, keylen);
441 SAVEDELETE(hv, savepvn(key, keylen),
442 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
445 if (val) { /* local $x{$keysv} = $val; */
446 SvSetMagicSV(*svp, val);
447 } else { /* local $x{$keysv}; delete $x{$keysv}; */
448 (void)hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
452 /* ... Saving code slots from a glob ....................................... */
454 #if !SU_HAS_PERL(5, 10, 0) && !defined(mro_method_changed_in)
455 # define mro_method_changed_in(G) PL_sub_generation++
463 STATIC void su_restore_gvcv(pTHX_ void *ud_) {
464 su_save_gvcv_ud *ud = ud_;
467 GvCV_set(gv, ud->old_cv);
469 mro_method_changed_in(GvSTASH(gv));
474 STATIC void su_save_gvcv(pTHX_ GV *gv) {
475 #define su_save_gvcv(G) su_save_gvcv(aTHX_ (G))
478 Newx(ud, 1, su_save_gvcv_ud);
480 ud->old_cv = GvCV(gv);
484 mro_method_changed_in(GvSTASH(gv));
486 SAVEDESTRUCTOR_X(su_restore_gvcv, ud);
489 /* --- Actions ------------------------------------------------------------- */
495 void (*handler)(pTHX_ void *);
498 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
499 #define SU_UD_PAD(U) (((su_ud_common *) (U))->pad)
500 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
501 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
503 #define SU_UD_FREE(U) STMT_START { \
504 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
508 /* ... Reap ................................................................ */
515 STATIC void su_call(pTHX_ void *ud_) {
516 su_ud_reap *ud = (su_ud_reap *) ud_;
517 #if SU_HAS_PERL(5, 9, 5)
518 PERL_CONTEXT saved_cx;
525 PerlIO_printf(Perl_debug_log,
526 "%p: @@@ call\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
527 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
536 /* If the recently popped context isn't saved there, it will be overwritten by
537 * the sub scope from call_sv, although it's still needed in our caller. */
539 #if SU_HAS_PERL(5, 9, 5)
540 if (cxstack_ix < cxstack_max)
541 cxix = cxstack_ix + 1;
543 cxix = Perl_cxinc(aTHX);
544 saved_cx = cxstack[cxix];
547 call_sv(ud->cb, G_VOID);
549 #if SU_HAS_PERL(5, 9, 5)
550 cxstack[cxix] = saved_cx;
558 SvREFCNT_dec(ud->cb);
562 STATIC void su_reap(pTHX_ void *ud) {
563 #define su_reap(U) su_reap(aTHX_ (U))
565 PerlIO_printf(Perl_debug_log,
566 "%p: === reap\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
567 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
570 SAVEDESTRUCTOR_X(su_call, ud);
573 /* ... Localize & localize array/hash element .............................. */
583 #define SU_UD_LOCALIZE_FREE(U) STMT_START { \
584 SvREFCNT_dec((U)->elem); \
585 SvREFCNT_dec((U)->val); \
586 SvREFCNT_dec((U)->sv); \
590 STATIC I32 su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) {
591 #define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E))
596 SvREFCNT_inc_simple_void(sv);
598 if (SvTYPE(sv) >= SVt_PVGV) {
599 if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */
601 } else { /* local *x = \$val; */
602 t = SvTYPE(SvRV(val));
605 } else if (SvROK(sv)) {
606 croak("Invalid %s reference as the localization target",
607 sv_reftype(SvRV(sv), 0));
610 const char *p = SvPV_const(sv, len), *s;
611 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
617 case '$': t = SVt_PV; break;
618 case '@': t = SVt_PVAV; break;
619 case '%': t = SVt_PVHV; break;
620 case '&': t = SVt_PVCV; break;
621 case '*': t = SVt_PVGV; break;
626 } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
627 if (SvROK(val) && !sv_isobject(val)) {
628 t = SvTYPE(SvRV(val));
640 size = elem ? SU_SAVE_AELEM_OR_ADELETE_SIZE
645 size = elem ? SU_SAVE_HELEM_OR_HDELETE_SIZE
650 size = SU_SAVE_GP_SIZE;
654 size = SU_SAVE_GVCV_SIZE;
658 size = SU_SAVE_SCALAR_SIZE;
661 /* When deref is set, val isn't NULL */
664 ud->val = val ? newSVsv(deref ? SvRV(val) : val) : NULL;
665 ud->elem = SvREFCNT_inc(elem);
671 STATIC void su_localize(pTHX_ void *ud_) {
672 #define su_localize(U) su_localize(aTHX_ (U))
673 su_ud_localize *ud = (su_ud_localize *) ud_;
680 if (SvTYPE(sv) >= SVt_PVGV) {
684 gv = gv_fetchsv(sv, GV_ADDMULTI, t);
687 const char *name = SvPV_const(sv, len);
688 gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t);
695 PerlIO_printf(Perl_debug_log, "%p: === localize a %s\n",ud, sv_reftype(z, 0));
696 PerlIO_printf(Perl_debug_log,
697 "%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
698 ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
702 /* Inspired from Alias.pm */
706 su_save_aelem(GvAV(gv), elem, val);
713 su_save_helem(GvHV(gv), elem, val);
719 save_gp(gv, 1); /* hide previous entry in symtab */
725 gv = (GV *) save_scalar(gv);
730 SvSetMagicSV((SV *) gv, val);
733 SU_UD_LOCALIZE_FREE(ud);
736 /* --- Pop a context back -------------------------------------------------- */
740 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
742 # define SU_CXNAME "XXX"
746 STATIC void su_pop(pTHX_ void *ud) {
747 #define su_pop(U) su_pop(aTHX_ (U))
748 I32 depth, base, mark, *origin;
749 depth = SU_UD_DEPTH(ud);
752 PerlIO_printf(Perl_debug_log,
754 "%p: leave scope at depth=%2d scope_ix=%2d cur_top=%2d cur_base=%2d\n",
756 ud, depth, PL_scopestack_ix,PL_savestack_ix,PL_scopestack[PL_scopestack_ix])
759 origin = SU_UD_ORIGIN(ud);
760 mark = origin[depth];
761 base = origin[depth - 1];
763 SU_D(PerlIO_printf(Perl_debug_log,
764 "%p: original scope was %*c top=%2d base=%2d\n",
765 ud, 24, ' ', mark, base));
768 SU_D(PerlIO_printf(Perl_debug_log, "%p: clear leftovers\n", ud));
769 PL_savestack_ix = mark;
772 PL_savestack_ix = base;
774 SU_UD_DEPTH(ud) = --depth;
779 if ((pad = SU_UD_PAD(ud))) {
782 SU_D(PerlIO_printf(Perl_debug_log,
783 "%p: push a pad slot at depth=%2d scope_ix=%2d save_ix=%2d\n",
784 ud, depth, PL_scopestack_ix, PL_savestack_ix));
785 SU_SAVE_PLACEHOLDER();
789 SU_D(PerlIO_printf(Perl_debug_log,
790 "%p: push destructor at depth=%2d scope_ix=%2d save_ix=%2d\n",
791 ud, depth, PL_scopestack_ix, PL_savestack_ix));
792 SAVEDESTRUCTOR_X(su_pop, ud);
794 SU_UD_HANDLER(ud)(aTHX_ ud);
797 SU_D(PerlIO_printf(Perl_debug_log,
798 "%p: --- end pop: cur_top=%2d == cur_base=%2d\n",
799 ud, PL_savestack_ix, PL_scopestack[PL_scopestack_ix]));
802 /* --- Initialize the stack and the action userdata ------------------------ */
804 STATIC I32 su_init(pTHX_ void *ud, I32 cxix, I32 size) {
805 #define su_init(U, C, S) su_init(aTHX_ (U), (C), (S))
806 I32 i, depth = 1, pad, offset, *origin;
808 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
810 if (size <= SU_SAVE_DESTRUCTOR_SIZE)
813 I32 extra = size - SU_SAVE_DESTRUCTOR_SIZE;
814 pad = extra / SU_SAVE_PLACEHOLDER_SIZE;
815 if (extra % SU_SAVE_PLACEHOLDER_SIZE)
818 offset = SU_SAVE_DESTRUCTOR_SIZE + SU_SAVE_PLACEHOLDER_SIZE * pad;
820 SU_D(PerlIO_printf(Perl_debug_log, "%p: size=%d pad=%d offset=%d\n",
821 ud, size, pad, offset));
823 for (i = cxstack_ix; i > cxix; --i) {
824 PERL_CONTEXT *cx = cxstack + i;
825 switch (CxTYPE(cx)) {
826 #if SU_HAS_PERL(5, 10, 0)
828 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is block\n", ud, i));
829 /* Given and when blocks are actually followed by a simple block, so skip
831 if (cxix > 0) { /* Implies i > 0 */
832 PERL_CONTEXT *next = cx - 1;
833 if (CxTYPE(next) == CXt_GIVEN || CxTYPE(next) == CXt_WHEN)
839 #if SU_HAS_PERL(5, 11, 0)
842 case CXt_LOOP_LAZYSV:
843 case CXt_LOOP_LAZYIV:
847 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
851 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is other\n", ud, i));
856 SU_D(PerlIO_printf(Perl_debug_log, "%p: going down to depth %d\n", ud, depth));
858 Newx(origin, depth + 1, I32);
859 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
860 PL_scopestack[PL_scopestack_ix - depth] += size;
861 for (i = depth - 1; i >= 1; --i) {
862 I32 j = PL_scopestack_ix - i;
863 origin[depth - i] = PL_scopestack[j];
864 PL_scopestack[j] += offset;
866 origin[depth] = PL_savestack_ix;
868 SU_UD_ORIGIN(ud) = origin;
869 SU_UD_DEPTH(ud) = depth;
872 /* Make sure the first destructor fires by pushing enough fake slots on the
874 if (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
875 <= PL_scopestack[PL_scopestack_ix - 1]) {
878 SU_D(PerlIO_printf(Perl_debug_log,
879 "%p: push a fake slot at scope_ix=%2d save_ix=%2d\n",
880 ud, PL_scopestack_ix, PL_savestack_ix));
881 SU_SAVE_PLACEHOLDER();
882 } while (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
883 <= PL_scopestack[PL_scopestack_ix - 1]);
885 SU_D(PerlIO_printf(Perl_debug_log,
886 "%p: push first destructor at scope_ix=%2d save_ix=%2d\n",
887 ud, PL_scopestack_ix, PL_savestack_ix));
888 SAVEDESTRUCTOR_X(su_pop, ud);
891 for (i = 0; i <= depth; ++i) {
892 I32 j = PL_scopestack_ix - i;
893 PerlIO_printf(Perl_debug_log,
894 "%p: depth=%2d scope_ix=%2d saved_floor=%2d new_floor=%2d\n",
895 ud, i, j, origin[depth - i],
896 i == 0 ? PL_savestack_ix : PL_scopestack[j]);
903 /* --- Unwind stack -------------------------------------------------------- */
905 STATIC void su_unwind(pTHX_ void *ud_) {
907 I32 cxix = MY_CXT.unwind_storage.cxix;
908 I32 items = MY_CXT.unwind_storage.items - 1;
909 SV **savesp = MY_CXT.unwind_storage.savesp;
912 PERL_UNUSED_VAR(ud_);
915 PL_stack_sp = savesp;
917 if (cxstack_ix > cxix)
924 mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
925 *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
929 PerlIO_printf(Perl_debug_log,
930 "%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
932 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
933 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
936 PL_op = (OP *) &(MY_CXT.unwind_storage.return_op);
937 PL_op = PL_op->op_ppaddr(aTHX);
939 *PL_markstack_ptr = mark;
941 MY_CXT.unwind_storage.proxy_op.op_next = PL_op;
942 PL_op = &(MY_CXT.unwind_storage.proxy_op);
945 /* --- Uplevel ------------------------------------------------------------- */
947 #ifndef OP_GIMME_REVERSE
948 STATIC U8 su_op_gimme_reverse(U8 gimme) {
951 return OPf_WANT_VOID;
953 return OPf_WANT_LIST;
958 return OPf_WANT_SCALAR;
960 #define OP_GIMME_REVERSE(G) su_op_gimme_reverse(G)
963 #define SU_UPLEVEL_SAVE(f, t) STMT_START { sud->old_##f = PL_##f; PL_##f = (t); } STMT_END
964 #define SU_UPLEVEL_RESTORE(f) STMT_START { PL_##f = sud->old_##f; } STMT_END
966 STATIC su_uplevel_ud *su_uplevel_storage_new(pTHX) {
967 #define su_uplevel_storage_new() su_uplevel_storage_new(aTHX)
971 sud = MY_CXT.uplevel_storage.root;
973 MY_CXT.uplevel_storage.root = sud->next;
974 MY_CXT.uplevel_storage.count--;
976 sud = su_uplevel_ud_new();
979 sud->next = MY_CXT.uplevel_storage.top;
980 MY_CXT.uplevel_storage.top = sud;
985 STATIC void su_uplevel_storage_delete(pTHX_ su_uplevel_ud *sud) {
986 #define su_uplevel_storage_delete(S) su_uplevel_storage_delete(aTHX_ (S))
989 MY_CXT.uplevel_storage.top = sud->next;
991 if (MY_CXT.uplevel_storage.count >= SU_UPLEVEL_STORAGE_SIZE) {
992 su_uplevel_ud_delete(sud);
994 sud->next = MY_CXT.uplevel_storage.root;
995 MY_CXT.uplevel_storage.root = sud;
996 MY_CXT.uplevel_storage.count++;
1000 STATIC int su_uplevel_goto_static(const OP *o) {
1001 for (; o; o = o->op_sibling) {
1002 /* goto ops are unops with kids. */
1003 if (!(o->op_flags & OPf_KIDS))
1006 switch (o->op_type) {
1009 /* Don't care about gotos inside eval, as they are forbidden at run time. */
1014 if (su_uplevel_goto_static(cUNOPo->op_first))
1023 STATIC int su_uplevel_goto_runops(pTHX) {
1024 #define su_uplevel_goto_runops() su_uplevel_goto_runops(aTHX)
1030 if (op->op_type == OP_GOTO) {
1031 AV *argarray = NULL;
1034 for (cxix = cxstack_ix; cxix >= 0; --cxix) {
1035 const PERL_CONTEXT *cx = cxstack + cxix;
1037 switch (CxTYPE(cx)) {
1039 if (CxHASARGS(cx)) {
1040 argarray = cx->blk_sub.argarray;
1056 if (MY_CXT.uplevel_storage.top->cxix == cxix) {
1057 AV *args = GvAV(PL_defgv);
1058 I32 items = AvFILLp(args);
1060 av_extend(argarray, items);
1061 Copy(AvARRAY(args), AvARRAY(argarray), items + 1, SV *);
1062 AvFILLp(argarray) = items;
1067 PL_op = op = op->op_ppaddr(aTHX);
1069 #if !SU_HAS_PERL(5, 13, 0)
1079 #define su_at_underscore(C) AvARRAY(AvARRAY(CvPADLIST(C))[CvDEPTH(C)])[0]
1081 STATIC void su_uplevel_restore(pTHX_ void *sus_) {
1082 su_uplevel_ud *sud = sus_;
1083 PERL_SI *cur = sud->old_curstackinfo;
1084 PERL_SI *si = sud->si;
1086 if (PL_runops == su_uplevel_goto_runops)
1087 PL_runops = sud->old_runops;
1089 if (sud->callback) {
1090 PERL_CONTEXT *cx = cxstack + sud->cxix;
1091 AV *argarray = MUTABLE_AV(su_at_underscore(sud->callback));
1093 /* We have to fix the pad entry for @_ in the original callback because it
1094 * may have been reified. */
1095 if (AvREAL(argarray)) {
1096 const I32 fill = AvFILLp(argarray);
1097 SvREFCNT_dec(argarray);
1099 AvREAL_off(argarray);
1100 AvREIFY_on(argarray);
1101 av_extend(argarray, fill);
1102 su_at_underscore(sud->callback) = MUTABLE_SV(argarray);
1104 CLEAR_ARGARRAY(argarray);
1107 /* If the old cv member is our renamed CV, it means that this place has been
1108 * reached without a goto() happening, and the old argarray member is
1109 * actually our fake argarray. Destroy it properly in that case. */
1110 if (cx->blk_sub.cv == sud->renamed) {
1111 SvREFCNT_dec(cx->blk_sub.argarray);
1112 cx->blk_sub.argarray = argarray;
1115 CvDEPTH(sud->callback)--;
1116 SvREFCNT_dec(sud->callback);
1119 /* Free the renamed CV. We must do it ourselves so that we can force the
1120 * depth to be 0, or perl would complain about it being "still in use".
1121 * But we *know* that it cannot be so. */
1123 CvDEPTH(sud->renamed) = 0;
1124 CvPADLIST(sud->renamed) = NULL;
1125 SvREFCNT_dec(sud->renamed);
1128 CATCH_SET(sud->old_catch);
1130 SU_UPLEVEL_RESTORE(op);
1132 /* stack_grow() wants PL_curstack so restore the old stack first */
1133 if (PL_curstackinfo == si) {
1134 PL_curstack = cur->si_stack;
1135 if (sud->old_mainstack)
1136 SU_UPLEVEL_RESTORE(mainstack);
1137 SU_UPLEVEL_RESTORE(curstackinfo);
1140 CV *target = sud->target;
1143 /* When we die, the depth of the target CV is not updated because of the
1144 * stack switcheroo. So we have to look at all the frames between the
1145 * uplevel call and the catch block to count how many call frames to the
1146 * target CV were skipped. */
1147 for (i = cur->si_cxix; i > sud->cxix; i--) {
1148 register const PERL_CONTEXT *cx = cxstack + i;
1150 if (CxTYPE(cx) == CXt_SUB) {
1151 if (cx->blk_sub.cv == target)
1156 /* If we died, the replacement stack was already unwinded to the first
1157 * eval frame, and all the contexts down there were popped. We don't have
1158 * to pop manually any context of the original stack, because they must
1159 * have been in the replacement stack as well (since the second was copied
1160 * from the first). Thus we only have to make sure the original stack index
1161 * points to the context just below the first eval scope under the target
1163 for (; i >= 0; i--) {
1164 register const PERL_CONTEXT *cx = cxstack + i;
1166 switch (CxTYPE(cx)) {
1168 if (cx->blk_sub.cv == target)
1180 CvDEPTH(target) = sud->target_depth - levels;
1181 PL_curstackinfo->si_cxix = i - 1;
1183 #if !SU_HAS_PERL(5, 13, 1)
1184 /* Since $@ was maybe localized between the target frame and the uplevel
1185 * call, we forcefully flush the save stack to get rid of it and then
1186 * reset $@ to its proper value. Note that the the call to
1187 * su_uplevel_restore() must happen before the "reset $@" item of the save
1188 * stack is processed, as uplevel was called after the localization.
1189 * Andrew's changes to how $@ was handled, which were mainly integrated
1190 * between perl 5.13.0 and 5.13.1, fixed this. */
1191 if (ERRSV && SvTRUE(ERRSV)) {
1192 register const PERL_CONTEXT *cx = cxstack + i; /* This is the eval scope */
1193 SV *errsv = SvREFCNT_inc(ERRSV);
1194 PL_scopestack_ix = cx->blk_oldscopesp;
1195 leave_scope(PL_scopestack[PL_scopestack_ix]);
1196 sv_setsv(ERRSV, errsv);
1197 SvREFCNT_dec(errsv);
1203 SU_UPLEVEL_RESTORE(curcop);
1205 SvREFCNT_dec(sud->target);
1207 PL_stack_base = AvARRAY(cur->si_stack);
1208 PL_stack_sp = PL_stack_base + AvFILLp(cur->si_stack);
1209 PL_stack_max = PL_stack_base + AvMAX(cur->si_stack);
1211 /* When an exception is thrown from the uplevel'd subroutine,
1212 * su_uplevel_restore() may be called by the LEAVE in die_unwind() (renamed
1213 * die_where() in more recent perls), which has the sad habit of keeping a
1214 * pointer to the current context frame across this call. This means that we
1215 * can't free the temporary context stack we used for the uplevel call right
1216 * now, or that pointer upwards would point to garbage. */
1217 #if SU_HAS_PERL(5, 13, 7)
1218 /* This issue has been fixed in perl with commit 8f89e5a9, which was made
1219 * public in perl 5.13.7. */
1220 su_uplevel_storage_delete(sud);
1222 /* Otherwise, we just enqueue it back in the global storage list. */
1226 MY_CXT.uplevel_storage.top = sud->next;
1227 sud->next = MY_CXT.uplevel_storage.root;
1228 MY_CXT.uplevel_storage.root = sud;
1229 MY_CXT.uplevel_storage.count++;
1236 STATIC CV *su_cv_clone(pTHX_ CV *proto, GV *gv) {
1237 #define su_cv_clone(P, G) su_cv_clone(aTHX_ (P), (G))
1241 cv = MUTABLE_CV(newSV_type(SvTYPE(proto)));
1243 CvFLAGS(cv) = CvFLAGS(proto);
1245 CvFLAGS(cv) &= ~CVf_CVGV_RC;
1247 CvDEPTH(cv) = CvDEPTH(proto);
1249 CvFILE(cv) = CvISXSUB(proto) ? CvFILE(proto) : savepv(CvFILE(proto));
1251 CvFILE(cv) = CvFILE(proto);
1255 CvSTASH_set(cv, CvSTASH(proto));
1256 /* Commit 4c74a7df, publicized with perl 5.13.3, began to add backrefs to
1257 * stashes. CvSTASH_set() started to do it as well with commit c68d95645
1258 * (which was part of perl 5.13.7). */
1259 #if SU_HAS_PERL(5, 13, 3) && !SU_HAS_PERL(5, 13, 7)
1261 Perl_sv_add_backref(aTHX_ CvSTASH(proto), MUTABLE_SV(cv));
1264 if (CvISXSUB(proto)) {
1265 CvXSUB(cv) = CvXSUB(proto);
1266 CvXSUBANY(cv) = CvXSUBANY(proto);
1269 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
1271 CvSTART(cv) = CvSTART(proto);
1273 CvOUTSIDE(cv) = CvOUTSIDE(proto);
1274 #ifdef CVf_WEAKOUTSIDE
1275 if (!(CvFLAGS(proto) & CVf_WEAKOUTSIDE))
1277 SvREFCNT_inc_simple_void(CvOUTSIDE(cv));
1278 CvPADLIST(cv) = CvPADLIST(proto);
1279 #ifdef CvOUTSIDE_SEQ
1280 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
1284 sv_setpvn(MUTABLE_SV(cv), SvPVX_const(proto), SvCUR(proto));
1294 STATIC I32 su_uplevel(pTHX_ CV *callback, I32 cxix, I32 args) {
1295 #define su_uplevel(C, I, A) su_uplevel(aTHX_ (C), (I), (A))
1297 const PERL_CONTEXT *cx = cxstack + cxix;
1299 PERL_SI *cur = PL_curstackinfo;
1305 I32 old_mark, new_mark;
1312 /* Make PL_stack_sp point just before the CV. */
1313 PL_stack_sp -= args + 1;
1314 old_mark = AvFILLp(PL_curstack) = PL_stack_sp - PL_stack_base;
1317 sud = su_uplevel_storage_new();
1321 sud->callback = NULL;
1322 sud->renamed = NULL;
1323 SAVEDESTRUCTOR_X(su_uplevel_restore, sud);
1327 si->si_type = cur->si_type;
1329 si->si_prev = cur->si_prev;
1331 si->si_markoff = cx->blk_oldmarksp;
1334 /* Allocate enough space for all the elements of the original stack up to the
1335 * target context, plus the forthcoming arguments. */
1336 new_mark = cx->blk_oldsp;
1337 av_extend(si->si_stack, new_mark + 1 + args + 1);
1338 Copy(PL_curstack, AvARRAY(si->si_stack), new_mark + 1, SV *);
1339 AvFILLp(si->si_stack) = new_mark;
1340 SU_POISON(AvARRAY(si->si_stack) + new_mark + 1, args + 1, SV *);
1342 /* Specialized SWITCHSTACK() */
1343 PL_stack_base = AvARRAY(si->si_stack);
1344 old_stack_sp = PL_stack_sp;
1345 PL_stack_sp = PL_stack_base + AvFILLp(si->si_stack);
1346 PL_stack_max = PL_stack_base + AvMAX(si->si_stack);
1349 /* Copy the context stack up to the context just below the target. */
1350 si->si_cxix = (cxix < 0) ? -1 : (cxix - 1);
1351 if (si->si_cxmax < cxix) {
1352 /* The max size must be at least two so that GROW(max) = (max*3)/2 > max */
1353 si->si_cxmax = (cxix < 4) ? 4 : cxix;
1354 Renew(si->si_cxstack, si->si_cxmax + 1, PERL_CONTEXT);
1356 Copy(cur->si_cxstack, si->si_cxstack, cxix, PERL_CONTEXT);
1357 SU_POISON(si->si_cxstack + cxix, si->si_cxmax + 1 - cxix, PERL_CONTEXT);
1359 target = cx->blk_sub.cv;
1360 sud->target = (CV *) SvREFCNT_inc(target);
1361 sud->target_depth = CvDEPTH(target);
1363 /* blk_oldcop is essentially needed for caller() and stack traces. It has no
1364 * run-time implication, since PL_curcop will be overwritten as soon as we
1365 * enter a sub (a sub starts by a nextstate/dbstate). Hence it's safe to just
1366 * make it point to the blk_oldcop for the target frame, so that caller()
1367 * reports the right file name, line number and lexical hints. */
1368 SU_UPLEVEL_SAVE(curcop, cx->blk_oldcop);
1369 /* Don't reset PL_markstack_ptr, or we would overwrite the mark stack below
1370 * this point. Don't reset PL_curpm either, we want the most recent matches. */
1372 SU_UPLEVEL_SAVE(curstackinfo, si);
1373 /* If those two are equal, we need to fool POPSTACK_TO() */
1374 if (PL_mainstack == PL_curstack)
1375 SU_UPLEVEL_SAVE(mainstack, si->si_stack);
1377 sud->old_mainstack = NULL;
1378 PL_curstack = si->si_stack;
1380 renamed = su_cv_clone(callback, CvGV(target));
1381 sud->renamed = renamed;
1384 /* Both SP and old_stack_sp point just before the CV. */
1385 Copy(old_stack_sp + 2, SP + 1, args, SV *);
1387 PUSHs((SV *) renamed);
1390 Zero(&sub_op, 1, UNOP);
1391 sub_op.op_type = OP_ENTERSUB;
1392 sub_op.op_next = NULL;
1393 sub_op.op_flags = OP_GIMME_REVERSE(gimme) | OPf_STACKED;
1395 sub_op.op_flags |= OPpENTERSUB_DB;
1397 SU_UPLEVEL_SAVE(op, (OP *) &sub_op);
1399 sud->old_runops = PL_runops;
1401 sud->old_catch = CATCH_GET;
1404 if ((PL_op = PL_ppaddr[OP_ENTERSUB](aTHX))) {
1405 PERL_CONTEXT *sub_cx = cxstack + cxstack_ix;
1407 /* If pp_entersub() returns a non-null OP, it means that the callback is not
1410 sud->callback = MUTABLE_CV(SvREFCNT_inc(callback));
1411 CvDEPTH(callback)++;
1413 if (CxHASARGS(cx) && cx->blk_sub.argarray) {
1414 /* The call to pp_entersub() has saved the current @_ (in XS terms,
1415 * GvAV(PL_defgv)) in the savearray member, and has created a new argarray
1416 * with what we put on the stack. But we want to fake up the same arguments
1417 * as the ones in use at the context we uplevel to, so we replace the
1418 * argarray with an unreal copy of the original @_. */
1422 av_extend(av, AvMAX(cx->blk_sub.argarray));
1423 AvFILLp(av) = AvFILLp(cx->blk_sub.argarray);
1424 Copy(AvARRAY(cx->blk_sub.argarray), AvARRAY(av), AvFILLp(av) + 1, SV *);
1425 sub_cx->blk_sub.argarray = av;
1427 SvREFCNT_inc_simple_void(sub_cx->blk_sub.argarray);
1430 if (su_uplevel_goto_static(CvROOT(renamed))) {
1431 if (PL_runops != Perl_runops_standard) {
1432 if (PL_runops == Perl_runops_debug) {
1434 croak("uplevel() can't execute code that calls goto when debugging flags are set");
1435 } else if (PL_runops != su_uplevel_goto_runops)
1436 croak("uplevel() can't execute code that calls goto with a custom runloop");
1439 PL_runops = su_uplevel_goto_runops;
1447 ret = PL_stack_sp - (PL_stack_base + new_mark);
1449 AV *old_stack = sud->old_curstackinfo->si_stack;
1451 if (old_mark + ret > AvMAX(old_stack)) {
1452 /* Specialized EXTEND(old_sp, ret) */
1453 av_extend(old_stack, old_mark + ret + 1);
1454 old_stack_sp = AvARRAY(old_stack) + old_mark;
1457 Copy(PL_stack_sp - ret + 1, old_stack_sp + 1, ret, SV *);
1459 AvFILLp(old_stack) += ret;
1467 /* --- Interpreter setup/teardown ------------------------------------------ */
1469 STATIC void su_teardown(pTHX_ void *param) {
1473 cur = MY_CXT.uplevel_storage.root;
1475 su_uplevel_ud *prev;
1479 su_uplevel_ud_delete(prev);
1486 STATIC void su_setup(pTHX) {
1487 #define su_setup() su_setup(aTHX)
1490 MY_CXT.stack_placeholder = NULL;
1492 /* NewOp() calls calloc() which just zeroes the memory with memset(). */
1493 Zero(&(MY_CXT.unwind_storage.return_op), 1, LISTOP);
1494 MY_CXT.unwind_storage.return_op.op_type = OP_RETURN;
1495 MY_CXT.unwind_storage.return_op.op_ppaddr = PL_ppaddr[OP_RETURN];
1497 Zero(&(MY_CXT.unwind_storage.proxy_op), 1, OP);
1498 MY_CXT.unwind_storage.proxy_op.op_type = OP_STUB;
1499 MY_CXT.unwind_storage.proxy_op.op_ppaddr = NULL;
1501 MY_CXT.uplevel_storage.top = NULL;
1502 MY_CXT.uplevel_storage.root = NULL;
1503 MY_CXT.uplevel_storage.count = 0;
1505 call_atexit(su_teardown, NULL);
1510 /* --- XS ------------------------------------------------------------------ */
1512 #if SU_HAS_PERL(5, 8, 9)
1513 # define SU_SKIP_DB_MAX 2
1515 # define SU_SKIP_DB_MAX 3
1518 /* Skip context sequences of 1 to SU_SKIP_DB_MAX (included) block contexts
1519 * followed by a DB sub */
1521 #define SU_SKIP_DB(C) \
1524 PERL_CONTEXT *base = cxstack; \
1525 PERL_CONTEXT *cx = base + (C); \
1526 while (cx >= base && (C) > skipped && CxTYPE(cx) == CXt_BLOCK) \
1528 if (cx >= base && (C) > skipped) { \
1529 switch (CxTYPE(cx)) { \
1531 if (skipped <= SU_SKIP_DB_MAX && cx->blk_sub.cv == GvCV(PL_DBsub)) \
1532 (C) -= skipped + 1; \
1540 #define SU_GET_CONTEXT(A, B) \
1549 else if (cxix > cxstack_ix) \
1550 cxix = cxstack_ix; \
1553 cxix = cxstack_ix; \
1559 #define SU_GET_LEVEL(A, B) \
1565 level = SvIV(lsv); \
1572 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
1574 XS(XS_Scope__Upper_unwind) {
1583 PERL_UNUSED_VAR(cv); /* -W */
1584 PERL_UNUSED_VAR(ax); /* -Wall */
1586 SU_GET_CONTEXT(0, items - 1);
1588 PERL_CONTEXT *cx = cxstack + cxix;
1589 switch (CxTYPE(cx)) {
1591 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1595 MY_CXT.unwind_storage.cxix = cxix;
1596 MY_CXT.unwind_storage.items = items;
1597 /* pp_entersub will want to sanitize the stack after returning from there
1598 * Screw that, we're insane */
1599 if (GIMME_V == G_SCALAR) {
1600 MY_CXT.unwind_storage.savesp = PL_stack_sp;
1601 /* dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
1602 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
1604 MY_CXT.unwind_storage.savesp = NULL;
1606 SAVEDESTRUCTOR_X(su_unwind, NULL);
1611 } while (--cxix >= 0);
1612 croak("Can't return outside a subroutine");
1615 MODULE = Scope::Upper PACKAGE = Scope::Upper
1623 stash = gv_stashpv(__PACKAGE__, 1);
1624 newCONSTSUB(stash, "TOP", newSViv(0));
1625 newCONSTSUB(stash, "SU_THREADSAFE", newSVuv(SU_THREADSAFE));
1627 newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
1640 MY_CXT.uplevel_storage.top = NULL;
1641 MY_CXT.uplevel_storage.root = NULL;
1642 MY_CXT.uplevel_storage.count = 0;
1646 #endif /* SU_THREADSAFE */
1652 I32 cxix = cxstack_ix;
1666 SU_GET_CONTEXT(0, 0);
1681 SU_GET_CONTEXT(0, 0);
1683 for (; cxix >= 0; --cxix) {
1684 PERL_CONTEXT *cx = cxstack + cxix;
1685 switch (CxTYPE(cx)) {
1689 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1703 SU_GET_CONTEXT(0, 0);
1705 for (; cxix >= 0; --cxix) {
1706 PERL_CONTEXT *cx = cxstack + cxix;
1707 switch (CxTYPE(cx)) {
1749 for (cxix = cxstack_ix; cxix > 0; --cxix) {
1750 PERL_CONTEXT *cx = cxstack + cxix;
1751 switch (CxTYPE(cx)) {
1753 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1773 SU_GET_CONTEXT(0, 0);
1776 PERL_CONTEXT *cx = cxstack + cxix--;
1777 switch (CxTYPE(cx)) {
1781 I32 gimme = cx->blk_gimme;
1783 case G_VOID: XSRETURN_UNDEF; break;
1784 case G_SCALAR: XSRETURN_NO; break;
1785 case G_ARRAY: XSRETURN_YES; break;
1800 SU_GET_CONTEXT(1, 1);
1801 Newx(ud, 1, su_ud_reap);
1802 SU_UD_ORIGIN(ud) = NULL;
1803 SU_UD_HANDLER(ud) = su_reap;
1804 ud->cb = newSVsv(hook);
1805 su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE);
1808 localize(SV *sv, SV *val, ...)
1815 SU_GET_CONTEXT(2, 2);
1816 Newx(ud, 1, su_ud_localize);
1817 SU_UD_ORIGIN(ud) = NULL;
1818 SU_UD_HANDLER(ud) = su_localize;
1819 size = su_ud_localize_init(ud, sv, val, NULL);
1820 su_init(ud, cxix, size);
1823 localize_elem(SV *sv, SV *elem, SV *val, ...)
1830 if (SvTYPE(sv) >= SVt_PVGV)
1831 croak("Can't infer the element localization type from a glob and the value");
1832 SU_GET_CONTEXT(3, 3);
1833 Newx(ud, 1, su_ud_localize);
1834 SU_UD_ORIGIN(ud) = NULL;
1835 SU_UD_HANDLER(ud) = su_localize;
1836 size = su_ud_localize_init(ud, sv, val, elem);
1837 if (ud->type != SVt_PVAV && ud->type != SVt_PVHV) {
1838 SU_UD_LOCALIZE_FREE(ud);
1839 croak("Can't localize an element of something that isn't an array or a hash");
1841 su_init(ud, cxix, size);
1844 localize_delete(SV *sv, SV *elem, ...)
1851 SU_GET_CONTEXT(2, 2);
1852 Newx(ud, 1, su_ud_localize);
1853 SU_UD_ORIGIN(ud) = NULL;
1854 SU_UD_HANDLER(ud) = su_localize;
1855 size = su_ud_localize_init(ud, sv, NULL, elem);
1856 su_init(ud, cxix, size);
1859 uplevel(SV *code, ...)
1862 I32 cxix, ret, args = 0;
1866 if (SvTYPE(code) < SVt_PVCV)
1867 croak("First argument to uplevel must be a code reference");
1868 SU_GET_CONTEXT(1, items - 1);
1870 PERL_CONTEXT *cx = cxstack + cxix;
1871 switch (CxTYPE(cx)) {
1873 croak("Can't uplevel to an eval frame");
1875 croak("Can't uplevel to a format frame");
1877 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1883 /* su_uplevel() takes care of extending the stack if needed. */
1884 ret = su_uplevel((CV *) code, cxix, args);
1889 } while (--cxix >= 0);
1890 croak("Can't uplevel outside a subroutine");