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 ............................................... */
74 STATIC I32 su_av_preeminent(pTHX_ AV *av, I32 key) {
75 #define su_av_preeminent(A, K) su_av_preeminent(aTHX_ (A), (K))
81 if (SvCANEXISTDELETE(av))
82 return av_exists(av, key);
94 STATIC void su_adelete(pTHX_ void *ud_) {
95 su_ud_adelete *ud = ud_;
97 av_delete(ud->av, ud->key, G_DISCARD);
103 STATIC void su_save_adelete(pTHX_ AV *av, I32 key) {
104 #define su_save_adelete(A, K) su_save_adelete(aTHX_ (A), (K))
107 Newx(ud, 1, su_ud_adelete);
112 SAVEDESTRUCTOR_X(su_adelete, ud);
115 #define SAVEADELETE(A, K) su_save_adelete((A), (K))
117 #endif /* SAVEADELETE */
119 STATIC void su_save_aelem(pTHX_ AV *av, I32 key, SV **svp, I32 preeminent) {
120 #define su_save_aelem(A, K, S, P) su_save_aelem(aTHX_ (A), (K), (S), (P))
122 save_aelem(av, key, svp);
124 SAVEADELETE(av, key);
127 /* ... Saving hash elements ................................................ */
129 STATIC I32 su_hv_preeminent(pTHX_ HV *hv, SV *keysv) {
130 #define su_hv_preeminent(H, K) su_hv_preeminent(aTHX_ (H), (K))
136 if (SvCANEXISTDELETE(hv) || mg_find((SV *) hv, PERL_MAGIC_env))
137 return hv_exists_ent(hv, keysv, 0);
142 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV **svp, I32 preeminent) {
143 #define su_save_helem(H, K, S, P) su_save_helem(aTHX_ (H), (K), (S), (P))
144 if (HvNAME_get(hv) && isGV(*svp)) {
145 save_gp((GV *) *svp, 0);
149 save_helem(hv, keysv, svp);
152 const char * const key = SvPV_const(keysv, keylen);
153 SAVEDELETE(hv, savepvn(key, keylen),
154 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
158 /* --- Actions ------------------------------------------------------------- */
163 void (*handler)(pTHX_ void *);
166 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
167 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
168 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
170 #define SU_UD_FREE(U) do { \
171 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
175 /* ... Reap ................................................................ */
182 STATIC void su_call(pTHX_ void *ud_) {
183 su_ud_reap *ud = (su_ud_reap *) ud_;
184 #if SU_HAS_PERL(5, 10, 0)
185 I32 dieing = PL_op->op_type == OP_DIE;
190 SU_D(PerlIO_printf(Perl_debug_log, "%p: @@@ call at %d (save is %d)\n",
191 ud, PL_scopestack_ix, PL_savestack_ix));
198 /* If cxstack_ix isn't incremented there, the eval context will be overwritten
199 * when the new sub scope will be created in call_sv. */
201 #if SU_HAS_PERL(5, 10, 0)
203 if (cxstack_ix < cxstack_max)
206 cxstack_ix = Perl_cxinc(aTHX);
209 call_sv(ud->cb, G_VOID);
211 #if SU_HAS_PERL(5, 10, 0)
212 if (dieing && cxstack_ix > 0)
222 SvREFCNT_dec(ud->cb);
226 STATIC void su_reap(pTHX_ void *ud) {
227 #define su_reap(U) su_reap(aTHX_ (U))
228 SU_D(PerlIO_printf(Perl_debug_log, "%p: === reap at %d (save is %d)\n",
229 ud, PL_scopestack_ix, PL_savestack_ix));
230 SAVEDESTRUCTOR_X(su_call, ud);
231 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
233 PL_scopestack[PL_scopestack_ix]));
236 /* ... Localize & localize array/hash element .............................. */
245 STATIC void su_localize(pTHX_ void *ud_) {
246 #define su_localize(U) su_localize(aTHX_ (U))
247 su_ud_localize *ud = (su_ud_localize *) ud_;
255 if (SvTYPE(sv) >= SVt_PVGV) {
259 t = SvTYPE(SvRV(val));
263 const char *p = SvPV_const(sv, len), *s;
264 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
270 case '$': t = SVt_PV; break;
271 case '@': t = SVt_PVAV; break;
272 case '%': t = SVt_PVHV; break;
273 case '&': t = SVt_PVCV; break;
274 case '*': t = SVt_PVGV; break;
277 if (SvROK(val) && !sv_isobject(val)) {
278 t = SvTYPE(SvRV(val));
287 gv = gv_fetchpvn_flags(s, l, GV_ADDMULTI, SVt_PVGV);
291 SV *z = newSV_type(t);
292 PerlIO_printf(Perl_debug_log, "%p: === localize a %s at %d (save is %d)\n",
293 ud, sv_reftype(z, 0),
294 PL_scopestack_ix, PL_savestack_ix);
298 /* Inspired from Alias.pm */
302 I32 idx = SvIV(elem);
304 I32 preeminent = su_av_preeminent(av, idx);
305 SV **svp = av_fetch(av, idx, 1);
306 if (!*svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
307 su_save_aelem(av, idx, svp, preeminent);
316 I32 preeminent = su_hv_preeminent(hv, elem);
317 HE *he = hv_fetch_ent(hv, elem, 1, 0);
318 SV **svp = he ? &HeVAL(he) : NULL;
319 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
320 su_save_helem(hv, elem, svp, preeminent);
327 save_gp(gv, 1); /* hide previous entry in symtab */
334 gv = (GV *) save_scalar(gv);
341 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
343 PL_scopestack[PL_scopestack_ix]));
346 SvSetMagicSV((SV *) gv, val);
348 SvREFCNT_dec(ud->elem);
349 SvREFCNT_dec(ud->val);
350 SvREFCNT_dec(ud->sv);
354 /* --- Pop a context back -------------------------------------------------- */
358 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
360 # define SU_CXNAME "XXX"
364 STATIC void su_pop(pTHX_ void *ud) {
365 #define su_pop(U) su_pop(aTHX_ (U))
366 I32 depth, base, mark, *origin;
367 depth = SU_UD_DEPTH(ud);
369 SU_D(PerlIO_printf(Perl_debug_log, "%p: --- pop %s at %d from %d to %d [%d]\n",
371 PL_scopestack_ix, PL_savestack_ix,
372 PL_scopestack[PL_scopestack_ix],
375 origin = SU_UD_ORIGIN(ud);
376 mark = origin[depth];
377 base = origin[depth - 1];
379 SU_D(PerlIO_printf(Perl_debug_log, "%p: clean from %d down to %d\n",
383 PL_savestack_ix = mark;
386 PL_savestack_ix = base;
388 SU_UD_DEPTH(ud) = depth;
389 SU_D(PerlIO_printf(Perl_debug_log, "%p: save new destructor at %d [%d]\n",
390 ud, PL_savestack_ix, depth));
391 SAVEDESTRUCTOR_X(su_pop, ud);
392 SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
393 ud, PL_savestack_ix, depth));
395 SU_UD_HANDLER(ud)(aTHX_ ud);
399 /* --- Initialize the stack and the action userdata ------------------------ */
401 STATIC I32 su_init(pTHX_ I32 level, void *ud, I32 size) {
402 #define su_init(L, U, S) su_init(aTHX_ (L), (U), (S))
403 I32 i, depth = 0, *origin;
406 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for level %d\n", ud, level));
408 for (i = 0; i < level; ++i) {
409 PERL_CONTEXT *cx = &cxstack[cxstack_ix - i];
410 switch (CxTYPE(cx)) {
411 #if SU_HAS_PERL(5, 11, 0)
414 case CXt_LOOP_LAZYSV:
415 case CXt_LOOP_LAZYIV:
419 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
423 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is normal\n", ud, i));
428 SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth));
430 Newx(origin, depth + 1, I32);
431 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
432 PL_scopestack[PL_scopestack_ix - depth] += size;
433 for (i = depth - 1; i >= 1; --i) {
434 I32 j = PL_scopestack_ix - i;
435 origin[depth - i] = PL_scopestack[j];
436 PL_scopestack[j] += 3;
438 origin[depth] = PL_savestack_ix;
441 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
442 depth, 0, PL_scopestack_ix - 1, PL_savestack_ix, origin[depth]);
443 for (i = depth - 1; i >= 0; --i) {
444 I32 x = PL_scopestack_ix - depth + i;
445 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
446 i, depth - i, x, PL_scopestack[x], origin[i]);
450 SU_UD_ORIGIN(ud) = origin;
451 SU_UD_DEPTH(ud) = depth;
455 #define SU_GET_LEVEL(A) \
463 if (level > cxstack_ix) \
466 /* --- XS ------------------------------------------------------------------ */
468 MODULE = Scope::Upper PACKAGE = Scope::Upper
476 RETVAL = newSViv(cxstack_ix);
488 Newx(ud, 1, su_ud_reap);
489 SU_UD_ORIGIN(ud) = NULL;
490 SU_UD_HANDLER(ud) = su_reap;
491 ud->cb = newSVsv(hook);
494 I32 depth = su_init(level, ud, 3);
495 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
496 ud, PL_savestack_ix, depth));
497 SAVEDESTRUCTOR_X(su_pop, ud);
503 localize(SV *sv, SV *val, ...)
510 Newx(ud, 1, su_ud_localize);
511 SU_UD_ORIGIN(ud) = NULL;
512 SU_UD_HANDLER(ud) = su_localize;
515 ud->val = newSVsv(val);
519 I32 depth = su_init(level, ud, 3);
520 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
521 ud, PL_savestack_ix, depth));
522 SAVEDESTRUCTOR_X(su_pop, ud);
528 localize_elem(SV *sv, SV *elem, SV *val, ...)
535 Newx(ud, 1, su_ud_localize);
536 SU_UD_ORIGIN(ud) = NULL;
537 SU_UD_HANDLER(ud) = su_localize;
540 ud->val = newSVsv(val);
545 I32 depth = su_init(level, ud, 4);
546 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
547 ud, PL_savestack_ix, depth));
548 SAVEDESTRUCTOR_X(su_pop, ud);