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
13 /* --- Compatibility ------------------------------------------------------- */
16 # define STMT_START do
20 # define STMT_END while (0)
24 # define SU_D(X) STMT_START X STMT_END
30 # define Newx(v, n, c) New(0, v, n, c)
34 # define SvPV_const(S, L) SvPV(S, L)
37 #ifndef SvPV_nolen_const
38 # define SvPV_nolen_const(S) SvPV_nolen(S)
42 # define HvNAME_get(H) HvNAME(H)
45 #ifndef gv_fetchpvn_flags
46 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
49 #ifndef PERL_MAGIC_tied
50 # define PERL_MAGIC_tied 'P'
53 #ifndef PERL_MAGIC_env
54 # define PERL_MAGIC_env 'E'
57 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
59 /* --- Stack manipulations ------------------------------------------------- */
61 #ifndef SvCANEXISTDELETE
62 # define SvCANEXISTDELETE(sv) \
64 || ((mg = mg_find((SV *) sv, PERL_MAGIC_tied)) \
65 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) sv, mg)))) \
66 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
67 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
72 /* ... Saving array elements ............................................... */
81 STATIC void su_adelete(pTHX_ void *ud_) {
82 su_ud_adelete *ud = ud_;
84 av_delete(ud->av, ud->key, G_DISCARD);
90 STATIC void su_save_adelete(pTHX_ AV *av, I32 key) {
91 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
94 Newx(ud, 1, su_ud_adelete);
99 SAVEDESTRUCTOR_X(su_adelete, ud);
102 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
104 #endif /* SAVEADELETE */
106 STATIC void su_save_aelem(pTHX_ AV *av, SV *key, SV *val) {
107 #define su_save_aelem(A, K, V) su_save_aelem(aTHX_ (A), (K), (V))
114 if (SvCANEXISTDELETE(av))
115 preeminent = av_exists(av, idx);
117 svp = av_fetch(av, idx, 1);
118 if (!*svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
121 save_aelem(av, idx, svp);
123 SAVEADELETE(av, idx);
125 if (val) { /* local $x[$idx] = $val; */
126 SvSetMagicSV(*svp, val);
127 } else { /* local $x[$idx]; delete $x[$idx]; */
128 av_delete(av, idx, G_DISCARD);
132 /* ... Saving hash elements ................................................ */
134 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
135 #define su_save_helem(H, K, V) su_save_helem(aTHX_ (H), (K), (V))
142 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
143 preeminent = hv_exists_ent(hv, keysv, 0);
145 he = hv_fetch_ent(hv, keysv, 1, 0);
146 svp = he ? &HeVAL(he) : NULL;
147 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
149 if (HvNAME_get(hv) && isGV(*svp)) {
150 save_gp((GV *) *svp, 0);
155 save_helem(hv, keysv, svp);
158 const char * const key = SvPV_const(keysv, keylen);
159 SAVEDELETE(hv, savepvn(key, keylen),
160 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
163 if (val) { /* local $x{$keysv} = $val; */
164 SvSetMagicSV(*svp, val);
165 } else { /* local $x{$keysv}; delete $x{$keysv}; */
166 hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
170 /* --- Actions ------------------------------------------------------------- */
175 void (*handler)(pTHX_ void *);
178 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
179 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
180 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
182 #define SU_UD_FREE(U) STMT_START { \
183 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
187 /* ... Reap ................................................................ */
194 STATIC void su_call(pTHX_ void *ud_) {
195 su_ud_reap *ud = (su_ud_reap *) ud_;
196 #if SU_HAS_PERL(5, 10, 0)
197 I32 dieing = PL_op->op_type == OP_DIE;
202 SU_D(PerlIO_printf(Perl_debug_log, "%p: @@@ call at %d (save is %d)\n",
203 ud, PL_scopestack_ix, PL_savestack_ix));
210 /* If cxstack_ix isn't incremented there, the eval context will be overwritten
211 * when the new sub scope will be created in call_sv. */
213 #if SU_HAS_PERL(5, 10, 0)
215 if (cxstack_ix < cxstack_max)
218 cxstack_ix = Perl_cxinc(aTHX);
221 call_sv(ud->cb, G_VOID);
223 #if SU_HAS_PERL(5, 10, 0)
224 if (dieing && cxstack_ix > 0)
234 SvREFCNT_dec(ud->cb);
238 STATIC void su_reap(pTHX_ void *ud) {
239 #define su_reap(U) su_reap(aTHX_ (U))
240 SU_D(PerlIO_printf(Perl_debug_log, "%p: === reap at %d (save is %d)\n",
241 ud, PL_scopestack_ix, PL_savestack_ix));
242 SAVEDESTRUCTOR_X(su_call, ud);
243 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
245 PL_scopestack[PL_scopestack_ix]));
248 /* ... Localize & localize array/hash element .............................. */
257 STATIC void su_localize(pTHX_ void *ud_) {
258 #define su_localize(U) su_localize(aTHX_ (U))
259 su_ud_localize *ud = (su_ud_localize *) ud_;
267 if (SvTYPE(sv) >= SVt_PVGV) {
269 if (!val) { /* local *x; */
271 } else if (!SvROK(val)) { /* local *x = $val; */
273 } else { /* local *x = \$val; */
274 t = SvTYPE(SvRV(val));
279 const char *p = SvPV_const(sv, len), *s;
280 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
286 case '$': t = SVt_PV; break;
287 case '@': t = SVt_PVAV; break;
288 case '%': t = SVt_PVHV; break;
289 case '&': t = SVt_PVCV; break;
290 case '*': t = SVt_PVGV; break;
295 } else if (val) { /* t == SVt_NULL, type can't be inferred from the sigil */
296 if (SvROK(val) && !sv_isobject(val)) {
297 t = SvTYPE(SvRV(val));
303 gv = gv_fetchpvn_flags(s, l, GV_ADDMULTI, SVt_PVGV);
307 SV *z = newSV_type(t);
308 PerlIO_printf(Perl_debug_log, "%p: === localize a %s at %d (save is %d)\n",
309 ud, sv_reftype(z, 0),
310 PL_scopestack_ix, PL_savestack_ix);
314 /* Inspired from Alias.pm */
318 su_save_aelem(GvAV(gv), elem, val);
325 su_save_helem(GvHV(gv), elem, val);
331 save_gp(gv, 1); /* hide previous entry in symtab */
338 gv = (GV *) save_scalar(gv);
340 if (deref) /* val != NULL */
345 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
347 PL_scopestack[PL_scopestack_ix]));
351 SvSetMagicSV((SV *) gv, val);
354 SvREFCNT_dec(ud->elem);
355 SvREFCNT_dec(ud->val);
356 SvREFCNT_dec(ud->sv);
360 /* --- Pop a context back -------------------------------------------------- */
364 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
366 # define SU_CXNAME "XXX"
370 STATIC void su_pop(pTHX_ void *ud) {
371 #define su_pop(U) su_pop(aTHX_ (U))
372 I32 depth, base, mark, *origin;
373 depth = SU_UD_DEPTH(ud);
375 SU_D(PerlIO_printf(Perl_debug_log, "%p: --- pop %s at %d from %d to %d [%d]\n",
377 PL_scopestack_ix, PL_savestack_ix,
378 PL_scopestack[PL_scopestack_ix],
381 origin = SU_UD_ORIGIN(ud);
382 mark = origin[depth];
383 base = origin[depth - 1];
385 SU_D(PerlIO_printf(Perl_debug_log, "%p: clean from %d down to %d\n",
389 PL_savestack_ix = mark;
392 PL_savestack_ix = base;
394 SU_UD_DEPTH(ud) = depth;
395 SU_D(PerlIO_printf(Perl_debug_log, "%p: save new destructor at %d [%d]\n",
396 ud, PL_savestack_ix, depth));
397 SAVEDESTRUCTOR_X(su_pop, ud);
398 SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
399 ud, PL_savestack_ix, depth));
401 SU_UD_HANDLER(ud)(aTHX_ ud);
405 /* --- Initialize the stack and the action userdata ------------------------ */
407 STATIC I32 su_init(pTHX_ I32 level, void *ud, I32 size) {
408 #define su_init(L, U, S) su_init(aTHX_ (L), (U), (S))
409 I32 i, depth = 0, *origin;
415 SU_UD_HANDLER(ud)(aTHX_ ud);
419 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for level %d\n", ud, level));
421 for (i = 0; i < level; ++i) {
422 PERL_CONTEXT *cx = &cxstack[cxstack_ix - i];
423 switch (CxTYPE(cx)) {
424 #if SU_HAS_PERL(5, 11, 0)
427 case CXt_LOOP_LAZYSV:
428 case CXt_LOOP_LAZYIV:
432 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
436 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is normal\n", ud, i));
441 SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth));
443 Newx(origin, depth + 1, I32);
444 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
445 PL_scopestack[PL_scopestack_ix - depth] += size;
446 for (i = depth - 1; i >= 1; --i) {
447 I32 j = PL_scopestack_ix - i;
448 origin[depth - i] = PL_scopestack[j];
449 PL_scopestack[j] += 3;
451 origin[depth] = PL_savestack_ix;
454 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
455 depth, 0, PL_scopestack_ix - 1, PL_savestack_ix, origin[depth]);
456 for (i = depth - 1; i >= 0; --i) {
457 I32 x = PL_scopestack_ix - depth + i;
458 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
459 i, depth - i, x, PL_scopestack[x], origin[i]);
463 SU_UD_ORIGIN(ud) = origin;
464 SU_UD_DEPTH(ud) = depth;
466 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
467 ud, PL_savestack_ix, depth));
469 SAVEDESTRUCTOR_X(su_pop, ud);
477 #define SU_GET_LEVEL(A) \
485 if (level > cxstack_ix) \
488 /* --- XS ------------------------------------------------------------------ */
490 MODULE = Scope::Upper PACKAGE = Scope::Upper
498 RETVAL = newSViv(cxstack_ix);
510 Newx(ud, 1, su_ud_reap);
511 SU_UD_ORIGIN(ud) = NULL;
512 SU_UD_HANDLER(ud) = su_reap;
513 ud->cb = newSVsv(hook);
514 su_init(level, ud, 3);
517 localize(SV *sv, SV *val, ...)
524 Newx(ud, 1, su_ud_localize);
525 SU_UD_ORIGIN(ud) = NULL;
526 SU_UD_HANDLER(ud) = su_localize;
529 ud->val = newSVsv(val);
531 su_init(level, ud, 3);
534 localize_elem(SV *sv, SV *elem, SV *val, ...)
541 Newx(ud, 1, su_ud_localize);
542 SU_UD_ORIGIN(ud) = NULL;
543 SU_UD_HANDLER(ud) = su_localize;
546 ud->val = newSVsv(val);
549 su_init(level, ud, 4);
552 localize_delete(SV *sv, SV *elem, ...)
559 Newx(ud, 1, su_ud_localize);
560 SU_UD_ORIGIN(ud) = NULL;
561 SU_UD_HANDLER(ud) = su_localize;
567 su_init(level, ud, 4);