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