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