This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
missed a file
[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
28 DEBUG_l(deb("Setting up local jumplevel %p, was %p\n",
29 &cur_env, PL_top_env));
30 JMPENV_PUSH(ex);
31 if (ex)
32 ret = NULL;
33 else {
34 va_start(args, body);
a6c40364 35 ret = CALL_FPTR(body)(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 *
864dbfa3 192save_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
438 croak("panic: save_threadsv called in non-threaded perl");
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
76e3520e 537#ifdef PERL_OBJECT
864dbfa3 538Perl_save_destructor(pTHX_ DESTRUCTORFUNC f, void* p)
76e3520e 539#else
864dbfa3 540Perl_save_destructor(pTHX_ void (*f) (void *), void *p)
76e3520e 541#endif
a0d0e21e 542{
11343788 543 dTHR;
a0d0e21e
LW
544 SSCHECK(3);
545 SSPUSHDPTR(f);
546 SSPUSHPTR(p);
547 SSPUSHINT(SAVEt_DESTRUCTOR);
548}
549
550void
864dbfa3 551Perl_save_aelem(pTHX_ AV *av, I32 idx, SV **sptr)
4e4c362e
GS
552{
553 dTHR;
554 SSCHECK(4);
555 SSPUSHPTR(SvREFCNT_inc(av));
556 SSPUSHINT(idx);
557 SSPUSHPTR(SvREFCNT_inc(*sptr));
558 SSPUSHINT(SAVEt_AELEM);
559 save_scalar_at(sptr);
560}
561
562void
864dbfa3 563Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
4e4c362e
GS
564{
565 dTHR;
566 SSCHECK(4);
567 SSPUSHPTR(SvREFCNT_inc(hv));
568 SSPUSHPTR(SvREFCNT_inc(key));
569 SSPUSHPTR(SvREFCNT_inc(*sptr));
570 SSPUSHINT(SAVEt_HELEM);
571 save_scalar_at(sptr);
572}
573
574void
864dbfa3 575Perl_save_op(pTHX)
462e5cf6
MB
576{
577 dTHR;
578 SSCHECK(2);
533c011a 579 SSPUSHPTR(PL_op);
462e5cf6
MB
580 SSPUSHINT(SAVEt_OP);
581}
582
455ece5e 583I32
864dbfa3 584Perl_save_alloc(pTHX_ I32 size, I32 pad)
455ece5e
AD
585{
586 dTHR;
587 register I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
588 - (char*)PL_savestack);
589 register I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
590
591 /* SSCHECK may not be good enough */
592 while (PL_savestack_ix + elems + 2 > PL_savestack_max)
593 savestack_grow();
594
595 PL_savestack_ix += elems;
596 SSPUSHINT(elems);
597 SSPUSHINT(SAVEt_ALLOC);
598 return start;
599}
600
462e5cf6 601void
864dbfa3 602Perl_leave_scope(pTHX_ I32 base)
79072805 603{
11343788 604 dTHR;
79072805
LW
605 register SV *sv;
606 register SV *value;
607 register GV *gv;
608 register AV *av;
609 register HV *hv;
610 register void* ptr;
161b7d16 611 I32 i;
79072805
LW
612
613 if (base < -1)
463ee0b2 614 croak("panic: corrupt saved stack index");
3280af22 615 while (PL_savestack_ix > base) {
79072805
LW
616 switch (SSPOPINT) {
617 case SAVEt_ITEM: /* normal string */
618 value = (SV*)SSPOPPTR;
619 sv = (SV*)SSPOPPTR;
620 sv_replace(sv,value);
3280af22 621 PL_localizing = 2;
79072805 622 SvSETMAGIC(sv);
3280af22 623 PL_localizing = 0;
79072805
LW
624 break;
625 case SAVEt_SV: /* scalar reference */
626 value = (SV*)SSPOPPTR;
627 gv = (GV*)SSPOPPTR;
7a4c00b4 628 ptr = &GvSV(gv);
4e4c362e 629 SvREFCNT_dec(gv);
7a4c00b4 630 goto restore_sv;
b9d12d37
GS
631 case SAVEt_GENERIC_SVREF: /* generic sv */
632 value = (SV*)SSPOPPTR;
633 ptr = SSPOPPTR;
634 if (ptr) {
635 sv = *(SV**)ptr;
636 *(SV**)ptr = value;
637 SvREFCNT_dec(sv);
638 }
639 SvREFCNT_dec(value);
640 break;
79072805 641 case SAVEt_SVREF: /* scalar reference */
7a4c00b4 642 value = (SV*)SSPOPPTR;
79072805 643 ptr = SSPOPPTR;
7a4c00b4 644 restore_sv:
79072805 645 sv = *(SV**)ptr;
8b73bbec 646 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
54b9620d
MB
647 "restore svref: %p %p:%s -> %p:%s\n",
648 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
748a9306
LW
649 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
650 SvTYPE(sv) != SVt_PVGV)
651 {
a0d0e21e
LW
652 (void)SvUPGRADE(value, SvTYPE(sv));
653 SvMAGIC(value) = SvMAGIC(sv);
654 SvFLAGS(value) |= SvMAGICAL(sv);
655 SvMAGICAL_off(sv);
79072805 656 SvMAGIC(sv) = 0;
a0d0e21e 657 }
7a4c00b4
PP
658 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
659 SvTYPE(value) != SVt_PVGV)
660 {
661 SvFLAGS(value) |= (SvFLAGS(value) &
662 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
663 SvMAGICAL_off(value);
664 SvMAGIC(value) = 0;
665 }
8990e307 666 SvREFCNT_dec(sv);
a0d0e21e 667 *(SV**)ptr = value;
3280af22 668 PL_localizing = 2;
a0d0e21e 669 SvSETMAGIC(value);
3280af22 670 PL_localizing = 0;
4e4c362e 671 SvREFCNT_dec(value);
79072805
LW
672 break;
673 case SAVEt_AV: /* array reference */
674 av = (AV*)SSPOPPTR;
675 gv = (GV*)SSPOPPTR;
fb73857a
PP
676 if (GvAV(gv)) {
677 AV *goner = GvAV(gv);
678 SvMAGIC(av) = SvMAGIC(goner);
679 SvFLAGS(av) |= SvMAGICAL(goner);
680 SvMAGICAL_off(goner);
681 SvMAGIC(goner) = 0;
682 SvREFCNT_dec(goner);
683 }
79072805 684 GvAV(gv) = av;
fb73857a 685 if (SvMAGICAL(av)) {
3280af22 686 PL_localizing = 2;
fb73857a 687 SvSETMAGIC((SV*)av);
3280af22 688 PL_localizing = 0;
fb73857a 689 }
79072805
LW
690 break;
691 case SAVEt_HV: /* hash reference */
692 hv = (HV*)SSPOPPTR;
693 gv = (GV*)SSPOPPTR;
fb73857a
PP
694 if (GvHV(gv)) {
695 HV *goner = GvHV(gv);
696 SvMAGIC(hv) = SvMAGIC(goner);
697 SvFLAGS(hv) |= SvMAGICAL(goner);
698 SvMAGICAL_off(goner);
699 SvMAGIC(goner) = 0;
700 SvREFCNT_dec(goner);
701 }
79072805 702 GvHV(gv) = hv;
fb73857a 703 if (SvMAGICAL(hv)) {
3280af22 704 PL_localizing = 2;
fb73857a 705 SvSETMAGIC((SV*)hv);
3280af22 706 PL_localizing = 0;
fb73857a 707 }
79072805
LW
708 break;
709 case SAVEt_INT: /* int reference */
710 ptr = SSPOPPTR;
711 *(int*)ptr = (int)SSPOPINT;
712 break;
85e6fe83
LW
713 case SAVEt_LONG: /* long reference */
714 ptr = SSPOPPTR;
715 *(long*)ptr = (long)SSPOPLONG;
716 break;
79072805
LW
717 case SAVEt_I32: /* I32 reference */
718 ptr = SSPOPPTR;
719 *(I32*)ptr = (I32)SSPOPINT;
720 break;
55497cff
PP
721 case SAVEt_I16: /* I16 reference */
722 ptr = SSPOPPTR;
723 *(I16*)ptr = (I16)SSPOPINT;
724 break;
a0d0e21e
LW
725 case SAVEt_IV: /* IV reference */
726 ptr = SSPOPPTR;
727 *(IV*)ptr = (IV)SSPOPIV;
728 break;
79072805
LW
729 case SAVEt_SPTR: /* SV* reference */
730 ptr = SSPOPPTR;
731 *(SV**)ptr = (SV*)SSPOPPTR;
732 break;
85e6fe83
LW
733 case SAVEt_PPTR: /* char* reference */
734 ptr = SSPOPPTR;
735 *(char**)ptr = (char*)SSPOPPTR;
736 break;
79072805
LW
737 case SAVEt_HPTR: /* HV* reference */
738 ptr = SSPOPPTR;
739 *(HV**)ptr = (HV*)SSPOPPTR;
740 break;
741 case SAVEt_APTR: /* AV* reference */
742 ptr = SSPOPPTR;
743 *(AV**)ptr = (AV*)SSPOPPTR;
744 break;
745 case SAVEt_NSTAB:
746 gv = (GV*)SSPOPPTR;
1f96a9ed 747 (void)sv_clear((SV*)gv);
79072805 748 break;
fb73857a 749 case SAVEt_GP: /* scalar reference */
79072805
LW
750 ptr = SSPOPPTR;
751 gv = (GV*)SSPOPPTR;
ed6c1d6b 752 if (SvPVX(gv) && SvLEN(gv) > 0) {
fb73857a
PP
753 Safefree(SvPVX(gv));
754 }
755 SvPVX(gv) = (char *)SSPOPPTR;
756 SvCUR(gv) = (STRLEN)SSPOPIV;
757 SvLEN(gv) = (STRLEN)SSPOPIV;
f46d017c
GS
758 gp_free(gv);
759 GvGP(gv) = (GP*)ptr;
fae75791 760 if (GvCVu(gv))
3280af22 761 PL_sub_generation++; /* putting a method back into circulation */
4633a7c4 762 SvREFCNT_dec(gv);
79072805 763 break;
8990e307
LW
764 case SAVEt_FREESV:
765 ptr = SSPOPPTR;
766 SvREFCNT_dec((SV*)ptr);
767 break;
768 case SAVEt_FREEOP:
769 ptr = SSPOPPTR;
3280af22
NIS
770 if (PL_comppad)
771 PL_curpad = AvARRAY(PL_comppad);
8990e307
LW
772 op_free((OP*)ptr);
773 break;
774 case SAVEt_FREEPV:
775 ptr = SSPOPPTR;
776 Safefree((char*)ptr);
777 break;
778 case SAVEt_CLEARSV:
3280af22 779 ptr = (void*)&PL_curpad[SSPOPLONG];
8990e307 780 sv = *(SV**)ptr;
bc44cdaf
GS
781 /* Can clear pad variable in place? */
782 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
6fc92669
GS
783 if (SvTHINKFIRST(sv))
784 sv_force_normal(sv);
a0d0e21e
LW
785 if (SvMAGICAL(sv))
786 mg_free(sv);
8990e307
LW
787
788 switch (SvTYPE(sv)) {
789 case SVt_NULL:
790 break;
791 case SVt_PVAV:
44a8e56a 792 av_clear((AV*)sv);
8990e307
LW
793 break;
794 case SVt_PVHV:
44a8e56a 795 hv_clear((HV*)sv);
8990e307
LW
796 break;
797 case SVt_PVCV:
5377b701
CS
798 croak("panic: leave_scope pad code");
799 case SVt_RV:
800 case SVt_IV:
801 case SVt_NV:
802 (void)SvOK_off(sv);
8990e307
LW
803 break;
804 default:
a0d0e21e 805 (void)SvOK_off(sv);
5377b701 806 (void)SvOOK_off(sv);
8990e307
LW
807 break;
808 }
809 }
810 else { /* Someone has a claim on this, so abandon it. */
4aa0a1f7 811 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
8990e307
LW
812 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
813 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
814 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
815 default: *(SV**)ptr = NEWSV(0,0); break;
816 }
53868620 817 SvREFCNT_dec(sv); /* Cast current value to the winds. */
4aa0a1f7 818 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
8990e307
LW
819 }
820 break;
821 case SAVEt_DELETE:
822 ptr = SSPOPPTR;
823 hv = (HV*)ptr;
824 ptr = SSPOPPTR;
748a9306 825 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
4e4c362e 826 SvREFCNT_dec(hv);
adbc6bb1 827 Safefree(ptr);
8990e307 828 break;
a0d0e21e
LW
829 case SAVEt_DESTRUCTOR:
830 ptr = SSPOPPTR;
864dbfa3 831 CALLDESTRUCTOR(ptr);
a0d0e21e
LW
832 break;
833 case SAVEt_REGCONTEXT:
455ece5e 834 case SAVEt_ALLOC:
161b7d16 835 i = SSPOPINT;
3280af22 836 PL_savestack_ix -= i; /* regexp must have croaked */
a0d0e21e 837 break;
55497cff 838 case SAVEt_STACK_POS: /* Position on Perl stack */
161b7d16 839 i = SSPOPINT;
3280af22 840 PL_stack_sp = PL_stack_base + i;
55497cff 841 break;
161b7d16
SM
842 case SAVEt_AELEM: /* array element */
843 value = (SV*)SSPOPPTR;
844 i = SSPOPINT;
845 av = (AV*)SSPOPPTR;
846 ptr = av_fetch(av,i,1);
4e4c362e
GS
847 if (ptr) {
848 sv = *(SV**)ptr;
3280af22 849 if (sv && sv != &PL_sv_undef) {
33c27489 850 if (SvTIED_mg((SV*)av, 'P'))
4e4c362e
GS
851 (void)SvREFCNT_inc(sv);
852 SvREFCNT_dec(av);
853 goto restore_sv;
854 }
855 }
856 SvREFCNT_dec(av);
857 SvREFCNT_dec(value);
858 break;
161b7d16
SM
859 case SAVEt_HELEM: /* hash element */
860 value = (SV*)SSPOPPTR;
9002cb76 861 sv = (SV*)SSPOPPTR;
161b7d16
SM
862 hv = (HV*)SSPOPPTR;
863 ptr = hv_fetch_ent(hv, sv, 1, 0);
4e4c362e
GS
864 if (ptr) {
865 SV *oval = HeVAL((HE*)ptr);
3280af22 866 if (oval && oval != &PL_sv_undef) {
4e4c362e 867 ptr = &HeVAL((HE*)ptr);
33c27489 868 if (SvTIED_mg((SV*)hv, 'P'))
4e4c362e
GS
869 (void)SvREFCNT_inc(*(SV**)ptr);
870 SvREFCNT_dec(hv);
871 SvREFCNT_dec(sv);
872 goto restore_sv;
873 }
874 }
875 SvREFCNT_dec(hv);
876 SvREFCNT_dec(sv);
877 SvREFCNT_dec(value);
878 break;
462e5cf6 879 case SAVEt_OP:
533c011a 880 PL_op = (OP*)SSPOPPTR;
462e5cf6 881 break;
25eaa213 882 case SAVEt_HINTS:
3280af22
NIS
883 if (GvHV(PL_hintgv)) {
884 SvREFCNT_dec((SV*)GvHV(PL_hintgv));
885 GvHV(PL_hintgv) = NULL;
b3ac6de7 886 }
3280af22 887 *(I32*)&PL_hints = (I32)SSPOPINT;
b3ac6de7 888 break;
79072805 889 default:
463ee0b2 890 croak("panic: leave_scope inconsistency");
79072805
LW
891 }
892 }
893}
8990e307 894
8990e307 895void
864dbfa3 896Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 897{
35ff7856 898#ifdef DEBUGGING
11343788 899 dTHR;
22c35a8c 900 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 901 if (CxTYPE(cx) != CXt_SUBST) {
760ac839
LW
902 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
903 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
904 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
905 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
906 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
907 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
908 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
8990e307 909 }
6b35e009 910 switch (CxTYPE(cx)) {
8990e307
LW
911 case CXt_NULL:
912 case CXt_BLOCK:
913 break;
914 case CXt_SUB:
760ac839 915 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
8990e307 916 (long)cx->blk_sub.cv);
760ac839 917 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
8990e307 918 (long)cx->blk_sub.gv);
760ac839 919 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
8990e307 920 (long)cx->blk_sub.dfoutgv);
760ac839 921 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 922 (long)cx->blk_sub.olddepth);
760ac839 923 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
8990e307
LW
924 (int)cx->blk_sub.hasargs);
925 break;
926 case CXt_EVAL:
760ac839 927 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
8990e307 928 (long)cx->blk_eval.old_in_eval);
760ac839 929 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
22c35a8c
GS
930 PL_op_name[cx->blk_eval.old_op_type],
931 PL_op_desc[cx->blk_eval.old_op_type]);
760ac839 932 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
8990e307 933 cx->blk_eval.old_name);
760ac839 934 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
8990e307
LW
935 (long)cx->blk_eval.old_eval_root);
936 break;
937
938 case CXt_LOOP:
760ac839 939 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
8990e307 940 cx->blk_loop.label);
760ac839 941 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
8990e307 942 (long)cx->blk_loop.resetsp);
760ac839 943 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
8990e307 944 (long)cx->blk_loop.redo_op);
760ac839 945 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
8990e307 946 (long)cx->blk_loop.next_op);
760ac839 947 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
8990e307 948 (long)cx->blk_loop.last_op);
760ac839 949 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
8990e307 950 (long)cx->blk_loop.iterix);
760ac839 951 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
8990e307 952 (long)cx->blk_loop.iterary);
760ac839 953 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
8990e307
LW
954 (long)cx->blk_loop.itervar);
955 if (cx->blk_loop.itervar)
760ac839 956 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
8990e307 957 (long)cx->blk_loop.itersave);
7a4c00b4
PP
958 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
959 (long)cx->blk_loop.iterlval);
8990e307
LW
960 break;
961
962 case CXt_SUBST:
760ac839 963 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 964 (long)cx->sb_iters);
760ac839 965 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 966 (long)cx->sb_maxiters);
35ef4773
GS
967 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
968 (long)cx->sb_rflags);
760ac839 969 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
8990e307 970 (long)cx->sb_once);
760ac839 971 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 972 cx->sb_orig);
760ac839 973 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
8990e307 974 (long)cx->sb_dstr);
760ac839 975 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
8990e307 976 (long)cx->sb_targ);
760ac839 977 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
8990e307 978 (long)cx->sb_s);
760ac839 979 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
8990e307 980 (long)cx->sb_m);
760ac839 981 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
8990e307 982 (long)cx->sb_strend);
c90c0ff4
PP
983 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",
984 (long)cx->sb_rxres);
8990e307
LW
985 break;
986 }
17c3b450 987#endif /* DEBUGGING */
35ff7856 988}