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