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