Re: Exceptions in IPC::Open2
[p5sagit/p5-mst-13.2.git] / scope.c
CommitLineData
a0d0e21e 1/* scope.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
79072805 4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e 8 */
9
10/*
11 * "For the fashion of Minas Tirith was such that it was built on seven
12 * levels..."
79072805 13 */
14
15#include "EXTERN.h"
16#include "perl.h"
17
a0d0e21e 18SV**
8ac85365 19stack_grow(SV **sp, SV **p, int n)
a0d0e21e 20{
11343788 21 dTHR;
a60c0954 22#if defined(DEBUGGING) && !defined(USE_THREADS)
23 static int growing = 0;
24 if (growing++)
25 abort();
26#endif
a0d0e21e 27 stack_sp = sp;
2ce36478 28#ifndef STRESS_REALLOC
1f96a9ed 29 av_extend(curstack, (p - stack_base) + (n) + 128);
2ce36478 30#else
31 av_extend(curstack, (p - stack_base) + (n) + 1);
32#endif
a60c0954 33#if defined(DEBUGGING) && !defined(USE_THREADS)
34 growing--;
35#endif
a0d0e21e 36 return stack_sp;
37}
38
2ce36478 39#ifndef STRESS_REALLOC
40#define GROW(old) ((old) * 3 / 2)
41#else
42#define GROW(old) ((old) + 1)
43#endif
44
e336de0d 45PERL_SI *
46new_stackinfo(I32 stitems, I32 cxitems)
47{
48 PERL_SI *si;
49 PERL_CONTEXT *cxt;
50 New(56, si, 1, PERL_SI);
51 si->si_stack = newAV();
52 AvREAL_off(si->si_stack);
53 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
54 AvALLOC(si->si_stack)[0] = &sv_undef;
55 AvFILLp(si->si_stack) = 0;
56 si->si_prev = 0;
57 si->si_next = 0;
58 si->si_cxmax = cxitems - 1;
59 si->si_cxix = -1;
60 si->si_type = SI_UNDEF;
61 New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
62 return si;
63}
64
79072805 65I32
8ac85365 66cxinc(void)
79072805 67{
11343788 68 dTHR;
2ce36478 69 cxstack_max = GROW(cxstack_max);
c09156bb 70 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
79072805 71 return cxstack_ix + 1;
72}
73
74void
8ac85365 75push_return(OP *retop)
79072805 76{
11343788 77 dTHR;
79072805 78 if (retstack_ix == retstack_max) {
2ce36478 79 retstack_max = GROW(retstack_max);
79072805 80 Renew(retstack, retstack_max, OP*);
81 }
82 retstack[retstack_ix++] = retop;
83}
84
85OP *
8ac85365 86pop_return(void)
79072805 87{
11343788 88 dTHR;
79072805 89 if (retstack_ix > 0)
90 return retstack[--retstack_ix];
91 else
92 return Nullop;
93}
94
95void
8ac85365 96push_scope(void)
79072805 97{
11343788 98 dTHR;
79072805 99 if (scopestack_ix == scopestack_max) {
2ce36478 100 scopestack_max = GROW(scopestack_max);
79072805 101 Renew(scopestack, scopestack_max, I32);
102 }
103 scopestack[scopestack_ix++] = savestack_ix;
104
105}
106
107void
8ac85365 108pop_scope(void)
79072805 109{
11343788 110 dTHR;
79072805 111 I32 oldsave = scopestack[--scopestack_ix];
8990e307 112 LEAVE_SCOPE(oldsave);
79072805 113}
114
115void
8ac85365 116markstack_grow(void)
a0d0e21e 117{
11343788 118 dTHR;
a0d0e21e 119 I32 oldmax = markstack_max - markstack;
2ce36478 120 I32 newmax = GROW(oldmax);
a0d0e21e 121
122 Renew(markstack, newmax, I32);
123 markstack_ptr = markstack + oldmax;
124 markstack_max = markstack + newmax;
125}
126
127void
8ac85365 128savestack_grow(void)
79072805 129{
11343788 130 dTHR;
2ce36478 131 savestack_max = GROW(savestack_max) + 4;
79072805 132 Renew(savestack, savestack_max, ANY);
133}
134
2ce36478 135#undef GROW
136
79072805 137void
8ac85365 138free_tmps(void)
79072805 139{
11343788 140 dTHR;
79072805 141 /* XXX should tmps_floor live in cxstack? */
142 I32 myfloor = tmps_floor;
143 while (tmps_ix > myfloor) { /* clean up after last statement */
144 SV* sv = tmps_stack[tmps_ix];
145 tmps_stack[tmps_ix--] = Nullsv;
463ee0b2 146 if (sv) {
147#ifdef DEBUGGING
148 SvTEMP_off(sv);
149#endif
8990e307 150 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
463ee0b2 151 }
79072805 152 }
153}
154
76e3520e 155STATIC SV *
8ac85365 156save_scalar_at(SV **sptr)
79072805 157{
11343788 158 dTHR;
79072805 159 register SV *sv;
7a4c00b4 160 SV *osv = *sptr;
79072805 161
7a4c00b4 162 sv = *sptr = NEWSV(0,0);
a0d0e21e 163 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
79072805 164 sv_upgrade(sv, SvTYPE(osv));
a0d0e21e 165 if (SvGMAGICAL(osv)) {
748a9306 166 MAGIC* mg;
167 bool oldtainted = tainted;
a0d0e21e 168 mg_get(osv);
748a9306 169 if (tainting && tainted && (mg = mg_find(osv, 't'))) {
170 SAVESPTR(mg->mg_obj);
171 mg->mg_obj = osv;
172 }
a0d0e21e 173 SvFLAGS(osv) |= (SvFLAGS(osv) &
174 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
748a9306 175 tainted = oldtainted;
a0d0e21e 176 }
79072805 177 SvMAGIC(sv) = SvMAGIC(osv);
a0d0e21e 178 SvFLAGS(sv) |= SvMAGICAL(osv);
748a9306 179 localizing = 1;
79072805 180 SvSETMAGIC(sv);
748a9306 181 localizing = 0;
79072805 182 }
183 return sv;
184}
185
7a4c00b4 186SV *
8ac85365 187save_scalar(GV *gv)
7a4c00b4 188{
11343788 189 dTHR;
4e4c362e 190 SV **sptr = &GvSV(gv);
7a4c00b4 191 SSCHECK(3);
4e4c362e 192 SSPUSHPTR(SvREFCNT_inc(gv));
193 SSPUSHPTR(SvREFCNT_inc(*sptr));
7a4c00b4 194 SSPUSHINT(SAVEt_SV);
4e4c362e 195 return save_scalar_at(sptr);
7a4c00b4 196}
197
198SV*
8ac85365 199save_svref(SV **sptr)
7a4c00b4 200{
11343788 201 dTHR;
7a4c00b4 202 SSCHECK(3);
203 SSPUSHPTR(sptr);
4e4c362e 204 SSPUSHPTR(SvREFCNT_inc(*sptr));
7a4c00b4 205 SSPUSHINT(SAVEt_SVREF);
206 return save_scalar_at(sptr);
207}
208
79072805 209void
8ac85365 210save_gp(GV *gv, I32 empty)
79072805 211{
e858de61 212 dTHR;
fb73857a 213 SSCHECK(6);
214 SSPUSHIV((IV)SvLEN(gv));
215 SvLEN(gv) = 0; /* forget that anything was allocated here */
216 SSPUSHIV((IV)SvCUR(gv));
217 SSPUSHPTR(SvPVX(gv));
218 SvPOK_off(gv);
4633a7c4 219 SSPUSHPTR(SvREFCNT_inc(gv));
5f05dabc 220 SSPUSHPTR(GvGP(gv));
79072805 221 SSPUSHINT(SAVEt_GP);
222
5f05dabc 223 if (empty) {
224 register GP *gp;
fae75791 225
226 if (GvCVu(gv))
227 sub_generation++; /* taking a method out of circulation */
5f05dabc 228 Newz(602, gp, 1, GP);
44a8e56a 229 GvGP(gv) = gp_ref(gp);
5f05dabc 230 GvSV(gv) = NEWSV(72,0);
231 GvLINE(gv) = curcop->cop_line;
232 GvEGV(gv) = gv;
233 }
234 else {
44a8e56a 235 gp_ref(GvGP(gv));
5f05dabc 236 GvINTRO_on(gv);
237 }
79072805 238}
79072805 239
79072805 240AV *
8ac85365 241save_ary(GV *gv)
79072805 242{
11343788 243 dTHR;
67a38de0 244 AV *oav = GvAVn(gv);
245 AV *av;
fb73857a 246
67a38de0 247 if (!AvREAL(oav) && AvREIFY(oav))
248 av_reify(oav);
79072805 249 SSCHECK(3);
250 SSPUSHPTR(gv);
67a38de0 251 SSPUSHPTR(oav);
79072805 252 SSPUSHINT(SAVEt_AV);
253
254 GvAV(gv) = Null(AV*);
fb73857a 255 av = GvAVn(gv);
256 if (SvMAGIC(oav)) {
257 SvMAGIC(av) = SvMAGIC(oav);
258 SvFLAGS(av) |= SvMAGICAL(oav);
259 SvMAGICAL_off(oav);
260 SvMAGIC(oav) = 0;
261 localizing = 1;
262 SvSETMAGIC((SV*)av);
263 localizing = 0;
264 }
265 return av;
79072805 266}
267
268HV *
8ac85365 269save_hash(GV *gv)
79072805 270{
11343788 271 dTHR;
fb73857a 272 HV *ohv, *hv;
273
79072805 274 SSCHECK(3);
275 SSPUSHPTR(gv);
fb73857a 276 SSPUSHPTR(ohv = GvHVn(gv));
79072805 277 SSPUSHINT(SAVEt_HV);
278
279 GvHV(gv) = Null(HV*);
fb73857a 280 hv = GvHVn(gv);
281 if (SvMAGIC(ohv)) {
282 SvMAGIC(hv) = SvMAGIC(ohv);
283 SvFLAGS(hv) |= SvMAGICAL(ohv);
284 SvMAGICAL_off(ohv);
285 SvMAGIC(ohv) = 0;
286 localizing = 1;
287 SvSETMAGIC((SV*)hv);
288 localizing = 0;
289 }
290 return hv;
79072805 291}
292
293void
8ac85365 294save_item(register SV *item)
79072805 295{
11343788 296 dTHR;
f46d017c 297 register SV *sv = NEWSV(0,0);
79072805 298
f46d017c 299 sv_setsv(sv,item);
79072805 300 SSCHECK(3);
301 SSPUSHPTR(item); /* remember the pointer */
79072805 302 SSPUSHPTR(sv); /* remember the value */
303 SSPUSHINT(SAVEt_ITEM);
304}
305
306void
8ac85365 307save_int(int *intp)
79072805 308{
11343788 309 dTHR;
79072805 310 SSCHECK(3);
311 SSPUSHINT(*intp);
312 SSPUSHPTR(intp);
313 SSPUSHINT(SAVEt_INT);
314}
315
316void
8ac85365 317save_long(long int *longp)
85e6fe83 318{
11343788 319 dTHR;
85e6fe83 320 SSCHECK(3);
321 SSPUSHLONG(*longp);
322 SSPUSHPTR(longp);
323 SSPUSHINT(SAVEt_LONG);
324}
325
326void
8ac85365 327save_I32(I32 *intp)
79072805 328{
11343788 329 dTHR;
79072805 330 SSCHECK(3);
331 SSPUSHINT(*intp);
332 SSPUSHPTR(intp);
333 SSPUSHINT(SAVEt_I32);
334}
335
a0d0e21e 336void
8ac85365 337save_I16(I16 *intp)
55497cff 338{
e858de61 339 dTHR;
55497cff 340 SSCHECK(3);
341 SSPUSHINT(*intp);
342 SSPUSHPTR(intp);
343 SSPUSHINT(SAVEt_I16);
344}
345
346void
8ac85365 347save_iv(IV *ivp)
a0d0e21e 348{
11343788 349 dTHR;
a0d0e21e 350 SSCHECK(3);
4aa0a1f7 351 SSPUSHIV(*ivp);
a0d0e21e 352 SSPUSHPTR(ivp);
353 SSPUSHINT(SAVEt_IV);
354}
355
85e6fe83 356/* Cannot use save_sptr() to store a char* since the SV** cast will
357 * force word-alignment and we'll miss the pointer.
358 */
359void
8ac85365 360save_pptr(char **pptr)
85e6fe83 361{
11343788 362 dTHR;
85e6fe83 363 SSCHECK(3);
364 SSPUSHPTR(*pptr);
365 SSPUSHPTR(pptr);
366 SSPUSHINT(SAVEt_PPTR);
367}
368
79072805 369void
8ac85365 370save_sptr(SV **sptr)
79072805 371{
11343788 372 dTHR;
79072805 373 SSCHECK(3);
374 SSPUSHPTR(*sptr);
375 SSPUSHPTR(sptr);
376 SSPUSHINT(SAVEt_SPTR);
377}
378
54b9620d 379SV **
380save_threadsv(PADOFFSET i)
381{
382#ifdef USE_THREADS
383 dTHR;
940cb80d 384 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
54b9620d 385 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
386 i, svp, *svp, SvPEEK(*svp)));
387 save_svref(svp);
388 return svp;
389#else
390 croak("panic: save_threadsv called in non-threaded perl");
391 return 0;
392#endif /* USE_THREADS */
393}
394
79072805 395void
8ac85365 396save_nogv(GV *gv)
79072805 397{
11343788 398 dTHR;
79072805 399 SSCHECK(2);
400 SSPUSHPTR(gv);
401 SSPUSHINT(SAVEt_NSTAB);
402}
403
404void
8ac85365 405save_hptr(HV **hptr)
79072805 406{
11343788 407 dTHR;
79072805 408 SSCHECK(3);
85e6fe83 409 SSPUSHPTR(*hptr);
79072805 410 SSPUSHPTR(hptr);
411 SSPUSHINT(SAVEt_HPTR);
412}
413
414void
8ac85365 415save_aptr(AV **aptr)
79072805 416{
11343788 417 dTHR;
79072805 418 SSCHECK(3);
85e6fe83 419 SSPUSHPTR(*aptr);
79072805 420 SSPUSHPTR(aptr);
421 SSPUSHINT(SAVEt_APTR);
422}
423
424void
8ac85365 425save_freesv(SV *sv)
8990e307 426{
11343788 427 dTHR;
8990e307 428 SSCHECK(2);
429 SSPUSHPTR(sv);
430 SSPUSHINT(SAVEt_FREESV);
431}
432
433void
8ac85365 434save_freeop(OP *o)
8990e307 435{
11343788 436 dTHR;
8990e307 437 SSCHECK(2);
11343788 438 SSPUSHPTR(o);
8990e307 439 SSPUSHINT(SAVEt_FREEOP);
440}
441
442void
8ac85365 443save_freepv(char *pv)
8990e307 444{
11343788 445 dTHR;
8990e307 446 SSCHECK(2);
447 SSPUSHPTR(pv);
448 SSPUSHINT(SAVEt_FREEPV);
449}
450
451void
8ac85365 452save_clearsv(SV **svp)
8990e307 453{
11343788 454 dTHR;
8990e307 455 SSCHECK(2);
4aa0a1f7 456 SSPUSHLONG((long)(svp-curpad));
8990e307 457 SSPUSHINT(SAVEt_CLEARSV);
458}
459
460void
8ac85365 461save_delete(HV *hv, char *key, I32 klen)
8990e307 462{
11343788 463 dTHR;
8990e307 464 SSCHECK(4);
465 SSPUSHINT(klen);
466 SSPUSHPTR(key);
4e4c362e 467 SSPUSHPTR(SvREFCNT_inc(hv));
8990e307 468 SSPUSHINT(SAVEt_DELETE);
469}
470
471void
8ac85365 472save_list(register SV **sarg, I32 maxsarg)
79072805 473{
11343788 474 dTHR;
79072805 475 register SV *sv;
476 register I32 i;
477
79072805 478 for (i = 1; i <= maxsarg; i++) {
79072805 479 sv = NEWSV(0,0);
480 sv_setsv(sv,sarg[i]);
f46d017c 481 SSCHECK(3);
482 SSPUSHPTR(sarg[i]); /* remember the pointer */
79072805 483 SSPUSHPTR(sv); /* remember the value */
484 SSPUSHINT(SAVEt_ITEM);
485 }
486}
487
488void
76e3520e 489#ifdef PERL_OBJECT
565764a8 490save_destructor(DESTRUCTORFUNC f, void* p)
76e3520e 491#else
8ac85365 492save_destructor(void (*f) (void *), void *p)
76e3520e 493#endif
a0d0e21e 494{
11343788 495 dTHR;
a0d0e21e 496 SSCHECK(3);
497 SSPUSHDPTR(f);
498 SSPUSHPTR(p);
499 SSPUSHINT(SAVEt_DESTRUCTOR);
500}
501
502void
4e4c362e 503save_aelem(AV *av, I32 idx, SV **sptr)
504{
505 dTHR;
506 SSCHECK(4);
507 SSPUSHPTR(SvREFCNT_inc(av));
508 SSPUSHINT(idx);
509 SSPUSHPTR(SvREFCNT_inc(*sptr));
510 SSPUSHINT(SAVEt_AELEM);
511 save_scalar_at(sptr);
512}
513
514void
515save_helem(HV *hv, SV *key, SV **sptr)
516{
517 dTHR;
518 SSCHECK(4);
519 SSPUSHPTR(SvREFCNT_inc(hv));
520 SSPUSHPTR(SvREFCNT_inc(key));
521 SSPUSHPTR(SvREFCNT_inc(*sptr));
522 SSPUSHINT(SAVEt_HELEM);
523 save_scalar_at(sptr);
524}
525
526void
8ac85365 527save_op(void)
462e5cf6 528{
529 dTHR;
530 SSCHECK(2);
531 SSPUSHPTR(op);
532 SSPUSHINT(SAVEt_OP);
533}
534
535void
8ac85365 536leave_scope(I32 base)
79072805 537{
11343788 538 dTHR;
79072805 539 register SV *sv;
540 register SV *value;
541 register GV *gv;
542 register AV *av;
543 register HV *hv;
544 register void* ptr;
161b7d16 545 I32 i;
79072805 546
547 if (base < -1)
463ee0b2 548 croak("panic: corrupt saved stack index");
79072805 549 while (savestack_ix > base) {
550 switch (SSPOPINT) {
551 case SAVEt_ITEM: /* normal string */
552 value = (SV*)SSPOPPTR;
553 sv = (SV*)SSPOPPTR;
554 sv_replace(sv,value);
748a9306 555 localizing = 2;
79072805 556 SvSETMAGIC(sv);
748a9306 557 localizing = 0;
79072805 558 break;
559 case SAVEt_SV: /* scalar reference */
560 value = (SV*)SSPOPPTR;
561 gv = (GV*)SSPOPPTR;
7a4c00b4 562 ptr = &GvSV(gv);
4e4c362e 563 SvREFCNT_dec(gv);
7a4c00b4 564 goto restore_sv;
79072805 565 case SAVEt_SVREF: /* scalar reference */
7a4c00b4 566 value = (SV*)SSPOPPTR;
79072805 567 ptr = SSPOPPTR;
7a4c00b4 568 restore_sv:
79072805 569 sv = *(SV**)ptr;
54b9620d 570 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
571 "restore svref: %p %p:%s -> %p:%s\n",
572 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
748a9306 573 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
574 SvTYPE(sv) != SVt_PVGV)
575 {
a0d0e21e 576 (void)SvUPGRADE(value, SvTYPE(sv));
577 SvMAGIC(value) = SvMAGIC(sv);
578 SvFLAGS(value) |= SvMAGICAL(sv);
579 SvMAGICAL_off(sv);
79072805 580 SvMAGIC(sv) = 0;
a0d0e21e 581 }
7a4c00b4 582 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
583 SvTYPE(value) != SVt_PVGV)
584 {
585 SvFLAGS(value) |= (SvFLAGS(value) &
586 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
587 SvMAGICAL_off(value);
588 SvMAGIC(value) = 0;
589 }
8990e307 590 SvREFCNT_dec(sv);
a0d0e21e 591 *(SV**)ptr = value;
748a9306 592 localizing = 2;
a0d0e21e 593 SvSETMAGIC(value);
748a9306 594 localizing = 0;
4e4c362e 595 SvREFCNT_dec(value);
79072805 596 break;
597 case SAVEt_AV: /* array reference */
598 av = (AV*)SSPOPPTR;
599 gv = (GV*)SSPOPPTR;
fb73857a 600 if (GvAV(gv)) {
601 AV *goner = GvAV(gv);
602 SvMAGIC(av) = SvMAGIC(goner);
603 SvFLAGS(av) |= SvMAGICAL(goner);
604 SvMAGICAL_off(goner);
605 SvMAGIC(goner) = 0;
606 SvREFCNT_dec(goner);
607 }
79072805 608 GvAV(gv) = av;
fb73857a 609 if (SvMAGICAL(av)) {
610 localizing = 2;
611 SvSETMAGIC((SV*)av);
612 localizing = 0;
613 }
79072805 614 break;
615 case SAVEt_HV: /* hash reference */
616 hv = (HV*)SSPOPPTR;
617 gv = (GV*)SSPOPPTR;
fb73857a 618 if (GvHV(gv)) {
619 HV *goner = GvHV(gv);
620 SvMAGIC(hv) = SvMAGIC(goner);
621 SvFLAGS(hv) |= SvMAGICAL(goner);
622 SvMAGICAL_off(goner);
623 SvMAGIC(goner) = 0;
624 SvREFCNT_dec(goner);
625 }
79072805 626 GvHV(gv) = hv;
fb73857a 627 if (SvMAGICAL(hv)) {
628 localizing = 2;
629 SvSETMAGIC((SV*)hv);
630 localizing = 0;
631 }
79072805 632 break;
633 case SAVEt_INT: /* int reference */
634 ptr = SSPOPPTR;
635 *(int*)ptr = (int)SSPOPINT;
636 break;
85e6fe83 637 case SAVEt_LONG: /* long reference */
638 ptr = SSPOPPTR;
639 *(long*)ptr = (long)SSPOPLONG;
640 break;
79072805 641 case SAVEt_I32: /* I32 reference */
642 ptr = SSPOPPTR;
643 *(I32*)ptr = (I32)SSPOPINT;
644 break;
55497cff 645 case SAVEt_I16: /* I16 reference */
646 ptr = SSPOPPTR;
647 *(I16*)ptr = (I16)SSPOPINT;
648 break;
a0d0e21e 649 case SAVEt_IV: /* IV reference */
650 ptr = SSPOPPTR;
651 *(IV*)ptr = (IV)SSPOPIV;
652 break;
79072805 653 case SAVEt_SPTR: /* SV* reference */
654 ptr = SSPOPPTR;
655 *(SV**)ptr = (SV*)SSPOPPTR;
656 break;
85e6fe83 657 case SAVEt_PPTR: /* char* reference */
658 ptr = SSPOPPTR;
659 *(char**)ptr = (char*)SSPOPPTR;
660 break;
79072805 661 case SAVEt_HPTR: /* HV* reference */
662 ptr = SSPOPPTR;
663 *(HV**)ptr = (HV*)SSPOPPTR;
664 break;
665 case SAVEt_APTR: /* AV* reference */
666 ptr = SSPOPPTR;
667 *(AV**)ptr = (AV*)SSPOPPTR;
668 break;
669 case SAVEt_NSTAB:
670 gv = (GV*)SSPOPPTR;
1f96a9ed 671 (void)sv_clear((SV*)gv);
79072805 672 break;
fb73857a 673 case SAVEt_GP: /* scalar reference */
79072805 674 ptr = SSPOPPTR;
675 gv = (GV*)SSPOPPTR;
fb73857a 676 if (SvPOK(gv) && SvLEN(gv) > 0) {
677 Safefree(SvPVX(gv));
678 }
679 SvPVX(gv) = (char *)SSPOPPTR;
680 SvCUR(gv) = (STRLEN)SSPOPIV;
681 SvLEN(gv) = (STRLEN)SSPOPIV;
f46d017c 682 gp_free(gv);
683 GvGP(gv) = (GP*)ptr;
fae75791 684 if (GvCVu(gv))
685 sub_generation++; /* putting a method back into circulation */
4633a7c4 686 SvREFCNT_dec(gv);
79072805 687 break;
8990e307 688 case SAVEt_FREESV:
689 ptr = SSPOPPTR;
690 SvREFCNT_dec((SV*)ptr);
691 break;
692 case SAVEt_FREEOP:
693 ptr = SSPOPPTR;
46fc3d4c 694 if (comppad)
695 curpad = AvARRAY(comppad);
8990e307 696 op_free((OP*)ptr);
697 break;
698 case SAVEt_FREEPV:
699 ptr = SSPOPPTR;
700 Safefree((char*)ptr);
701 break;
702 case SAVEt_CLEARSV:
4aa0a1f7 703 ptr = (void*)&curpad[SSPOPLONG];
8990e307 704 sv = *(SV**)ptr;
bc44cdaf 705 /* Can clear pad variable in place? */
706 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
8990e307 707 if (SvTHINKFIRST(sv)) {
708 if (SvREADONLY(sv))
709 croak("panic: leave_scope clearsv");
710 if (SvROK(sv))
711 sv_unref(sv);
712 }
a0d0e21e 713 if (SvMAGICAL(sv))
714 mg_free(sv);
8990e307 715
716 switch (SvTYPE(sv)) {
717 case SVt_NULL:
718 break;
719 case SVt_PVAV:
44a8e56a 720 av_clear((AV*)sv);
8990e307 721 break;
722 case SVt_PVHV:
44a8e56a 723 hv_clear((HV*)sv);
8990e307 724 break;
725 case SVt_PVCV:
5377b701 726 croak("panic: leave_scope pad code");
727 case SVt_RV:
728 case SVt_IV:
729 case SVt_NV:
730 (void)SvOK_off(sv);
8990e307 731 break;
732 default:
a0d0e21e 733 (void)SvOK_off(sv);
5377b701 734 (void)SvOOK_off(sv);
8990e307 735 break;
736 }
737 }
738 else { /* Someone has a claim on this, so abandon it. */
4aa0a1f7 739 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
8990e307 740 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
741 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
742 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
743 default: *(SV**)ptr = NEWSV(0,0); break;
744 }
53868620 745 SvREFCNT_dec(sv); /* Cast current value to the winds. */
4aa0a1f7 746 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
8990e307 747 }
748 break;
749 case SAVEt_DELETE:
750 ptr = SSPOPPTR;
751 hv = (HV*)ptr;
752 ptr = SSPOPPTR;
748a9306 753 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
4e4c362e 754 SvREFCNT_dec(hv);
adbc6bb1 755 Safefree(ptr);
8990e307 756 break;
a0d0e21e 757 case SAVEt_DESTRUCTOR:
758 ptr = SSPOPPTR;
565764a8 759 (CALLDESTRUCTOR)(ptr);
a0d0e21e 760 break;
761 case SAVEt_REGCONTEXT:
161b7d16 762 i = SSPOPINT;
763 savestack_ix -= i; /* regexp must have croaked */
a0d0e21e 764 break;
55497cff 765 case SAVEt_STACK_POS: /* Position on Perl stack */
161b7d16 766 i = SSPOPINT;
767 stack_sp = stack_base + i;
55497cff 768 break;
161b7d16 769 case SAVEt_AELEM: /* array element */
770 value = (SV*)SSPOPPTR;
771 i = SSPOPINT;
772 av = (AV*)SSPOPPTR;
773 ptr = av_fetch(av,i,1);
4e4c362e 774 if (ptr) {
775 sv = *(SV**)ptr;
776 if (sv && sv != &sv_undef) {
777 if (SvRMAGICAL(av) && mg_find((SV*)av, 'P'))
778 (void)SvREFCNT_inc(sv);
779 SvREFCNT_dec(av);
780 goto restore_sv;
781 }
782 }
783 SvREFCNT_dec(av);
784 SvREFCNT_dec(value);
785 break;
161b7d16 786 case SAVEt_HELEM: /* hash element */
787 value = (SV*)SSPOPPTR;
9002cb76 788 sv = (SV*)SSPOPPTR;
161b7d16 789 hv = (HV*)SSPOPPTR;
790 ptr = hv_fetch_ent(hv, sv, 1, 0);
4e4c362e 791 if (ptr) {
792 SV *oval = HeVAL((HE*)ptr);
793 if (oval && oval != &sv_undef) {
794 ptr = &HeVAL((HE*)ptr);
795 if (SvRMAGICAL(hv) && mg_find((SV*)hv, 'P'))
796 (void)SvREFCNT_inc(*(SV**)ptr);
797 SvREFCNT_dec(hv);
798 SvREFCNT_dec(sv);
799 goto restore_sv;
800 }
801 }
802 SvREFCNT_dec(hv);
803 SvREFCNT_dec(sv);
804 SvREFCNT_dec(value);
805 break;
462e5cf6 806 case SAVEt_OP:
807 op = (OP*)SSPOPPTR;
808 break;
79072805 809 default:
463ee0b2 810 croak("panic: leave_scope inconsistency");
79072805 811 }
812 }
813}
8990e307 814
8990e307 815void
c09156bb 816cx_dump(PERL_CONTEXT *cx)
8990e307 817{
35ff7856 818#ifdef DEBUGGING
11343788 819 dTHR;
fb73857a 820 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), block_type[cx->cx_type]);
8990e307 821 if (cx->cx_type != CXt_SUBST) {
760ac839 822 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
823 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
824 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
825 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
826 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
827 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
828 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
8990e307 829 }
830 switch (cx->cx_type) {
831 case CXt_NULL:
832 case CXt_BLOCK:
833 break;
834 case CXt_SUB:
760ac839 835 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
8990e307 836 (long)cx->blk_sub.cv);
760ac839 837 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
8990e307 838 (long)cx->blk_sub.gv);
760ac839 839 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
8990e307 840 (long)cx->blk_sub.dfoutgv);
760ac839 841 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 842 (long)cx->blk_sub.olddepth);
760ac839 843 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
8990e307 844 (int)cx->blk_sub.hasargs);
845 break;
846 case CXt_EVAL:
760ac839 847 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
8990e307 848 (long)cx->blk_eval.old_in_eval);
760ac839 849 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
c07a80fd 850 op_name[cx->blk_eval.old_op_type],
851 op_desc[cx->blk_eval.old_op_type]);
760ac839 852 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
8990e307 853 cx->blk_eval.old_name);
760ac839 854 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
8990e307 855 (long)cx->blk_eval.old_eval_root);
856 break;
857
858 case CXt_LOOP:
760ac839 859 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
8990e307 860 cx->blk_loop.label);
760ac839 861 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
8990e307 862 (long)cx->blk_loop.resetsp);
760ac839 863 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
8990e307 864 (long)cx->blk_loop.redo_op);
760ac839 865 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
8990e307 866 (long)cx->blk_loop.next_op);
760ac839 867 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
8990e307 868 (long)cx->blk_loop.last_op);
760ac839 869 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
8990e307 870 (long)cx->blk_loop.iterix);
760ac839 871 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
8990e307 872 (long)cx->blk_loop.iterary);
760ac839 873 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
8990e307 874 (long)cx->blk_loop.itervar);
875 if (cx->blk_loop.itervar)
760ac839 876 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
8990e307 877 (long)cx->blk_loop.itersave);
7a4c00b4 878 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
879 (long)cx->blk_loop.iterlval);
8990e307 880 break;
881
882 case CXt_SUBST:
760ac839 883 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 884 (long)cx->sb_iters);
760ac839 885 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 886 (long)cx->sb_maxiters);
760ac839 887 PerlIO_printf(Perl_debug_log, "SB_SAFEBASE = %ld\n",
8990e307 888 (long)cx->sb_safebase);
760ac839 889 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
8990e307 890 (long)cx->sb_once);
760ac839 891 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 892 cx->sb_orig);
760ac839 893 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
8990e307 894 (long)cx->sb_dstr);
760ac839 895 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
8990e307 896 (long)cx->sb_targ);
760ac839 897 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
8990e307 898 (long)cx->sb_s);
760ac839 899 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
8990e307 900 (long)cx->sb_m);
760ac839 901 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
8990e307 902 (long)cx->sb_strend);
c90c0ff4 903 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",
904 (long)cx->sb_rxres);
8990e307 905 break;
906 }
17c3b450 907#endif /* DEBUGGING */
35ff7856 908}