/* scope.c
*
- * Copyright (c) 1991-1994, Larry Wall
+ * Copyright (c) 1991-2001, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*/
#include "EXTERN.h"
+#define PERL_IN_SCOPE_C
#include "perl.h"
+#if defined(PERL_FLEXIBLE_EXCEPTIONS)
+void *
+Perl_default_protect(pTHX_ volatile JMPENV *pcur_env, int *excpt,
+ protect_body_t body, ...)
+{
+ void *ret;
+ va_list args;
+ va_start(args, body);
+ ret = vdefault_protect(pcur_env, excpt, body, &args);
+ va_end(args);
+ return ret;
+}
+
+void *
+Perl_vdefault_protect(pTHX_ volatile JMPENV *pcur_env, int *excpt,
+ protect_body_t body, va_list *args)
+{
+ int ex;
+ void *ret;
+
+ JMPENV_PUSH(ex);
+ if (ex)
+ ret = NULL;
+ else
+ ret = CALL_FPTR(body)(aTHX_ *args);
+ *excpt = ex;
+ JMPENV_POP;
+ return ret;
+}
+#endif
+
SV**
-stack_grow(sp, p, n)
-SV** sp;
-SV** p;
-int n;
+Perl_stack_grow(pTHX_ SV **sp, SV **p, int n)
+{
+ PL_stack_sp = sp;
+#ifndef STRESS_REALLOC
+ av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
+#else
+ av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
+#endif
+ return PL_stack_sp;
+}
+
+#ifndef STRESS_REALLOC
+#define GROW(old) ((old) * 3 / 2)
+#else
+#define GROW(old) ((old) + 1)
+#endif
+
+PERL_SI *
+Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
{
- stack_sp = sp;
- av_extend(stack, (p - stack_base) + (n) + 128);
- return stack_sp;
+ PERL_SI *si;
+ New(56, si, 1, PERL_SI);
+ si->si_stack = newAV();
+ AvREAL_off(si->si_stack);
+ av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
+ AvALLOC(si->si_stack)[0] = &PL_sv_undef;
+ AvFILLp(si->si_stack) = 0;
+ si->si_prev = 0;
+ si->si_next = 0;
+ si->si_cxmax = cxitems - 1;
+ si->si_cxix = -1;
+ si->si_type = PERLSI_UNDEF;
+ New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
+ return si;
}
I32
-cxinc()
+Perl_cxinc(pTHX)
{
- cxstack_max = cxstack_max * 3 / 2;
- Renew(cxstack, cxstack_max, CONTEXT);
+ cxstack_max = GROW(cxstack_max);
+ Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
return cxstack_ix + 1;
}
void
-push_return(retop)
-OP *retop;
+Perl_push_return(pTHX_ OP *retop)
{
- if (retstack_ix == retstack_max) {
- retstack_max = retstack_max * 3 / 2;
- Renew(retstack, retstack_max, OP*);
+ if (PL_retstack_ix == PL_retstack_max) {
+ PL_retstack_max = GROW(PL_retstack_max);
+ Renew(PL_retstack, PL_retstack_max, OP*);
}
- retstack[retstack_ix++] = retop;
+ PL_retstack[PL_retstack_ix++] = retop;
}
OP *
-pop_return()
+Perl_pop_return(pTHX)
{
- if (retstack_ix > 0)
- return retstack[--retstack_ix];
+ if (PL_retstack_ix > 0)
+ return PL_retstack[--PL_retstack_ix];
else
return Nullop;
}
void
-push_scope()
+Perl_push_scope(pTHX)
{
- if (scopestack_ix == scopestack_max) {
- scopestack_max = scopestack_max * 3 / 2;
- Renew(scopestack, scopestack_max, I32);
+ if (PL_scopestack_ix == PL_scopestack_max) {
+ PL_scopestack_max = GROW(PL_scopestack_max);
+ Renew(PL_scopestack, PL_scopestack_max, I32);
}
- scopestack[scopestack_ix++] = savestack_ix;
+ PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
}
void
-pop_scope()
+Perl_pop_scope(pTHX)
{
- I32 oldsave = scopestack[--scopestack_ix];
+ I32 oldsave = PL_scopestack[--PL_scopestack_ix];
LEAVE_SCOPE(oldsave);
}
void
-markstack_grow()
+Perl_markstack_grow(pTHX)
{
- I32 oldmax = markstack_max - markstack;
- I32 newmax = oldmax * 3 / 2;
+ I32 oldmax = PL_markstack_max - PL_markstack;
+ I32 newmax = GROW(oldmax);
- Renew(markstack, newmax, I32);
- markstack_ptr = markstack + oldmax;
- markstack_max = markstack + newmax;
+ Renew(PL_markstack, newmax, I32);
+ PL_markstack_ptr = PL_markstack + oldmax;
+ PL_markstack_max = PL_markstack + newmax;
}
void
-savestack_grow()
+Perl_savestack_grow(pTHX)
{
- savestack_max = savestack_max * 3 / 2;
- Renew(savestack, savestack_max, ANY);
+ PL_savestack_max = GROW(PL_savestack_max) + 4;
+ Renew(PL_savestack, PL_savestack_max, ANY);
}
+#undef GROW
+
void
-free_tmps()
+Perl_tmps_grow(pTHX_ I32 n)
+{
+#ifndef STRESS_REALLOC
+ if (n < 128)
+ n = (PL_tmps_max < 512) ? 128 : 512;
+#endif
+ PL_tmps_max = PL_tmps_ix + n + 1;
+ Renew(PL_tmps_stack, PL_tmps_max, SV*);
+}
+
+
+void
+Perl_free_tmps(pTHX)
{
/* XXX should tmps_floor live in cxstack? */
- I32 myfloor = tmps_floor;
- while (tmps_ix > myfloor) { /* clean up after last statement */
- SV* sv = tmps_stack[tmps_ix];
- tmps_stack[tmps_ix--] = Nullsv;
- if (sv) {
-#ifdef DEBUGGING
+ I32 myfloor = PL_tmps_floor;
+ while (PL_tmps_ix > myfloor) { /* clean up after last statement */
+ SV* sv = PL_tmps_stack[PL_tmps_ix];
+ PL_tmps_stack[PL_tmps_ix--] = Nullsv;
+ if (sv && sv != &PL_sv_undef) {
SvTEMP_off(sv);
-#endif
SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
}
}
}
-SV *
-save_scalar(gv)
-GV *gv;
+STATIC SV *
+S_save_scalar_at(pTHX_ SV **sptr)
{
register SV *sv;
- SV *osv = GvSV(gv);
-
- SSCHECK(3);
- SSPUSHPTR(gv);
- SSPUSHPTR(osv);
- SSPUSHINT(SAVEt_SV);
+ SV *osv = *sptr;
- sv = GvSV(gv) = NEWSV(0,0);
+ sv = *sptr = NEWSV(0,0);
if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
sv_upgrade(sv, SvTYPE(osv));
if (SvGMAGICAL(osv)) {
MAGIC* mg;
- bool oldtainted = tainted;
- mg_get(osv);
- if (tainting && tainted && (mg = mg_find(osv, 't'))) {
+ bool oldtainted = PL_tainted;
+ mg_get(osv); /* note, can croak! */
+ if (PL_tainting && PL_tainted &&
+ (mg = mg_find(osv, PERL_MAGIC_taint))) {
SAVESPTR(mg->mg_obj);
mg->mg_obj = osv;
}
SvFLAGS(osv) |= (SvFLAGS(osv) &
- (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
- tainted = oldtainted;
+ (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
+ PL_tainted = oldtainted;
}
SvMAGIC(sv) = SvMAGIC(osv);
SvFLAGS(sv) |= SvMAGICAL(osv);
- localizing = 1;
+ /* XXX SvMAGIC() is *shared* between osv and sv. This can
+ * lead to coredumps when both SVs are destroyed without one
+ * of their SvMAGIC() slots being NULLed. */
+ PL_localizing = 1;
SvSETMAGIC(sv);
- localizing = 0;
+ PL_localizing = 0;
}
return sv;
}
-#ifdef INLINED_ELSEWHERE
-void
-save_gp(gv)
-GV *gv;
+SV *
+Perl_save_scalar(pTHX_ GV *gv)
{
- register GP *gp;
- GP *ogp = GvGP(gv);
-
+ SV **sptr = &GvSV(gv);
SSCHECK(3);
SSPUSHPTR(SvREFCNT_inc(gv));
- SSPUSHPTR(ogp);
- SSPUSHINT(SAVEt_GP);
-
- Newz(602,gp, 1, GP);
- GvGP(gv) = gp;
- GvREFCNT(gv) = 1;
- GvSV(gv) = NEWSV(72,0);
- GvLINE(gv) = curcop->cop_line;
- GvEGV(gv) = gv;
+ SSPUSHPTR(SvREFCNT_inc(*sptr));
+ SSPUSHINT(SAVEt_SV);
+ return save_scalar_at(sptr);
}
-#endif
SV*
-save_svref(sptr)
-SV **sptr;
+Perl_save_svref(pTHX_ SV **sptr)
{
- register SV *sv;
- SV *osv = *sptr;
-
SSCHECK(3);
- SSPUSHPTR(*sptr);
SSPUSHPTR(sptr);
+ SSPUSHPTR(SvREFCNT_inc(*sptr));
SSPUSHINT(SAVEt_SVREF);
+ return save_scalar_at(sptr);
+}
- sv = *sptr = NEWSV(0,0);
- if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
- sv_upgrade(sv, SvTYPE(osv));
- if (SvGMAGICAL(osv)) {
- MAGIC* mg;
- bool oldtainted = tainted;
- mg_get(osv);
- if (tainting && tainted && (mg = mg_find(osv, 't'))) {
- SAVESPTR(mg->mg_obj);
- mg->mg_obj = osv;
- }
- SvFLAGS(osv) |= (SvFLAGS(osv) &
- (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
- tainted = oldtainted;
+/* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
+ * restore a global SV to its prior contents, freeing new value. */
+void
+Perl_save_generic_svref(pTHX_ SV **sptr)
+{
+ SSCHECK(3);
+ SSPUSHPTR(sptr);
+ SSPUSHPTR(SvREFCNT_inc(*sptr));
+ SSPUSHINT(SAVEt_GENERIC_SVREF);
+}
+
+/* Like save_pptr(), but also Safefree()s the new value if it is different
+ * from the old one. Can be used to restore a global char* to its prior
+ * contents, freeing new value. */
+void
+Perl_save_generic_pvref(pTHX_ char **str)
+{
+ SSCHECK(3);
+ SSPUSHPTR(str);
+ SSPUSHPTR(*str);
+ SSPUSHINT(SAVEt_GENERIC_PVREF);
+}
+
+void
+Perl_save_gp(pTHX_ GV *gv, I32 empty)
+{
+ SSCHECK(6);
+ SSPUSHIV((IV)SvLEN(gv));
+ SvLEN(gv) = 0; /* forget that anything was allocated here */
+ SSPUSHIV((IV)SvCUR(gv));
+ SSPUSHPTR(SvPVX(gv));
+ SvPOK_off(gv);
+ SSPUSHPTR(SvREFCNT_inc(gv));
+ SSPUSHPTR(GvGP(gv));
+ SSPUSHINT(SAVEt_GP);
+
+ if (empty) {
+ register GP *gp;
+
+ Newz(602, gp, 1, GP);
+
+ if (GvCVu(gv))
+ PL_sub_generation++; /* taking a method out of circulation */
+ if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
+ gp->gp_io = newIO();
+ IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
}
- SvMAGIC(sv) = SvMAGIC(osv);
- SvFLAGS(sv) |= SvMAGICAL(osv);
- localizing = 1;
- SvSETMAGIC(sv);
- localizing = 0;
+ GvGP(gv) = gp_ref(gp);
+ GvSV(gv) = NEWSV(72,0);
+ GvLINE(gv) = CopLINE(PL_curcop);
+ GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : "";
+ GvEGV(gv) = gv;
+ }
+ else {
+ gp_ref(GvGP(gv));
+ GvINTRO_on(gv);
}
- return sv;
}
AV *
-save_ary(gv)
-GV *gv;
+Perl_save_ary(pTHX_ GV *gv)
{
+ AV *oav = GvAVn(gv);
+ AV *av;
+
+ if (!AvREAL(oav) && AvREIFY(oav))
+ av_reify(oav);
SSCHECK(3);
SSPUSHPTR(gv);
- SSPUSHPTR(GvAVn(gv));
+ SSPUSHPTR(oav);
SSPUSHINT(SAVEt_AV);
GvAV(gv) = Null(AV*);
- return GvAVn(gv);
+ av = GvAVn(gv);
+ if (SvMAGIC(oav)) {
+ SvMAGIC(av) = SvMAGIC(oav);
+ SvFLAGS((SV*)av) |= SvMAGICAL(oav);
+ SvMAGICAL_off(oav);
+ SvMAGIC(oav) = 0;
+ PL_localizing = 1;
+ SvSETMAGIC((SV*)av);
+ PL_localizing = 0;
+ }
+ return av;
}
HV *
-save_hash(gv)
-GV *gv;
+Perl_save_hash(pTHX_ GV *gv)
{
+ HV *ohv, *hv;
+
SSCHECK(3);
SSPUSHPTR(gv);
- SSPUSHPTR(GvHVn(gv));
+ SSPUSHPTR(ohv = GvHVn(gv));
SSPUSHINT(SAVEt_HV);
GvHV(gv) = Null(HV*);
- return GvHVn(gv);
+ hv = GvHVn(gv);
+ if (SvMAGIC(ohv)) {
+ SvMAGIC(hv) = SvMAGIC(ohv);
+ SvFLAGS((SV*)hv) |= SvMAGICAL(ohv);
+ SvMAGICAL_off(ohv);
+ SvMAGIC(ohv) = 0;
+ PL_localizing = 1;
+ SvSETMAGIC((SV*)hv);
+ PL_localizing = 0;
+ }
+ return hv;
}
void
-save_item(item)
-register SV *item;
+Perl_save_item(pTHX_ register SV *item)
{
- register SV *sv;
+ register SV *sv = NEWSV(0,0);
+ sv_setsv(sv,item);
SSCHECK(3);
SSPUSHPTR(item); /* remember the pointer */
- sv = NEWSV(0,0);
- sv_setsv(sv,item);
SSPUSHPTR(sv); /* remember the value */
SSPUSHINT(SAVEt_ITEM);
}
void
-save_int(intp)
-int *intp;
+Perl_save_int(pTHX_ int *intp)
{
SSCHECK(3);
SSPUSHINT(*intp);
}
void
-save_long(longp)
-long *longp;
+Perl_save_long(pTHX_ long int *longp)
{
SSCHECK(3);
SSPUSHLONG(*longp);
}
void
-save_I32(intp)
-I32 *intp;
+Perl_save_I32(pTHX_ I32 *intp)
{
SSCHECK(3);
SSPUSHINT(*intp);
}
void
-save_iv(ivp)
-IV *ivp;
+Perl_save_I16(pTHX_ I16 *intp)
+{
+ SSCHECK(3);
+ SSPUSHINT(*intp);
+ SSPUSHPTR(intp);
+ SSPUSHINT(SAVEt_I16);
+}
+
+void
+Perl_save_I8(pTHX_ I8 *bytep)
+{
+ SSCHECK(3);
+ SSPUSHINT(*bytep);
+ SSPUSHPTR(bytep);
+ SSPUSHINT(SAVEt_I8);
+}
+
+void
+Perl_save_iv(pTHX_ IV *ivp)
{
SSCHECK(3);
SSPUSHIV(*ivp);
* force word-alignment and we'll miss the pointer.
*/
void
-save_pptr(pptr)
-char **pptr;
+Perl_save_pptr(pTHX_ char **pptr)
{
SSCHECK(3);
SSPUSHPTR(*pptr);
}
void
-save_sptr(sptr)
-SV **sptr;
+Perl_save_vptr(pTHX_ void *ptr)
+{
+ SSCHECK(3);
+ SSPUSHPTR(*(char**)ptr);
+ SSPUSHPTR(ptr);
+ SSPUSHINT(SAVEt_VPTR);
+}
+
+void
+Perl_save_sptr(pTHX_ SV **sptr)
{
SSCHECK(3);
SSPUSHPTR(*sptr);
}
void
-save_nogv(gv)
-GV *gv;
+Perl_save_padsv(pTHX_ PADOFFSET off)
+{
+ SSCHECK(4);
+ SSPUSHPTR(PL_curpad[off]);
+ SSPUSHPTR(PL_curpad);
+ SSPUSHLONG((long)off);
+ SSPUSHINT(SAVEt_PADSV);
+}
+
+SV **
+Perl_save_threadsv(pTHX_ PADOFFSET i)
+{
+#ifdef USE_5005THREADS
+ SV **svp = &THREADSV(i); /* XXX Change to save by offset */
+ DEBUG_S(PerlIO_printf(Perl_debug_log, "save_threadsv %"UVuf": %p %p:%s\n",
+ (UV)i, svp, *svp, SvPEEK(*svp)));
+ save_svref(svp);
+ return svp;
+#else
+ Perl_croak(aTHX_ "panic: save_threadsv called in non-threaded perl");
+ return 0;
+#endif /* USE_5005THREADS */
+}
+
+void
+Perl_save_nogv(pTHX_ GV *gv)
{
SSCHECK(2);
SSPUSHPTR(gv);
}
void
-save_hptr(hptr)
-HV **hptr;
+Perl_save_hptr(pTHX_ HV **hptr)
{
SSCHECK(3);
SSPUSHPTR(*hptr);
}
void
-save_aptr(aptr)
-AV **aptr;
+Perl_save_aptr(pTHX_ AV **aptr)
{
SSCHECK(3);
SSPUSHPTR(*aptr);
}
void
-save_freesv(sv)
-SV *sv;
+Perl_save_freesv(pTHX_ SV *sv)
{
SSCHECK(2);
SSPUSHPTR(sv);
}
void
-save_freeop(op)
-OP *op;
+Perl_save_mortalizesv(pTHX_ SV *sv)
{
SSCHECK(2);
- SSPUSHPTR(op);
+ SSPUSHPTR(sv);
+ SSPUSHINT(SAVEt_MORTALIZESV);
+}
+
+void
+Perl_save_freeop(pTHX_ OP *o)
+{
+ SSCHECK(2);
+ SSPUSHPTR(o);
SSPUSHINT(SAVEt_FREEOP);
}
void
-save_freepv(pv)
-char *pv;
+Perl_save_freepv(pTHX_ char *pv)
{
SSCHECK(2);
SSPUSHPTR(pv);
}
void
-save_clearsv(svp)
-SV** svp;
+Perl_save_clearsv(pTHX_ SV **svp)
{
SSCHECK(2);
- SSPUSHLONG((long)(svp-curpad));
+ SSPUSHLONG((long)(svp-PL_curpad));
SSPUSHINT(SAVEt_CLEARSV);
}
void
-save_delete(hv,key,klen)
-HV *hv;
-char *key;
-I32 klen;
+Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
{
SSCHECK(4);
SSPUSHINT(klen);
SSPUSHPTR(key);
- SSPUSHPTR(hv);
+ SSPUSHPTR(SvREFCNT_inc(hv));
SSPUSHINT(SAVEt_DELETE);
}
void
-save_list(sarg,maxsarg)
-register SV **sarg;
-I32 maxsarg;
+Perl_save_list(pTHX_ register SV **sarg, I32 maxsarg)
{
register SV *sv;
register I32 i;
- SSCHECK(3 * maxsarg);
for (i = 1; i <= maxsarg; i++) {
- SSPUSHPTR(sarg[i]); /* remember the pointer */
sv = NEWSV(0,0);
sv_setsv(sv,sarg[i]);
+ SSCHECK(3);
+ SSPUSHPTR(sarg[i]); /* remember the pointer */
SSPUSHPTR(sv); /* remember the value */
SSPUSHINT(SAVEt_ITEM);
}
}
void
-save_destructor(f,p)
-void (*f) _((void*));
-void* p;
+Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
{
SSCHECK(3);
SSPUSHDPTR(f);
}
void
-leave_scope(base)
-I32 base;
+Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
+{
+ SSCHECK(3);
+ SSPUSHDXPTR(f);
+ SSPUSHPTR(p);
+ SSPUSHINT(SAVEt_DESTRUCTOR_X);
+}
+
+void
+Perl_save_aelem(pTHX_ AV *av, I32 idx, SV **sptr)
+{
+ SSCHECK(4);
+ SSPUSHPTR(SvREFCNT_inc(av));
+ SSPUSHINT(idx);
+ SSPUSHPTR(SvREFCNT_inc(*sptr));
+ SSPUSHINT(SAVEt_AELEM);
+ save_scalar_at(sptr);
+}
+
+void
+Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
+{
+ SSCHECK(4);
+ SSPUSHPTR(SvREFCNT_inc(hv));
+ SSPUSHPTR(SvREFCNT_inc(key));
+ SSPUSHPTR(SvREFCNT_inc(*sptr));
+ SSPUSHINT(SAVEt_HELEM);
+ save_scalar_at(sptr);
+}
+
+void
+Perl_save_op(pTHX)
+{
+ SSCHECK(2);
+ SSPUSHPTR(PL_op);
+ SSPUSHINT(SAVEt_OP);
+}
+
+I32
+Perl_save_alloc(pTHX_ I32 size, I32 pad)
+{
+ register I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
+ - (char*)PL_savestack);
+ register I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
+
+ /* SSCHECK may not be good enough */
+ while (PL_savestack_ix + elems + 2 > PL_savestack_max)
+ savestack_grow();
+
+ PL_savestack_ix += elems;
+ SSPUSHINT(elems);
+ SSPUSHINT(SAVEt_ALLOC);
+ return start;
+}
+
+void
+Perl_leave_scope(pTHX_ I32 base)
{
register SV *sv;
register SV *value;
register AV *av;
register HV *hv;
register void* ptr;
+ register char* str;
+ I32 i;
if (base < -1)
- croak("panic: corrupt saved stack index");
- while (savestack_ix > base) {
+ Perl_croak(aTHX_ "panic: corrupt saved stack index");
+ while (PL_savestack_ix > base) {
switch (SSPOPINT) {
case SAVEt_ITEM: /* normal string */
value = (SV*)SSPOPPTR;
sv = (SV*)SSPOPPTR;
sv_replace(sv,value);
- localizing = 2;
+ PL_localizing = 2;
SvSETMAGIC(sv);
- localizing = 0;
+ PL_localizing = 0;
break;
- case SAVEt_SV: /* scalar reference */
+ case SAVEt_SV: /* scalar reference */
value = (SV*)SSPOPPTR;
gv = (GV*)SSPOPPTR;
- sv = GvSV(gv);
- if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
- SvTYPE(sv) != SVt_PVGV)
- {
- (void)SvUPGRADE(value, SvTYPE(sv));
- SvMAGIC(value) = SvMAGIC(sv);
- SvFLAGS(value) |= SvMAGICAL(sv);
- SvMAGICAL_off(sv);
- SvMAGIC(sv) = 0;
+ ptr = &GvSV(gv);
+ SvREFCNT_dec(gv);
+ goto restore_sv;
+ case SAVEt_GENERIC_PVREF: /* generic pv */
+ str = (char*)SSPOPPTR;
+ ptr = SSPOPPTR;
+ if (*(char**)ptr != str) {
+ Safefree(*(char**)ptr);
+ *(char**)ptr = str;
}
- SvREFCNT_dec(sv);
- GvSV(gv) = value;
- localizing = 2;
- SvSETMAGIC(value);
- localizing = 0;
- break;
- case SAVEt_SVREF: /* scalar reference */
+ break;
+ case SAVEt_GENERIC_SVREF: /* generic sv */
+ value = (SV*)SSPOPPTR;
ptr = SSPOPPTR;
sv = *(SV**)ptr;
+ *(SV**)ptr = value;
+ SvREFCNT_dec(sv);
+ SvREFCNT_dec(value);
+ break;
+ case SAVEt_SVREF: /* scalar reference */
value = (SV*)SSPOPPTR;
+ ptr = SSPOPPTR;
+ restore_sv:
+ sv = *(SV**)ptr;
+ DEBUG_S(PerlIO_printf(Perl_debug_log,
+ "restore svref: %p %p:%s -> %p:%s\n",
+ ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
SvTYPE(sv) != SVt_PVGV)
{
SvMAGICAL_off(sv);
SvMAGIC(sv) = 0;
}
- SvREFCNT_dec(sv);
+ /* XXX This branch is pretty bogus. This code irretrievably
+ * clears(!) the magic on the SV (either to avoid further
+ * croaking that might ensue when the SvSETMAGIC() below is
+ * called, or to avoid two different SVs pointing at the same
+ * SvMAGIC()). This needs a total rethink. --GSAR */
+ else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
+ SvTYPE(value) != SVt_PVGV)
+ {
+ SvFLAGS(value) |= (SvFLAGS(value) &
+ (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
+ SvMAGICAL_off(value);
+ /* XXX this is a leak when we get here because the
+ * mg_get() in save_scalar_at() croaked */
+ SvMAGIC(value) = 0;
+ }
+ SvREFCNT_dec(sv);
*(SV**)ptr = value;
- localizing = 2;
+ PL_localizing = 2;
SvSETMAGIC(value);
- localizing = 0;
- break;
- case SAVEt_AV: /* array reference */
+ PL_localizing = 0;
+ SvREFCNT_dec(value);
+ break;
+ case SAVEt_AV: /* array reference */
av = (AV*)SSPOPPTR;
gv = (GV*)SSPOPPTR;
- SvREFCNT_dec(GvAV(gv));
- GvAV(gv) = av;
- break;
- case SAVEt_HV: /* hash reference */
+ if (GvAV(gv)) {
+ AV *goner = GvAV(gv);
+ SvMAGIC(av) = SvMAGIC(goner);
+ SvFLAGS((SV*)av) |= SvMAGICAL(goner);
+ SvMAGICAL_off(goner);
+ SvMAGIC(goner) = 0;
+ SvREFCNT_dec(goner);
+ }
+ GvAV(gv) = av;
+ if (SvMAGICAL(av)) {
+ PL_localizing = 2;
+ SvSETMAGIC((SV*)av);
+ PL_localizing = 0;
+ }
+ break;
+ case SAVEt_HV: /* hash reference */
hv = (HV*)SSPOPPTR;
gv = (GV*)SSPOPPTR;
- SvREFCNT_dec(GvHV(gv));
- GvHV(gv) = hv;
- break;
+ if (GvHV(gv)) {
+ HV *goner = GvHV(gv);
+ SvMAGIC(hv) = SvMAGIC(goner);
+ SvFLAGS(hv) |= SvMAGICAL(goner);
+ SvMAGICAL_off(goner);
+ SvMAGIC(goner) = 0;
+ SvREFCNT_dec(goner);
+ }
+ GvHV(gv) = hv;
+ if (SvMAGICAL(hv)) {
+ PL_localizing = 2;
+ SvSETMAGIC((SV*)hv);
+ PL_localizing = 0;
+ }
+ break;
case SAVEt_INT: /* int reference */
ptr = SSPOPPTR;
*(int*)ptr = (int)SSPOPINT;
ptr = SSPOPPTR;
*(I32*)ptr = (I32)SSPOPINT;
break;
+ case SAVEt_I16: /* I16 reference */
+ ptr = SSPOPPTR;
+ *(I16*)ptr = (I16)SSPOPINT;
+ break;
+ case SAVEt_I8: /* I8 reference */
+ ptr = SSPOPPTR;
+ *(I8*)ptr = (I8)SSPOPINT;
+ break;
case SAVEt_IV: /* IV reference */
ptr = SSPOPPTR;
*(IV*)ptr = (IV)SSPOPIV;
ptr = SSPOPPTR;
*(SV**)ptr = (SV*)SSPOPPTR;
break;
+ case SAVEt_VPTR: /* random* reference */
case SAVEt_PPTR: /* char* reference */
ptr = SSPOPPTR;
*(char**)ptr = (char*)SSPOPPTR;
break;
case SAVEt_NSTAB:
gv = (GV*)SSPOPPTR;
- (void)sv_clear(gv);
+ (void)sv_clear((SV*)gv);
break;
- case SAVEt_GP: /* scalar reference */
+ case SAVEt_GP: /* scalar reference */
ptr = SSPOPPTR;
gv = (GV*)SSPOPPTR;
- gp_free(gv);
- GvGP(gv) = (GP*)ptr;
+ if (SvPVX(gv) && SvLEN(gv) > 0) {
+ Safefree(SvPVX(gv));
+ }
+ SvPVX(gv) = (char *)SSPOPPTR;
+ SvCUR(gv) = (STRLEN)SSPOPIV;
+ SvLEN(gv) = (STRLEN)SSPOPIV;
+ gp_free(gv);
+ GvGP(gv) = (GP*)ptr;
+ if (GvCVu(gv))
+ PL_sub_generation++; /* putting a method back into circulation */
SvREFCNT_dec(gv);
- break;
+ break;
case SAVEt_FREESV:
ptr = SSPOPPTR;
SvREFCNT_dec((SV*)ptr);
break;
+ case SAVEt_MORTALIZESV:
+ ptr = SSPOPPTR;
+ sv_2mortal((SV*)ptr);
+ break;
case SAVEt_FREEOP:
ptr = SSPOPPTR;
- curpad = AvARRAY(comppad);
+ if (PL_comppad)
+ PL_curpad = AvARRAY(PL_comppad);
op_free((OP*)ptr);
break;
case SAVEt_FREEPV:
Safefree((char*)ptr);
break;
case SAVEt_CLEARSV:
- ptr = (void*)&curpad[SSPOPLONG];
+ ptr = (void*)&PL_curpad[SSPOPLONG];
sv = *(SV**)ptr;
- if (SvREFCNT(sv) <= 1) { /* Can clear pad variable in place. */
- if (SvTHINKFIRST(sv)) {
- if (SvREADONLY(sv))
- croak("panic: leave_scope clearsv");
- if (SvROK(sv))
- sv_unref(sv);
- }
+ /* Can clear pad variable in place? */
+ if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
+ /*
+ * if a my variable that was made readonly is going out of
+ * scope, we want to remove the readonlyness so that it can
+ * go out of scope quietly
+ */
+ if (SvPADMY(sv) && !SvFAKE(sv))
+ SvREADONLY_off(sv);
+
+ if (SvTHINKFIRST(sv))
+ sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF);
if (SvMAGICAL(sv))
mg_free(sv);
hv_clear((HV*)sv);
break;
case SVt_PVCV:
- sub_generation++;
- cv_undef((CV*)sv);
+ Perl_croak(aTHX_ "panic: leave_scope pad code");
+ case SVt_RV:
+ case SVt_IV:
+ case SVt_NV:
+ (void)SvOK_off(sv);
break;
default:
- if (SvPOK(sv) && SvLEN(sv))
- (void)SvOOK_off(sv);
(void)SvOK_off(sv);
+ (void)SvOOK_off(sv);
break;
}
}
else { /* Someone has a claim on this, so abandon it. */
U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
- SvREFCNT_dec(sv); /* Cast current value to the winds. */
switch (SvTYPE(sv)) { /* Console ourselves with a new value */
case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
default: *(SV**)ptr = NEWSV(0,0); break;
}
+ SvREFCNT_dec(sv); /* Cast current value to the winds. */
SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
}
break;
hv = (HV*)ptr;
ptr = SSPOPPTR;
(void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
+ SvREFCNT_dec(hv);
Safefree(ptr);
break;
case SAVEt_DESTRUCTOR:
ptr = SSPOPPTR;
(*SSPOPDPTR)(ptr);
break;
+ case SAVEt_DESTRUCTOR_X:
+ ptr = SSPOPPTR;
+ (*SSPOPDXPTR)(aTHX_ ptr);
+ break;
case SAVEt_REGCONTEXT:
+ case SAVEt_ALLOC:
+ i = SSPOPINT;
+ PL_savestack_ix -= i; /* regexp must have croaked */
+ break;
+ case SAVEt_STACK_POS: /* Position on Perl stack */
+ i = SSPOPINT;
+ PL_stack_sp = PL_stack_base + i;
+ break;
+ case SAVEt_AELEM: /* array element */
+ value = (SV*)SSPOPPTR;
+ i = SSPOPINT;
+ av = (AV*)SSPOPPTR;
+ ptr = av_fetch(av,i,1);
+ if (ptr) {
+ sv = *(SV**)ptr;
+ if (sv && sv != &PL_sv_undef) {
+ if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
+ (void)SvREFCNT_inc(sv);
+ SvREFCNT_dec(av);
+ goto restore_sv;
+ }
+ }
+ SvREFCNT_dec(av);
+ SvREFCNT_dec(value);
+ break;
+ case SAVEt_HELEM: /* hash element */
+ value = (SV*)SSPOPPTR;
+ sv = (SV*)SSPOPPTR;
+ hv = (HV*)SSPOPPTR;
+ ptr = hv_fetch_ent(hv, sv, 1, 0);
+ if (ptr) {
+ SV *oval = HeVAL((HE*)ptr);
+ if (oval && oval != &PL_sv_undef) {
+ ptr = &HeVAL((HE*)ptr);
+ if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
+ (void)SvREFCNT_inc(*(SV**)ptr);
+ SvREFCNT_dec(hv);
+ SvREFCNT_dec(sv);
+ goto restore_sv;
+ }
+ }
+ SvREFCNT_dec(hv);
+ SvREFCNT_dec(sv);
+ SvREFCNT_dec(value);
+ break;
+ case SAVEt_OP:
+ PL_op = (OP*)SSPOPPTR;
+ break;
+ case SAVEt_HINTS:
+ *(I32*)&PL_hints = (I32)SSPOPINT;
+ break;
+ case SAVEt_COMPPAD:
+ PL_comppad = (AV*)SSPOPPTR;
+ if (PL_comppad)
+ PL_curpad = AvARRAY(PL_comppad);
+ else
+ PL_curpad = Null(SV**);
+ break;
+ case SAVEt_PADSV:
{
- I32 delta = SSPOPINT;
- savestack_ix -= delta; /* regexp must have croaked */
+ PADOFFSET off = (PADOFFSET)SSPOPLONG;
+ ptr = SSPOPPTR;
+ if (ptr)
+ ((SV**)ptr)[off] = (SV*)SSPOPPTR;
}
break;
default:
- croak("panic: leave_scope inconsistency");
+ Perl_croak(aTHX_ "panic: leave_scope inconsistency");
}
}
}
-#ifdef DEBUGGING
void
-cx_dump(cx)
-CONTEXT* cx;
-{
- fprintf(stderr, "CX %d = %s\n", cx - cxstack, block_type[cx->cx_type]);
- if (cx->cx_type != CXt_SUBST) {
- fprintf(stderr, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
- fprintf(stderr, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
- fprintf(stderr, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
- fprintf(stderr, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
- fprintf(stderr, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
- fprintf(stderr, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
- fprintf(stderr, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
+Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
+{
+#ifdef DEBUGGING
+ PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
+ if (CxTYPE(cx) != CXt_SUBST) {
+ PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
+ PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_oldcop));
+ PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
+ PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
+ PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
+ PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_oldpm));
+ PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
}
- switch (cx->cx_type) {
+ switch (CxTYPE(cx)) {
case CXt_NULL:
case CXt_BLOCK:
break;
+ case CXt_FORMAT:
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_sub.cv));
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_sub.gv));
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_sub.dfoutgv));
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
+ (int)cx->blk_sub.hasargs);
+ break;
case CXt_SUB:
- fprintf(stderr, "BLK_SUB.CV = 0x%lx\n",
- (long)cx->blk_sub.cv);
- fprintf(stderr, "BLK_SUB.GV = 0x%lx\n",
- (long)cx->blk_sub.gv);
- fprintf(stderr, "BLK_SUB.DFOUTGV = 0x%lx\n",
- (long)cx->blk_sub.dfoutgv);
- fprintf(stderr, "BLK_SUB.OLDDEPTH = %ld\n",
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_sub.cv));
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
(long)cx->blk_sub.olddepth);
- fprintf(stderr, "BLK_SUB.HASARGS = %d\n",
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
(int)cx->blk_sub.hasargs);
+ PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n",
+ (int)cx->blk_sub.lval);
break;
case CXt_EVAL:
- fprintf(stderr, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
+ PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
(long)cx->blk_eval.old_in_eval);
- fprintf(stderr, "BLK_EVAL.OLD_OP_TYPE = %s\n",
- op_name[cx->blk_eval.old_op_type]);
- fprintf(stderr, "BLK_EVAL.OLD_NAME = %s\n",
- cx->blk_eval.old_name);
- fprintf(stderr, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
- (long)cx->blk_eval.old_eval_root);
+ PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
+ PL_op_name[cx->blk_eval.old_op_type],
+ PL_op_desc[cx->blk_eval.old_op_type]);
+ if (cx->blk_eval.old_namesv)
+ PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
+ SvPVX(cx->blk_eval.old_namesv));
+ PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_eval.old_eval_root));
break;
case CXt_LOOP:
- fprintf(stderr, "BLK_LOOP.LABEL = %s\n",
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
cx->blk_loop.label);
- fprintf(stderr, "BLK_LOOP.RESETSP = %ld\n",
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
(long)cx->blk_loop.resetsp);
- fprintf(stderr, "BLK_LOOP.REDO_OP = 0x%lx\n",
- (long)cx->blk_loop.redo_op);
- fprintf(stderr, "BLK_LOOP.NEXT_OP = 0x%lx\n",
- (long)cx->blk_loop.next_op);
- fprintf(stderr, "BLK_LOOP.LAST_OP = 0x%lx\n",
- (long)cx->blk_loop.last_op);
- fprintf(stderr, "BLK_LOOP.ITERIX = %ld\n",
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_loop.redo_op));
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_loop.next_op));
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_loop.last_op));
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
(long)cx->blk_loop.iterix);
- fprintf(stderr, "BLK_LOOP.ITERARY = 0x%lx\n",
- (long)cx->blk_loop.iterary);
- fprintf(stderr, "BLK_LOOP.ITERVAR = 0x%lx\n",
- (long)cx->blk_loop.itervar);
- if (cx->blk_loop.itervar)
- fprintf(stderr, "BLK_LOOP.ITERSAVE = 0x%lx\n",
- (long)cx->blk_loop.itersave);
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_loop.iterary));
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
+ PTR2UV(CxITERVAR(cx)));
+ if (CxITERVAR(cx))
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_loop.itersave));
+ PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%"UVxf"\n",
+ PTR2UV(cx->blk_loop.iterlval));
break;
case CXt_SUBST:
- fprintf(stderr, "SB_ITERS = %ld\n",
+ PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
(long)cx->sb_iters);
- fprintf(stderr, "SB_MAXITERS = %ld\n",
+ PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
(long)cx->sb_maxiters);
- fprintf(stderr, "SB_SAFEBASE = %ld\n",
- (long)cx->sb_safebase);
- fprintf(stderr, "SB_ONCE = %ld\n",
+ PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
+ (long)cx->sb_rflags);
+ PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
(long)cx->sb_once);
- fprintf(stderr, "SB_ORIG = %s\n",
+ PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
cx->sb_orig);
- fprintf(stderr, "SB_DSTR = 0x%lx\n",
- (long)cx->sb_dstr);
- fprintf(stderr, "SB_TARG = 0x%lx\n",
- (long)cx->sb_targ);
- fprintf(stderr, "SB_S = 0x%lx\n",
- (long)cx->sb_s);
- fprintf(stderr, "SB_M = 0x%lx\n",
- (long)cx->sb_m);
- fprintf(stderr, "SB_STREND = 0x%lx\n",
- (long)cx->sb_strend);
- fprintf(stderr, "SB_SUBBASE = 0x%lx\n",
- (long)cx->sb_subbase);
+ PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
+ PTR2UV(cx->sb_dstr));
+ PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
+ PTR2UV(cx->sb_targ));
+ PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
+ PTR2UV(cx->sb_s));
+ PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
+ PTR2UV(cx->sb_m));
+ PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
+ PTR2UV(cx->sb_strend));
+ PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
+ PTR2UV(cx->sb_rxres));
break;
}
+#endif /* DEBUGGING */
}
-#endif