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