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