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
14 # define STMT_START do
18 # define STMT_END while (0)
22 # define SU_D(X) STMT_START X STMT_END
28 # define SvPV_const(S, L) SvPV(S, L)
31 #ifndef SvPV_nolen_const
32 # define SvPV_nolen_const(S) SvPV_nolen(S)
36 # define HvNAME_get(H) HvNAME(H)
39 #ifndef gv_fetchpvn_flags
40 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
43 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
48 void (*handler)(pTHX_ void *);
51 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
52 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
53 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
55 #define SU_UD_FREE(U) do { \
56 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
65 STATIC void su_call(pTHX_ void *ud_) {
66 su_ud_reap *ud = (su_ud_reap *) ud_;
67 #if SU_HAS_PERL(5, 10, 0)
68 I32 dieing = PL_op->op_type == OP_DIE;
73 SU_D(PerlIO_printf(Perl_debug_log, "%p: @@@ call at %d (save is %d)\n",
74 ud, PL_scopestack_ix, PL_savestack_ix));
81 /* If cxstack_ix isn't incremented there, the eval context will be overwritten
82 * when the new sub scope will be created in call_sv. */
84 #if SU_HAS_PERL(5, 10, 0)
86 if (cxstack_ix < cxstack_max)
89 cxstack_ix = Perl_cxinc(aTHX);
92 call_sv(ud->cb, G_VOID);
94 #if SU_HAS_PERL(5, 10, 0)
95 if (dieing && cxstack_ix > 0)
105 SvREFCNT_dec(ud->cb);
109 STATIC void su_reap(pTHX_ void *ud) {
110 #define su_reap(U) su_reap(aTHX_ (U))
111 SU_D(PerlIO_printf(Perl_debug_log, "%p: === reap at %d (save is %d)\n",
112 ud, PL_scopestack_ix, PL_savestack_ix));
113 SAVEDESTRUCTOR_X(su_call, ud);
114 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
116 PL_scopestack[PL_scopestack_ix]));
126 /* Those two functions are courtesy of pp_hot.c:pp_helem */
128 STATIC I32 su_hv_preeminent(pTHX_ HV *hv, SV *keysv) {
129 #define su_hv_preeminent(H, K) su_hv_preeminent(aTHX_ (H), (K))
132 return (!SvRMAGICAL(hv)
133 || mg_find((SV *) hv, PERL_MAGIC_env)
134 || ((mg = mg_find((SV *) hv, PERL_MAGIC_tied))
135 /* Try to preserve the existenceness of a tied hash
136 * element by using EXISTS and DELETE if possible.
137 * Fallback to FETCH and STORE otherwise */
138 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) hv, mg))))
139 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
140 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
142 ) ? hv_exists_ent(hv, keysv, 0) : 1;
145 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV **svp, I32 preeminent) {
146 #define su_save_helem(H, K, S, P) su_save_helem(aTHX_ (H), (K), (S), (P))
147 if (HvNAME_get(hv) && isGV(*svp)) {
148 save_gp((GV *) *svp, 0);
153 const char * const key = SvPV_const(keysv, keylen);
154 SAVEDELETE(hv, savepvn(key, keylen),
155 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
157 save_helem(hv, keysv, svp);
161 STATIC void su_localize(pTHX_ void *ud_) {
162 #define su_localize(U) su_localize(aTHX_ (U))
163 su_ud_localize *ud = (su_ud_localize *) ud_;
171 if (SvTYPE(sv) >= SVt_PVGV) {
175 t = SvTYPE(SvRV(val));
179 const char *p = SvPV_const(sv, len), *s;
180 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
186 case '$': t = SVt_PV; break;
187 case '@': t = SVt_PVAV; break;
188 case '%': t = SVt_PVHV; break;
189 case '&': t = SVt_PVCV; break;
190 case '*': t = SVt_PVGV; break;
193 if (SvROK(val) && !sv_isobject(val)) {
194 t = SvTYPE(SvRV(val));
203 gv = gv_fetchpvn_flags(s, l, GV_ADDMULTI, SVt_PVGV);
207 SV *z = newSV_type(t);
208 PerlIO_printf(Perl_debug_log, "%p: === localize a %s at %d (save is %d)\n",
209 ud, sv_reftype(z, 0),
210 PL_scopestack_ix, PL_savestack_ix);
214 /* Inspired from Alias.pm */
218 I32 idx = SvIV(elem);
220 SV **svp = av_fetch(av, idx, 1);
221 if (!*svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
222 save_aelem(av, idx, svp);
231 I32 preeminent = hv ? su_hv_preeminent(hv, elem) : 0;
232 HE *he = hv_fetch_ent(hv, elem, 1, 0);
233 SV **svp = he ? &HeVAL(he) : NULL;
234 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
235 su_save_helem(hv, elem, svp, preeminent);
242 save_gp(gv, 1); /* hide previous entry in symtab */
249 gv = (GV *) save_scalar(gv);
256 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
258 PL_scopestack[PL_scopestack_ix]));
261 SvSetMagicSV((SV *) gv, val);
263 SvREFCNT_dec(ud->elem);
264 SvREFCNT_dec(ud->val);
265 SvREFCNT_dec(ud->sv);
271 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
273 # define SU_CXNAME "XXX"
277 STATIC void su_pop(pTHX_ void *ud) {
278 #define su_pop(U) su_pop(aTHX_ (U))
279 I32 depth, base, mark, *origin;
280 depth = SU_UD_DEPTH(ud);
282 SU_D(PerlIO_printf(Perl_debug_log, "%p: --- pop %s at %d from %d to %d [%d]\n",
284 PL_scopestack_ix, PL_savestack_ix,
285 PL_scopestack[PL_scopestack_ix],
288 origin = SU_UD_ORIGIN(ud);
289 mark = origin[depth];
290 base = origin[depth - 1];
292 SU_D(PerlIO_printf(Perl_debug_log, "%p: clean from %d down to %d\n",
296 PL_savestack_ix = mark;
299 PL_savestack_ix = base;
301 SU_UD_DEPTH(ud) = depth;
302 SU_D(PerlIO_printf(Perl_debug_log, "%p: save new destructor at %d [%d]\n",
303 ud, PL_savestack_ix, depth));
304 SAVEDESTRUCTOR_X(su_pop, ud);
305 SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
306 ud, PL_savestack_ix, depth));
308 SU_UD_HANDLER(ud)(aTHX_ ud);
312 STATIC I32 su_init(pTHX_ I32 level, void *ud, I32 size) {
313 #define su_init(L, U, S) su_init(aTHX_ (L), (U), (S))
314 I32 i, depth = 0, *origin;
317 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for level %d\n", ud, level));
319 for (i = 0; i < level; ++i) {
320 PERL_CONTEXT *cx = &cxstack[cxstack_ix - i];
321 switch (CxTYPE(cx)) {
322 #if SU_HAS_PERL(5, 11, 0)
325 case CXt_LOOP_LAZYSV:
326 case CXt_LOOP_LAZYIV:
330 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
334 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is normal\n", ud, i));
339 SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth));
341 Newx(origin, depth + 1, I32);
342 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
343 PL_scopestack[PL_scopestack_ix - depth] += size;
344 for (i = depth - 1; i >= 1; --i) {
345 I32 j = PL_scopestack_ix - i;
346 origin[depth - i] = PL_scopestack[j];
347 PL_scopestack[j] += 3;
349 origin[depth] = PL_savestack_ix;
352 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
353 depth, 0, PL_scopestack_ix - 1, PL_savestack_ix, origin[depth]);
354 for (i = depth - 1; i >= 0; --i) {
355 I32 x = PL_scopestack_ix - depth + i;
356 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
357 i, depth - i, x, PL_scopestack[x], origin[i]);
361 SU_UD_ORIGIN(ud) = origin;
362 SU_UD_DEPTH(ud) = depth;
366 #define SU_GET_LEVEL(A) \
374 if (level > cxstack_ix) \
377 /* --- XS ------------------------------------------------------------------ */
379 MODULE = Scope::Upper PACKAGE = Scope::Upper
387 RETVAL = newSViv(cxstack_ix);
399 Newx(ud, 1, su_ud_reap);
400 SU_UD_ORIGIN(ud) = NULL;
401 SU_UD_HANDLER(ud) = su_reap;
402 ud->cb = newSVsv(hook);
405 I32 depth = su_init(level, ud, 3);
406 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
407 ud, PL_savestack_ix, depth));
408 SAVEDESTRUCTOR_X(su_pop, ud);
414 localize(SV *sv, SV *val, ...)
421 Newx(ud, 1, su_ud_localize);
422 SU_UD_ORIGIN(ud) = NULL;
423 SU_UD_HANDLER(ud) = su_localize;
426 ud->val = newSVsv(val);
430 I32 depth = su_init(level, ud, 3);
431 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
432 ud, PL_savestack_ix, depth));
433 SAVEDESTRUCTOR_X(su_pop, ud);
439 localize_elem(SV *sv, SV *elem, SV *val, ...)
446 Newx(ud, 1, su_ud_localize);
447 SU_UD_ORIGIN(ud) = NULL;
448 SU_UD_HANDLER(ud) = su_localize;
451 ud->val = newSVsv(val);
456 I32 depth = su_init(level, ud, 4);
457 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
458 ud, PL_savestack_ix, depth));
459 SAVEDESTRUCTOR_X(su_pop, ud);