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