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 Newx(v, n, c) New(0, v, n, c)
32 # define SvPV_const(S, L) SvPV(S, L)
35 #ifndef SvPV_nolen_const
36 # define SvPV_nolen_const(S) SvPV_nolen(S)
40 # define HvNAME_get(H) HvNAME(H)
43 #ifndef gv_fetchpvn_flags
44 # define gv_fetchpvn_flags(A, B, C, D) gv_fetchpv((A), (C), (D))
47 #ifndef PERL_MAGIC_tied
48 # define PERL_MAGIC_tied 'P'
51 #ifndef PERL_MAGIC_env
52 # define PERL_MAGIC_env 'E'
55 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
60 void (*handler)(pTHX_ void *);
63 #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth)
64 #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin)
65 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
67 #define SU_UD_FREE(U) do { \
68 if (SU_UD_ORIGIN(U)) Safefree(SU_UD_ORIGIN(U)); \
77 STATIC void su_call(pTHX_ void *ud_) {
78 su_ud_reap *ud = (su_ud_reap *) ud_;
79 #if SU_HAS_PERL(5, 10, 0)
80 I32 dieing = PL_op->op_type == OP_DIE;
85 SU_D(PerlIO_printf(Perl_debug_log, "%p: @@@ call at %d (save is %d)\n",
86 ud, PL_scopestack_ix, PL_savestack_ix));
93 /* If cxstack_ix isn't incremented there, the eval context will be overwritten
94 * when the new sub scope will be created in call_sv. */
96 #if SU_HAS_PERL(5, 10, 0)
98 if (cxstack_ix < cxstack_max)
101 cxstack_ix = Perl_cxinc(aTHX);
104 call_sv(ud->cb, G_VOID);
106 #if SU_HAS_PERL(5, 10, 0)
107 if (dieing && cxstack_ix > 0)
117 SvREFCNT_dec(ud->cb);
121 STATIC void su_reap(pTHX_ void *ud) {
122 #define su_reap(U) su_reap(aTHX_ (U))
123 SU_D(PerlIO_printf(Perl_debug_log, "%p: === reap at %d (save is %d)\n",
124 ud, PL_scopestack_ix, PL_savestack_ix));
125 SAVEDESTRUCTOR_X(su_call, ud);
126 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
128 PL_scopestack[PL_scopestack_ix]));
138 /* Those two functions are courtesy of pp_hot.c:pp_helem */
140 STATIC I32 su_hv_preeminent(pTHX_ HV *hv, SV *keysv) {
141 #define su_hv_preeminent(H, K) su_hv_preeminent(aTHX_ (H), (K))
144 return (!SvRMAGICAL(hv)
145 || mg_find((SV *) hv, PERL_MAGIC_env)
146 || ((mg = mg_find((SV *) hv, PERL_MAGIC_tied))
147 /* Try to preserve the existenceness of a tied hash
148 * element by using EXISTS and DELETE if possible.
149 * Fallback to FETCH and STORE otherwise */
150 && (stash = SvSTASH(SvRV(SvTIED_obj((SV *) hv, mg))))
151 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
152 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
154 ) ? hv_exists_ent(hv, keysv, 0) : 1;
157 STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV **svp, I32 preeminent) {
158 #define su_save_helem(H, K, S, P) su_save_helem(aTHX_ (H), (K), (S), (P))
159 if (HvNAME_get(hv) && isGV(*svp)) {
160 save_gp((GV *) *svp, 0);
165 const char * const key = SvPV_const(keysv, keylen);
166 SAVEDELETE(hv, savepvn(key, keylen),
167 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
169 save_helem(hv, keysv, svp);
173 STATIC void su_localize(pTHX_ void *ud_) {
174 #define su_localize(U) su_localize(aTHX_ (U))
175 su_ud_localize *ud = (su_ud_localize *) ud_;
183 if (SvTYPE(sv) >= SVt_PVGV) {
187 t = SvTYPE(SvRV(val));
191 const char *p = SvPV_const(sv, len), *s;
192 for (s = p, l = len; l > 0 && isSPACE(*s); ++s, --l) { }
198 case '$': t = SVt_PV; break;
199 case '@': t = SVt_PVAV; break;
200 case '%': t = SVt_PVHV; break;
201 case '&': t = SVt_PVCV; break;
202 case '*': t = SVt_PVGV; break;
205 if (SvROK(val) && !sv_isobject(val)) {
206 t = SvTYPE(SvRV(val));
215 gv = gv_fetchpvn_flags(s, l, GV_ADDMULTI, SVt_PVGV);
219 SV *z = newSV_type(t);
220 PerlIO_printf(Perl_debug_log, "%p: === localize a %s at %d (save is %d)\n",
221 ud, sv_reftype(z, 0),
222 PL_scopestack_ix, PL_savestack_ix);
226 /* Inspired from Alias.pm */
230 I32 idx = SvIV(elem);
232 SV **svp = av_fetch(av, idx, 1);
233 if (!*svp || *svp == &PL_sv_undef) croak(PL_no_aelem, idx);
234 save_aelem(av, idx, svp);
243 I32 preeminent = hv ? su_hv_preeminent(hv, elem) : 0;
244 HE *he = hv_fetch_ent(hv, elem, 1, 0);
245 SV **svp = he ? &HeVAL(he) : NULL;
246 if (!svp || *svp == &PL_sv_undef) croak("Modification of non-creatable hash value attempted, subscript \"%s\"", SvPV_nolen_const(*svp));
247 su_save_helem(hv, elem, svp, preeminent);
254 save_gp(gv, 1); /* hide previous entry in symtab */
261 gv = (GV *) save_scalar(gv);
268 SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
270 PL_scopestack[PL_scopestack_ix]));
273 SvSetMagicSV((SV *) gv, val);
275 SvREFCNT_dec(ud->elem);
276 SvREFCNT_dec(ud->val);
277 SvREFCNT_dec(ud->sv);
283 # define SU_CXNAME PL_block_type[CxTYPE(&cxstack[cxstack_ix])]
285 # define SU_CXNAME "XXX"
289 STATIC void su_pop(pTHX_ void *ud) {
290 #define su_pop(U) su_pop(aTHX_ (U))
291 I32 depth, base, mark, *origin;
292 depth = SU_UD_DEPTH(ud);
294 SU_D(PerlIO_printf(Perl_debug_log, "%p: --- pop %s at %d from %d to %d [%d]\n",
296 PL_scopestack_ix, PL_savestack_ix,
297 PL_scopestack[PL_scopestack_ix],
300 origin = SU_UD_ORIGIN(ud);
301 mark = origin[depth];
302 base = origin[depth - 1];
304 SU_D(PerlIO_printf(Perl_debug_log, "%p: clean from %d down to %d\n",
308 PL_savestack_ix = mark;
311 PL_savestack_ix = base;
313 SU_UD_DEPTH(ud) = depth;
314 SU_D(PerlIO_printf(Perl_debug_log, "%p: save new destructor at %d [%d]\n",
315 ud, PL_savestack_ix, depth));
316 SAVEDESTRUCTOR_X(su_pop, ud);
317 SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
318 ud, PL_savestack_ix, depth));
320 SU_UD_HANDLER(ud)(aTHX_ ud);
324 STATIC I32 su_init(pTHX_ I32 level, void *ud, I32 size) {
325 #define su_init(L, U, S) su_init(aTHX_ (L), (U), (S))
326 I32 i, depth = 0, *origin;
329 SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for level %d\n", ud, level));
331 for (i = 0; i < level; ++i) {
332 PERL_CONTEXT *cx = &cxstack[cxstack_ix - i];
333 switch (CxTYPE(cx)) {
334 #if SU_HAS_PERL(5, 11, 0)
337 case CXt_LOOP_LAZYSV:
338 case CXt_LOOP_LAZYIV:
342 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is loop\n", ud, i));
346 SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is normal\n", ud, i));
351 SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth));
353 Newx(origin, depth + 1, I32);
354 origin[0] = PL_scopestack[PL_scopestack_ix - depth];
355 PL_scopestack[PL_scopestack_ix - depth] += size;
356 for (i = depth - 1; i >= 1; --i) {
357 I32 j = PL_scopestack_ix - i;
358 origin[depth - i] = PL_scopestack[j];
359 PL_scopestack[j] += 3;
361 origin[depth] = PL_savestack_ix;
364 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
365 depth, 0, PL_scopestack_ix - 1, PL_savestack_ix, origin[depth]);
366 for (i = depth - 1; i >= 0; --i) {
367 I32 x = PL_scopestack_ix - depth + i;
368 PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
369 i, depth - i, x, PL_scopestack[x], origin[i]);
373 SU_UD_ORIGIN(ud) = origin;
374 SU_UD_DEPTH(ud) = depth;
378 #define SU_GET_LEVEL(A) \
386 if (level > cxstack_ix) \
389 /* --- XS ------------------------------------------------------------------ */
391 MODULE = Scope::Upper PACKAGE = Scope::Upper
399 RETVAL = newSViv(cxstack_ix);
411 Newx(ud, 1, su_ud_reap);
412 SU_UD_ORIGIN(ud) = NULL;
413 SU_UD_HANDLER(ud) = su_reap;
414 ud->cb = newSVsv(hook);
417 I32 depth = su_init(level, ud, 3);
418 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
419 ud, PL_savestack_ix, depth));
420 SAVEDESTRUCTOR_X(su_pop, ud);
426 localize(SV *sv, SV *val, ...)
433 Newx(ud, 1, su_ud_localize);
434 SU_UD_ORIGIN(ud) = NULL;
435 SU_UD_HANDLER(ud) = su_localize;
438 ud->val = newSVsv(val);
442 I32 depth = su_init(level, ud, 3);
443 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
444 ud, PL_savestack_ix, depth));
445 SAVEDESTRUCTOR_X(su_pop, ud);
451 localize_elem(SV *sv, SV *elem, SV *val, ...)
458 Newx(ud, 1, su_ud_localize);
459 SU_UD_ORIGIN(ud) = NULL;
460 SU_UD_HANDLER(ud) = su_localize;
463 ud->val = newSVsv(val);
468 I32 depth = su_init(level, ud, 4);
469 SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
470 ud, PL_savestack_ix, depth));
471 SAVEDESTRUCTOR_X(su_pop, ud);