1 /* This file is part of the Scope::Upper Perl module.
2 * See http://search.cpan.org/dist/Scope-Upper/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "Scope::Upper"
15 /* --- Compatibility ------------------------------------------------------- */
17 #ifndef PERL_UNUSED_VAR
18 # define PERL_UNUSED_VAR(V)
22 # define STMT_START do
26 # define STMT_END while (0)
30 # define SU_D(X) STMT_START X STMT_END
36 # define Newx(v, n, c) New(0, v, n, c)
40 # define SvPV_const(S, L) SvPV(S, L)
43 #ifndef SvPV_nolen_const
44 # define SvPV_nolen_const(S) SvPV_nolen(S)
48 # define HvNAME_get(H) HvNAME(H)
51 #ifndef gv_fetchpvn_flags
52 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
55 #ifndef PERL_MAGIC_tied
56 # define PERL_MAGIC_tied 'P'
59 #ifndef PERL_MAGIC_env
60 # define PERL_MAGIC_env 'E'
63 #ifndef NEGATIVE_INDICES_VAR
64 # define NEGATIVE_INDICES_VAR "NEGATIVE_INDICES"
67 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
69 /* --- Stack manipulations ------------------------------------------------- */
71 #ifndef SvCANEXISTDELETE
72 # define SvCANEXISTDELETE(sv) \
74 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
75 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
76 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
77 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
82 /* ... Saving array elements ............................................... */
84 STATIC I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
85 #define su_av_key2idx(A, K) su_av_key2idx(aTHX_ (A), (K))
91 /* Added by MJD in perl-5.8.1 with 6f12eb6d2a1dfaf441504d869b27d2e40ef4966a */
92 #if SU_HAS_PERL(5, 8, 1)
94 const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
97 SV * const * const negative_indices_glob =
98 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
99 NEGATIVE_INDICES_VAR, 16, 0);
100 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
106 idx = key + av_len(av) + 1;
120 STATIC void su_adelete(pTHX_ void *ud_) {
121 su_ud_adelete *ud = (su_ud_adelete *) ud_;
123 av_delete(ud->av, ud->idx, G_DISCARD);
124 SvREFCNT_dec(ud->av);
129 STATIC void su_save_adelete(pTHX_ AV *av, I32 idx) {
130 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
133 Newx(ud, 1, su_ud_adelete);
138 SAVEDESTRUCTOR_X(su_adelete, ud);
141 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
143 #endif /* SAVEADELETE */
145 STATIC void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
146 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
153 idx = su_av_key2idx(av, SvIV(key));
155 if (SvCANEXISTDELETE(av))
156 preeminent = av_exists(av, idx);
158 svp = av_fetch(av, idx, 1);
159 if (!svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
162 save_aelem(av, idx, svp);
164 SAVEADELETE(av, idx);
166 if (val) { /* local $x[$idx] = $val; */
167 SvSetMagicSV(*svp, val);
168 } else { /* local $x[$idx]; delete $x[$idx]; */
169 av_delete(av, idx, G_DISCARD);
173 /* ... Saving hash elements ................................................ */
175 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
176 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
183 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
184 preeminent = hv_exists_ent(hv, keysv, 0);
186 he = hv_fetch_ent(hv, keysv, 1, 0);
187 svp = he ? &HeVAL(he) : NULL;
188 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
190 if (HvNAME_get(hv) && isGV(*svp)) {
191 save_gp((GV *) *svp, 0);
196 save_helem(hv, keysv, svp);
199 const char * const key = SvPV_const(keysv, keylen);
200 SAVEDELETE(hv, savepvn(key, keylen),
201 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
204 if (val) { /* local $x{$keysv} = $val; */
205 SvSetMagicSV(*svp, val);
206 } else { /* local $x{$keysv}; delete $x{$keysv}; */
207 hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
211 /* --- Actions ------------------------------------------------------------- */
216 void (*handler)(pTHX_ void *);
219 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
220 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
221 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
223 #define SU_UD_FREE(U) STMT_START { \
224 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
228 /* ... Reap ................................................................ */
235 STATIC void su_call(pTHX_ void *ud_) {
236 su_ud_reap *ud = (su_ud_reap *) ud_;
237 #if SU_HAS_PERL(5, 10, 0)
238 I32 dieing = PL_op->op_type == OP_DIE;
243 SU_D(PerlIO_printf(Perl_debug_log, "%p: @@@ call at %d (save is %d)\n",
244 ud, PL_scopestack_ix, PL_savestack_ix));
251 /* If cxstack_ix isn't incremented there, the eval context will be overwritten
252 * when the new sub scope will be created in call_sv. */
254 #if SU_HAS_PERL(5, 10, 0)
256 if (cxstack_ix < cxstack_max)
259 cxstack_ix = Perl_cxinc(aTHX);
262 call_sv(ud->cb, G_VOID);
264 #if SU_HAS_PERL(5, 10, 0)
265 if (dieing && cxstack_ix > 0)
275 SvREFCNT_dec(ud->cb);
279 STATIC void su_reap(pTHX_ void *ud) {
280 #define su_reap(U) su_reap(aTHX_ (U))
281 SU_D(PerlIO_printf(Perl_debug_log, "%p: === reap at %d (save is %d)\n",
282 ud, PL_scopestack_ix, PL_savestack_ix));
283 SAVEDESTRUCTOR_X(su_call, ud);
284 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
286 PL_scopestack[PL_scopestack_ix]));
289 /* ... Localize & localize array/hash element .............................. */
298 STATIC void su_localize(pTHX_ void *ud_) {
299 #define su_localize(U) su_localize(aTHX_ (U))
300 su_ud_localize *ud = (su_ud_localize *) ud_;
308 if (SvTYPE(sv) >= SVt_PVGV) {
310 if (!val) { /* local *x; */
312 } else if (!SvROK(val)) { /* local *x = $val; */
314 } else { /* local *x = \$val; */
315 t = SvTYPE(SvRV(val));
320 const char *p = SvPV_const(sv, len), *s;
321 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
327 case '$': t = SVt_PV; break;
328 case '@': t = SVt_PVAV; break;
329 case '%': t = SVt_PVHV; break;
330 case '&': t = SVt_PVCV; break;
331 case '*': t = SVt_PVGV; break;
336 } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
337 if (SvROK(val) && !sv_isobject(val)) {
338 t = SvTYPE(SvRV(val));
344 gv = gv_fetchpvn_flags(s, l, GV_ADDMULTI, SVt_PVGV);
348 SV *z = newSV_type(t);
349 PerlIO_printf(Perl_debug_log, "%p: === localize a %s at %d (save is %d)\n",
350 ud, sv_reftype(z, 0),
351 PL_scopestack_ix, PL_savestack_ix);
355 /* Inspired from Alias.pm */
359 su_save_aelem(GvAV(gv), elem, val);
366 su_save_helem(GvHV(gv), elem, val);
372 save_gp(gv, 1); /* hide previous entry in symtab */
379 gv = (GV *) save_scalar(gv);
381 if (deref) /* val != NULL */
386 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
388 PL_scopestack[PL_scopestack_ix]));
392 SvSetMagicSV((SV *) gv, val);
395 SvREFCNT_dec(ud->elem);
396 SvREFCNT_dec(ud->val);
397 SvREFCNT_dec(ud->sv);
401 /* --- Pop a context back -------------------------------------------------- */
405 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
407 # define SU_CXNAME "XXX"
411 STATIC void su_pop(pTHX_ void *ud) {
412 #define su_pop(U) su_pop(aTHX_ (U))
413 I32 depth, base, mark, *origin;
414 depth = SU_UD_DEPTH(ud);
416 SU_D(PerlIO_printf(Perl_debug_log, "%p: --- pop %s at %d from %d to %d [%d]\n",
418 PL_scopestack_ix, PL_savestack_ix,
419 PL_scopestack[PL_scopestack_ix],
422 origin = SU_UD_ORIGIN(ud);
423 mark = origin[depth];
424 base = origin[depth - 1];
426 SU_D(PerlIO_printf(Perl_debug_log, "%p: clean from %d down to %d\n",
430 PL_savestack_ix = mark;
433 PL_savestack_ix = base;
435 SU_UD_DEPTH(ud) = depth;
436 SU_D(PerlIO_printf(Perl_debug_log, "%p: save new destructor at %d [%d]\n",
437 ud, PL_savestack_ix, depth));
438 SAVEDESTRUCTOR_X(su_pop, ud);
439 SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
440 ud, PL_savestack_ix, depth));
442 SU_UD_HANDLER(ud)(aTHX_ ud);
446 /* --- Initialize the stack and the action userdata ------------------------ */
448 STATIC I32 su_init(pTHX_ I32 level, void *ud, I32 size) {
449 #define su_init(L, U, S) su_init(aTHX_ (L), (U), (S))
450 I32 i, depth = 0, *origin;
456 SU_UD_HANDLER(ud)(aTHX_ ud);
460 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for level %d\n", ud, level));
462 for (i = 0; i < level; ++i) {
463 PERL_CONTEXT *cx = &cxstack[cxstack_ix - i];
464 switch (CxTYPE(cx)) {
465 #if SU_HAS_PERL(5, 11, 0)
468 case CXt_LOOP_LAZYSV:
469 case CXt_LOOP_LAZYIV:
473 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
477 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is normal\n", ud, i));
482 SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth));
484 Newx(origin, depth + 1, I32);
485 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
486 PL_scopestack[PL_scopestack_ix - depth] += size;
487 for (i = depth - 1; i >= 1; --i) {
488 I32 j = PL_scopestack_ix - i;
489 origin[depth - i] = PL_scopestack[j];
490 PL_scopestack[j] += 3;
492 origin[depth] = PL_savestack_ix;
495 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
496 depth, 0, PL_scopestack_ix - 1, PL_savestack_ix, origin[depth]);
497 for (i = depth - 1; i >= 0; --i) {
498 I32 x = PL_scopestack_ix - depth + i;
499 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
500 i, depth - i, x, PL_scopestack[x], origin[i]);
504 SU_UD_ORIGIN(ud) = origin;
505 SU_UD_DEPTH(ud) = depth;
507 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
508 ud, PL_savestack_ix, depth));
510 SAVEDESTRUCTOR_X(su_pop, ud);
518 #define SU_GET_LEVEL(A) \
526 if (level > cxstack_ix) \
529 #define SU_DOPOPTOCX(t) \
531 I32 i, cxix = cxstack_ix, from = 0; \
533 from = SvIV(ST(0)); \
534 for (i = cxix - from; i >= 0; --i) { \
535 if (CxTYPE(&cxstack[i]) == t) { \
536 ST(0) = sv_2mortal(newSViv(cxix - i)); \
548 STATIC void su_unwind(pTHX_ void *ud_) {
549 su_ud_unwind *ud = (su_ud_unwind *) ud_;
552 I32 items = ud->items - 1;
555 if (cxstack_ix > cxix)
563 mark = cxstack[cxix - 1].blk_oldsp;
565 if (gimme == G_SCALAR) {
566 *PL_markstack_ptr = PL_stack_sp - PL_stack_base;
567 PL_stack_sp += items;
569 *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
572 PL_op = PL_ppaddr[OP_RETURN](aTHX);
573 *PL_markstack_ptr = mark;
575 fakeop.op_next = PL_op;
581 /* --- XS ------------------------------------------------------------------ */
583 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
585 XS(XS_Scope__Upper_unwind) {
595 Perl_croak(aTHX_ "Usage: Scope::Upper::unwind(..., level)");
596 PERL_UNUSED_VAR(cv); /* -W */
597 PERL_UNUSED_VAR(ax); /* -Wall */
598 level = ST(items - 1);
599 cxix = SvOK(level) ? SvIV(level) : 0;
602 else if (cxix > cxstack_ix)
604 cxix = cxstack_ix - cxix;
606 PERL_CONTEXT *cx = cxstack + cxix;
607 switch (CxTYPE(cx)) {
611 /* pp_entersub will try to sanitize the stack - screw that, we're insane */
612 if (GIMME_V == G_SCALAR)
613 PL_stack_sp = PL_stack_base + TOPMARK + 1;
614 Newx(ud, 1, su_ud_unwind);
617 SAVEDESTRUCTOR_X(su_unwind, ud);
622 } while (--cxix >= 0);
623 croak("Can't return outside a subroutine");
626 MODULE = Scope::Upper PACKAGE = Scope::Upper
632 HV *stash = gv_stashpv(__PACKAGE__, 1);
633 newCONSTSUB(stash, "HERE", newSViv(0));
634 newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
641 RETVAL = newSViv(cxstack_ix);
650 I32 cxix = cxstack_ix;
678 SU_DOPOPTOCX(CXt_SUB);
684 SU_DOPOPTOCX(CXt_EVAL);
694 Newx(ud, 1, su_ud_reap);
695 SU_UD_ORIGIN(ud) = NULL;
696 SU_UD_HANDLER(ud) = su_reap;
697 ud->cb = newSVsv(hook);
698 su_init(level, ud, 3);
701 localize(SV *sv, SV *val, ...)
708 Newx(ud, 1, su_ud_localize);
709 SU_UD_ORIGIN(ud) = NULL;
710 SU_UD_HANDLER(ud) = su_localize;
713 ud->val = newSVsv(val);
715 su_init(level, ud, 3);
718 localize_elem(SV *sv, SV *elem, SV *val, ...)
725 Newx(ud, 1, su_ud_localize);
726 SU_UD_ORIGIN(ud) = NULL;
727 SU_UD_HANDLER(ud) = su_localize;
730 ud->val = newSVsv(val);
733 su_init(level, ud, 4);
736 localize_delete(SV *sv, SV *elem, ...)
743 Newx(ud, 1, su_ud_localize);
744 SU_UD_ORIGIN(ud) = NULL;
745 SU_UD_HANDLER(ud) = su_localize;
751 su_init(level, ud, 4);