Commit | Line | Data |
a0d0e21e |
1 | /* scope.c |
79072805 |
2 | * |
1129b882 |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others |
79072805 |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8 | * |
a0d0e21e |
9 | */ |
10 | |
11 | /* |
4ac71550 |
12 | * For the fashion of Minas Tirith was such that it was built on seven |
13 | * levels... |
14 | * |
15 | * [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"] |
79072805 |
16 | */ |
17 | |
ddfa107c |
18 | /* This file contains functions to manipulate several of Perl's stacks; |
166f8a29 |
19 | * in particular it contains code to push various types of things onto |
20 | * the savestack, then to pop them off and perform the correct restorative |
21 | * action for each one. This corresponds to the cleanup Perl does at |
22 | * each scope exit. |
23 | */ |
24 | |
79072805 |
25 | #include "EXTERN.h" |
864dbfa3 |
26 | #define PERL_IN_SCOPE_C |
79072805 |
27 | #include "perl.h" |
28 | |
a0d0e21e |
29 | SV** |
864dbfa3 |
30 | Perl_stack_grow(pTHX_ SV **sp, SV **p, int n) |
a0d0e21e |
31 | { |
97aff369 |
32 | dVAR; |
7918f24d |
33 | |
34 | PERL_ARGS_ASSERT_STACK_GROW; |
35 | |
3280af22 |
36 | PL_stack_sp = sp; |
2ce36478 |
37 | #ifndef STRESS_REALLOC |
3280af22 |
38 | av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128); |
2ce36478 |
39 | #else |
6b88bc9c |
40 | av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1); |
2ce36478 |
41 | #endif |
3280af22 |
42 | return PL_stack_sp; |
a0d0e21e |
43 | } |
44 | |
2ce36478 |
45 | #ifndef STRESS_REALLOC |
46 | #define GROW(old) ((old) * 3 / 2) |
47 | #else |
48 | #define GROW(old) ((old) + 1) |
49 | #endif |
50 | |
e336de0d |
51 | PERL_SI * |
864dbfa3 |
52 | Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems) |
e336de0d |
53 | { |
97aff369 |
54 | dVAR; |
e336de0d |
55 | PERL_SI *si; |
a02a5408 |
56 | Newx(si, 1, PERL_SI); |
e336de0d |
57 | si->si_stack = newAV(); |
58 | AvREAL_off(si->si_stack); |
59 | av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0); |
3280af22 |
60 | AvALLOC(si->si_stack)[0] = &PL_sv_undef; |
e336de0d |
61 | AvFILLp(si->si_stack) = 0; |
62 | si->si_prev = 0; |
63 | si->si_next = 0; |
64 | si->si_cxmax = cxitems - 1; |
65 | si->si_cxix = -1; |
e788e7d3 |
66 | si->si_type = PERLSI_UNDEF; |
a02a5408 |
67 | Newx(si->si_cxstack, cxitems, PERL_CONTEXT); |
9965345d |
68 | /* Without any kind of initialising PUSHSUBST() |
69 | * in pp_subst() will read uninitialised heap. */ |
7e337ee0 |
70 | PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT); |
e336de0d |
71 | return si; |
72 | } |
73 | |
79072805 |
74 | I32 |
864dbfa3 |
75 | Perl_cxinc(pTHX) |
79072805 |
76 | { |
97aff369 |
77 | dVAR; |
a3b680e6 |
78 | const IV old_max = cxstack_max; |
2ce36478 |
79 | cxstack_max = GROW(cxstack_max); |
c09156bb |
80 | Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */ |
9965345d |
81 | /* Without any kind of initialising deep enough recursion |
82 | * will end up reading uninitialised PERL_CONTEXTs. */ |
7e337ee0 |
83 | PoisonNew(cxstack + old_max + 1, cxstack_max - old_max, PERL_CONTEXT); |
79072805 |
84 | return cxstack_ix + 1; |
85 | } |
86 | |
79072805 |
87 | void |
864dbfa3 |
88 | Perl_push_scope(pTHX) |
79072805 |
89 | { |
97aff369 |
90 | dVAR; |
3280af22 |
91 | if (PL_scopestack_ix == PL_scopestack_max) { |
92 | PL_scopestack_max = GROW(PL_scopestack_max); |
93 | Renew(PL_scopestack, PL_scopestack_max, I32); |
d343c3ef |
94 | #ifdef DEBUGGING |
95 | Renew(PL_scopestack_name, PL_scopestack_max, const char*); |
520bb150 |
96 | #endif |
79072805 |
97 | } |
d343c3ef |
98 | #ifdef DEBUGGING |
99 | PL_scopestack_name[PL_scopestack_ix] = "unknown"; |
100 | #endif |
3280af22 |
101 | PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix; |
79072805 |
102 | |
103 | } |
104 | |
105 | void |
864dbfa3 |
106 | Perl_pop_scope(pTHX) |
79072805 |
107 | { |
97aff369 |
108 | dVAR; |
35a4481c |
109 | const I32 oldsave = PL_scopestack[--PL_scopestack_ix]; |
8990e307 |
110 | LEAVE_SCOPE(oldsave); |
79072805 |
111 | } |
112 | |
113 | void |
864dbfa3 |
114 | Perl_markstack_grow(pTHX) |
a0d0e21e |
115 | { |
97aff369 |
116 | dVAR; |
35a4481c |
117 | const I32 oldmax = PL_markstack_max - PL_markstack; |
118 | const I32 newmax = GROW(oldmax); |
a0d0e21e |
119 | |
3280af22 |
120 | Renew(PL_markstack, newmax, I32); |
121 | PL_markstack_ptr = PL_markstack + oldmax; |
122 | PL_markstack_max = PL_markstack + newmax; |
a0d0e21e |
123 | } |
124 | |
125 | void |
864dbfa3 |
126 | Perl_savestack_grow(pTHX) |
79072805 |
127 | { |
97aff369 |
128 | dVAR; |
8aacddc1 |
129 | PL_savestack_max = GROW(PL_savestack_max) + 4; |
3280af22 |
130 | Renew(PL_savestack, PL_savestack_max, ANY); |
79072805 |
131 | } |
132 | |
4b3c1a47 |
133 | void |
134 | Perl_savestack_grow_cnt(pTHX_ I32 need) |
135 | { |
97aff369 |
136 | dVAR; |
4b3c1a47 |
137 | PL_savestack_max = PL_savestack_ix + need; |
138 | Renew(PL_savestack, PL_savestack_max, ANY); |
139 | } |
140 | |
2ce36478 |
141 | #undef GROW |
142 | |
79072805 |
143 | void |
864dbfa3 |
144 | Perl_tmps_grow(pTHX_ I32 n) |
677b06e3 |
145 | { |
97aff369 |
146 | dVAR; |
677b06e3 |
147 | #ifndef STRESS_REALLOC |
148 | if (n < 128) |
149 | n = (PL_tmps_max < 512) ? 128 : 512; |
150 | #endif |
151 | PL_tmps_max = PL_tmps_ix + n + 1; |
152 | Renew(PL_tmps_stack, PL_tmps_max, SV*); |
153 | } |
154 | |
155 | |
156 | void |
864dbfa3 |
157 | Perl_free_tmps(pTHX) |
79072805 |
158 | { |
97aff369 |
159 | dVAR; |
79072805 |
160 | /* XXX should tmps_floor live in cxstack? */ |
35a4481c |
161 | const I32 myfloor = PL_tmps_floor; |
3280af22 |
162 | while (PL_tmps_ix > myfloor) { /* clean up after last statement */ |
901017d6 |
163 | SV* const sv = PL_tmps_stack[PL_tmps_ix]; |
a0714e2c |
164 | PL_tmps_stack[PL_tmps_ix--] = NULL; |
8aacddc1 |
165 | if (sv && sv != &PL_sv_undef) { |
463ee0b2 |
166 | SvTEMP_off(sv); |
8990e307 |
167 | SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */ |
463ee0b2 |
168 | } |
79072805 |
169 | } |
170 | } |
171 | |
76e3520e |
172 | STATIC SV * |
af7df257 |
173 | S_save_scalar_at(pTHX_ SV **sptr, const U32 flags) |
79072805 |
174 | { |
97aff369 |
175 | dVAR; |
75d34a09 |
176 | SV * osv; |
177 | register SV *sv; |
79072805 |
178 | |
7918f24d |
179 | PERL_ARGS_ASSERT_SAVE_SCALAR_AT; |
180 | |
75d34a09 |
181 | osv = *sptr; |
182 | sv = (flags & SAVEf_KEEPOLDELEM) ? osv : (*sptr = newSV(0)); |
183 | |
a0d0e21e |
184 | if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) { |
a0d0e21e |
185 | if (SvGMAGICAL(osv)) { |
35a4481c |
186 | const bool oldtainted = PL_tainted; |
a0d0e21e |
187 | SvFLAGS(osv) |= (SvFLAGS(osv) & |
c268c2a6 |
188 | (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
3280af22 |
189 | PL_tainted = oldtainted; |
a0d0e21e |
190 | } |
75d34a09 |
191 | if (!(flags & SAVEf_KEEPOLDELEM)) |
192 | mg_localize(osv, sv, (flags & SAVEf_SETMAGIC) != 0); |
79072805 |
193 | } |
75d34a09 |
194 | |
79072805 |
195 | return sv; |
196 | } |
197 | |
dfcd3de5 |
198 | void |
199 | Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type) |
e22024d3 |
200 | { |
201 | dVAR; |
202 | SSCHECK(3); |
203 | SSPUSHPTR(ptr1); |
204 | SSPUSHPTR(ptr2); |
205 | SSPUSHINT(type); |
206 | } |
207 | |
7a4c00b4 |
208 | SV * |
864dbfa3 |
209 | Perl_save_scalar(pTHX_ GV *gv) |
7a4c00b4 |
210 | { |
97aff369 |
211 | dVAR; |
fb4fc1fa |
212 | SV ** const sptr = &GvSVn(gv); |
7918f24d |
213 | |
214 | PERL_ARGS_ASSERT_SAVE_SCALAR; |
215 | |
27cc343c |
216 | PL_localizing = 1; |
0cbee0a4 |
217 | SvGETMAGIC(*sptr); |
27cc343c |
218 | PL_localizing = 0; |
e22024d3 |
219 | save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV); |
af7df257 |
220 | return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */ |
7a4c00b4 |
221 | } |
222 | |
f4dd75d9 |
223 | /* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to |
b9d12d37 |
224 | * restore a global SV to its prior contents, freeing new value. */ |
225 | void |
864dbfa3 |
226 | Perl_save_generic_svref(pTHX_ SV **sptr) |
b9d12d37 |
227 | { |
97aff369 |
228 | dVAR; |
7918f24d |
229 | |
230 | PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF; |
231 | |
e22024d3 |
232 | save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF); |
b9d12d37 |
233 | } |
234 | |
f4dd75d9 |
235 | /* Like save_pptr(), but also Safefree()s the new value if it is different |
236 | * from the old one. Can be used to restore a global char* to its prior |
237 | * contents, freeing new value. */ |
238 | void |
239 | Perl_save_generic_pvref(pTHX_ char **str) |
240 | { |
97aff369 |
241 | dVAR; |
7918f24d |
242 | |
243 | PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF; |
244 | |
e22024d3 |
245 | save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF); |
f4dd75d9 |
246 | } |
247 | |
05ec9bb3 |
248 | /* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree(). |
249 | * Can be used to restore a shared global char* to its prior |
250 | * contents, freeing new value. */ |
251 | void |
252 | Perl_save_shared_pvref(pTHX_ char **str) |
253 | { |
97aff369 |
254 | dVAR; |
7918f24d |
255 | |
256 | PERL_ARGS_ASSERT_SAVE_SHARED_PVREF; |
257 | |
e22024d3 |
258 | save_pushptrptr(str, *str, SAVEt_SHARED_PVREF); |
05ec9bb3 |
259 | } |
260 | |
14f338dc |
261 | /* set the SvFLAGS specified by mask to the values in val */ |
262 | |
263 | void |
264 | Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val) |
265 | { |
97aff369 |
266 | dVAR; |
7918f24d |
267 | |
268 | PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS; |
269 | |
14f338dc |
270 | SSCHECK(4); |
271 | SSPUSHPTR(sv); |
272 | SSPUSHINT(mask); |
273 | SSPUSHINT(val); |
274 | SSPUSHINT(SAVEt_SET_SVFLAGS); |
275 | } |
276 | |
79072805 |
277 | void |
864dbfa3 |
278 | Perl_save_gp(pTHX_ GV *gv, I32 empty) |
79072805 |
279 | { |
97aff369 |
280 | dVAR; |
7918f24d |
281 | |
282 | PERL_ARGS_ASSERT_SAVE_GP; |
283 | |
e22024d3 |
284 | save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP); |
79072805 |
285 | |
5f05dabc |
286 | if (empty) { |
12816592 |
287 | GP *gp = Perl_newGP(aTHX_ gv); |
146174a9 |
288 | |
fae75791 |
289 | if (GvCVu(gv)) |
e1a479c5 |
290 | mro_method_changed_in(GvSTASH(gv)); /* taking a method out of circulation ("local")*/ |
146174a9 |
291 | if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) { |
292 | gp->gp_io = newIO(); |
293 | IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START; |
294 | } |
72651472 |
295 | #ifdef PERL_DONT_CREATE_GVSV |
296 | if (gv == PL_errgv) { |
297 | /* We could scatter this logic everywhere by changing the |
298 | definition of ERRSV from GvSV() to GvSVn(), but it seems more |
299 | efficient to do this check once here. */ |
300 | gp->gp_sv = newSV(0); |
301 | } |
302 | #endif |
12816592 |
303 | GvGP(gv) = gp; |
5f05dabc |
304 | } |
305 | else { |
44a8e56a |
306 | gp_ref(GvGP(gv)); |
5f05dabc |
307 | GvINTRO_on(gv); |
308 | } |
79072805 |
309 | } |
79072805 |
310 | |
79072805 |
311 | AV * |
864dbfa3 |
312 | Perl_save_ary(pTHX_ GV *gv) |
79072805 |
313 | { |
97aff369 |
314 | dVAR; |
901017d6 |
315 | AV * const oav = GvAVn(gv); |
67a38de0 |
316 | AV *av; |
fb73857a |
317 | |
7918f24d |
318 | PERL_ARGS_ASSERT_SAVE_ARY; |
319 | |
67a38de0 |
320 | if (!AvREAL(oav) && AvREIFY(oav)) |
321 | av_reify(oav); |
e22024d3 |
322 | save_pushptrptr(gv, oav, SAVEt_AV); |
79072805 |
323 | |
4608196e |
324 | GvAV(gv) = NULL; |
fb73857a |
325 | av = GvAVn(gv); |
0cbee0a4 |
326 | if (SvMAGIC(oav)) |
9711599e |
327 | mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE); |
fb73857a |
328 | return av; |
79072805 |
329 | } |
330 | |
331 | HV * |
864dbfa3 |
332 | Perl_save_hash(pTHX_ GV *gv) |
79072805 |
333 | { |
97aff369 |
334 | dVAR; |
fb73857a |
335 | HV *ohv, *hv; |
336 | |
7918f24d |
337 | PERL_ARGS_ASSERT_SAVE_HASH; |
338 | |
e22024d3 |
339 | save_pushptrptr(gv, (ohv = GvHVn(gv)), SAVEt_HV); |
79072805 |
340 | |
4608196e |
341 | GvHV(gv) = NULL; |
fb73857a |
342 | hv = GvHVn(gv); |
0cbee0a4 |
343 | if (SvMAGIC(ohv)) |
9711599e |
344 | mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE); |
fb73857a |
345 | return hv; |
79072805 |
346 | } |
347 | |
348 | void |
864dbfa3 |
349 | Perl_save_item(pTHX_ register SV *item) |
79072805 |
350 | { |
97aff369 |
351 | dVAR; |
901017d6 |
352 | register SV * const sv = newSVsv(item); |
79072805 |
353 | |
7918f24d |
354 | PERL_ARGS_ASSERT_SAVE_ITEM; |
355 | |
e22024d3 |
356 | save_pushptrptr(item, /* remember the pointer */ |
357 | sv, /* remember the value */ |
358 | SAVEt_ITEM); |
79072805 |
359 | } |
360 | |
361 | void |
1409bc06 |
362 | Perl_save_bool(pTHX_ bool *boolp) |
79072805 |
363 | { |
97aff369 |
364 | dVAR; |
7918f24d |
365 | |
1409bc06 |
366 | PERL_ARGS_ASSERT_SAVE_BOOL; |
7918f24d |
367 | |
79072805 |
368 | SSCHECK(3); |
1409bc06 |
369 | SSPUSHBOOL(*boolp); |
370 | SSPUSHPTR(boolp); |
371 | SSPUSHINT(SAVEt_BOOL); |
372 | } |
373 | |
7623d426 |
374 | void |
375 | Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type) |
1409bc06 |
376 | { |
377 | dVAR; |
378 | SSCHECK(3); |
379 | SSPUSHINT(i); |
380 | SSPUSHPTR(ptr); |
381 | SSPUSHINT(type); |
79072805 |
382 | } |
383 | |
384 | void |
1409bc06 |
385 | Perl_save_int(pTHX_ int *intp) |
9febdf04 |
386 | { |
97aff369 |
387 | dVAR; |
7918f24d |
388 | |
1409bc06 |
389 | PERL_ARGS_ASSERT_SAVE_INT; |
7918f24d |
390 | |
1409bc06 |
391 | save_pushi32ptr(*intp, intp, SAVEt_INT); |
9febdf04 |
392 | } |
393 | |
394 | void |
58188858 |
395 | Perl_save_I8(pTHX_ I8 *bytep) |
396 | { |
397 | dVAR; |
7918f24d |
398 | |
399 | PERL_ARGS_ASSERT_SAVE_I8; |
400 | |
1409bc06 |
401 | save_pushi32ptr(*bytep, bytep, SAVEt_I8); |
58188858 |
402 | } |
403 | |
404 | void |
87a84751 |
405 | Perl_save_I16(pTHX_ I16 *intp) |
406 | { |
407 | dVAR; |
7918f24d |
408 | |
409 | PERL_ARGS_ASSERT_SAVE_I16; |
410 | |
1409bc06 |
411 | save_pushi32ptr(*intp, intp, SAVEt_I16); |
87a84751 |
412 | } |
413 | |
414 | void |
864dbfa3 |
415 | Perl_save_I32(pTHX_ I32 *intp) |
79072805 |
416 | { |
97aff369 |
417 | dVAR; |
7918f24d |
418 | |
419 | PERL_ARGS_ASSERT_SAVE_I32; |
420 | |
1409bc06 |
421 | save_pushi32ptr(*intp, intp, SAVEt_I32); |
79072805 |
422 | } |
423 | |
85e6fe83 |
424 | /* Cannot use save_sptr() to store a char* since the SV** cast will |
425 | * force word-alignment and we'll miss the pointer. |
426 | */ |
427 | void |
864dbfa3 |
428 | Perl_save_pptr(pTHX_ char **pptr) |
85e6fe83 |
429 | { |
97aff369 |
430 | dVAR; |
7918f24d |
431 | |
432 | PERL_ARGS_ASSERT_SAVE_PPTR; |
433 | |
e22024d3 |
434 | save_pushptrptr(*pptr, pptr, SAVEt_PPTR); |
85e6fe83 |
435 | } |
436 | |
79072805 |
437 | void |
146174a9 |
438 | Perl_save_vptr(pTHX_ void *ptr) |
439 | { |
97aff369 |
440 | dVAR; |
7918f24d |
441 | |
442 | PERL_ARGS_ASSERT_SAVE_VPTR; |
443 | |
e22024d3 |
444 | save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR); |
146174a9 |
445 | } |
446 | |
447 | void |
864dbfa3 |
448 | Perl_save_sptr(pTHX_ SV **sptr) |
79072805 |
449 | { |
97aff369 |
450 | dVAR; |
7918f24d |
451 | |
452 | PERL_ARGS_ASSERT_SAVE_SPTR; |
453 | |
e22024d3 |
454 | save_pushptrptr(*sptr, sptr, SAVEt_SPTR); |
79072805 |
455 | } |
456 | |
c3564e5c |
457 | void |
09edbca0 |
458 | Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off) |
c3564e5c |
459 | { |
97aff369 |
460 | dVAR; |
c3564e5c |
461 | SSCHECK(4); |
f3548bdc |
462 | ASSERT_CURPAD_ACTIVE("save_padsv"); |
09edbca0 |
463 | SSPUSHPTR(SvREFCNT_inc_simple_NN(PL_curpad[off])); |
f3548bdc |
464 | SSPUSHPTR(PL_comppad); |
c3564e5c |
465 | SSPUSHLONG((long)off); |
09edbca0 |
466 | SSPUSHINT(SAVEt_PADSV_AND_MORTALIZE); |
c3564e5c |
467 | } |
468 | |
79072805 |
469 | void |
864dbfa3 |
470 | Perl_save_hptr(pTHX_ HV **hptr) |
79072805 |
471 | { |
97aff369 |
472 | dVAR; |
7918f24d |
473 | |
474 | PERL_ARGS_ASSERT_SAVE_HPTR; |
475 | |
e22024d3 |
476 | save_pushptrptr(*hptr, hptr, SAVEt_HPTR); |
79072805 |
477 | } |
478 | |
479 | void |
864dbfa3 |
480 | Perl_save_aptr(pTHX_ AV **aptr) |
79072805 |
481 | { |
97aff369 |
482 | dVAR; |
7918f24d |
483 | |
484 | PERL_ARGS_ASSERT_SAVE_APTR; |
485 | |
e22024d3 |
486 | save_pushptrptr(*aptr, aptr, SAVEt_APTR); |
79072805 |
487 | } |
488 | |
489 | void |
2fd8beea |
490 | Perl_save_pushptr(pTHX_ void *const ptr, const int type) |
8990e307 |
491 | { |
97aff369 |
492 | dVAR; |
8990e307 |
493 | SSCHECK(2); |
2fd8beea |
494 | SSPUSHPTR(ptr); |
495 | SSPUSHINT(type); |
8990e307 |
496 | } |
497 | |
498 | void |
864dbfa3 |
499 | Perl_save_clearsv(pTHX_ SV **svp) |
8990e307 |
500 | { |
97aff369 |
501 | dVAR; |
7918f24d |
502 | |
503 | PERL_ARGS_ASSERT_SAVE_CLEARSV; |
504 | |
f3548bdc |
505 | ASSERT_CURPAD_ACTIVE("save_clearsv"); |
8990e307 |
506 | SSCHECK(2); |
3280af22 |
507 | SSPUSHLONG((long)(svp-PL_curpad)); |
8990e307 |
508 | SSPUSHINT(SAVEt_CLEARSV); |
d9d18af6 |
509 | SvPADSTALE_off(*svp); /* mark lexical as active */ |
8990e307 |
510 | } |
511 | |
512 | void |
864dbfa3 |
513 | Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen) |
8990e307 |
514 | { |
97aff369 |
515 | dVAR; |
7918f24d |
516 | |
517 | PERL_ARGS_ASSERT_SAVE_DELETE; |
518 | |
85a721ca |
519 | save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE); |
8990e307 |
520 | } |
521 | |
522 | void |
af097752 |
523 | Perl_save_hdelete(pTHX_ HV *hv, SV *keysv) |
524 | { |
525 | STRLEN len; |
526 | I32 klen; |
527 | const char *key; |
528 | |
529 | PERL_ARGS_ASSERT_SAVE_HDELETE; |
530 | |
531 | key = SvPV_const(keysv, len); |
532 | klen = SvUTF8(keysv) ? -(I32)len : (I32)len; |
533 | SvREFCNT_inc_simple_void_NN(hv); |
534 | save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE); |
535 | } |
536 | |
537 | void |
c68ec7a9 |
538 | Perl_save_adelete(pTHX_ AV *av, I32 key) |
539 | { |
540 | dVAR; |
541 | |
542 | PERL_ARGS_ASSERT_SAVE_ADELETE; |
543 | |
544 | SvREFCNT_inc_void(av); |
545 | save_pushi32ptr(key, av, SAVEt_ADELETE); |
546 | } |
547 | |
548 | void |
12ab1f58 |
549 | Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p) |
550 | { |
551 | dVAR; |
7918f24d |
552 | |
553 | PERL_ARGS_ASSERT_SAVE_DESTRUCTOR; |
554 | |
12ab1f58 |
555 | SSCHECK(3); |
556 | SSPUSHDPTR(f); |
557 | SSPUSHPTR(p); |
558 | SSPUSHINT(SAVEt_DESTRUCTOR); |
559 | } |
560 | |
561 | void |
146174a9 |
562 | Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p) |
563 | { |
97aff369 |
564 | dVAR; |
146174a9 |
565 | SSCHECK(3); |
566 | SSPUSHDXPTR(f); |
567 | SSPUSHPTR(p); |
568 | SSPUSHINT(SAVEt_DESTRUCTOR_X); |
569 | } |
570 | |
571 | void |
da8315f8 |
572 | Perl_save_hints(pTHX) |
573 | { |
574 | dVAR; |
575 | if (PL_compiling.cop_hints_hash) { |
576 | HINTS_REFCNT_LOCK; |
577 | PL_compiling.cop_hints_hash->refcounted_he_refcnt++; |
578 | HINTS_REFCNT_UNLOCK; |
579 | } |
da8315f8 |
580 | if (PL_hints & HINT_LOCALIZE_HH) { |
be84297e |
581 | save_pushptri32ptr(GvHV(PL_hintgv), PL_hints, |
582 | PL_compiling.cop_hints_hash, SAVEt_HINTS); |
da8315f8 |
583 | GvHV(PL_hintgv) = Perl_hv_copy_hints_hv(aTHX_ GvHV(PL_hintgv)); |
be84297e |
584 | } else { |
585 | save_pushi32ptr(PL_hints, PL_compiling.cop_hints_hash, SAVEt_HINTS); |
da8315f8 |
586 | } |
be84297e |
587 | } |
588 | |
589 | static void |
590 | S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2, |
591 | const int type) |
592 | { |
593 | SSCHECK(4); |
594 | SSPUSHPTR(ptr1); |
595 | SSPUSHINT(i); |
596 | SSPUSHPTR(ptr2); |
597 | SSPUSHINT(type); |
da8315f8 |
598 | } |
599 | |
600 | void |
91d1c79f |
601 | Perl_save_aelem_flags(pTHX_ AV *av, I32 idx, SV **sptr, const U32 flags) |
4e4c362e |
602 | { |
97aff369 |
603 | dVAR; |
bfc4de9f |
604 | SV *sv; |
7918f24d |
605 | |
91d1c79f |
606 | PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS; |
7918f24d |
607 | |
0cbee0a4 |
608 | SvGETMAGIC(*sptr); |
be84297e |
609 | save_pushptri32ptr(SvREFCNT_inc_simple(av), idx, SvREFCNT_inc(*sptr), |
610 | SAVEt_AELEM); |
5dd42e15 |
611 | /* if it gets reified later, the restore will have the wrong refcnt */ |
612 | if (!AvREAL(av) && AvREIFY(av)) |
b37c2d43 |
613 | SvREFCNT_inc_void(*sptr); |
91d1c79f |
614 | save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */ |
75d34a09 |
615 | if (flags & SAVEf_KEEPOLDELEM) |
616 | return; |
bfc4de9f |
617 | sv = *sptr; |
618 | /* If we're localizing a tied array element, this new sv |
619 | * won't actually be stored in the array - so it won't get |
620 | * reaped when the localize ends. Ensure it gets reaped by |
621 | * mortifying it instead. DAPM */ |
622 | if (SvTIED_mg(sv, PERL_MAGIC_tiedelem)) |
623 | sv_2mortal(sv); |
4e4c362e |
624 | } |
625 | |
626 | void |
af7df257 |
627 | Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags) |
4e4c362e |
628 | { |
97aff369 |
629 | dVAR; |
bfc4de9f |
630 | SV *sv; |
7918f24d |
631 | |
af7df257 |
632 | PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS; |
7918f24d |
633 | |
0cbee0a4 |
634 | SvGETMAGIC(*sptr); |
4e4c362e |
635 | SSCHECK(4); |
b37c2d43 |
636 | SSPUSHPTR(SvREFCNT_inc_simple(hv)); |
b2096149 |
637 | SSPUSHPTR(newSVsv(key)); |
4e4c362e |
638 | SSPUSHPTR(SvREFCNT_inc(*sptr)); |
639 | SSPUSHINT(SAVEt_HELEM); |
af7df257 |
640 | save_scalar_at(sptr, flags); |
75d34a09 |
641 | if (flags & SAVEf_KEEPOLDELEM) |
642 | return; |
bfc4de9f |
643 | sv = *sptr; |
644 | /* If we're localizing a tied hash element, this new sv |
645 | * won't actually be stored in the hash - so it won't get |
646 | * reaped when the localize ends. Ensure it gets reaped by |
647 | * mortifying it instead. DAPM */ |
648 | if (SvTIED_mg(sv, PERL_MAGIC_tiedelem)) |
649 | sv_2mortal(sv); |
4e4c362e |
650 | } |
651 | |
2053acbf |
652 | SV* |
653 | Perl_save_svref(pTHX_ SV **sptr) |
654 | { |
655 | dVAR; |
7918f24d |
656 | |
657 | PERL_ARGS_ASSERT_SAVE_SVREF; |
658 | |
2053acbf |
659 | SvGETMAGIC(*sptr); |
e22024d3 |
660 | save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF); |
af7df257 |
661 | return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */ |
2053acbf |
662 | } |
663 | |
455ece5e |
664 | I32 |
864dbfa3 |
665 | Perl_save_alloc(pTHX_ I32 size, I32 pad) |
455ece5e |
666 | { |
97aff369 |
667 | dVAR; |
35a4481c |
668 | register const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix] |
8aacddc1 |
669 | - (char*)PL_savestack); |
35a4481c |
670 | register const I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack)); |
455ece5e |
671 | |
1bb4c835 |
672 | SSGROW(elems + 2); |
455ece5e |
673 | |
674 | PL_savestack_ix += elems; |
675 | SSPUSHINT(elems); |
676 | SSPUSHINT(SAVEt_ALLOC); |
677 | return start; |
678 | } |
679 | |
462e5cf6 |
680 | void |
864dbfa3 |
681 | Perl_leave_scope(pTHX_ I32 base) |
79072805 |
682 | { |
97aff369 |
683 | dVAR; |
79072805 |
684 | register SV *sv; |
685 | register SV *value; |
686 | register GV *gv; |
687 | register AV *av; |
688 | register HV *hv; |
20454177 |
689 | void* ptr; |
f4dd75d9 |
690 | register char* str; |
161b7d16 |
691 | I32 i; |
302c0c93 |
692 | /* Localise the effects of the TAINT_NOT inside the loop. */ |
693 | const bool was = PL_tainted; |
79072805 |
694 | |
695 | if (base < -1) |
cea2e8a9 |
696 | Perl_croak(aTHX_ "panic: corrupt saved stack index"); |
3280af22 |
697 | while (PL_savestack_ix > base) { |
c6ae7647 |
698 | TAINT_NOT; |
699 | |
79072805 |
700 | switch (SSPOPINT) { |
701 | case SAVEt_ITEM: /* normal string */ |
ad64d0ec |
702 | value = MUTABLE_SV(SSPOPPTR); |
703 | sv = MUTABLE_SV(SSPOPPTR); |
79072805 |
704 | sv_replace(sv,value); |
3280af22 |
705 | PL_localizing = 2; |
79072805 |
706 | SvSETMAGIC(sv); |
3280af22 |
707 | PL_localizing = 0; |
79072805 |
708 | break; |
8aacddc1 |
709 | case SAVEt_SV: /* scalar reference */ |
ad64d0ec |
710 | value = MUTABLE_SV(SSPOPPTR); |
159b6efe |
711 | gv = MUTABLE_GV(SSPOPPTR); |
7a4c00b4 |
712 | ptr = &GvSV(gv); |
502c6561 |
713 | av = MUTABLE_AV(gv); /* what to refcnt_dec */ |
2053acbf |
714 | restore_sv: |
715 | sv = *(SV**)ptr; |
2053acbf |
716 | *(SV**)ptr = value; |
717 | SvREFCNT_dec(sv); |
718 | PL_localizing = 2; |
719 | SvSETMAGIC(value); |
720 | PL_localizing = 0; |
721 | SvREFCNT_dec(value); |
722 | if (av) /* actually an av, hv or gv */ |
723 | SvREFCNT_dec(av); |
724 | break; |
8aacddc1 |
725 | case SAVEt_GENERIC_PVREF: /* generic pv */ |
f4dd75d9 |
726 | ptr = SSPOPPTR; |
b03d03b0 |
727 | str = (char*)SSPOPPTR; |
f4dd75d9 |
728 | if (*(char**)ptr != str) { |
729 | Safefree(*(char**)ptr); |
730 | *(char**)ptr = str; |
731 | } |
732 | break; |
05ec9bb3 |
733 | case SAVEt_SHARED_PVREF: /* shared pv */ |
734 | str = (char*)SSPOPPTR; |
735 | ptr = SSPOPPTR; |
736 | if (*(char**)ptr != str) { |
5e54c26f |
737 | #ifdef NETWARE |
9ecbcc42 |
738 | PerlMem_free(*(char**)ptr); |
5e54c26f |
739 | #else |
05ec9bb3 |
740 | PerlMemShared_free(*(char**)ptr); |
5e54c26f |
741 | #endif |
05ec9bb3 |
742 | *(char**)ptr = str; |
743 | } |
744 | break; |
8aacddc1 |
745 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
ad64d0ec |
746 | value = MUTABLE_SV(SSPOPPTR); |
b9d12d37 |
747 | ptr = SSPOPPTR; |
f4dd75d9 |
748 | sv = *(SV**)ptr; |
749 | *(SV**)ptr = value; |
750 | SvREFCNT_dec(sv); |
b9d12d37 |
751 | SvREFCNT_dec(value); |
752 | break; |
8aacddc1 |
753 | case SAVEt_AV: /* array reference */ |
502c6561 |
754 | av = MUTABLE_AV(SSPOPPTR); |
159b6efe |
755 | gv = MUTABLE_GV(SSPOPPTR); |
ef8d46e8 |
756 | SvREFCNT_dec(GvAV(gv)); |
8aacddc1 |
757 | GvAV(gv) = av; |
fb73857a |
758 | if (SvMAGICAL(av)) { |
3280af22 |
759 | PL_localizing = 2; |
ad64d0ec |
760 | SvSETMAGIC(MUTABLE_SV(av)); |
3280af22 |
761 | PL_localizing = 0; |
fb73857a |
762 | } |
8aacddc1 |
763 | break; |
764 | case SAVEt_HV: /* hash reference */ |
85fbaab2 |
765 | hv = MUTABLE_HV(SSPOPPTR); |
159b6efe |
766 | gv = MUTABLE_GV(SSPOPPTR); |
ef8d46e8 |
767 | SvREFCNT_dec(GvHV(gv)); |
8aacddc1 |
768 | GvHV(gv) = hv; |
fb73857a |
769 | if (SvMAGICAL(hv)) { |
3280af22 |
770 | PL_localizing = 2; |
ad64d0ec |
771 | SvSETMAGIC(MUTABLE_SV(hv)); |
3280af22 |
772 | PL_localizing = 0; |
fb73857a |
773 | } |
8aacddc1 |
774 | break; |
79072805 |
775 | case SAVEt_INT: /* int reference */ |
776 | ptr = SSPOPPTR; |
777 | *(int*)ptr = (int)SSPOPINT; |
778 | break; |
9febdf04 |
779 | case SAVEt_BOOL: /* bool reference */ |
780 | ptr = SSPOPPTR; |
781 | *(bool*)ptr = (bool)SSPOPBOOL; |
782 | break; |
79072805 |
783 | case SAVEt_I32: /* I32 reference */ |
784 | ptr = SSPOPPTR; |
3235b7a3 |
785 | #ifdef PERL_DEBUG_READONLY_OPS |
786 | { |
787 | const I32 val = SSPOPINT; |
788 | if (*(I32*)ptr != val) |
789 | *(I32*)ptr = val; |
790 | } |
791 | #else |
79072805 |
792 | *(I32*)ptr = (I32)SSPOPINT; |
3235b7a3 |
793 | #endif |
79072805 |
794 | break; |
795 | case SAVEt_SPTR: /* SV* reference */ |
796 | ptr = SSPOPPTR; |
ad64d0ec |
797 | *(SV**)ptr = MUTABLE_SV(SSPOPPTR); |
79072805 |
798 | break; |
146174a9 |
799 | case SAVEt_VPTR: /* random* reference */ |
85e6fe83 |
800 | case SAVEt_PPTR: /* char* reference */ |
801 | ptr = SSPOPPTR; |
802 | *(char**)ptr = (char*)SSPOPPTR; |
803 | break; |
79072805 |
804 | case SAVEt_HPTR: /* HV* reference */ |
805 | ptr = SSPOPPTR; |
85fbaab2 |
806 | *(HV**)ptr = MUTABLE_HV(SSPOPPTR); |
79072805 |
807 | break; |
808 | case SAVEt_APTR: /* AV* reference */ |
809 | ptr = SSPOPPTR; |
502c6561 |
810 | *(AV**)ptr = MUTABLE_AV(SSPOPPTR); |
79072805 |
811 | break; |
fb73857a |
812 | case SAVEt_GP: /* scalar reference */ |
79072805 |
813 | ptr = SSPOPPTR; |
159b6efe |
814 | gv = MUTABLE_GV(SSPOPPTR); |
8aacddc1 |
815 | gp_free(gv); |
816 | GvGP(gv) = (GP*)ptr; |
dd69841b |
817 | /* putting a method back into circulation ("local")*/ |
818 | if (GvCVu(gv) && (hv=GvSTASH(gv)) && HvNAME_get(hv)) |
819 | mro_method_changed_in(hv); |
4633a7c4 |
820 | SvREFCNT_dec(gv); |
8aacddc1 |
821 | break; |
8990e307 |
822 | case SAVEt_FREESV: |
823 | ptr = SSPOPPTR; |
ad64d0ec |
824 | SvREFCNT_dec(MUTABLE_SV(ptr)); |
8990e307 |
825 | break; |
26d9b02f |
826 | case SAVEt_MORTALIZESV: |
827 | ptr = SSPOPPTR; |
ad64d0ec |
828 | sv_2mortal(MUTABLE_SV(ptr)); |
26d9b02f |
829 | break; |
8990e307 |
830 | case SAVEt_FREEOP: |
831 | ptr = SSPOPPTR; |
f3548bdc |
832 | ASSERT_CURPAD_LEGAL("SAVEt_FREEOP"); /* XXX DAPM tmp */ |
8990e307 |
833 | op_free((OP*)ptr); |
834 | break; |
835 | case SAVEt_FREEPV: |
836 | ptr = SSPOPPTR; |
1df70142 |
837 | Safefree(ptr); |
8990e307 |
838 | break; |
839 | case SAVEt_CLEARSV: |
3280af22 |
840 | ptr = (void*)&PL_curpad[SSPOPLONG]; |
8990e307 |
841 | sv = *(SV**)ptr; |
dd2155a4 |
842 | |
843 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
f3548bdc |
844 | "Pad 0x%"UVxf"[0x%"UVxf"] clearsv: %ld sv=0x%"UVxf"<%"IVdf"> %s\n", |
845 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), |
846 | (long)((SV **)ptr-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv), |
dd2155a4 |
847 | (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon" |
848 | )); |
849 | |
bc44cdaf |
850 | /* Can clear pad variable in place? */ |
851 | if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) { |
8aacddc1 |
852 | /* |
853 | * if a my variable that was made readonly is going out of |
854 | * scope, we want to remove the readonlyness so that it can |
855 | * go out of scope quietly |
8aacddc1 |
856 | */ |
a26e96df |
857 | if (SvPADMY(sv) && !SvFAKE(sv)) |
8aacddc1 |
858 | SvREADONLY_off(sv); |
859 | |
6fc92669 |
860 | if (SvTHINKFIRST(sv)) |
840a7b70 |
861 | sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF); |
a0d0e21e |
862 | if (SvMAGICAL(sv)) |
863 | mg_free(sv); |
8990e307 |
864 | |
865 | switch (SvTYPE(sv)) { |
866 | case SVt_NULL: |
867 | break; |
868 | case SVt_PVAV: |
502c6561 |
869 | av_clear(MUTABLE_AV(sv)); |
8990e307 |
870 | break; |
871 | case SVt_PVHV: |
85fbaab2 |
872 | hv_clear(MUTABLE_HV(sv)); |
8990e307 |
873 | break; |
874 | case SVt_PVCV: |
cea2e8a9 |
875 | Perl_croak(aTHX_ "panic: leave_scope pad code"); |
8990e307 |
876 | default: |
0c34ef67 |
877 | SvOK_off(sv); |
8990e307 |
878 | break; |
879 | } |
d9d18af6 |
880 | SvPADSTALE_on(sv); /* mark as no longer live */ |
8990e307 |
881 | } |
882 | else { /* Someone has a claim on this, so abandon it. */ |
35a4481c |
883 | const U32 padflags = SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP); |
8990e307 |
884 | switch (SvTYPE(sv)) { /* Console ourselves with a new value */ |
ad64d0ec |
885 | case SVt_PVAV: *(SV**)ptr = MUTABLE_SV(newAV()); break; |
886 | case SVt_PVHV: *(SV**)ptr = MUTABLE_SV(newHV()); break; |
561b68a9 |
887 | default: *(SV**)ptr = newSV(0); break; |
8990e307 |
888 | } |
53868620 |
889 | SvREFCNT_dec(sv); /* Cast current value to the winds. */ |
d9d18af6 |
890 | /* preserve pad nature, but also mark as not live |
891 | * for any closure capturing */ |
2740392c |
892 | SvFLAGS(*(SV**)ptr) |= padflags | SVs_PADSTALE; |
8990e307 |
893 | } |
894 | break; |
895 | case SAVEt_DELETE: |
896 | ptr = SSPOPPTR; |
85fbaab2 |
897 | hv = MUTABLE_HV(ptr); |
35d4f826 |
898 | i = SSPOPINT; |
8990e307 |
899 | ptr = SSPOPPTR; |
35d4f826 |
900 | (void)hv_delete(hv, (char*)ptr, i, G_DISCARD); |
4e4c362e |
901 | SvREFCNT_dec(hv); |
8aacddc1 |
902 | Safefree(ptr); |
8990e307 |
903 | break; |
c68ec7a9 |
904 | case SAVEt_ADELETE: |
905 | ptr = SSPOPPTR; |
906 | av = MUTABLE_AV(ptr); |
907 | i = SSPOPINT; |
908 | (void)av_delete(av, i, G_DISCARD); |
909 | SvREFCNT_dec(av); |
910 | break; |
146174a9 |
911 | case SAVEt_DESTRUCTOR_X: |
912 | ptr = SSPOPPTR; |
acfe0abc |
913 | (*SSPOPDXPTR)(aTHX_ ptr); |
a0d0e21e |
914 | break; |
915 | case SAVEt_REGCONTEXT: |
455ece5e |
916 | case SAVEt_ALLOC: |
161b7d16 |
917 | i = SSPOPINT; |
3280af22 |
918 | PL_savestack_ix -= i; /* regexp must have croaked */ |
a0d0e21e |
919 | break; |
55497cff |
920 | case SAVEt_STACK_POS: /* Position on Perl stack */ |
161b7d16 |
921 | i = SSPOPINT; |
3280af22 |
922 | PL_stack_sp = PL_stack_base + i; |
55497cff |
923 | break; |
ea8d6ae1 |
924 | case SAVEt_STACK_CXPOS: /* blk_oldsp on context stack */ |
925 | i = SSPOPINT; |
926 | cxstack[i].blk_oldsp = SSPOPINT; |
927 | break; |
161b7d16 |
928 | case SAVEt_AELEM: /* array element */ |
ad64d0ec |
929 | value = MUTABLE_SV(SSPOPPTR); |
161b7d16 |
930 | i = SSPOPINT; |
502c6561 |
931 | av = MUTABLE_AV(SSPOPPTR); |
658aef79 |
932 | ptr = av_fetch(av,i,1); |
5dd42e15 |
933 | if (!AvREAL(av) && AvREIFY(av)) /* undo reify guard */ |
934 | SvREFCNT_dec(value); |
4e4c362e |
935 | if (ptr) { |
936 | sv = *(SV**)ptr; |
3280af22 |
937 | if (sv && sv != &PL_sv_undef) { |
ad64d0ec |
938 | if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied)) |
b37c2d43 |
939 | SvREFCNT_inc_void_NN(sv); |
4e4c362e |
940 | goto restore_sv; |
941 | } |
942 | } |
943 | SvREFCNT_dec(av); |
944 | SvREFCNT_dec(value); |
945 | break; |
161b7d16 |
946 | case SAVEt_HELEM: /* hash element */ |
ad64d0ec |
947 | value = MUTABLE_SV(SSPOPPTR); |
948 | sv = MUTABLE_SV(SSPOPPTR); |
85fbaab2 |
949 | hv = MUTABLE_HV(SSPOPPTR); |
161b7d16 |
950 | ptr = hv_fetch_ent(hv, sv, 1, 0); |
fd1b367e |
951 | SvREFCNT_dec(sv); |
4e4c362e |
952 | if (ptr) { |
35a4481c |
953 | const SV * const oval = HeVAL((HE*)ptr); |
3280af22 |
954 | if (oval && oval != &PL_sv_undef) { |
4e4c362e |
955 | ptr = &HeVAL((HE*)ptr); |
ad64d0ec |
956 | if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) |
b37c2d43 |
957 | SvREFCNT_inc_void(*(SV**)ptr); |
502c6561 |
958 | av = MUTABLE_AV(hv); /* what to refcnt_dec */ |
4e4c362e |
959 | goto restore_sv; |
960 | } |
961 | } |
962 | SvREFCNT_dec(hv); |
4e4c362e |
963 | SvREFCNT_dec(value); |
964 | break; |
462e5cf6 |
965 | case SAVEt_OP: |
533c011a |
966 | PL_op = (OP*)SSPOPPTR; |
462e5cf6 |
967 | break; |
25eaa213 |
968 | case SAVEt_HINTS: |
045ac317 |
969 | if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) { |
ad64d0ec |
970 | SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv))); |
045ac317 |
971 | GvHV(PL_hintgv) = NULL; |
972 | } |
c28fe1ec |
973 | Perl_refcounted_he_free(aTHX_ PL_compiling.cop_hints_hash); |
974 | PL_compiling.cop_hints_hash = (struct refcounted_he *) SSPOPPTR; |
601cee3b |
975 | *(I32*)&PL_hints = (I32)SSPOPINT; |
dfa41748 |
976 | if (PL_hints & HINT_LOCALIZE_HH) { |
ad64d0ec |
977 | SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv))); |
85fbaab2 |
978 | GvHV(PL_hintgv) = MUTABLE_HV(SSPOPPTR); |
5b9c0671 |
979 | assert(GvHV(PL_hintgv)); |
980 | } else if (!GvHV(PL_hintgv)) { |
981 | /* Need to add a new one manually, else gv_fetchpv() can |
982 | add one in this code: |
983 | |
984 | if (SvTYPE(gv) == SVt_PVGV) { |
985 | if (add) { |
986 | GvMULTI_on(gv); |
987 | gv_init_sv(gv, sv_type); |
988 | if (*name=='!' && sv_type == SVt_PVHV && len==1) |
989 | require_errno(gv); |
990 | } |
991 | return gv; |
992 | } |
993 | |
994 | and it won't have the magic set. */ |
995 | |
996 | HV *const hv = newHV(); |
997 | hv_magic(hv, NULL, PERL_MAGIC_hints); |
998 | GvHV(PL_hintgv) = hv; |
dfa41748 |
999 | } |
5b9c0671 |
1000 | assert(GvHV(PL_hintgv)); |
b3ac6de7 |
1001 | break; |
cb50131a |
1002 | case SAVEt_COMPPAD: |
f3548bdc |
1003 | PL_comppad = (PAD*)SSPOPPTR; |
58ed4fbe |
1004 | if (PL_comppad) |
cb50131a |
1005 | PL_curpad = AvARRAY(PL_comppad); |
1006 | else |
4608196e |
1007 | PL_curpad = NULL; |
cb50131a |
1008 | break; |
09edbca0 |
1009 | case SAVEt_PADSV_AND_MORTALIZE: |
c3564e5c |
1010 | { |
35a4481c |
1011 | const PADOFFSET off = (PADOFFSET)SSPOPLONG; |
09edbca0 |
1012 | SV **svp; |
c3564e5c |
1013 | ptr = SSPOPPTR; |
09edbca0 |
1014 | assert (ptr); |
1015 | svp = AvARRAY((PAD*)ptr) + off; |
1016 | /* This mortalizing used to be done by POPLOOP() via itersave. |
1017 | But as we have all the information here, we can do it here, |
1018 | save even having to have itersave in the struct. */ |
1019 | sv_2mortal(*svp); |
ad64d0ec |
1020 | *svp = MUTABLE_SV(SSPOPPTR); |
c3564e5c |
1021 | } |
1022 | break; |
8b7059b1 |
1023 | case SAVEt_SAVESWITCHSTACK: |
1024 | { |
1025 | dSP; |
502c6561 |
1026 | AV *const t = MUTABLE_AV(SSPOPPTR); |
1027 | AV *const f = MUTABLE_AV(SSPOPPTR); |
8b7059b1 |
1028 | SWITCHSTACK(t,f); |
1029 | PL_curstackinfo->si_stack = f; |
1030 | } |
1031 | break; |
14f338dc |
1032 | case SAVEt_SET_SVFLAGS: |
1033 | { |
35a4481c |
1034 | const U32 val = (U32)SSPOPINT; |
1035 | const U32 mask = (U32)SSPOPINT; |
ad64d0ec |
1036 | sv = MUTABLE_SV(SSPOPPTR); |
14f338dc |
1037 | SvFLAGS(sv) &= ~mask; |
1038 | SvFLAGS(sv) |= val; |
1039 | } |
1040 | break; |
95e06916 |
1041 | |
1042 | /* This would be a mathom, but Perl_save_svref() calls a static |
1043 | function, S_save_scalar_at(), so has to stay in this file. */ |
2053acbf |
1044 | case SAVEt_SVREF: /* scalar reference */ |
ad64d0ec |
1045 | value = MUTABLE_SV(SSPOPPTR); |
2053acbf |
1046 | ptr = SSPOPPTR; |
1047 | av = NULL; /* what to refcnt_dec */ |
1048 | goto restore_sv; |
95e06916 |
1049 | |
1050 | /* These are only saved in mathoms.c */ |
1051 | case SAVEt_NSTAB: |
159b6efe |
1052 | gv = MUTABLE_GV(SSPOPPTR); |
ad64d0ec |
1053 | (void)sv_clear(MUTABLE_SV(gv)); |
95e06916 |
1054 | break; |
2053acbf |
1055 | case SAVEt_LONG: /* long reference */ |
1056 | ptr = SSPOPPTR; |
1057 | *(long*)ptr = (long)SSPOPLONG; |
1058 | break; |
95e06916 |
1059 | case SAVEt_IV: /* IV reference */ |
1060 | ptr = SSPOPPTR; |
1061 | *(IV*)ptr = (IV)SSPOPIV; |
1062 | break; |
1063 | |
2053acbf |
1064 | case SAVEt_I16: /* I16 reference */ |
1065 | ptr = SSPOPPTR; |
1066 | *(I16*)ptr = (I16)SSPOPINT; |
1067 | break; |
1068 | case SAVEt_I8: /* I8 reference */ |
1069 | ptr = SSPOPPTR; |
1070 | *(I8*)ptr = (I8)SSPOPINT; |
1071 | break; |
2053acbf |
1072 | case SAVEt_DESTRUCTOR: |
1073 | ptr = SSPOPPTR; |
1074 | (*SSPOPDPTR)(ptr); |
1075 | break; |
fc15ae8f |
1076 | case SAVEt_COP_ARYBASE: |
1077 | ptr = SSPOPPTR; |
1078 | i = SSPOPINT; |
1079 | CopARYBASE_set((COP *)ptr, i); |
1080 | break; |
68da3b2f |
1081 | case SAVEt_COMPILE_WARNINGS: |
1082 | ptr = SSPOPPTR; |
72dc9ed5 |
1083 | |
68da3b2f |
1084 | if (!specialWARN(PL_compiling.cop_warnings)) |
1085 | PerlMemShared_free(PL_compiling.cop_warnings); |
72dc9ed5 |
1086 | |
10edeb5d |
1087 | PL_compiling.cop_warnings = (STRLEN*)ptr; |
72dc9ed5 |
1088 | break; |
1ade1aa1 |
1089 | case SAVEt_RE_STATE: |
1090 | { |
1091 | const struct re_save_state *const state |
1092 | = (struct re_save_state *) |
1093 | (PL_savestack + PL_savestack_ix |
1094 | - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE); |
1095 | PL_savestack_ix -= SAVESTACK_ALLOC_FOR_RE_SAVE_STATE; |
1096 | |
1ade1aa1 |
1097 | if (PL_reg_start_tmp != state->re_state_reg_start_tmp) { |
1098 | Safefree(PL_reg_start_tmp); |
1ade1aa1 |
1099 | } |
1ade1aa1 |
1100 | if (PL_reg_poscache != state->re_state_reg_poscache) { |
1101 | Safefree(PL_reg_poscache); |
1ade1aa1 |
1102 | } |
46ab3289 |
1103 | Copy(state, &PL_reg_state, 1, struct re_save_state); |
1ade1aa1 |
1104 | } |
1105 | break; |
7c197c94 |
1106 | case SAVEt_PARSER: |
1107 | ptr = SSPOPPTR; |
1108 | parser_free((yy_parser *) ptr); |
1109 | break; |
79072805 |
1110 | default: |
cea2e8a9 |
1111 | Perl_croak(aTHX_ "panic: leave_scope inconsistency"); |
79072805 |
1112 | } |
1113 | } |
302c0c93 |
1114 | |
1115 | PL_tainted = was; |
79072805 |
1116 | } |
8990e307 |
1117 | |
8990e307 |
1118 | void |
864dbfa3 |
1119 | Perl_cx_dump(pTHX_ PERL_CONTEXT *cx) |
8990e307 |
1120 | { |
97aff369 |
1121 | dVAR; |
7918f24d |
1122 | |
1123 | PERL_ARGS_ASSERT_CX_DUMP; |
1124 | |
35ff7856 |
1125 | #ifdef DEBUGGING |
22c35a8c |
1126 | PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]); |
6b35e009 |
1127 | if (CxTYPE(cx) != CXt_SUBST) { |
760ac839 |
1128 | PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp); |
146174a9 |
1129 | PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n", |
1130 | PTR2UV(cx->blk_oldcop)); |
760ac839 |
1131 | PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp); |
1132 | PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp); |
146174a9 |
1133 | PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n", |
1134 | PTR2UV(cx->blk_oldpm)); |
760ac839 |
1135 | PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR"); |
8990e307 |
1136 | } |
6b35e009 |
1137 | switch (CxTYPE(cx)) { |
8990e307 |
1138 | case CXt_NULL: |
1139 | case CXt_BLOCK: |
1140 | break; |
146174a9 |
1141 | case CXt_FORMAT: |
f9c764c5 |
1142 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%"UVxf"\n", |
1143 | PTR2UV(cx->blk_format.cv)); |
1144 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%"UVxf"\n", |
1145 | PTR2UV(cx->blk_format.gv)); |
1146 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%"UVxf"\n", |
1147 | PTR2UV(cx->blk_format.dfoutgv)); |
1148 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n", |
bafb2adc |
1149 | (int)CxHASARGS(cx)); |
f9c764c5 |
1150 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%"UVxf"\n", |
1151 | PTR2UV(cx->blk_format.retop)); |
146174a9 |
1152 | break; |
8990e307 |
1153 | case CXt_SUB: |
146174a9 |
1154 | PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n", |
1155 | PTR2UV(cx->blk_sub.cv)); |
760ac839 |
1156 | PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n", |
8990e307 |
1157 | (long)cx->blk_sub.olddepth); |
760ac839 |
1158 | PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n", |
bafb2adc |
1159 | (int)CxHASARGS(cx)); |
1160 | PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx)); |
f39bc417 |
1161 | PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n", |
1162 | PTR2UV(cx->blk_sub.retop)); |
8990e307 |
1163 | break; |
1164 | case CXt_EVAL: |
760ac839 |
1165 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n", |
85a64632 |
1166 | (long)CxOLD_IN_EVAL(cx)); |
760ac839 |
1167 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n", |
85a64632 |
1168 | PL_op_name[CxOLD_OP_TYPE(cx)], |
1169 | PL_op_desc[CxOLD_OP_TYPE(cx)]); |
0f79a09d |
1170 | if (cx->blk_eval.old_namesv) |
1171 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n", |
aa07b2f6 |
1172 | SvPVX_const(cx->blk_eval.old_namesv)); |
146174a9 |
1173 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n", |
1174 | PTR2UV(cx->blk_eval.old_eval_root)); |
f39bc417 |
1175 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%"UVxf"\n", |
1176 | PTR2UV(cx->blk_eval.retop)); |
8990e307 |
1177 | break; |
1178 | |
c6fdafd0 |
1179 | case CXt_LOOP_LAZYIV: |
d01136d6 |
1180 | case CXt_LOOP_LAZYSV: |
3b719c58 |
1181 | case CXt_LOOP_FOR: |
1182 | case CXt_LOOP_PLAIN: |
0cbdab38 |
1183 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx)); |
760ac839 |
1184 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n", |
8990e307 |
1185 | (long)cx->blk_loop.resetsp); |
022eaa24 |
1186 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%"UVxf"\n", |
1187 | PTR2UV(cx->blk_loop.my_op)); |
146174a9 |
1188 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%"UVxf"\n", |
022eaa24 |
1189 | PTR2UV(CX_LOOP_NEXTOP_GET(cx))); |
d01136d6 |
1190 | /* XXX: not accurate for LAZYSV/IV */ |
146174a9 |
1191 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n", |
d01136d6 |
1192 | PTR2UV(cx->blk_loop.state_u.ary.ary)); |
1193 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n", |
1194 | (long)cx->blk_loop.state_u.ary.ix); |
146174a9 |
1195 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n", |
1196 | PTR2UV(CxITERVAR(cx))); |
8990e307 |
1197 | break; |
1198 | |
1199 | case CXt_SUBST: |
760ac839 |
1200 | PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n", |
8990e307 |
1201 | (long)cx->sb_iters); |
760ac839 |
1202 | PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n", |
8990e307 |
1203 | (long)cx->sb_maxiters); |
35ef4773 |
1204 | PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n", |
1205 | (long)cx->sb_rflags); |
760ac839 |
1206 | PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n", |
c5bed6a7 |
1207 | (long)CxONCE(cx)); |
760ac839 |
1208 | PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n", |
8990e307 |
1209 | cx->sb_orig); |
146174a9 |
1210 | PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n", |
1211 | PTR2UV(cx->sb_dstr)); |
1212 | PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n", |
1213 | PTR2UV(cx->sb_targ)); |
1214 | PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n", |
1215 | PTR2UV(cx->sb_s)); |
1216 | PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n", |
1217 | PTR2UV(cx->sb_m)); |
1218 | PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n", |
1219 | PTR2UV(cx->sb_strend)); |
1220 | PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n", |
1221 | PTR2UV(cx->sb_rxres)); |
8990e307 |
1222 | break; |
1223 | } |
65e66c80 |
1224 | #else |
96a5add6 |
1225 | PERL_UNUSED_CONTEXT; |
65e66c80 |
1226 | PERL_UNUSED_ARG(cx); |
17c3b450 |
1227 | #endif /* DEBUGGING */ |
35ff7856 |
1228 | } |
241d1a3b |
1229 | |
1230 | /* |
1231 | * Local variables: |
1232 | * c-indentation-style: bsd |
1233 | * c-basic-offset: 4 |
1234 | * indent-tabs-mode: t |
1235 | * End: |
1236 | * |
37442d52 |
1237 | * ex: set ts=8 sts=4 sw=4 noet: |
1238 | */ |