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