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