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 ------------------------------------------------------- */
25 #ifndef PERL_UNUSED_VAR
26 # define PERL_UNUSED_VAR(V)
30 # define STMT_START do
34 # define STMT_END while (0)
38 # define SU_D(X) STMT_START X STMT_END
44 # define Newx(v, n, c) New(0, v, n, c)
49 # define SU_POISON(D, N, T) PoisonNew((D), (N), T)
50 # elif defined(Poison)
51 # define SU_POISON(D, N, T) Poison((D), (N), T)
55 # define SU_POISON(D, N, T) NOOP
59 # define SvPV_const(S, L) SvPV(S, L)
62 #ifndef SvPV_nolen_const
63 # define SvPV_nolen_const(S) SvPV_nolen(S)
66 #ifndef SvREFCNT_inc_simple_void
67 # define SvREFCNT_inc_simple_void(sv) SvREFCNT_inc(sv)
71 # define GvCV_set(G, C) (GvCV(G) = (C))
75 # define CvGV_set(C, G) (CvGV(C) = (G))
79 # define CxHASARGS(C) ((C)->blk_sub.hasargs)
83 # define HvNAME_get(H) HvNAME(H)
86 #ifndef gv_fetchpvn_flags
87 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
91 # define cv_clone(P) Perl_cv_clone(aTHX_ (P))
94 #ifndef PERL_MAGIC_tied
95 # define PERL_MAGIC_tied 'P'
98 #ifndef PERL_MAGIC_env
99 # define PERL_MAGIC_env 'E'
102 #ifndef NEGATIVE_INDICES_VAR
103 # define NEGATIVE_INDICES_VAR "NEGATIVE_INDICES"
106 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
107 #define SU_HAS_PERL_EXACT(R, V, S) ((PERL_REVISION == (R)) && (PERL_VERSION == (V)) && (PERL_SUBVERSION == (S)))
109 /* --- Threads and multiplicity -------------------------------------------- */
111 #ifndef SU_MULTIPLICITY
112 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
113 # define SU_MULTIPLICITY 1
115 # define SU_MULTIPLICITY 0
118 #if SU_MULTIPLICITY && !defined(tTHX)
119 # define tTHX PerlInterpreter*
122 #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))
123 # define SU_THREADSAFE 1
124 # ifndef MY_CXT_CLONE
125 # define MY_CXT_CLONE \
127 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
128 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
129 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
132 # define SU_THREADSAFE 0
134 # define dMY_CXT dNOOP
136 # define MY_CXT su_globaldata
138 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
140 # define MY_CXT_INIT NOOP
142 # define MY_CXT_CLONE NOOP
145 /* --- uplevel() data tokens ----------------------------------------------- */
155 PERL_SI *old_curstackinfo;
166 STATIC su_uplevel_ud *su_uplevel_ud_new(pTHX) {
167 #define su_uplevel_ud_new() su_uplevel_ud_new(aTHX)
171 Newx(sud, 1, su_uplevel_ud);
174 Newx(si, 1, PERL_SI);
175 si->si_stack = newAV();
176 AvREAL_off(si->si_stack);
177 si->si_cxstack = NULL;
183 STATIC void su_uplevel_ud_delete(pTHX_ su_uplevel_ud *sud) {
184 #define su_uplevel_ud_delete(S) su_uplevel_ud_delete(aTHX_ (S))
185 PERL_SI *si = sud->si;
187 Safefree(si->si_cxstack);
188 SvREFCNT_dec(si->si_stack);
198 } su_uplevel_storage;
200 #ifndef SU_UPLEVEL_STORAGE_SIZE
201 # define SU_UPLEVEL_STORAGE_SIZE 4
204 /* --- Global data --------------------------------------------------------- */
206 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
209 char *stack_placeholder;
217 su_uplevel_storage uplevel_storage;
222 /* --- Stack manipulations ------------------------------------------------- */
224 #define SU_SAVE_PLACEHOLDER() save_pptr(&MY_CXT.stack_placeholder)
226 #define SU_SAVE_DESTRUCTOR_SIZE 3
227 #define SU_SAVE_PLACEHOLDER_SIZE 3
229 #define SU_SAVE_SCALAR_SIZE 3
231 #define SU_SAVE_ARY_SIZE 3
232 #define SU_SAVE_AELEM_SIZE 4
234 # define SU_SAVE_ADELETE_SIZE 3
236 # define SU_SAVE_ADELETE_SIZE SU_SAVE_DESTRUCTOR_SIZE
238 #if SU_SAVE_AELEM_SIZE < SU_SAVE_ADELETE_SIZE
239 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_ADELETE_SIZE
241 # define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_AELEM_SIZE
244 #define SU_SAVE_HASH_SIZE 3
245 #define SU_SAVE_HELEM_SIZE 4
246 #define SU_SAVE_HDELETE_SIZE 4
247 #if SU_SAVE_HELEM_SIZE < SU_SAVE_HDELETE_SIZE
248 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HDELETE_SIZE
250 # define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HELEM_SIZE
253 #define SU_SAVE_GVCV_SIZE SU_SAVE_DESTRUCTOR_SIZE
255 #if !SU_HAS_PERL(5, 8, 9)
256 # define SU_SAVE_GP_SIZE 6
257 #elif !SU_HAS_PERL(5, 13, 0) || (SU_RELEASE && SU_HAS_PERL_EXACT(5, 13, 0))
258 # define SU_SAVE_GP_SIZE 3
259 #elif !SU_HAS_PERL(5, 13, 8)
260 # define SU_SAVE_GP_SIZE 4
262 # define SU_SAVE_GP_SIZE 3
265 #ifndef SvCANEXISTDELETE
266 # define SvCANEXISTDELETE(sv) \
268 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
269 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
270 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
271 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
276 /* ... Saving array elements ............................................... */
278 STATIC I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
279 #define su_av_key2idx(A, K) su_av_key2idx(aTHX_ (A), (K))
285 /* Added by MJD in perl-5.8.1 with 6f12eb6d2a1dfaf441504d869b27d2e40ef4966a */
286 #if SU_HAS_PERL(5, 8, 1)
287 if (SvRMAGICAL(av)) {
288 const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
290 SV * const * const negative_indices_glob =
291 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
292 NEGATIVE_INDICES_VAR, 16, 0);
293 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
299 idx = key + av_len(av) + 1;
313 STATIC void su_adelete(pTHX_ void *ud_) {
314 su_ud_adelete *ud = (su_ud_adelete *) ud_;
316 av_delete(ud->av, ud->idx, G_DISCARD);
317 SvREFCNT_dec(ud->av);
322 STATIC void su_save_adelete(pTHX_ AV *av, I32 idx) {
323 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
326 Newx(ud, 1, su_ud_adelete);
329 SvREFCNT_inc_simple_void(av);
331 SAVEDESTRUCTOR_X(su_adelete, ud);
334 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
336 #endif /* SAVEADELETE */
338 STATIC void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
339 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
346 idx = su_av_key2idx(av, SvIV(key));
348 if (SvCANEXISTDELETE(av))
349 preeminent = av_exists(av, idx);
351 svp = av_fetch(av, idx, 1);
352 if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
355 save_aelem(av, idx, svp);
357 SAVEADELETE(av, idx);
359 if (val) { /* local $x[$idx] = $val; */
360 SvSetMagicSV(*svp, val);
361 } else { /* local $x[$idx]; delete $x[$idx]; */
362 av_delete(av, idx, G_DISCARD);
366 /* ... Saving hash elements ................................................ */
368 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
369 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
376 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
377 preeminent = hv_exists_ent(hv, keysv, 0);
379 he = hv_fetch_ent(hv, keysv, 1, 0);
380 svp = he ? &HeVAL(he) : NULL;
381 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
383 if (HvNAME_get(hv) && isGV(*svp)) {
384 save_gp((GV *) *svp, 0);
389 save_helem(hv, keysv, svp);
392 const char * const key = SvPV_const(keysv, keylen);
393 SAVEDELETE(hv, savepvn(key, keylen),
394 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
397 if (val) { /* local $x{$keysv} = $val; */
398 SvSetMagicSV(*svp, val);
399 } else { /* local $x{$keysv}; delete $x{$keysv}; */
400 (void)hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
404 /* ... Saving code slots from a glob ....................................... */
406 #if !SU_HAS_PERL(5, 10, 0) && !defined(mro_method_changed_in)
407 # define mro_method_changed_in(G) PL_sub_generation++
415 STATIC void su_restore_gvcv(pTHX_ void *ud_) {
416 su_save_gvcv_ud *ud = ud_;
419 GvCV_set(gv, ud->old_cv);
421 mro_method_changed_in(GvSTASH(gv));
426 STATIC void su_save_gvcv(pTHX_ GV *gv) {
427 #define su_save_gvcv(G) su_save_gvcv(aTHX_ (G))
430 Newx(ud, 1, su_save_gvcv_ud);
432 ud->old_cv = GvCV(gv);
436 mro_method_changed_in(GvSTASH(gv));
438 SAVEDESTRUCTOR_X(su_restore_gvcv, ud);
441 /* --- Actions ------------------------------------------------------------- */
447 void (*handler)(pTHX_ void *);
450 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
451 #define SU_UD_PAD(U) (((su_ud_common *) (U))->pad)
452 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
453 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
455 #define SU_UD_FREE(U) STMT_START { \
456 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
460 /* ... Reap ................................................................ */
467 STATIC void su_call(pTHX_ void *ud_) {
468 su_ud_reap *ud = (su_ud_reap *) ud_;
469 #if SU_HAS_PERL(5, 9, 5)
470 PERL_CONTEXT saved_cx;
477 PerlIO_printf(Perl_debug_log,
478 "%p: @@@ call\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
479 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
488 /* If the recently popped context isn't saved there, it will be overwritten by
489 * the sub scope from call_sv, although it's still needed in our caller. */
491 #if SU_HAS_PERL(5, 9, 5)
492 if (cxstack_ix < cxstack_max)
493 cxix = cxstack_ix + 1;
495 cxix = Perl_cxinc(aTHX);
496 saved_cx = cxstack[cxix];
499 call_sv(ud->cb, G_VOID);
501 #if SU_HAS_PERL(5, 9, 5)
502 cxstack[cxix] = saved_cx;
510 SvREFCNT_dec(ud->cb);
514 STATIC void su_reap(pTHX_ void *ud) {
515 #define su_reap(U) su_reap(aTHX_ (U))
517 PerlIO_printf(Perl_debug_log,
518 "%p: === reap\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
519 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
522 SAVEDESTRUCTOR_X(su_call, ud);
525 /* ... Localize & localize array/hash element .............................. */
535 #define SU_UD_LOCALIZE_FREE(U) STMT_START { \
536 SvREFCNT_dec((U)->elem); \
537 SvREFCNT_dec((U)->val); \
538 SvREFCNT_dec((U)->sv); \
542 STATIC I32 su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) {
543 #define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E))
548 SvREFCNT_inc_simple_void(sv);
550 if (SvTYPE(sv) >= SVt_PVGV) {
551 if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */
553 } else { /* local *x = \$val; */
554 t = SvTYPE(SvRV(val));
557 } else if (SvROK(sv)) {
558 croak("Invalid %s reference as the localization target",
559 sv_reftype(SvRV(sv), 0));
562 const char *p = SvPV_const(sv, len), *s;
563 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
569 case '$': t = SVt_PV; break;
570 case '@': t = SVt_PVAV; break;
571 case '%': t = SVt_PVHV; break;
572 case '&': t = SVt_PVCV; break;
573 case '*': t = SVt_PVGV; break;
578 } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
579 if (SvROK(val) && !sv_isobject(val)) {
580 t = SvTYPE(SvRV(val));
592 size = elem ? SU_SAVE_AELEM_OR_ADELETE_SIZE
597 size = elem ? SU_SAVE_HELEM_OR_HDELETE_SIZE
602 size = SU_SAVE_GP_SIZE;
606 size = SU_SAVE_GVCV_SIZE;
610 size = SU_SAVE_SCALAR_SIZE;
613 /* When deref is set, val isn't NULL */
616 ud->val = val ? newSVsv(deref ? SvRV(val) : val) : NULL;
617 ud->elem = SvREFCNT_inc(elem);
623 STATIC void su_localize(pTHX_ void *ud_) {
624 #define su_localize(U) su_localize(aTHX_ (U))
625 su_ud_localize *ud = (su_ud_localize *) ud_;
632 if (SvTYPE(sv) >= SVt_PVGV) {
636 gv = gv_fetchsv(sv, GV_ADDMULTI, t);
639 const char *name = SvPV_const(sv, len);
640 gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t);
647 PerlIO_printf(Perl_debug_log, "%p: === localize a %s\n",ud, sv_reftype(z, 0));
648 PerlIO_printf(Perl_debug_log,
649 "%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
650 ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
654 /* Inspired from Alias.pm */
658 su_save_aelem(GvAV(gv), elem, val);
665 su_save_helem(GvHV(gv), elem, val);
671 save_gp(gv, 1); /* hide previous entry in symtab */
677 gv = (GV *) save_scalar(gv);
682 SvSetMagicSV((SV *) gv, val);
685 SU_UD_LOCALIZE_FREE(ud);
688 /* --- Pop a context back -------------------------------------------------- */
692 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
694 # define SU_CXNAME "XXX"
698 STATIC void su_pop(pTHX_ void *ud) {
699 #define su_pop(U) su_pop(aTHX_ (U))
700 I32 depth, base, mark, *origin;
701 depth = SU_UD_DEPTH(ud);
704 PerlIO_printf(Perl_debug_log,
706 "%p: leave scope at depth=%2d scope_ix=%2d cur_top=%2d cur_base=%2d\n",
708 ud, depth, PL_scopestack_ix,PL_savestack_ix,PL_scopestack[PL_scopestack_ix])
711 origin = SU_UD_ORIGIN(ud);
712 mark = origin[depth];
713 base = origin[depth - 1];
715 SU_D(PerlIO_printf(Perl_debug_log,
716 "%p: original scope was %*c top=%2d base=%2d\n",
717 ud, 24, ' ', mark, base));
720 SU_D(PerlIO_printf(Perl_debug_log, "%p: clear leftovers\n", ud));
721 PL_savestack_ix = mark;
724 PL_savestack_ix = base;
726 SU_UD_DEPTH(ud) = --depth;
731 if ((pad = SU_UD_PAD(ud))) {
734 SU_D(PerlIO_printf(Perl_debug_log,
735 "%p: push a pad slot at depth=%2d scope_ix=%2d save_ix=%2d\n",
736 ud, depth, PL_scopestack_ix, PL_savestack_ix));
737 SU_SAVE_PLACEHOLDER();
741 SU_D(PerlIO_printf(Perl_debug_log,
742 "%p: push destructor at depth=%2d scope_ix=%2d save_ix=%2d\n",
743 ud, depth, PL_scopestack_ix, PL_savestack_ix));
744 SAVEDESTRUCTOR_X(su_pop, ud);
746 SU_UD_HANDLER(ud)(aTHX_ ud);
749 SU_D(PerlIO_printf(Perl_debug_log,
750 "%p: --- end pop: cur_top=%2d == cur_base=%2d\n",
751 ud, PL_savestack_ix, PL_scopestack[PL_scopestack_ix]));
754 /* --- Initialize the stack and the action userdata ------------------------ */
756 STATIC I32 su_init(pTHX_ void *ud, I32 cxix, I32 size) {
757 #define su_init(U, C, S) su_init(aTHX_ (U), (C), (S))
758 I32 i, depth = 1, pad, offset, *origin;
760 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
762 if (size <= SU_SAVE_DESTRUCTOR_SIZE)
765 I32 extra = size - SU_SAVE_DESTRUCTOR_SIZE;
766 pad = extra / SU_SAVE_PLACEHOLDER_SIZE;
767 if (extra % SU_SAVE_PLACEHOLDER_SIZE)
770 offset = SU_SAVE_DESTRUCTOR_SIZE + SU_SAVE_PLACEHOLDER_SIZE * pad;
772 SU_D(PerlIO_printf(Perl_debug_log, "%p: size=%d pad=%d offset=%d\n",
773 ud, size, pad, offset));
775 for (i = cxstack_ix; i > cxix; --i) {
776 PERL_CONTEXT *cx = cxstack + i;
777 switch (CxTYPE(cx)) {
778 #if SU_HAS_PERL(5, 10, 0)
780 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is block\n", ud, i));
781 /* Given and when blocks are actually followed by a simple block, so skip
783 if (cxix > 0) { /* Implies i > 0 */
784 PERL_CONTEXT *next = cx - 1;
785 if (CxTYPE(next) == CXt_GIVEN || CxTYPE(next) == CXt_WHEN)
791 #if SU_HAS_PERL(5, 11, 0)
794 case CXt_LOOP_LAZYSV:
795 case CXt_LOOP_LAZYIV:
799 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
803 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is other\n", ud, i));
808 SU_D(PerlIO_printf(Perl_debug_log, "%p: going down to depth %d\n", ud, depth));
810 Newx(origin, depth + 1, I32);
811 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
812 PL_scopestack[PL_scopestack_ix - depth] += size;
813 for (i = depth - 1; i >= 1; --i) {
814 I32 j = PL_scopestack_ix - i;
815 origin[depth - i] = PL_scopestack[j];
816 PL_scopestack[j] += offset;
818 origin[depth] = PL_savestack_ix;
820 SU_UD_ORIGIN(ud) = origin;
821 SU_UD_DEPTH(ud) = depth;
824 /* Make sure the first destructor fires by pushing enough fake slots on the
826 if (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
827 <= PL_scopestack[PL_scopestack_ix - 1]) {
830 SU_D(PerlIO_printf(Perl_debug_log,
831 "%p: push a fake slot at scope_ix=%2d save_ix=%2d\n",
832 ud, PL_scopestack_ix, PL_savestack_ix));
833 SU_SAVE_PLACEHOLDER();
834 } while (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
835 <= PL_scopestack[PL_scopestack_ix - 1]);
837 SU_D(PerlIO_printf(Perl_debug_log,
838 "%p: push first destructor at scope_ix=%2d save_ix=%2d\n",
839 ud, PL_scopestack_ix, PL_savestack_ix));
840 SAVEDESTRUCTOR_X(su_pop, ud);
843 for (i = 0; i <= depth; ++i) {
844 I32 j = PL_scopestack_ix - i;
845 PerlIO_printf(Perl_debug_log,
846 "%p: depth=%2d scope_ix=%2d saved_floor=%2d new_floor=%2d\n",
847 ud, i, j, origin[depth - i],
848 i == 0 ? PL_savestack_ix : PL_scopestack[j]);
855 /* --- Unwind stack -------------------------------------------------------- */
857 STATIC void su_unwind(pTHX_ void *ud_) {
859 I32 cxix = MY_CXT.cxix;
860 I32 items = MY_CXT.items - 1;
861 SV **savesp = MY_CXT.savesp;
864 PERL_UNUSED_VAR(ud_);
867 PL_stack_sp = savesp;
869 if (cxstack_ix > cxix)
876 mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
877 *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
881 PerlIO_printf(Perl_debug_log,
882 "%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
884 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
885 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
888 PL_op = (OP *) &(MY_CXT.return_op);
889 PL_op = PL_op->op_ppaddr(aTHX);
891 *PL_markstack_ptr = mark;
893 MY_CXT.proxy_op.op_next = PL_op;
894 PL_op = &(MY_CXT.proxy_op);
897 /* --- Uplevel ------------------------------------------------------------- */
899 #ifndef OP_GIMME_REVERSE
900 STATIC U8 su_op_gimme_reverse(U8 gimme) {
903 return OPf_WANT_VOID;
905 return OPf_WANT_LIST;
910 return OPf_WANT_SCALAR;
912 #define OP_GIMME_REVERSE(G) su_op_gimme_reverse(G)
915 #define SU_UPLEVEL_SAVE(f, t) STMT_START { sud->old_##f = PL_##f; PL_##f = (t); } STMT_END
916 #define SU_UPLEVEL_RESTORE(f) STMT_START { PL_##f = sud->old_##f; } STMT_END
918 STATIC int su_uplevel_restore_free(pTHX_ SV *sv, MAGIC *mg) {
919 su_uplevel_ud_delete((su_uplevel_ud *) mg->mg_ptr);
924 STATIC MGVTBL su_uplevel_restore_vtbl = {
929 su_uplevel_restore_free
932 STATIC void su_uplevel_restore(pTHX_ void *sus_) {
933 su_uplevel_ud *sud = sus_;
934 PERL_SI *cur = sud->old_curstackinfo;
935 PERL_SI *si = sud->si;
938 /* When we reach this place, POPSUB has already been called (with our fake
939 * argarray). GvAV(PL_defgv) points to the savearray (that is, what @_ was
940 * before uplevel). argarray is either the fake AV we created in su_uplevel()
941 * or some empty replacement POPSUB creates when @_ is reified. In both cases
942 * we have to destroy it before the context stack is swapped back to its
944 SvREFCNT_dec(cxstack[sud->cxix].blk_sub.argarray);
946 CATCH_SET(sud->old_catch);
948 SvREFCNT_dec(sud->cloned_cv);
950 SU_UPLEVEL_RESTORE(op);
952 /* stack_grow() wants PL_curstack so restore the old stack first */
953 if (PL_curstackinfo == si) {
954 PL_curstack = cur->si_stack;
955 if (sud->old_mainstack)
956 SU_UPLEVEL_RESTORE(mainstack);
957 SU_UPLEVEL_RESTORE(curstackinfo);
960 CV *target_cv = sud->target;
963 /* When we die, the depth of the target CV is not updated because of the
964 * stack switcheroo. So we have to look at all the frames between the
965 * uplevel call and the catch block to count how many call frames to the
966 * target CV were skipped. */
967 for (i = cur->si_cxix; i > sud->cxix; i--) {
968 register const PERL_CONTEXT *cx = cxstack + i;
970 if (CxTYPE(cx) == CXt_SUB) {
971 if (cx->blk_sub.cv == target_cv)
976 /* If we died, the replacement stack was already unwinded to the first
977 * eval frame, and all the contexts down there were popped. We don't have
978 * to pop manually any context of the original stack, because they must
979 * have been in the replacement stack as well (since the second was copied
980 * from the first). Thus we only have to make sure the original stack index
981 * points to the context just below the first eval scope under the target
983 for (; i >= 0; i--) {
984 register const PERL_CONTEXT *cx = cxstack + i;
986 switch (CxTYPE(cx)) {
988 if (cx->blk_sub.cv == target_cv)
1000 CvDEPTH(target_cv) = sud->old_depth - levels;
1001 PL_curstackinfo->si_cxix = i - 1;
1003 #if !SU_HAS_PERL(5, 13, 1)
1004 /* Since $@ was maybe localized between the target frame and the uplevel
1005 * call, we forcefully flush the save stack to get rid of it and then
1006 * reset $@ to its proper value. Note that the the call to
1007 * su_uplevel_restore() must happen before the "reset $@" item of the save
1008 * stack is processed, as uplevel was called after the localization.
1009 * Andrew's change to how $@ was treated, which were mainly integrated
1010 * between perl 5.13.0 and 5.13.1, fixed this. */
1011 if (ERRSV && SvTRUE(ERRSV)) {
1012 register const PERL_CONTEXT *cx = cxstack + i; /* This is the eval scope */
1013 SV *errsv = SvREFCNT_inc(ERRSV);
1014 PL_scopestack_ix = cx->blk_oldscopesp;
1015 leave_scope(PL_scopestack[PL_scopestack_ix]);
1016 sv_setsv(ERRSV, errsv);
1017 SvREFCNT_dec(errsv);
1023 SU_UPLEVEL_RESTORE(curcop);
1025 SvREFCNT_dec(sud->target);
1027 PL_stack_base = AvARRAY(cur->si_stack);
1028 PL_stack_sp = PL_stack_base + AvFILLp(cur->si_stack);
1029 PL_stack_max = PL_stack_base + AvMAX(cur->si_stack);
1031 #if SU_HAS_PERL(5, 8, 0)
1032 if (MY_CXT.uplevel_storage.count >= SU_UPLEVEL_STORAGE_SIZE) {
1033 /* When an exception is thrown from the uplevel'd subroutine,
1034 * su_uplevel_restore() may be called by the LEAVE in die_unwind() (called
1035 * die_where() in more recent perls), which has the sad habit of keeping a
1036 * pointer to the current context frame across this call. This means that
1037 * we can't free the temporary context stack we used for the uplevel call
1038 * right now, or that pointer upwards would point to garbage. We work around
1039 * this by attaching the state data to a scalar that will be freed "soon".
1040 * This issue has been fixed in perl with commit 8f89e5a9. */
1041 SV *sv = sv_newmortal();
1042 sv_magicext(sv, NULL, PERL_MAGIC_ext, &su_uplevel_restore_vtbl,
1043 (const char *) sud, 0);
1046 sud->next = MY_CXT.uplevel_storage.root;
1047 MY_CXT.uplevel_storage.root = sud;
1048 MY_CXT.uplevel_storage.count++;
1049 #if SU_HAS_PERL(5, 8, 0)
1056 STATIC CV *su_cv_clone(pTHX_ CV *old_cv) {
1057 #define su_cv_clone(C) su_cv_clone(aTHX_ (C))
1060 /* Starting from commit b5c19bd7, cv_clone() has an assert that checks whether
1061 * CvDEPTH(CvOUTSIDE(proto)) > 0, so we have to fool cv_clone() with a little
1063 #if defined(DEBUGGING) && SU_HAS_PERL(5, 9, 0)
1065 CV *outside = CvOUTSIDE(old_cv);
1067 if (outside && CvCLONE(outside) && !CvCLONED(outside))
1068 outside = find_runcv(NULL);
1069 old_depth = CvDEPTH(outside);
1071 CvDEPTH(outside) = 1;
1074 new_cv = cv_clone(old_cv);
1076 #if defined(DEBUGGING) && SU_HAS_PERL(5, 9, 0)
1077 CvDEPTH(outside) = old_depth;
1080 /* Starting from perl 5.9 (more exactly commit b5c19bd7), cv_clone() is no
1081 * longer able to clone named subs propery. With this commit, pad_findlex()
1082 * stores the parent index of a fake pad entry in the NV slot of the
1083 * corresponding pad name SV, but only for anonymous subs (since named subs
1084 * aren't supposed to be cloned in pure Perl land). To fix this, we just
1085 * manually relink the new fake pad entries to the new ones.
1086 * For some reason perl 5.8 crashes too without this, supposedly because of
1087 * other closure bugs. Hence we enable it everywhere. */
1088 if (!CvCLONE(old_cv)) {
1089 const AV *old_padname = (const AV *) AvARRAY(CvPADLIST(old_cv))[0];
1090 AV *old_pad = (AV *) AvARRAY(CvPADLIST(old_cv))[1];
1091 AV *new_pad = (AV *) AvARRAY(CvPADLIST(new_cv))[1];
1092 const SV **old_aryname = (const SV **) AvARRAY(old_padname);
1093 SV **old_ary = AvARRAY(old_pad);
1094 SV **new_ary = AvARRAY(new_pad);
1095 I32 fname = AvFILLp(old_padname);
1096 I32 fpad = AvFILLp(old_pad);
1099 for (ix = fpad; ix > 0; ix--) {
1100 const SV *namesv = (ix <= fname) ? old_aryname[ix] : NULL;
1102 if (namesv && namesv != &PL_sv_undef && SvFAKE(namesv)) {
1103 SvREFCNT_dec(new_ary[ix]);
1104 new_ary[ix] = SvREFCNT_inc(old_ary[ix]);
1112 STATIC I32 su_uplevel(pTHX_ CV *cv, I32 cxix, I32 args) {
1113 #define su_uplevel(C, I, A) su_uplevel(aTHX_ (C), (I), (A))
1115 const PERL_CONTEXT *cx = cxstack + cxix;
1117 PERL_SI *cur = PL_curstackinfo;
1123 I32 old_mark, new_mark;
1131 /* Make PL_stack_sp point just before the CV. */
1132 PL_stack_sp -= args + 1;
1133 old_mark = AvFILLp(PL_curstack) = PL_stack_sp - PL_stack_base;
1136 sud = MY_CXT.uplevel_storage.root;
1138 MY_CXT.uplevel_storage.root = sud->next;
1139 MY_CXT.uplevel_storage.count--;
1141 sud = su_uplevel_ud_new();
1147 SAVEDESTRUCTOR_X(su_uplevel_restore, sud);
1149 si->si_type = cur->si_type;
1151 si->si_prev = cur->si_prev;
1153 /* Allocate enough space for all the elements of the original stack up to the
1154 * target context, plus the forthcoming arguments. */
1155 new_mark = cx->blk_oldsp;
1156 av_extend(si->si_stack, new_mark + 1 + args + 1);
1157 Copy(PL_curstack, AvARRAY(si->si_stack), new_mark + 1, SV *);
1158 AvFILLp(si->si_stack) = new_mark;
1159 SU_POISON(AvARRAY(si->si_stack) + new_mark + 1, args + 1, SV *);
1161 /* Specialized SWITCHSTACK() */
1162 PL_stack_base = AvARRAY(si->si_stack);
1163 old_stack_sp = PL_stack_sp;
1164 PL_stack_sp = PL_stack_base + AvFILLp(si->si_stack);
1165 PL_stack_max = PL_stack_base + AvMAX(si->si_stack);
1169 si->si_markoff = cx->blk_oldmarksp;
1172 /* Copy the context stack up to the context just below the target. */
1173 si->si_cxix = (cxix < 0) ? -1 : (cxix - 1);
1174 /* The max size must be at least two so that GROW(max) = (max * 3) / 2 > max */
1175 si->si_cxmax = (cxix < 4) ? 4 : cxix;
1176 Renew(si->si_cxstack, si->si_cxmax + 1, PERL_CONTEXT);
1177 Copy(cur->si_cxstack, si->si_cxstack, cxix, PERL_CONTEXT);
1178 SU_POISON(si->si_cxstack + cxix, si->si_cxmax + 1 - cxix, PERL_CONTEXT);
1180 target_cv = cx->blk_sub.cv;
1181 sud->target = (CV *) SvREFCNT_inc(target_cv);
1182 sud->old_depth = CvDEPTH(target_cv);
1184 /* blk_oldcop is essentially needed for caller() and stack traces. It has no
1185 * run-time implication, since PL_curcop will be overwritten as soon as we
1186 * enter a sub (a sub starts by a nextstate/dbstate). Hence it's safe to just
1187 * make it point to the blk_oldcop for the target frame, so that caller()
1188 * reports the right file name, line number and lexical hints. */
1189 SU_UPLEVEL_SAVE(curcop, cx->blk_oldcop);
1190 /* Don't reset PL_markstack_ptr, or we would overwrite the mark stack below
1192 /* Don't reset PL_curpm, we want the most recent matches. */
1194 SU_UPLEVEL_SAVE(curstackinfo, si);
1195 /* If those two are equal, we need to fool POPSTACK_TO() */
1196 if (PL_mainstack == PL_curstack)
1197 SU_UPLEVEL_SAVE(mainstack, si->si_stack);
1199 sud->old_mainstack = NULL;
1200 PL_curstack = si->si_stack;
1202 cv = su_cv_clone(cv);
1203 sud->cloned_cv = cv;
1204 CvGV_set(cv, CvGV(target_cv));
1207 /* Both SP and old_stack_sp points just before the CV. */
1208 Copy(old_stack_sp + 2, SP + 1, args, SV *);
1213 Zero(&sub_op, 1, UNOP);
1214 sub_op.op_type = OP_ENTERSUB;
1215 sub_op.op_next = NULL;
1216 sub_op.op_flags = OP_GIMME_REVERSE(gimme) | OPf_STACKED;
1218 sub_op.op_flags |= OPpENTERSUB_DB;
1220 SU_UPLEVEL_SAVE(op, (OP *) &sub_op);
1222 sud->old_catch = CATCH_GET;
1225 if (PL_op = PL_ppaddr[OP_ENTERSUB](aTHX)) {
1226 if (CxHASARGS(cx) && cx->blk_sub.argarray) {
1227 /* The call to pp_entersub() has saved the current @_ (in XS terms,
1228 * GvAV(PL_defgv)) in the savearray member, and has created a new argarray
1229 * with what we put on the stack. But we want to fake up the same arguments
1230 * as the ones in use at the context we uplevel to, so we replace the
1231 * argarray with an unreal copy of the original @_. */
1234 av_extend(av, AvMAX(cx->blk_sub.argarray));
1235 AvFILLp(av) = AvFILLp(cx->blk_sub.argarray);
1236 Copy(AvARRAY(cx->blk_sub.argarray), AvARRAY(av), AvFILLp(av) + 1, SV *);
1237 cxstack[cxix].blk_sub.argarray = av;
1238 } else if (PL_DBsub) {
1239 SvREFCNT_inc(cxstack[cxix].blk_sub.argarray);
1244 ret = PL_stack_sp - (PL_stack_base + new_mark);
1252 AV *old_stack = sud->old_curstackinfo->si_stack;
1254 if (old_mark + ret > AvMAX(old_stack)) {
1255 /* Specialized EXTEND(old_sp, ret) */
1256 av_extend(old_stack, old_mark + ret + 1);
1257 old_stack_sp = AvARRAY(old_stack) + old_mark;
1260 Copy(PL_stack_sp - ret + 1, old_stack_sp + 1, ret, SV *);
1262 AvFILLp(old_stack) += ret;
1272 /* --- Interpreter setup/teardown ------------------------------------------ */
1274 STATIC void su_teardown(pTHX_ void *param) {
1275 su_uplevel_ud *cur, *prev;
1278 cur = MY_CXT.uplevel_storage.root;
1280 su_uplevel_ud *prev;
1284 su_uplevel_ud_delete(prev);
1291 STATIC void su_setup(pTHX) {
1292 #define su_setup() su_setup(aTHX)
1295 MY_CXT.stack_placeholder = NULL;
1297 /* NewOp() calls calloc() which just zeroes the memory with memset(). */
1298 Zero(&(MY_CXT.return_op), 1, sizeof(MY_CXT.return_op));
1299 MY_CXT.return_op.op_type = OP_RETURN;
1300 MY_CXT.return_op.op_ppaddr = PL_ppaddr[OP_RETURN];
1302 Zero(&(MY_CXT.proxy_op), 1, sizeof(MY_CXT.proxy_op));
1303 MY_CXT.proxy_op.op_type = OP_STUB;
1304 MY_CXT.proxy_op.op_ppaddr = NULL;
1306 MY_CXT.uplevel_storage.root = NULL;
1307 MY_CXT.uplevel_storage.count = 0;
1309 call_atexit(su_teardown, NULL);
1314 /* --- XS ------------------------------------------------------------------ */
1316 #if SU_HAS_PERL(5, 8, 9)
1317 # define SU_SKIP_DB_MAX 2
1319 # define SU_SKIP_DB_MAX 3
1322 /* Skip context sequences of 1 to SU_SKIP_DB_MAX (included) block contexts
1323 * followed by a DB sub */
1325 #define SU_SKIP_DB(C) \
1328 PERL_CONTEXT *base = cxstack; \
1329 PERL_CONTEXT *cx = base + (C); \
1330 while (cx >= base && (C) > skipped && CxTYPE(cx) == CXt_BLOCK) \
1332 if (cx >= base && (C) > skipped) { \
1333 switch (CxTYPE(cx)) { \
1335 if (skipped <= SU_SKIP_DB_MAX && cx->blk_sub.cv == GvCV(PL_DBsub)) \
1336 (C) -= skipped + 1; \
1344 #define SU_GET_CONTEXT(A, B) \
1353 else if (cxix > cxstack_ix) \
1354 cxix = cxstack_ix; \
1357 cxix = cxstack_ix; \
1363 #define SU_GET_LEVEL(A, B) \
1369 level = SvIV(lsv); \
1376 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
1378 XS(XS_Scope__Upper_unwind) {
1387 PERL_UNUSED_VAR(cv); /* -W */
1388 PERL_UNUSED_VAR(ax); /* -Wall */
1390 SU_GET_CONTEXT(0, items - 1);
1392 PERL_CONTEXT *cx = cxstack + cxix;
1393 switch (CxTYPE(cx)) {
1395 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1400 MY_CXT.items = items;
1401 /* pp_entersub will want to sanitize the stack after returning from there
1402 * Screw that, we're insane */
1403 if (GIMME_V == G_SCALAR) {
1404 MY_CXT.savesp = PL_stack_sp;
1405 /* dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
1406 PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
1408 MY_CXT.savesp = NULL;
1410 SAVEDESTRUCTOR_X(su_unwind, NULL);
1415 } while (--cxix >= 0);
1416 croak("Can't return outside a subroutine");
1419 MODULE = Scope::Upper PACKAGE = Scope::Upper
1427 stash = gv_stashpv(__PACKAGE__, 1);
1428 newCONSTSUB(stash, "TOP", newSViv(0));
1429 newCONSTSUB(stash, "SU_THREADSAFE", newSVuv(SU_THREADSAFE));
1431 newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
1444 MY_CXT.uplevel_storage.root = NULL;
1445 MY_CXT.uplevel_storage.count = 0;
1449 #endif /* SU_THREADSAFE */
1455 I32 cxix = cxstack_ix;
1459 RETVAL = newSViv(cxix);
1469 SU_GET_CONTEXT(0, 0);
1474 RETVAL = newSViv(cxix);
1484 SU_GET_CONTEXT(0, 0);
1485 for (; cxix >= 0; --cxix) {
1486 PERL_CONTEXT *cx = cxstack + cxix;
1487 switch (CxTYPE(cx)) {
1491 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1493 ST(0) = sv_2mortal(newSViv(cxix));
1505 SU_GET_CONTEXT(0, 0);
1506 for (; cxix >= 0; --cxix) {
1507 PERL_CONTEXT *cx = cxstack + cxix;
1508 switch (CxTYPE(cx)) {
1512 ST(0) = sv_2mortal(newSViv(cxix));
1539 ST(0) = sv_2mortal(newSViv(cxix));
1549 for (cxix = cxstack_ix; cxix > 0; --cxix) {
1550 PERL_CONTEXT *cx = cxstack + cxix;
1551 switch (CxTYPE(cx)) {
1553 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1563 ST(0) = sv_2mortal(newSViv(cxix));
1572 SU_GET_CONTEXT(0, 0);
1574 PERL_CONTEXT *cx = cxstack + cxix--;
1575 switch (CxTYPE(cx)) {
1579 I32 gimme = cx->blk_gimme;
1581 case G_VOID: XSRETURN_UNDEF; break;
1582 case G_SCALAR: XSRETURN_NO; break;
1583 case G_ARRAY: XSRETURN_YES; break;
1598 SU_GET_CONTEXT(1, 1);
1599 Newx(ud, 1, su_ud_reap);
1600 SU_UD_ORIGIN(ud) = NULL;
1601 SU_UD_HANDLER(ud) = su_reap;
1602 ud->cb = newSVsv(hook);
1603 su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE);
1606 localize(SV *sv, SV *val, ...)
1613 SU_GET_CONTEXT(2, 2);
1614 Newx(ud, 1, su_ud_localize);
1615 SU_UD_ORIGIN(ud) = NULL;
1616 SU_UD_HANDLER(ud) = su_localize;
1617 size = su_ud_localize_init(ud, sv, val, NULL);
1618 su_init(ud, cxix, size);
1621 localize_elem(SV *sv, SV *elem, SV *val, ...)
1628 if (SvTYPE(sv) >= SVt_PVGV)
1629 croak("Can't infer the element localization type from a glob and the value");
1630 SU_GET_CONTEXT(3, 3);
1631 Newx(ud, 1, su_ud_localize);
1632 SU_UD_ORIGIN(ud) = NULL;
1633 SU_UD_HANDLER(ud) = su_localize;
1634 size = su_ud_localize_init(ud, sv, val, elem);
1635 if (ud->type != SVt_PVAV && ud->type != SVt_PVHV) {
1636 SU_UD_LOCALIZE_FREE(ud);
1637 croak("Can't localize an element of something that isn't an array or a hash");
1639 su_init(ud, cxix, size);
1642 localize_delete(SV *sv, SV *elem, ...)
1649 SU_GET_CONTEXT(2, 2);
1650 Newx(ud, 1, su_ud_localize);
1651 SU_UD_ORIGIN(ud) = NULL;
1652 SU_UD_HANDLER(ud) = su_localize;
1653 size = su_ud_localize_init(ud, sv, NULL, elem);
1654 su_init(ud, cxix, size);
1657 uplevel(SV *code, ...)
1660 I32 cxix, ret, args = 0;
1664 if (SvTYPE(code) < SVt_PVCV)
1665 croak("First argument to uplevel must be a code reference");
1666 SU_GET_CONTEXT(1, items - 1);
1668 PERL_CONTEXT *cx = cxstack + cxix;
1669 switch (CxTYPE(cx)) {
1671 croak("Can't uplevel to an eval frame");
1673 croak("Can't uplevel to a format frame");
1675 if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
1681 ret = su_uplevel((CV *) code, cxix, args);
1686 } while (--cxix >= 0);
1687 croak("Can't uplevel outside a subroutine");