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