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