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