Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* pp_ctl.c |
2 | * | |
1129b882 NC |
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 | |
a0d0e21e LW |
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 | * | |
9 | */ | |
10 | ||
11 | /* | |
4ac71550 TC |
12 | * Now far ahead the Road has gone, |
13 | * And I must follow, if I can, | |
14 | * Pursuing it with eager feet, | |
15 | * Until it joins some larger way | |
16 | * Where many paths and errands meet. | |
17 | * And whither then? I cannot say. | |
18 | * | |
19 | * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] | |
a0d0e21e LW |
20 | */ |
21 | ||
166f8a29 DM |
22 | /* This file contains control-oriented pp ("push/pop") functions that |
23 | * execute the opcodes that make up a perl program. A typical pp function | |
24 | * expects to find its arguments on the stack, and usually pushes its | |
25 | * results onto the stack, hence the 'pp' terminology. Each OP structure | |
26 | * contains a pointer to the relevant pp_foo() function. | |
27 | * | |
28 | * Control-oriented means things like pp_enteriter() and pp_next(), which | |
29 | * alter the flow of control of the program. | |
30 | */ | |
31 | ||
32 | ||
a0d0e21e | 33 | #include "EXTERN.h" |
864dbfa3 | 34 | #define PERL_IN_PP_CTL_C |
a0d0e21e LW |
35 | #include "perl.h" |
36 | ||
54310121 | 37 | #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o)) |
1e422769 | 38 | |
94fcd414 NC |
39 | #define dopoptosub(plop) dopoptosub_at(cxstack, (plop)) |
40 | ||
a0d0e21e LW |
41 | PP(pp_wantarray) |
42 | { | |
97aff369 | 43 | dVAR; |
39644a26 | 44 | dSP; |
a0d0e21e | 45 | I32 cxix; |
93f0bc49 | 46 | const PERL_CONTEXT *cx; |
a0d0e21e LW |
47 | EXTEND(SP, 1); |
48 | ||
93f0bc49 FC |
49 | if (PL_op->op_private & OPpOFFBYONE) { |
50 | if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF; | |
51 | } | |
52 | else { | |
53 | cxix = dopoptosub(cxstack_ix); | |
54 | if (cxix < 0) | |
a0d0e21e | 55 | RETPUSHUNDEF; |
93f0bc49 FC |
56 | cx = &cxstack[cxix]; |
57 | } | |
a0d0e21e | 58 | |
93f0bc49 | 59 | switch (cx->blk_gimme) { |
54310121 | 60 | case G_ARRAY: |
a0d0e21e | 61 | RETPUSHYES; |
54310121 | 62 | case G_SCALAR: |
a0d0e21e | 63 | RETPUSHNO; |
54310121 PP |
64 | default: |
65 | RETPUSHUNDEF; | |
66 | } | |
a0d0e21e LW |
67 | } |
68 | ||
2cd61cdb IZ |
69 | PP(pp_regcreset) |
70 | { | |
97aff369 | 71 | dVAR; |
0b4182de | 72 | TAINT_NOT; |
2cd61cdb IZ |
73 | return NORMAL; |
74 | } | |
75 | ||
b3eb6a9b GS |
76 | PP(pp_regcomp) |
77 | { | |
97aff369 | 78 | dVAR; |
39644a26 | 79 | dSP; |
eb578fdb | 80 | PMOP *pm = (PMOP*)cLOGOP->op_other; |
9f141731 | 81 | SV **args; |
df787a7b | 82 | int nargs; |
84679df5 | 83 | REGEXP *re = NULL; |
9f141731 DM |
84 | REGEXP *new_re; |
85 | const regexp_engine *eng; | |
dbc200c5 | 86 | bool is_bare_re= FALSE; |
bfed75c6 | 87 | |
df787a7b DM |
88 | if (PL_op->op_flags & OPf_STACKED) { |
89 | dMARK; | |
90 | nargs = SP - MARK; | |
91 | args = ++MARK; | |
92 | } | |
93 | else { | |
94 | nargs = 1; | |
95 | args = SP; | |
96 | } | |
97 | ||
4b5a0d1c | 98 | /* prevent recompiling under /o and ithreads. */ |
3db8f154 | 99 | #if defined(USE_ITHREADS) |
131b3ad0 | 100 | if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) { |
df787a7b | 101 | SP = args-1; |
131b3ad0 DM |
102 | RETURN; |
103 | } | |
513629ba | 104 | #endif |
d4b87e75 | 105 | |
9f141731 DM |
106 | re = PM_GETRE(pm); |
107 | assert (re != (REGEXP*) &PL_sv_undef); | |
108 | eng = re ? RX_ENGINE(re) : current_re_engine(); | |
109 | ||
dbc200c5 YO |
110 | /* |
111 | In the below logic: these are basically the same - check if this regcomp is part of a split. | |
112 | ||
113 | (PL_op->op_pmflags & PMf_split ) | |
114 | (PL_op->op_next->op_type == OP_PUSHRE) | |
115 | ||
116 | We could add a new mask for this and copy the PMf_split, if we did | |
117 | some bit definition fiddling first. | |
118 | ||
119 | For now we leave this | |
120 | */ | |
121 | ||
3c13cae6 DM |
122 | new_re = (eng->op_comp |
123 | ? eng->op_comp | |
124 | : &Perl_re_op_compile | |
125 | )(aTHX_ args, nargs, pm->op_code_list, eng, re, | |
346d3070 | 126 | &is_bare_re, |
dbc200c5 | 127 | (pm->op_pmflags & RXf_PMf_FLAGCOPYMASK), |
a5ae69f0 DM |
128 | pm->op_pmflags | |
129 | (PL_op->op_flags & OPf_SPECIAL ? PMf_USE_RE_EVAL : 0)); | |
dbc200c5 | 130 | |
346d3070 | 131 | if (pm->op_pmflags & PMf_HAS_CV) |
8d919b0a | 132 | ReANY(new_re)->qr_anoncv |
9fe3265f | 133 | = (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ)); |
9f141731 DM |
134 | |
135 | if (is_bare_re) { | |
136 | REGEXP *tmp; | |
137 | /* The match's LHS's get-magic might need to access this op's regexp | |
138 | (e.g. $' =~ /$re/ while foo; see bug 70764). So we must call | |
139 | get-magic now before we replace the regexp. Hopefully this hack can | |
140 | be replaced with the approach described at | |
141 | http://www.nntp.perl.org/group/perl.perl5.porters/2007/03/msg122415.html | |
142 | some day. */ | |
143 | if (pm->op_type == OP_MATCH) { | |
144 | SV *lhs; | |
284167a5 | 145 | const bool was_tainted = TAINT_get; |
9f141731 DM |
146 | if (pm->op_flags & OPf_STACKED) |
147 | lhs = args[-1]; | |
148 | else if (pm->op_private & OPpTARGET_MY) | |
149 | lhs = PAD_SV(pm->op_targ); | |
150 | else lhs = DEFSV; | |
151 | SvGETMAGIC(lhs); | |
152 | /* Restore the previous value of PL_tainted (which may have been | |
153 | modified by get-magic), to avoid incorrectly setting the | |
284167a5 SM |
154 | RXf_TAINTED flag with RX_TAINT_on further down. */ |
155 | TAINT_set(was_tainted); | |
df787a7b | 156 | } |
9f141731 DM |
157 | tmp = reg_temp_copy(NULL, new_re); |
158 | ReREFCNT_dec(new_re); | |
159 | new_re = tmp; | |
df787a7b | 160 | } |
dbc200c5 | 161 | |
9f141731 DM |
162 | if (re != new_re) { |
163 | ReREFCNT_dec(re); | |
164 | PM_SETRE(pm, new_re); | |
c277df42 | 165 | } |
d4b87e75 | 166 | |
dbc200c5 | 167 | |
72311751 | 168 | #ifndef INCOMPLETE_TAINTS |
284167a5 | 169 | if (TAINTING_get && TAINT_get) { |
9f141731 | 170 | SvTAINTED_on((SV*)new_re); |
284167a5 | 171 | RX_TAINT_on(new_re); |
72311751 GS |
172 | } |
173 | #endif | |
174 | ||
c737faaf YO |
175 | #if !defined(USE_ITHREADS) |
176 | /* can't change the optree at runtime either */ | |
177 | /* PMf_KEEP is handled differently under threads to avoid these problems */ | |
9f141731 DM |
178 | if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm) |
179 | pm = PL_curpm; | |
a0d0e21e | 180 | if (pm->op_pmflags & PMf_KEEP) { |
c90c0ff4 | 181 | pm->op_private &= ~OPpRUNTIME; /* no point compiling again */ |
533c011a | 182 | cLOGOP->op_first->op_next = PL_op->op_next; |
a0d0e21e | 183 | } |
c737faaf | 184 | #endif |
9f141731 | 185 | |
df787a7b | 186 | SP = args-1; |
a0d0e21e LW |
187 | RETURN; |
188 | } | |
189 | ||
9f141731 | 190 | |
a0d0e21e LW |
191 | PP(pp_substcont) |
192 | { | |
97aff369 | 193 | dVAR; |
39644a26 | 194 | dSP; |
eb578fdb KW |
195 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
196 | PMOP * const pm = (PMOP*) cLOGOP->op_other; | |
197 | SV * const dstr = cx->sb_dstr; | |
198 | char *s = cx->sb_s; | |
199 | char *m = cx->sb_m; | |
a0d0e21e | 200 | char *orig = cx->sb_orig; |
eb578fdb | 201 | REGEXP * const rx = cx->sb_rx; |
c445ea15 | 202 | SV *nsv = NULL; |
988e6e7e | 203 | REGEXP *old = PM_GETRE(pm); |
f410a211 NC |
204 | |
205 | PERL_ASYNC_CHECK(); | |
206 | ||
988e6e7e | 207 | if(old != rx) { |
bfed75c6 | 208 | if(old) |
988e6e7e | 209 | ReREFCNT_dec(old); |
d6106309 | 210 | PM_SETRE(pm,ReREFCNT_inc(rx)); |
d8f2cf8a AB |
211 | } |
212 | ||
d9f97599 | 213 | rxres_restore(&cx->sb_rxres, rx); |
01b35787 | 214 | RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ)); |
c90c0ff4 | 215 | |
a0d0e21e | 216 | if (cx->sb_iters++) { |
a3b680e6 | 217 | const I32 saviters = cx->sb_iters; |
a0d0e21e | 218 | if (cx->sb_iters > cx->sb_maxiters) |
cea2e8a9 | 219 | DIE(aTHX_ "Substitution loop"); |
a0d0e21e | 220 | |
447ee134 DM |
221 | SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */ |
222 | ||
ef07e810 | 223 | /* See "how taint works" above pp_subst() */ |
20be6587 DM |
224 | if (SvTAINTED(TOPs)) |
225 | cx->sb_rxtainted |= SUBST_TAINT_REPL; | |
447ee134 | 226 | sv_catsv_nomg(dstr, POPs); |
2c296965 YO |
227 | /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */ |
228 | s -= RX_GOFS(rx); | |
a0d0e21e LW |
229 | |
230 | /* Are we done */ | |
2c296965 YO |
231 | if (CxONCE(cx) || s < orig || |
232 | !CALLREGEXEC(rx, s, cx->sb_strend, orig, | |
233 | (s == m) + RX_GOFS(rx), cx->sb_targ, NULL, | |
fbfb1899 | 234 | (REXEC_IGNOREPOS|REXEC_NOT_FIRST))) |
a0d0e21e | 235 | { |
8ca8a454 | 236 | SV *targ = cx->sb_targ; |
748a9306 | 237 | |
078c425b JH |
238 | assert(cx->sb_strend >= s); |
239 | if(cx->sb_strend > s) { | |
240 | if (DO_UTF8(dstr) && !SvUTF8(targ)) | |
4bac9ae4 | 241 | sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv); |
078c425b | 242 | else |
4bac9ae4 | 243 | sv_catpvn_nomg(dstr, s, cx->sb_strend - s); |
078c425b | 244 | } |
20be6587 DM |
245 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
246 | cx->sb_rxtainted |= SUBST_TAINT_PAT; | |
9212bbba | 247 | |
8ca8a454 NC |
248 | if (pm->op_pmflags & PMf_NONDESTRUCT) { |
249 | PUSHs(dstr); | |
250 | /* From here on down we're using the copy, and leaving the | |
251 | original untouched. */ | |
252 | targ = dstr; | |
253 | } | |
254 | else { | |
8ca8a454 NC |
255 | if (SvIsCOW(targ)) { |
256 | sv_force_normal_flags(targ, SV_COW_DROP_PV); | |
257 | } else | |
8ca8a454 NC |
258 | { |
259 | SvPV_free(targ); | |
260 | } | |
261 | SvPV_set(targ, SvPVX(dstr)); | |
262 | SvCUR_set(targ, SvCUR(dstr)); | |
263 | SvLEN_set(targ, SvLEN(dstr)); | |
264 | if (DO_UTF8(dstr)) | |
265 | SvUTF8_on(targ); | |
266 | SvPV_set(dstr, NULL); | |
267 | ||
52c47e16 | 268 | PL_tainted = 0; |
4f4d7508 | 269 | mPUSHi(saviters - 1); |
48c036b1 | 270 | |
8ca8a454 NC |
271 | (void)SvPOK_only_UTF8(targ); |
272 | } | |
5cd24f17 | 273 | |
20be6587 | 274 | /* update the taint state of various various variables in |
ef07e810 DM |
275 | * preparation for final exit. |
276 | * See "how taint works" above pp_subst() */ | |
284167a5 | 277 | if (TAINTING_get) { |
20be6587 DM |
278 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || |
279 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
280 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
281 | ) | |
282 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ | |
283 | ||
284 | if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET) | |
285 | && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT)) | |
286 | ) | |
287 | SvTAINTED_on(TOPs); /* taint return value */ | |
288 | /* needed for mg_set below */ | |
284167a5 SM |
289 | TAINT_set( |
290 | cBOOL(cx->sb_rxtainted & | |
291 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)) | |
292 | ); | |
20be6587 DM |
293 | SvTAINT(TARG); |
294 | } | |
295 | /* PL_tainted must be correctly set for this mg_set */ | |
296 | SvSETMAGIC(TARG); | |
297 | TAINT_NOT; | |
4633a7c4 | 298 | LEAVE_SCOPE(cx->sb_oldsave); |
a0d0e21e LW |
299 | POPSUBST(cx); |
300 | RETURNOP(pm->op_next); | |
118e2215 | 301 | assert(0); /* NOTREACHED */ |
a0d0e21e | 302 | } |
8e5e9ebe | 303 | cx->sb_iters = saviters; |
a0d0e21e | 304 | } |
07bc277f | 305 | if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) { |
a0d0e21e LW |
306 | m = s; |
307 | s = orig; | |
6502e081 | 308 | assert(!RX_SUBOFFSET(rx)); |
07bc277f | 309 | cx->sb_orig = orig = RX_SUBBEG(rx); |
a0d0e21e LW |
310 | s = orig + (m - s); |
311 | cx->sb_strend = s + (cx->sb_strend - m); | |
312 | } | |
07bc277f | 313 | cx->sb_m = m = RX_OFFS(rx)[0].start + orig; |
db79b45b | 314 | if (m > s) { |
bfed75c6 | 315 | if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ)) |
4bac9ae4 | 316 | sv_catpvn_nomg_utf8_upgrade(dstr, s, m - s, nsv); |
db79b45b | 317 | else |
4bac9ae4 | 318 | sv_catpvn_nomg(dstr, s, m-s); |
db79b45b | 319 | } |
07bc277f | 320 | cx->sb_s = RX_OFFS(rx)[0].end + orig; |
084916e3 | 321 | { /* Update the pos() information. */ |
8ca8a454 NC |
322 | SV * const sv |
323 | = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ; | |
084916e3 | 324 | MAGIC *mg; |
7a7f3e45 | 325 | SvUPGRADE(sv, SVt_PVMG); |
14befaf4 | 326 | if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) { |
d83f0a82 | 327 | #ifdef PERL_OLD_COPY_ON_WRITE |
51a9ea20 | 328 | if (SvIsCOW(sv)) |
d83f0a82 NC |
329 | sv_force_normal_flags(sv, 0); |
330 | #endif | |
331 | mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob, | |
332 | NULL, 0); | |
084916e3 | 333 | } |
ce474962 | 334 | mg->mg_len = m - orig; |
084916e3 | 335 | } |
988e6e7e | 336 | if (old != rx) |
d6106309 | 337 | (void)ReREFCNT_inc(rx); |
20be6587 | 338 | /* update the taint state of various various variables in preparation |
ef07e810 DM |
339 | * for calling the code block. |
340 | * See "how taint works" above pp_subst() */ | |
284167a5 | 341 | if (TAINTING_get) { |
20be6587 DM |
342 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
343 | cx->sb_rxtainted |= SUBST_TAINT_PAT; | |
344 | ||
345 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || | |
346 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
347 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
348 | ) | |
349 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ | |
350 | ||
351 | if (cx->sb_iters > 1 && (cx->sb_rxtainted & | |
352 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))) | |
8ca8a454 NC |
353 | SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT) |
354 | ? cx->sb_dstr : cx->sb_targ); | |
20be6587 DM |
355 | TAINT_NOT; |
356 | } | |
d9f97599 | 357 | rxres_save(&cx->sb_rxres, rx); |
af9838cc | 358 | PL_curpm = pm; |
29f2e912 | 359 | RETURNOP(pm->op_pmstashstartu.op_pmreplstart); |
a0d0e21e LW |
360 | } |
361 | ||
c90c0ff4 | 362 | void |
864dbfa3 | 363 | Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx) |
c90c0ff4 PP |
364 | { |
365 | UV *p = (UV*)*rsp; | |
366 | U32 i; | |
7918f24d NC |
367 | |
368 | PERL_ARGS_ASSERT_RXRES_SAVE; | |
96a5add6 | 369 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 370 | |
07bc277f | 371 | if (!p || p[1] < RX_NPARENS(rx)) { |
db2c6cb3 | 372 | #ifdef PERL_ANY_COW |
6502e081 | 373 | i = 7 + (RX_NPARENS(rx)+1) * 2; |
ed252734 | 374 | #else |
6502e081 | 375 | i = 6 + (RX_NPARENS(rx)+1) * 2; |
ed252734 | 376 | #endif |
c90c0ff4 | 377 | if (!p) |
a02a5408 | 378 | Newx(p, i, UV); |
c90c0ff4 PP |
379 | else |
380 | Renew(p, i, UV); | |
381 | *rsp = (void*)p; | |
382 | } | |
383 | ||
5eabab15 DM |
384 | /* what (if anything) to free on croak */ |
385 | *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL); | |
cf93c79d | 386 | RX_MATCH_COPIED_off(rx); |
6c31ff74 | 387 | *p++ = RX_NPARENS(rx); |
c90c0ff4 | 388 | |
db2c6cb3 | 389 | #ifdef PERL_ANY_COW |
bdd9a1b1 NC |
390 | *p++ = PTR2UV(RX_SAVED_COPY(rx)); |
391 | RX_SAVED_COPY(rx) = NULL; | |
ed252734 NC |
392 | #endif |
393 | ||
07bc277f NC |
394 | *p++ = PTR2UV(RX_SUBBEG(rx)); |
395 | *p++ = (UV)RX_SUBLEN(rx); | |
6502e081 DM |
396 | *p++ = (UV)RX_SUBOFFSET(rx); |
397 | *p++ = (UV)RX_SUBCOFFSET(rx); | |
07bc277f NC |
398 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
399 | *p++ = (UV)RX_OFFS(rx)[i].start; | |
400 | *p++ = (UV)RX_OFFS(rx)[i].end; | |
c90c0ff4 PP |
401 | } |
402 | } | |
403 | ||
9c105995 NC |
404 | static void |
405 | S_rxres_restore(pTHX_ void **rsp, REGEXP *rx) | |
c90c0ff4 PP |
406 | { |
407 | UV *p = (UV*)*rsp; | |
408 | U32 i; | |
7918f24d NC |
409 | |
410 | PERL_ARGS_ASSERT_RXRES_RESTORE; | |
96a5add6 | 411 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 412 | |
ed252734 | 413 | RX_MATCH_COPY_FREE(rx); |
cf93c79d | 414 | RX_MATCH_COPIED_set(rx, *p); |
c90c0ff4 | 415 | *p++ = 0; |
6c31ff74 | 416 | RX_NPARENS(rx) = *p++; |
c90c0ff4 | 417 | |
db2c6cb3 | 418 | #ifdef PERL_ANY_COW |
bdd9a1b1 NC |
419 | if (RX_SAVED_COPY(rx)) |
420 | SvREFCNT_dec (RX_SAVED_COPY(rx)); | |
421 | RX_SAVED_COPY(rx) = INT2PTR(SV*,*p); | |
ed252734 NC |
422 | *p++ = 0; |
423 | #endif | |
424 | ||
07bc277f NC |
425 | RX_SUBBEG(rx) = INT2PTR(char*,*p++); |
426 | RX_SUBLEN(rx) = (I32)(*p++); | |
6502e081 DM |
427 | RX_SUBOFFSET(rx) = (I32)*p++; |
428 | RX_SUBCOFFSET(rx) = (I32)*p++; | |
07bc277f NC |
429 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
430 | RX_OFFS(rx)[i].start = (I32)(*p++); | |
431 | RX_OFFS(rx)[i].end = (I32)(*p++); | |
c90c0ff4 PP |
432 | } |
433 | } | |
434 | ||
9c105995 NC |
435 | static void |
436 | S_rxres_free(pTHX_ void **rsp) | |
c90c0ff4 | 437 | { |
44f8325f | 438 | UV * const p = (UV*)*rsp; |
7918f24d NC |
439 | |
440 | PERL_ARGS_ASSERT_RXRES_FREE; | |
96a5add6 | 441 | PERL_UNUSED_CONTEXT; |
c90c0ff4 PP |
442 | |
443 | if (p) { | |
94010e71 | 444 | void *tmp = INT2PTR(char*,*p); |
6c31ff74 | 445 | #ifdef PERL_POISON |
db2c6cb3 | 446 | #ifdef PERL_ANY_COW |
6c31ff74 | 447 | U32 i = 9 + p[1] * 2; |
94010e71 | 448 | #else |
6c31ff74 | 449 | U32 i = 8 + p[1] * 2; |
94010e71 | 450 | #endif |
6c31ff74 NC |
451 | #endif |
452 | ||
db2c6cb3 | 453 | #ifdef PERL_ANY_COW |
6c31ff74 | 454 | SvREFCNT_dec (INT2PTR(SV*,p[2])); |
ed252734 | 455 | #endif |
6c31ff74 NC |
456 | #ifdef PERL_POISON |
457 | PoisonFree(p, i, sizeof(UV)); | |
458 | #endif | |
459 | ||
460 | Safefree(tmp); | |
c90c0ff4 | 461 | Safefree(p); |
4608196e | 462 | *rsp = NULL; |
c90c0ff4 PP |
463 | } |
464 | } | |
465 | ||
a701009a DM |
466 | #define FORM_NUM_BLANK (1<<30) |
467 | #define FORM_NUM_POINT (1<<29) | |
468 | ||
a0d0e21e LW |
469 | PP(pp_formline) |
470 | { | |
97aff369 | 471 | dVAR; dSP; dMARK; dORIGMARK; |
eb578fdb | 472 | SV * const tmpForm = *++MARK; |
086b26f3 | 473 | SV *formsv; /* contains text of original format */ |
eb578fdb KW |
474 | U32 *fpc; /* format ops program counter */ |
475 | char *t; /* current append position in target string */ | |
086b26f3 | 476 | const char *f; /* current position in format string */ |
eb578fdb KW |
477 | I32 arg; |
478 | SV *sv = NULL; /* current item */ | |
086b26f3 DM |
479 | const char *item = NULL;/* string value of current item */ |
480 | I32 itemsize = 0; /* length of current item, possibly truncated */ | |
481 | I32 fieldsize = 0; /* width of current field */ | |
482 | I32 lines = 0; /* number of lines that have been output */ | |
483 | bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */ | |
484 | const char *chophere = NULL; /* where to chop current item */ | |
f5ada144 | 485 | STRLEN linemark = 0; /* pos of start of line in output */ |
65202027 | 486 | NV value; |
086b26f3 | 487 | bool gotsome = FALSE; /* seen at least one non-blank item on this line */ |
a0d0e21e | 488 | STRLEN len; |
26e935cf | 489 | STRLEN linemax; /* estimate of output size in bytes */ |
1bd51a4c IH |
490 | bool item_is_utf8 = FALSE; |
491 | bool targ_is_utf8 = FALSE; | |
bfed75c6 | 492 | const char *fmt; |
74e0ddf7 | 493 | MAGIC *mg = NULL; |
4ff700b9 DM |
494 | U8 *source; /* source of bytes to append */ |
495 | STRLEN to_copy; /* how may bytes to append */ | |
ea60cfe8 | 496 | char trans; /* what chars to translate */ |
74e0ddf7 | 497 | |
3808a683 | 498 | mg = doparseform(tmpForm); |
a0d0e21e | 499 | |
74e0ddf7 | 500 | fpc = (U32*)mg->mg_ptr; |
3808a683 DM |
501 | /* the actual string the format was compiled from. |
502 | * with overload etc, this may not match tmpForm */ | |
503 | formsv = mg->mg_obj; | |
504 | ||
74e0ddf7 | 505 | |
3280af22 | 506 | SvPV_force(PL_formtarget, len); |
3808a683 | 507 | if (SvTAINTED(tmpForm) || SvTAINTED(formsv)) |
125b9982 | 508 | SvTAINTED_on(PL_formtarget); |
1bd51a4c IH |
509 | if (DO_UTF8(PL_formtarget)) |
510 | targ_is_utf8 = TRUE; | |
26e935cf DM |
511 | linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1); |
512 | t = SvGROW(PL_formtarget, len + linemax + 1); | |
513 | /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */ | |
a0d0e21e | 514 | t += len; |
3808a683 | 515 | f = SvPV_const(formsv, len); |
a0d0e21e LW |
516 | |
517 | for (;;) { | |
518 | DEBUG_f( { | |
bfed75c6 | 519 | const char *name = "???"; |
a0d0e21e LW |
520 | arg = -1; |
521 | switch (*fpc) { | |
522 | case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break; | |
523 | case FF_BLANK: arg = fpc[1]; name = "BLANK"; break; | |
524 | case FF_SKIP: arg = fpc[1]; name = "SKIP"; break; | |
525 | case FF_FETCH: arg = fpc[1]; name = "FETCH"; break; | |
526 | case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break; | |
527 | ||
528 | case FF_CHECKNL: name = "CHECKNL"; break; | |
529 | case FF_CHECKCHOP: name = "CHECKCHOP"; break; | |
530 | case FF_SPACE: name = "SPACE"; break; | |
531 | case FF_HALFSPACE: name = "HALFSPACE"; break; | |
532 | case FF_ITEM: name = "ITEM"; break; | |
533 | case FF_CHOP: name = "CHOP"; break; | |
534 | case FF_LINEGLOB: name = "LINEGLOB"; break; | |
535 | case FF_NEWLINE: name = "NEWLINE"; break; | |
536 | case FF_MORE: name = "MORE"; break; | |
537 | case FF_LINEMARK: name = "LINEMARK"; break; | |
538 | case FF_END: name = "END"; break; | |
bfed75c6 | 539 | case FF_0DECIMAL: name = "0DECIMAL"; break; |
a1b95068 | 540 | case FF_LINESNGL: name = "LINESNGL"; break; |
a0d0e21e LW |
541 | } |
542 | if (arg >= 0) | |
bf49b057 | 543 | PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg); |
a0d0e21e | 544 | else |
bf49b057 | 545 | PerlIO_printf(Perl_debug_log, "%-16s\n", name); |
5f80b19c | 546 | } ); |
a0d0e21e LW |
547 | switch (*fpc++) { |
548 | case FF_LINEMARK: | |
f5ada144 | 549 | linemark = t - SvPVX(PL_formtarget); |
a0d0e21e LW |
550 | lines++; |
551 | gotsome = FALSE; | |
552 | break; | |
553 | ||
554 | case FF_LITERAL: | |
ea60cfe8 DM |
555 | to_copy = *fpc++; |
556 | source = (U8 *)f; | |
557 | f += to_copy; | |
558 | trans = '~'; | |
75645721 | 559 | item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv); |
ea60cfe8 | 560 | goto append; |
a0d0e21e LW |
561 | |
562 | case FF_SKIP: | |
563 | f += *fpc++; | |
564 | break; | |
565 | ||
566 | case FF_FETCH: | |
567 | arg = *fpc++; | |
568 | f += arg; | |
569 | fieldsize = arg; | |
570 | ||
571 | if (MARK < SP) | |
572 | sv = *++MARK; | |
573 | else { | |
3280af22 | 574 | sv = &PL_sv_no; |
a2a5de95 | 575 | Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments"); |
a0d0e21e | 576 | } |
125b9982 NT |
577 | if (SvTAINTED(sv)) |
578 | SvTAINTED_on(PL_formtarget); | |
a0d0e21e LW |
579 | break; |
580 | ||
581 | case FF_CHECKNL: | |
5a34cab7 NC |
582 | { |
583 | const char *send; | |
584 | const char *s = item = SvPV_const(sv, len); | |
585 | itemsize = len; | |
586 | if (DO_UTF8(sv)) { | |
587 | itemsize = sv_len_utf8(sv); | |
588 | if (itemsize != (I32)len) { | |
589 | I32 itembytes; | |
590 | if (itemsize > fieldsize) { | |
591 | itemsize = fieldsize; | |
592 | itembytes = itemsize; | |
593 | sv_pos_u2b(sv, &itembytes, 0); | |
594 | } | |
595 | else | |
596 | itembytes = len; | |
597 | send = chophere = s + itembytes; | |
598 | while (s < send) { | |
599 | if (*s & ~31) | |
600 | gotsome = TRUE; | |
601 | else if (*s == '\n') | |
602 | break; | |
603 | s++; | |
604 | } | |
605 | item_is_utf8 = TRUE; | |
606 | itemsize = s - item; | |
607 | sv_pos_b2u(sv, &itemsize); | |
608 | break; | |
a0ed51b3 | 609 | } |
a0ed51b3 | 610 | } |
5a34cab7 NC |
611 | item_is_utf8 = FALSE; |
612 | if (itemsize > fieldsize) | |
613 | itemsize = fieldsize; | |
614 | send = chophere = s + itemsize; | |
615 | while (s < send) { | |
616 | if (*s & ~31) | |
617 | gotsome = TRUE; | |
618 | else if (*s == '\n') | |
619 | break; | |
620 | s++; | |
621 | } | |
622 | itemsize = s - item; | |
623 | break; | |
a0ed51b3 | 624 | } |
a0d0e21e LW |
625 | |
626 | case FF_CHECKCHOP: | |
5a34cab7 NC |
627 | { |
628 | const char *s = item = SvPV_const(sv, len); | |
629 | itemsize = len; | |
630 | if (DO_UTF8(sv)) { | |
631 | itemsize = sv_len_utf8(sv); | |
632 | if (itemsize != (I32)len) { | |
633 | I32 itembytes; | |
634 | if (itemsize <= fieldsize) { | |
635 | const char *send = chophere = s + itemsize; | |
636 | while (s < send) { | |
637 | if (*s == '\r') { | |
638 | itemsize = s - item; | |
a0ed51b3 | 639 | chophere = s; |
a0ed51b3 | 640 | break; |
5a34cab7 NC |
641 | } |
642 | if (*s++ & ~31) | |
a0ed51b3 | 643 | gotsome = TRUE; |
a0ed51b3 | 644 | } |
a0ed51b3 | 645 | } |
5a34cab7 NC |
646 | else { |
647 | const char *send; | |
648 | itemsize = fieldsize; | |
649 | itembytes = itemsize; | |
650 | sv_pos_u2b(sv, &itembytes, 0); | |
651 | send = chophere = s + itembytes; | |
652 | while (s < send || (s == send && isSPACE(*s))) { | |
653 | if (isSPACE(*s)) { | |
654 | if (chopspace) | |
655 | chophere = s; | |
656 | if (*s == '\r') | |
657 | break; | |
658 | } | |
659 | else { | |
660 | if (*s & ~31) | |
661 | gotsome = TRUE; | |
662 | if (strchr(PL_chopset, *s)) | |
663 | chophere = s + 1; | |
664 | } | |
665 | s++; | |
666 | } | |
667 | itemsize = chophere - item; | |
668 | sv_pos_b2u(sv, &itemsize); | |
669 | } | |
670 | item_is_utf8 = TRUE; | |
a0d0e21e LW |
671 | break; |
672 | } | |
a0d0e21e | 673 | } |
5a34cab7 NC |
674 | item_is_utf8 = FALSE; |
675 | if (itemsize <= fieldsize) { | |
676 | const char *const send = chophere = s + itemsize; | |
677 | while (s < send) { | |
678 | if (*s == '\r') { | |
679 | itemsize = s - item; | |
a0d0e21e | 680 | chophere = s; |
a0d0e21e | 681 | break; |
5a34cab7 NC |
682 | } |
683 | if (*s++ & ~31) | |
a0d0e21e | 684 | gotsome = TRUE; |
a0d0e21e | 685 | } |
a0d0e21e | 686 | } |
5a34cab7 NC |
687 | else { |
688 | const char *send; | |
689 | itemsize = fieldsize; | |
690 | send = chophere = s + itemsize; | |
691 | while (s < send || (s == send && isSPACE(*s))) { | |
692 | if (isSPACE(*s)) { | |
693 | if (chopspace) | |
694 | chophere = s; | |
695 | if (*s == '\r') | |
696 | break; | |
697 | } | |
698 | else { | |
699 | if (*s & ~31) | |
700 | gotsome = TRUE; | |
701 | if (strchr(PL_chopset, *s)) | |
702 | chophere = s + 1; | |
703 | } | |
704 | s++; | |
705 | } | |
706 | itemsize = chophere - item; | |
707 | } | |
708 | break; | |
a0d0e21e | 709 | } |
a0d0e21e LW |
710 | |
711 | case FF_SPACE: | |
712 | arg = fieldsize - itemsize; | |
713 | if (arg) { | |
714 | fieldsize -= arg; | |
715 | while (arg-- > 0) | |
716 | *t++ = ' '; | |
717 | } | |
718 | break; | |
719 | ||
720 | case FF_HALFSPACE: | |
721 | arg = fieldsize - itemsize; | |
722 | if (arg) { | |
723 | arg /= 2; | |
724 | fieldsize -= arg; | |
725 | while (arg-- > 0) | |
726 | *t++ = ' '; | |
727 | } | |
728 | break; | |
729 | ||
730 | case FF_ITEM: | |
8aa7beb6 DM |
731 | to_copy = itemsize; |
732 | source = (U8 *)item; | |
733 | trans = 1; | |
734 | if (item_is_utf8) { | |
735 | /* convert to_copy from chars to bytes */ | |
736 | U8 *s = source; | |
737 | while (to_copy--) | |
738 | s += UTF8SKIP(s); | |
739 | to_copy = s - source; | |
a0d0e21e | 740 | } |
8aa7beb6 | 741 | goto append; |
a0d0e21e LW |
742 | |
743 | case FF_CHOP: | |
5a34cab7 NC |
744 | { |
745 | const char *s = chophere; | |
746 | if (chopspace) { | |
af68e756 | 747 | while (isSPACE(*s)) |
5a34cab7 NC |
748 | s++; |
749 | } | |
750 | sv_chop(sv,s); | |
751 | SvSETMAGIC(sv); | |
752 | break; | |
a0d0e21e | 753 | } |
a0d0e21e | 754 | |
a1b95068 LW |
755 | case FF_LINESNGL: |
756 | chopspace = 0; | |
a0d0e21e | 757 | case FF_LINEGLOB: |
5a34cab7 | 758 | { |
e32383e2 | 759 | const bool oneline = fpc[-1] == FF_LINESNGL; |
5a34cab7 | 760 | const char *s = item = SvPV_const(sv, len); |
7440a75b | 761 | const char *const send = s + len; |
7440a75b | 762 | |
f3f2f1a3 | 763 | item_is_utf8 = DO_UTF8(sv); |
a1137ee5 | 764 | if (!len) |
7440a75b | 765 | break; |
ea60cfe8 | 766 | trans = 0; |
0d21cefe | 767 | gotsome = TRUE; |
a1137ee5 | 768 | chophere = s + len; |
4ff700b9 DM |
769 | source = (U8 *) s; |
770 | to_copy = len; | |
0d21cefe DM |
771 | while (s < send) { |
772 | if (*s++ == '\n') { | |
773 | if (oneline) { | |
774 | to_copy = s - SvPVX_const(sv) - 1; | |
775 | chophere = s; | |
776 | break; | |
777 | } else { | |
778 | if (s == send) { | |
0d21cefe DM |
779 | to_copy--; |
780 | } else | |
781 | lines++; | |
1bd51a4c | 782 | } |
a0d0e21e | 783 | } |
0d21cefe | 784 | } |
a2c0032b DM |
785 | } |
786 | ||
ea60cfe8 DM |
787 | append: |
788 | /* append to_copy bytes from source to PL_formstring. | |
789 | * item_is_utf8 implies source is utf8. | |
790 | * if trans, translate certain characters during the copy */ | |
a2c0032b DM |
791 | { |
792 | U8 *tmp = NULL; | |
26e935cf | 793 | STRLEN grow = 0; |
0325ce87 DM |
794 | |
795 | SvCUR_set(PL_formtarget, | |
796 | t - SvPVX_const(PL_formtarget)); | |
797 | ||
0d21cefe DM |
798 | if (targ_is_utf8 && !item_is_utf8) { |
799 | source = tmp = bytes_to_utf8(source, &to_copy); | |
0d21cefe DM |
800 | } else { |
801 | if (item_is_utf8 && !targ_is_utf8) { | |
f5ada144 | 802 | U8 *s; |
0d21cefe | 803 | /* Upgrade targ to UTF8, and then we reduce it to |
0325ce87 DM |
804 | a problem we have a simple solution for. |
805 | Don't need get magic. */ | |
0d21cefe | 806 | sv_utf8_upgrade_nomg(PL_formtarget); |
0325ce87 | 807 | targ_is_utf8 = TRUE; |
f5ada144 DM |
808 | /* re-calculate linemark */ |
809 | s = (U8*)SvPVX(PL_formtarget); | |
26e935cf DM |
810 | /* the bytes we initially allocated to append the |
811 | * whole line may have been gobbled up during the | |
812 | * upgrade, so allocate a whole new line's worth | |
813 | * for safety */ | |
814 | grow = linemax; | |
f5ada144 DM |
815 | while (linemark--) |
816 | s += UTF8SKIP(s); | |
817 | linemark = s - (U8*)SvPVX(PL_formtarget); | |
e8e72d41 | 818 | } |
0d21cefe DM |
819 | /* Easy. They agree. */ |
820 | assert (item_is_utf8 == targ_is_utf8); | |
821 | } | |
26e935cf DM |
822 | if (!trans) |
823 | /* @* and ^* are the only things that can exceed | |
824 | * the linemax, so grow by the output size, plus | |
825 | * a whole new form's worth in case of any further | |
826 | * output */ | |
827 | grow = linemax + to_copy; | |
828 | if (grow) | |
829 | SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1); | |
0d21cefe DM |
830 | t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget); |
831 | ||
832 | Copy(source, t, to_copy, char); | |
ea60cfe8 | 833 | if (trans) { |
8aa7beb6 DM |
834 | /* blank out ~ or control chars, depending on trans. |
835 | * works on bytes not chars, so relies on not | |
836 | * matching utf8 continuation bytes */ | |
ea60cfe8 DM |
837 | U8 *s = (U8*)t; |
838 | U8 *send = s + to_copy; | |
839 | while (s < send) { | |
8aa7beb6 DM |
840 | const int ch = *s; |
841 | if (trans == '~' ? (ch == '~') : | |
842 | #ifdef EBCDIC | |
843 | iscntrl(ch) | |
844 | #else | |
845 | (!(ch & ~31)) | |
846 | #endif | |
847 | ) | |
ea60cfe8 DM |
848 | *s = ' '; |
849 | s++; | |
850 | } | |
851 | } | |
852 | ||
0d21cefe DM |
853 | t += to_copy; |
854 | SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy); | |
a1137ee5 | 855 | if (tmp) |
0d21cefe | 856 | Safefree(tmp); |
5a34cab7 | 857 | break; |
a0d0e21e | 858 | } |
a0d0e21e | 859 | |
a1b95068 LW |
860 | case FF_0DECIMAL: |
861 | arg = *fpc++; | |
862 | #if defined(USE_LONG_DOUBLE) | |
10edeb5d | 863 | fmt = (const char *) |
a701009a | 864 | ((arg & FORM_NUM_POINT) ? |
10edeb5d | 865 | "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl); |
a1b95068 | 866 | #else |
10edeb5d | 867 | fmt = (const char *) |
a701009a | 868 | ((arg & FORM_NUM_POINT) ? |
10edeb5d | 869 | "%#0*.*f" : "%0*.*f"); |
a1b95068 LW |
870 | #endif |
871 | goto ff_dec; | |
a0d0e21e | 872 | case FF_DECIMAL: |
a0d0e21e | 873 | arg = *fpc++; |
65202027 | 874 | #if defined(USE_LONG_DOUBLE) |
10edeb5d | 875 | fmt = (const char *) |
a701009a | 876 | ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl); |
65202027 | 877 | #else |
10edeb5d | 878 | fmt = (const char *) |
a701009a | 879 | ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f"); |
65202027 | 880 | #endif |
a1b95068 | 881 | ff_dec: |
784707d5 JP |
882 | /* If the field is marked with ^ and the value is undefined, |
883 | blank it out. */ | |
a701009a | 884 | if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) { |
784707d5 JP |
885 | arg = fieldsize; |
886 | while (arg--) | |
887 | *t++ = ' '; | |
888 | break; | |
889 | } | |
890 | gotsome = TRUE; | |
891 | value = SvNV(sv); | |
a1b95068 | 892 | /* overflow evidence */ |
bfed75c6 | 893 | if (num_overflow(value, fieldsize, arg)) { |
a1b95068 LW |
894 | arg = fieldsize; |
895 | while (arg--) | |
896 | *t++ = '#'; | |
897 | break; | |
898 | } | |
784707d5 JP |
899 | /* Formats aren't yet marked for locales, so assume "yes". */ |
900 | { | |
901 | STORE_NUMERIC_STANDARD_SET_LOCAL(); | |
a701009a DM |
902 | arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
903 | my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value); | |
784707d5 JP |
904 | RESTORE_NUMERIC_STANDARD(); |
905 | } | |
906 | t += fieldsize; | |
907 | break; | |
a1b95068 | 908 | |
a0d0e21e LW |
909 | case FF_NEWLINE: |
910 | f++; | |
f5ada144 | 911 | while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ; |
a0d0e21e LW |
912 | t++; |
913 | *t++ = '\n'; | |
914 | break; | |
915 | ||
916 | case FF_BLANK: | |
917 | arg = *fpc++; | |
918 | if (gotsome) { | |
919 | if (arg) { /* repeat until fields exhausted? */ | |
11f9eeaf DM |
920 | fpc--; |
921 | goto end; | |
a0d0e21e LW |
922 | } |
923 | } | |
924 | else { | |
f5ada144 | 925 | t = SvPVX(PL_formtarget) + linemark; |
a0d0e21e LW |
926 | lines--; |
927 | } | |
928 | break; | |
929 | ||
930 | case FF_MORE: | |
5a34cab7 NC |
931 | { |
932 | const char *s = chophere; | |
933 | const char *send = item + len; | |
934 | if (chopspace) { | |
af68e756 | 935 | while (isSPACE(*s) && (s < send)) |
5a34cab7 | 936 | s++; |
a0d0e21e | 937 | } |
5a34cab7 NC |
938 | if (s < send) { |
939 | char *s1; | |
940 | arg = fieldsize - itemsize; | |
941 | if (arg) { | |
942 | fieldsize -= arg; | |
943 | while (arg-- > 0) | |
944 | *t++ = ' '; | |
945 | } | |
946 | s1 = t - 3; | |
947 | if (strnEQ(s1," ",3)) { | |
948 | while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1])) | |
949 | s1--; | |
950 | } | |
951 | *s1++ = '.'; | |
952 | *s1++ = '.'; | |
953 | *s1++ = '.'; | |
a0d0e21e | 954 | } |
5a34cab7 | 955 | break; |
a0d0e21e | 956 | } |
a0d0e21e | 957 | case FF_END: |
11f9eeaf | 958 | end: |
bf2bec63 | 959 | assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget)); |
a0d0e21e | 960 | *t = '\0'; |
b15aece3 | 961 | SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget)); |
1bd51a4c IH |
962 | if (targ_is_utf8) |
963 | SvUTF8_on(PL_formtarget); | |
3280af22 | 964 | FmLINES(PL_formtarget) += lines; |
a0d0e21e | 965 | SP = ORIGMARK; |
11f9eeaf DM |
966 | if (fpc[-1] == FF_BLANK) |
967 | RETURNOP(cLISTOP->op_first); | |
968 | else | |
969 | RETPUSHYES; | |
a0d0e21e LW |
970 | } |
971 | } | |
972 | } | |
973 | ||
974 | PP(pp_grepstart) | |
975 | { | |
27da23d5 | 976 | dVAR; dSP; |
a0d0e21e LW |
977 | SV *src; |
978 | ||
3280af22 | 979 | if (PL_stack_base + *PL_markstack_ptr == SP) { |
a0d0e21e | 980 | (void)POPMARK; |
54310121 | 981 | if (GIMME_V == G_SCALAR) |
6e449a3a | 982 | mXPUSHi(0); |
533c011a | 983 | RETURNOP(PL_op->op_next->op_next); |
a0d0e21e | 984 | } |
3280af22 | 985 | PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1; |
897d3989 NC |
986 | Perl_pp_pushmark(aTHX); /* push dst */ |
987 | Perl_pp_pushmark(aTHX); /* push src */ | |
d343c3ef | 988 | ENTER_with_name("grep"); /* enter outer scope */ |
a0d0e21e LW |
989 | |
990 | SAVETMPS; | |
59f00321 RGS |
991 | if (PL_op->op_private & OPpGREP_LEX) |
992 | SAVESPTR(PAD_SVl(PL_op->op_targ)); | |
993 | else | |
994 | SAVE_DEFSV; | |
d343c3ef | 995 | ENTER_with_name("grep_item"); /* enter inner scope */ |
7766f137 | 996 | SAVEVPTR(PL_curpm); |
a0d0e21e | 997 | |
3280af22 | 998 | src = PL_stack_base[*PL_markstack_ptr]; |
a0d0e21e | 999 | SvTEMP_off(src); |
59f00321 RGS |
1000 | if (PL_op->op_private & OPpGREP_LEX) |
1001 | PAD_SVl(PL_op->op_targ) = src; | |
1002 | else | |
414bf5ae | 1003 | DEFSV_set(src); |
a0d0e21e LW |
1004 | |
1005 | PUTBACK; | |
533c011a | 1006 | if (PL_op->op_type == OP_MAPSTART) |
897d3989 | 1007 | Perl_pp_pushmark(aTHX); /* push top */ |
533c011a | 1008 | return ((LOGOP*)PL_op->op_next)->op_other; |
a0d0e21e LW |
1009 | } |
1010 | ||
a0d0e21e LW |
1011 | PP(pp_mapwhile) |
1012 | { | |
27da23d5 | 1013 | dVAR; dSP; |
f54cb97a | 1014 | const I32 gimme = GIMME_V; |
544f3153 | 1015 | I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */ |
a0d0e21e LW |
1016 | I32 count; |
1017 | I32 shift; | |
1018 | SV** src; | |
ac27b0f5 | 1019 | SV** dst; |
a0d0e21e | 1020 | |
544f3153 | 1021 | /* first, move source pointer to the next item in the source list */ |
3280af22 | 1022 | ++PL_markstack_ptr[-1]; |
544f3153 GS |
1023 | |
1024 | /* if there are new items, push them into the destination list */ | |
4c90a460 | 1025 | if (items && gimme != G_VOID) { |
544f3153 GS |
1026 | /* might need to make room back there first */ |
1027 | if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) { | |
1028 | /* XXX this implementation is very pessimal because the stack | |
1029 | * is repeatedly extended for every set of items. Is possible | |
1030 | * to do this without any stack extension or copying at all | |
1031 | * by maintaining a separate list over which the map iterates | |
18ef8bea | 1032 | * (like foreach does). --gsar */ |
544f3153 GS |
1033 | |
1034 | /* everything in the stack after the destination list moves | |
1035 | * towards the end the stack by the amount of room needed */ | |
1036 | shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]); | |
1037 | ||
1038 | /* items to shift up (accounting for the moved source pointer) */ | |
1039 | count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1); | |
18ef8bea BT |
1040 | |
1041 | /* This optimization is by Ben Tilly and it does | |
1042 | * things differently from what Sarathy (gsar) | |
1043 | * is describing. The downside of this optimization is | |
1044 | * that leaves "holes" (uninitialized and hopefully unused areas) | |
1045 | * to the Perl stack, but on the other hand this | |
1046 | * shouldn't be a problem. If Sarathy's idea gets | |
1047 | * implemented, this optimization should become | |
1048 | * irrelevant. --jhi */ | |
1049 | if (shift < count) | |
1050 | shift = count; /* Avoid shifting too often --Ben Tilly */ | |
bfed75c6 | 1051 | |
924508f0 GS |
1052 | EXTEND(SP,shift); |
1053 | src = SP; | |
1054 | dst = (SP += shift); | |
3280af22 NIS |
1055 | PL_markstack_ptr[-1] += shift; |
1056 | *PL_markstack_ptr += shift; | |
544f3153 | 1057 | while (count--) |
a0d0e21e LW |
1058 | *dst-- = *src--; |
1059 | } | |
544f3153 | 1060 | /* copy the new items down to the destination list */ |
ac27b0f5 | 1061 | dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1; |
22023b26 | 1062 | if (gimme == G_ARRAY) { |
b2a2a901 DM |
1063 | /* add returned items to the collection (making mortal copies |
1064 | * if necessary), then clear the current temps stack frame | |
1065 | * *except* for those items. We do this splicing the items | |
1066 | * into the start of the tmps frame (so some items may be on | |
59d53fd6 | 1067 | * the tmps stack twice), then moving PL_tmps_floor above |
b2a2a901 DM |
1068 | * them, then freeing the frame. That way, the only tmps that |
1069 | * accumulate over iterations are the return values for map. | |
1070 | * We have to do to this way so that everything gets correctly | |
1071 | * freed if we die during the map. | |
1072 | */ | |
1073 | I32 tmpsbase; | |
1074 | I32 i = items; | |
1075 | /* make space for the slice */ | |
1076 | EXTEND_MORTAL(items); | |
1077 | tmpsbase = PL_tmps_floor + 1; | |
1078 | Move(PL_tmps_stack + tmpsbase, | |
1079 | PL_tmps_stack + tmpsbase + items, | |
1080 | PL_tmps_ix - PL_tmps_floor, | |
1081 | SV*); | |
1082 | PL_tmps_ix += items; | |
1083 | ||
1084 | while (i-- > 0) { | |
1085 | SV *sv = POPs; | |
1086 | if (!SvTEMP(sv)) | |
1087 | sv = sv_mortalcopy(sv); | |
1088 | *dst-- = sv; | |
1089 | PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv); | |
1090 | } | |
1091 | /* clear the stack frame except for the items */ | |
1092 | PL_tmps_floor += items; | |
1093 | FREETMPS; | |
1094 | /* FREETMPS may have cleared the TEMP flag on some of the items */ | |
1095 | i = items; | |
1096 | while (i-- > 0) | |
1097 | SvTEMP_on(PL_tmps_stack[--tmpsbase]); | |
22023b26 | 1098 | } |
bfed75c6 | 1099 | else { |
22023b26 TP |
1100 | /* scalar context: we don't care about which values map returns |
1101 | * (we use undef here). And so we certainly don't want to do mortal | |
1102 | * copies of meaningless values. */ | |
1103 | while (items-- > 0) { | |
b988aa42 | 1104 | (void)POPs; |
22023b26 TP |
1105 | *dst-- = &PL_sv_undef; |
1106 | } | |
b2a2a901 | 1107 | FREETMPS; |
22023b26 | 1108 | } |
a0d0e21e | 1109 | } |
b2a2a901 DM |
1110 | else { |
1111 | FREETMPS; | |
1112 | } | |
d343c3ef | 1113 | LEAVE_with_name("grep_item"); /* exit inner scope */ |
a0d0e21e LW |
1114 | |
1115 | /* All done yet? */ | |
3280af22 | 1116 | if (PL_markstack_ptr[-1] > *PL_markstack_ptr) { |
a0d0e21e LW |
1117 | |
1118 | (void)POPMARK; /* pop top */ | |
d343c3ef | 1119 | LEAVE_with_name("grep"); /* exit outer scope */ |
a0d0e21e | 1120 | (void)POPMARK; /* pop src */ |
3280af22 | 1121 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; |
a0d0e21e | 1122 | (void)POPMARK; /* pop dst */ |
3280af22 | 1123 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
54310121 | 1124 | if (gimme == G_SCALAR) { |
7cc47870 RGS |
1125 | if (PL_op->op_private & OPpGREP_LEX) { |
1126 | SV* sv = sv_newmortal(); | |
1127 | sv_setiv(sv, items); | |
1128 | PUSHs(sv); | |
1129 | } | |
1130 | else { | |
1131 | dTARGET; | |
1132 | XPUSHi(items); | |
1133 | } | |
a0d0e21e | 1134 | } |
54310121 PP |
1135 | else if (gimme == G_ARRAY) |
1136 | SP += items; | |
a0d0e21e LW |
1137 | RETURN; |
1138 | } | |
1139 | else { | |
1140 | SV *src; | |
1141 | ||
d343c3ef | 1142 | ENTER_with_name("grep_item"); /* enter inner scope */ |
7766f137 | 1143 | SAVEVPTR(PL_curpm); |
a0d0e21e | 1144 | |
544f3153 | 1145 | /* set $_ to the new source item */ |
3280af22 | 1146 | src = PL_stack_base[PL_markstack_ptr[-1]]; |
a0d0e21e | 1147 | SvTEMP_off(src); |
59f00321 RGS |
1148 | if (PL_op->op_private & OPpGREP_LEX) |
1149 | PAD_SVl(PL_op->op_targ) = src; | |
1150 | else | |
414bf5ae | 1151 | DEFSV_set(src); |
a0d0e21e LW |
1152 | |
1153 | RETURNOP(cLOGOP->op_other); | |
1154 | } | |
1155 | } | |
1156 | ||
a0d0e21e LW |
1157 | /* Range stuff. */ |
1158 | ||
1159 | PP(pp_range) | |
1160 | { | |
97aff369 | 1161 | dVAR; |
a0d0e21e | 1162 | if (GIMME == G_ARRAY) |
1a67a97c | 1163 | return NORMAL; |
538573f7 | 1164 | if (SvTRUEx(PAD_SV(PL_op->op_targ))) |
1a67a97c | 1165 | return cLOGOP->op_other; |
538573f7 | 1166 | else |
1a67a97c | 1167 | return NORMAL; |
a0d0e21e LW |
1168 | } |
1169 | ||
1170 | PP(pp_flip) | |
1171 | { | |
97aff369 | 1172 | dVAR; |
39644a26 | 1173 | dSP; |
a0d0e21e LW |
1174 | |
1175 | if (GIMME == G_ARRAY) { | |
1a67a97c | 1176 | RETURNOP(((LOGOP*)cUNOP->op_first)->op_other); |
a0d0e21e LW |
1177 | } |
1178 | else { | |
1179 | dTOPss; | |
44f8325f | 1180 | SV * const targ = PAD_SV(PL_op->op_targ); |
bfed75c6 | 1181 | int flip = 0; |
790090df | 1182 | |
bfed75c6 | 1183 | if (PL_op->op_private & OPpFLIP_LINENUM) { |
4e3399f9 YST |
1184 | if (GvIO(PL_last_in_gv)) { |
1185 | flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); | |
1186 | } | |
1187 | else { | |
fafc274c | 1188 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); |
44f8325f AL |
1189 | if (gv && GvSV(gv)) |
1190 | flip = SvIV(sv) == SvIV(GvSV(gv)); | |
4e3399f9 | 1191 | } |
bfed75c6 AL |
1192 | } else { |
1193 | flip = SvTRUE(sv); | |
1194 | } | |
1195 | if (flip) { | |
a0d0e21e | 1196 | sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1); |
533c011a | 1197 | if (PL_op->op_flags & OPf_SPECIAL) { |
a0d0e21e | 1198 | sv_setiv(targ, 1); |
3e3baf6d | 1199 | SETs(targ); |
a0d0e21e LW |
1200 | RETURN; |
1201 | } | |
1202 | else { | |
1203 | sv_setiv(targ, 0); | |
924508f0 | 1204 | SP--; |
1a67a97c | 1205 | RETURNOP(((LOGOP*)cUNOP->op_first)->op_other); |
a0d0e21e LW |
1206 | } |
1207 | } | |
76f68e9b | 1208 | sv_setpvs(TARG, ""); |
a0d0e21e LW |
1209 | SETs(targ); |
1210 | RETURN; | |
1211 | } | |
1212 | } | |
1213 | ||
8e9bbdb9 RGS |
1214 | /* This code tries to decide if "$left .. $right" should use the |
1215 | magical string increment, or if the range is numeric (we make | |
1216 | an exception for .."0" [#18165]). AMS 20021031. */ | |
1217 | ||
1218 | #define RANGE_IS_NUMERIC(left,right) ( \ | |
b0e74086 RGS |
1219 | SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \ |
1220 | SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \ | |
e0ab1c0e | 1221 | (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \ |
b15aece3 | 1222 | looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \ |
e0ab1c0e | 1223 | && (!SvOK(right) || looks_like_number(right)))) |
8e9bbdb9 | 1224 | |
a0d0e21e LW |
1225 | PP(pp_flop) |
1226 | { | |
97aff369 | 1227 | dVAR; dSP; |
a0d0e21e LW |
1228 | |
1229 | if (GIMME == G_ARRAY) { | |
1230 | dPOPPOPssrl; | |
86cb7173 | 1231 | |
5b295bef RD |
1232 | SvGETMAGIC(left); |
1233 | SvGETMAGIC(right); | |
a0d0e21e | 1234 | |
8e9bbdb9 | 1235 | if (RANGE_IS_NUMERIC(left,right)) { |
eb578fdb | 1236 | IV i, j; |
901017d6 | 1237 | IV max; |
f52e41ad FC |
1238 | if ((SvOK(left) && SvNV_nomg(left) < IV_MIN) || |
1239 | (SvOK(right) && SvNV_nomg(right) > IV_MAX)) | |
d470f89e | 1240 | DIE(aTHX_ "Range iterator outside integer range"); |
f52e41ad FC |
1241 | i = SvIV_nomg(left); |
1242 | max = SvIV_nomg(right); | |
bbce6d69 | 1243 | if (max >= i) { |
c1ab3db2 AK |
1244 | j = max - i + 1; |
1245 | EXTEND_MORTAL(j); | |
1246 | EXTEND(SP, j); | |
bbce6d69 | 1247 | } |
c1ab3db2 AK |
1248 | else |
1249 | j = 0; | |
1250 | while (j--) { | |
901017d6 | 1251 | SV * const sv = sv_2mortal(newSViv(i++)); |
a0d0e21e LW |
1252 | PUSHs(sv); |
1253 | } | |
1254 | } | |
1255 | else { | |
3c323193 FC |
1256 | STRLEN len, llen; |
1257 | const char * const lpv = SvPV_nomg_const(left, llen); | |
f52e41ad | 1258 | const char * const tmps = SvPV_nomg_const(right, len); |
a0d0e21e | 1259 | |
3c323193 | 1260 | SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP); |
89ea2908 | 1261 | while (!SvNIOKp(sv) && SvCUR(sv) <= len) { |
a0d0e21e | 1262 | XPUSHs(sv); |
b15aece3 | 1263 | if (strEQ(SvPVX_const(sv),tmps)) |
89ea2908 | 1264 | break; |
a0d0e21e LW |
1265 | sv = sv_2mortal(newSVsv(sv)); |
1266 | sv_inc(sv); | |
1267 | } | |
a0d0e21e LW |
1268 | } |
1269 | } | |
1270 | else { | |
1271 | dTOPss; | |
901017d6 | 1272 | SV * const targ = PAD_SV(cUNOP->op_first->op_targ); |
4e3399f9 | 1273 | int flop = 0; |
a0d0e21e | 1274 | sv_inc(targ); |
4e3399f9 YST |
1275 | |
1276 | if (PL_op->op_private & OPpFLIP_LINENUM) { | |
1277 | if (GvIO(PL_last_in_gv)) { | |
1278 | flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); | |
1279 | } | |
1280 | else { | |
fafc274c | 1281 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); |
4e3399f9 YST |
1282 | if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv)); |
1283 | } | |
1284 | } | |
1285 | else { | |
1286 | flop = SvTRUE(sv); | |
1287 | } | |
1288 | ||
1289 | if (flop) { | |
a0d0e21e | 1290 | sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0); |
396482e1 | 1291 | sv_catpvs(targ, "E0"); |
a0d0e21e LW |
1292 | } |
1293 | SETs(targ); | |
1294 | } | |
1295 | ||
1296 | RETURN; | |
1297 | } | |
1298 | ||
1299 | /* Control. */ | |
1300 | ||
27da23d5 | 1301 | static const char * const context_name[] = { |
515afda2 | 1302 | "pseudo-block", |
f31522f3 | 1303 | NULL, /* CXt_WHEN never actually needs "block" */ |
76753e7f | 1304 | NULL, /* CXt_BLOCK never actually needs "block" */ |
f31522f3 | 1305 | NULL, /* CXt_GIVEN never actually needs "block" */ |
76753e7f NC |
1306 | NULL, /* CXt_LOOP_FOR never actually needs "loop" */ |
1307 | NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */ | |
1308 | NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */ | |
1309 | NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */ | |
515afda2 | 1310 | "subroutine", |
76753e7f | 1311 | "format", |
515afda2 | 1312 | "eval", |
515afda2 | 1313 | "substitution", |
515afda2 NC |
1314 | }; |
1315 | ||
76e3520e | 1316 | STATIC I32 |
5db1eb8d | 1317 | S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags) |
a0d0e21e | 1318 | { |
97aff369 | 1319 | dVAR; |
eb578fdb | 1320 | I32 i; |
a0d0e21e | 1321 | |
7918f24d NC |
1322 | PERL_ARGS_ASSERT_DOPOPTOLABEL; |
1323 | ||
a0d0e21e | 1324 | for (i = cxstack_ix; i >= 0; i--) { |
eb578fdb | 1325 | const PERL_CONTEXT * const cx = &cxstack[i]; |
6b35e009 | 1326 | switch (CxTYPE(cx)) { |
a0d0e21e | 1327 | case CXt_SUBST: |
a0d0e21e | 1328 | case CXt_SUB: |
7766f137 | 1329 | case CXt_FORMAT: |
a0d0e21e | 1330 | case CXt_EVAL: |
0a753a76 | 1331 | case CXt_NULL: |
dcbac5bb | 1332 | /* diag_listed_as: Exiting subroutine via %s */ |
a2a5de95 NC |
1333 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", |
1334 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); | |
515afda2 NC |
1335 | if (CxTYPE(cx) == CXt_NULL) |
1336 | return -1; | |
1337 | break; | |
c6fdafd0 | 1338 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1339 | case CXt_LOOP_LAZYSV: |
3b719c58 NC |
1340 | case CXt_LOOP_FOR: |
1341 | case CXt_LOOP_PLAIN: | |
7e8f1eac | 1342 | { |
5db1eb8d BF |
1343 | STRLEN cx_label_len = 0; |
1344 | U32 cx_label_flags = 0; | |
1345 | const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags); | |
1346 | if (!cx_label || !( | |
1347 | ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ? | |
1348 | (flags & SVf_UTF8) | |
1349 | ? (bytes_cmp_utf8( | |
1350 | (const U8*)cx_label, cx_label_len, | |
1351 | (const U8*)label, len) == 0) | |
1352 | : (bytes_cmp_utf8( | |
1353 | (const U8*)label, len, | |
1354 | (const U8*)cx_label, cx_label_len) == 0) | |
eade7155 BF |
1355 | : (len == cx_label_len && ((cx_label == label) |
1356 | || memEQ(cx_label, label, len))) )) { | |
1c98cc53 | 1357 | DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n", |
7e8f1eac | 1358 | (long)i, cx_label)); |
a0d0e21e LW |
1359 | continue; |
1360 | } | |
1c98cc53 | 1361 | DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label)); |
a0d0e21e | 1362 | return i; |
7e8f1eac | 1363 | } |
a0d0e21e LW |
1364 | } |
1365 | } | |
1366 | return i; | |
1367 | } | |
1368 | ||
0d863452 RH |
1369 | |
1370 | ||
e50aee73 | 1371 | I32 |
864dbfa3 | 1372 | Perl_dowantarray(pTHX) |
e50aee73 | 1373 | { |
97aff369 | 1374 | dVAR; |
f54cb97a | 1375 | const I32 gimme = block_gimme(); |
54310121 PP |
1376 | return (gimme == G_VOID) ? G_SCALAR : gimme; |
1377 | } | |
1378 | ||
1379 | I32 | |
864dbfa3 | 1380 | Perl_block_gimme(pTHX) |
54310121 | 1381 | { |
97aff369 | 1382 | dVAR; |
06b5626a | 1383 | const I32 cxix = dopoptosub(cxstack_ix); |
e50aee73 | 1384 | if (cxix < 0) |
46fc3d4c | 1385 | return G_VOID; |
e50aee73 | 1386 | |
54310121 | 1387 | switch (cxstack[cxix].blk_gimme) { |
d2719217 GS |
1388 | case G_VOID: |
1389 | return G_VOID; | |
54310121 | 1390 | case G_SCALAR: |
e50aee73 | 1391 | return G_SCALAR; |
54310121 PP |
1392 | case G_ARRAY: |
1393 | return G_ARRAY; | |
1394 | default: | |
cea2e8a9 | 1395 | Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme); |
118e2215 | 1396 | assert(0); /* NOTREACHED */ |
d2719217 | 1397 | return 0; |
54310121 | 1398 | } |
e50aee73 AD |
1399 | } |
1400 | ||
78f9721b SM |
1401 | I32 |
1402 | Perl_is_lvalue_sub(pTHX) | |
1403 | { | |
97aff369 | 1404 | dVAR; |
06b5626a | 1405 | const I32 cxix = dopoptosub(cxstack_ix); |
78f9721b SM |
1406 | assert(cxix >= 0); /* We should only be called from inside subs */ |
1407 | ||
bafb2adc NC |
1408 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) |
1409 | return CxLVAL(cxstack + cxix); | |
78f9721b SM |
1410 | else |
1411 | return 0; | |
1412 | } | |
1413 | ||
777d9014 FC |
1414 | /* only used by PUSHSUB */ |
1415 | I32 | |
1416 | Perl_was_lvalue_sub(pTHX) | |
1417 | { | |
1418 | dVAR; | |
1419 | const I32 cxix = dopoptosub(cxstack_ix-1); | |
1420 | assert(cxix >= 0); /* We should only be called from inside subs */ | |
1421 | ||
1422 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) | |
1423 | return CxLVAL(cxstack + cxix); | |
1424 | else | |
1425 | return 0; | |
1426 | } | |
1427 | ||
76e3520e | 1428 | STATIC I32 |
901017d6 | 1429 | S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock) |
2c375eb9 | 1430 | { |
97aff369 | 1431 | dVAR; |
a0d0e21e | 1432 | I32 i; |
7918f24d NC |
1433 | |
1434 | PERL_ARGS_ASSERT_DOPOPTOSUB_AT; | |
1435 | ||
a0d0e21e | 1436 | for (i = startingblock; i >= 0; i--) { |
eb578fdb | 1437 | const PERL_CONTEXT * const cx = &cxstk[i]; |
6b35e009 | 1438 | switch (CxTYPE(cx)) { |
a0d0e21e LW |
1439 | default: |
1440 | continue; | |
1441 | case CXt_EVAL: | |
1442 | case CXt_SUB: | |
7766f137 | 1443 | case CXt_FORMAT: |
1c98cc53 | 1444 | DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); |
a0d0e21e LW |
1445 | return i; |
1446 | } | |
1447 | } | |
1448 | return i; | |
1449 | } | |
1450 | ||
76e3520e | 1451 | STATIC I32 |
cea2e8a9 | 1452 | S_dopoptoeval(pTHX_ I32 startingblock) |
a0d0e21e | 1453 | { |
97aff369 | 1454 | dVAR; |
a0d0e21e | 1455 | I32 i; |
a0d0e21e | 1456 | for (i = startingblock; i >= 0; i--) { |
eb578fdb | 1457 | const PERL_CONTEXT *cx = &cxstack[i]; |
6b35e009 | 1458 | switch (CxTYPE(cx)) { |
a0d0e21e LW |
1459 | default: |
1460 | continue; | |
1461 | case CXt_EVAL: | |
1c98cc53 | 1462 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i)); |
a0d0e21e LW |
1463 | return i; |
1464 | } | |
1465 | } | |
1466 | return i; | |
1467 | } | |
1468 | ||
76e3520e | 1469 | STATIC I32 |
cea2e8a9 | 1470 | S_dopoptoloop(pTHX_ I32 startingblock) |
a0d0e21e | 1471 | { |
97aff369 | 1472 | dVAR; |
a0d0e21e | 1473 | I32 i; |
a0d0e21e | 1474 | for (i = startingblock; i >= 0; i--) { |
eb578fdb | 1475 | const PERL_CONTEXT * const cx = &cxstack[i]; |
6b35e009 | 1476 | switch (CxTYPE(cx)) { |
a0d0e21e | 1477 | case CXt_SUBST: |
a0d0e21e | 1478 | case CXt_SUB: |
7766f137 | 1479 | case CXt_FORMAT: |
a0d0e21e | 1480 | case CXt_EVAL: |
0a753a76 | 1481 | case CXt_NULL: |
dcbac5bb | 1482 | /* diag_listed_as: Exiting subroutine via %s */ |
a2a5de95 NC |
1483 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", |
1484 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); | |
515afda2 NC |
1485 | if ((CxTYPE(cx)) == CXt_NULL) |
1486 | return -1; | |
1487 | break; | |
c6fdafd0 | 1488 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1489 | case CXt_LOOP_LAZYSV: |
3b719c58 NC |
1490 | case CXt_LOOP_FOR: |
1491 | case CXt_LOOP_PLAIN: | |
1c98cc53 | 1492 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i)); |
a0d0e21e LW |
1493 | return i; |
1494 | } | |
1495 | } | |
1496 | return i; | |
1497 | } | |
1498 | ||
0d863452 RH |
1499 | STATIC I32 |
1500 | S_dopoptogiven(pTHX_ I32 startingblock) | |
1501 | { | |
97aff369 | 1502 | dVAR; |
0d863452 RH |
1503 | I32 i; |
1504 | for (i = startingblock; i >= 0; i--) { | |
eb578fdb | 1505 | const PERL_CONTEXT *cx = &cxstack[i]; |
0d863452 RH |
1506 | switch (CxTYPE(cx)) { |
1507 | default: | |
1508 | continue; | |
1509 | case CXt_GIVEN: | |
1c98cc53 | 1510 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i)); |
0d863452 | 1511 | return i; |
3b719c58 NC |
1512 | case CXt_LOOP_PLAIN: |
1513 | assert(!CxFOREACHDEF(cx)); | |
1514 | break; | |
c6fdafd0 | 1515 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1516 | case CXt_LOOP_LAZYSV: |
3b719c58 | 1517 | case CXt_LOOP_FOR: |
0d863452 | 1518 | if (CxFOREACHDEF(cx)) { |
1c98cc53 | 1519 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i)); |
0d863452 RH |
1520 | return i; |
1521 | } | |
1522 | } | |
1523 | } | |
1524 | return i; | |
1525 | } | |
1526 | ||
1527 | STATIC I32 | |
1528 | S_dopoptowhen(pTHX_ I32 startingblock) | |
1529 | { | |
97aff369 | 1530 | dVAR; |
0d863452 RH |
1531 | I32 i; |
1532 | for (i = startingblock; i >= 0; i--) { | |
eb578fdb | 1533 | const PERL_CONTEXT *cx = &cxstack[i]; |
0d863452 RH |
1534 | switch (CxTYPE(cx)) { |
1535 | default: | |
1536 | continue; | |
1537 | case CXt_WHEN: | |
1c98cc53 | 1538 | DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i)); |
0d863452 RH |
1539 | return i; |
1540 | } | |
1541 | } | |
1542 | return i; | |
1543 | } | |
1544 | ||
a0d0e21e | 1545 | void |
864dbfa3 | 1546 | Perl_dounwind(pTHX_ I32 cxix) |
a0d0e21e | 1547 | { |
97aff369 | 1548 | dVAR; |
a0d0e21e LW |
1549 | I32 optype; |
1550 | ||
f144f1e3 DM |
1551 | if (!PL_curstackinfo) /* can happen if die during thread cloning */ |
1552 | return; | |
1553 | ||
a0d0e21e | 1554 | while (cxstack_ix > cxix) { |
b0d9ce38 | 1555 | SV *sv; |
eb578fdb | 1556 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1c98cc53 | 1557 | DEBUG_CX("UNWIND"); \ |
a0d0e21e | 1558 | /* Note: we don't need to restore the base context info till the end. */ |
6b35e009 | 1559 | switch (CxTYPE(cx)) { |
c90c0ff4 PP |
1560 | case CXt_SUBST: |
1561 | POPSUBST(cx); | |
1562 | continue; /* not break */ | |
a0d0e21e | 1563 | case CXt_SUB: |
b0d9ce38 GS |
1564 | POPSUB(cx,sv); |
1565 | LEAVESUB(sv); | |
a0d0e21e LW |
1566 | break; |
1567 | case CXt_EVAL: | |
1568 | POPEVAL(cx); | |
1569 | break; | |
c6fdafd0 | 1570 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1571 | case CXt_LOOP_LAZYSV: |
3b719c58 NC |
1572 | case CXt_LOOP_FOR: |
1573 | case CXt_LOOP_PLAIN: | |
a0d0e21e LW |
1574 | POPLOOP(cx); |
1575 | break; | |
0a753a76 | 1576 | case CXt_NULL: |
a0d0e21e | 1577 | break; |
7766f137 GS |
1578 | case CXt_FORMAT: |
1579 | POPFORMAT(cx); | |
1580 | break; | |
a0d0e21e | 1581 | } |
c90c0ff4 | 1582 | cxstack_ix--; |
a0d0e21e | 1583 | } |
1b6737cc | 1584 | PERL_UNUSED_VAR(optype); |
a0d0e21e LW |
1585 | } |
1586 | ||
5a844595 GS |
1587 | void |
1588 | Perl_qerror(pTHX_ SV *err) | |
1589 | { | |
97aff369 | 1590 | dVAR; |
7918f24d NC |
1591 | |
1592 | PERL_ARGS_ASSERT_QERROR; | |
1593 | ||
6b2fb389 DM |
1594 | if (PL_in_eval) { |
1595 | if (PL_in_eval & EVAL_KEEPERR) { | |
ecad31f0 BF |
1596 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf, |
1597 | SVfARG(err)); | |
6b2fb389 DM |
1598 | } |
1599 | else | |
1600 | sv_catsv(ERRSV, err); | |
1601 | } | |
5a844595 GS |
1602 | else if (PL_errors) |
1603 | sv_catsv(PL_errors, err); | |
1604 | else | |
be2597df | 1605 | Perl_warn(aTHX_ "%"SVf, SVfARG(err)); |
13765c85 DM |
1606 | if (PL_parser) |
1607 | ++PL_parser->error_count; | |
5a844595 GS |
1608 | } |
1609 | ||
bb4c52e0 | 1610 | void |
c5df3096 | 1611 | Perl_die_unwind(pTHX_ SV *msv) |
a0d0e21e | 1612 | { |
27da23d5 | 1613 | dVAR; |
c5df3096 | 1614 | SV *exceptsv = sv_mortalcopy(msv); |
96d9b9cd | 1615 | U8 in_eval = PL_in_eval; |
c5df3096 | 1616 | PERL_ARGS_ASSERT_DIE_UNWIND; |
87582a92 | 1617 | |
96d9b9cd | 1618 | if (in_eval) { |
a0d0e21e | 1619 | I32 cxix; |
a0d0e21e | 1620 | I32 gimme; |
a0d0e21e | 1621 | |
22a30693 Z |
1622 | /* |
1623 | * Historically, perl used to set ERRSV ($@) early in the die | |
1624 | * process and rely on it not getting clobbered during unwinding. | |
1625 | * That sucked, because it was liable to get clobbered, so the | |
1626 | * setting of ERRSV used to emit the exception from eval{} has | |
1627 | * been moved to much later, after unwinding (see just before | |
1628 | * JMPENV_JUMP below). However, some modules were relying on the | |
1629 | * early setting, by examining $@ during unwinding to use it as | |
1630 | * a flag indicating whether the current unwinding was caused by | |
1631 | * an exception. It was never a reliable flag for that purpose, | |
1632 | * being totally open to false positives even without actual | |
1633 | * clobberage, but was useful enough for production code to | |
1634 | * semantically rely on it. | |
1635 | * | |
1636 | * We'd like to have a proper introspective interface that | |
1637 | * explicitly describes the reason for whatever unwinding | |
1638 | * operations are currently in progress, so that those modules | |
1639 | * work reliably and $@ isn't further overloaded. But we don't | |
1640 | * have one yet. In its absence, as a stopgap measure, ERRSV is | |
1641 | * now *additionally* set here, before unwinding, to serve as the | |
1642 | * (unreliable) flag that it used to. | |
1643 | * | |
1644 | * This behaviour is temporary, and should be removed when a | |
1645 | * proper way to detect exceptional unwinding has been developed. | |
1646 | * As of 2010-12, the authors of modules relying on the hack | |
1647 | * are aware of the issue, because the modules failed on | |
1648 | * perls 5.13.{1..7} which had late setting of $@ without this | |
1649 | * early-setting hack. | |
1650 | */ | |
1651 | if (!(in_eval & EVAL_KEEPERR)) { | |
1652 | SvTEMP_off(exceptsv); | |
1653 | sv_setsv(ERRSV, exceptsv); | |
1654 | } | |
1655 | ||
fc941f37 Z |
1656 | if (in_eval & EVAL_KEEPERR) { |
1657 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf, | |
1658 | SVfARG(exceptsv)); | |
1659 | } | |
1660 | ||
5a844595 GS |
1661 | while ((cxix = dopoptoeval(cxstack_ix)) < 0 |
1662 | && PL_curstackinfo->si_prev) | |
1663 | { | |
bac4b2ad | 1664 | dounwind(-1); |
d3acc0f7 | 1665 | POPSTACK; |
bac4b2ad | 1666 | } |
e336de0d | 1667 | |
a0d0e21e LW |
1668 | if (cxix >= 0) { |
1669 | I32 optype; | |
b6494f15 | 1670 | SV *namesv; |
eb578fdb | 1671 | PERL_CONTEXT *cx; |
901017d6 | 1672 | SV **newsp; |
8f89e5a9 Z |
1673 | COP *oldcop; |
1674 | JMPENV *restartjmpenv; | |
1675 | OP *restartop; | |
a0d0e21e LW |
1676 | |
1677 | if (cxix < cxstack_ix) | |
1678 | dounwind(cxix); | |
1679 | ||
3280af22 | 1680 | POPBLOCK(cx,PL_curpm); |
6b35e009 | 1681 | if (CxTYPE(cx) != CXt_EVAL) { |
7d0994e0 | 1682 | STRLEN msglen; |
96d9b9cd | 1683 | const char* message = SvPVx_const(exceptsv, msglen); |
10edeb5d | 1684 | PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11); |
bf49b057 | 1685 | PerlIO_write(Perl_error_log, message, msglen); |
a0d0e21e LW |
1686 | my_exit(1); |
1687 | } | |
1688 | POPEVAL(cx); | |
b6494f15 | 1689 | namesv = cx->blk_eval.old_namesv; |
8f89e5a9 Z |
1690 | oldcop = cx->blk_oldcop; |
1691 | restartjmpenv = cx->blk_eval.cur_top_env; | |
1692 | restartop = cx->blk_eval.retop; | |
a0d0e21e LW |
1693 | |
1694 | if (gimme == G_SCALAR) | |
3280af22 NIS |
1695 | *++newsp = &PL_sv_undef; |
1696 | PL_stack_sp = newsp; | |
a0d0e21e LW |
1697 | |
1698 | LEAVE; | |
748a9306 | 1699 | |
7fb6a879 GS |
1700 | /* LEAVE could clobber PL_curcop (see save_re_context()) |
1701 | * XXX it might be better to find a way to avoid messing with | |
1702 | * PL_curcop in save_re_context() instead, but this is a more | |
1703 | * minimal fix --GSAR */ | |
8f89e5a9 | 1704 | PL_curcop = oldcop; |
7fb6a879 | 1705 | |
7a2e2cd6 | 1706 | if (optype == OP_REQUIRE) { |
b6494f15 | 1707 | (void)hv_store(GvHVn(PL_incgv), |
ecad31f0 | 1708 | SvPVX_const(namesv), |
c60dbbc3 | 1709 | SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv), |
27bcc0a7 | 1710 | &PL_sv_undef, 0); |
27e90453 DM |
1711 | /* note that unlike pp_entereval, pp_require isn't |
1712 | * supposed to trap errors. So now that we've popped the | |
1713 | * EVAL that pp_require pushed, and processed the error | |
1714 | * message, rethrow the error */ | |
ecad31f0 BF |
1715 | Perl_croak(aTHX_ "%"SVf"Compilation failed in require", |
1716 | SVfARG(exceptsv ? exceptsv : newSVpvs_flags("Unknown error\n", | |
1717 | SVs_TEMP))); | |
7a2e2cd6 | 1718 | } |
fc941f37 | 1719 | if (!(in_eval & EVAL_KEEPERR)) |
96d9b9cd | 1720 | sv_setsv(ERRSV, exceptsv); |
8f89e5a9 Z |
1721 | PL_restartjmpenv = restartjmpenv; |
1722 | PL_restartop = restartop; | |
bb4c52e0 | 1723 | JMPENV_JUMP(3); |
118e2215 | 1724 | assert(0); /* NOTREACHED */ |
a0d0e21e LW |
1725 | } |
1726 | } | |
87582a92 | 1727 | |
96d9b9cd | 1728 | write_to_stderr(exceptsv); |
f86702cc | 1729 | my_failure_exit(); |
118e2215 | 1730 | assert(0); /* NOTREACHED */ |
a0d0e21e LW |
1731 | } |
1732 | ||
1733 | PP(pp_xor) | |
1734 | { | |
97aff369 | 1735 | dVAR; dSP; dPOPTOPssrl; |
a0d0e21e LW |
1736 | if (SvTRUE(left) != SvTRUE(right)) |
1737 | RETSETYES; | |
1738 | else | |
1739 | RETSETNO; | |
1740 | } | |
1741 | ||
8dff4fc5 BM |
1742 | /* |
1743 | =for apidoc caller_cx | |
1744 | ||
1745 | The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The | |
1746 | returned C<PERL_CONTEXT> structure can be interrogated to find all the | |
1747 | information returned to Perl by C<caller>. Note that XSUBs don't get a | |
1748 | stack frame, so C<caller_cx(0, NULL)> will return information for the | |
1749 | immediately-surrounding Perl code. | |
1750 | ||
1751 | This function skips over the automatic calls to C<&DB::sub> made on the | |
1752 | behalf of the debugger. If the stack frame requested was a sub called by | |
1753 | C<DB::sub>, the return value will be the frame for the call to | |
1754 | C<DB::sub>, since that has the correct line number/etc. for the call | |
1755 | site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the | |
1756 | frame for the sub call itself. | |
1757 | ||
1758 | =cut | |
1759 | */ | |
1760 | ||
1761 | const PERL_CONTEXT * | |
1762 | Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp) | |
a0d0e21e | 1763 | { |
eb578fdb KW |
1764 | I32 cxix = dopoptosub(cxstack_ix); |
1765 | const PERL_CONTEXT *cx; | |
1766 | const PERL_CONTEXT *ccstack = cxstack; | |
901017d6 | 1767 | const PERL_SI *top_si = PL_curstackinfo; |
27d41816 | 1768 | |
a0d0e21e | 1769 | for (;;) { |
2c375eb9 GS |
1770 | /* we may be in a higher stacklevel, so dig down deeper */ |
1771 | while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { | |
1772 | top_si = top_si->si_prev; | |
1773 | ccstack = top_si->si_cxstack; | |
1774 | cxix = dopoptosub_at(ccstack, top_si->si_cxix); | |
1775 | } | |
8dff4fc5 BM |
1776 | if (cxix < 0) |
1777 | return NULL; | |
f2a7f298 | 1778 | /* caller() should not report the automatic calls to &DB::sub */ |
1779 | if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 && | |
3280af22 | 1780 | ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub)) |
a0d0e21e LW |
1781 | count++; |
1782 | if (!count--) | |
1783 | break; | |
2c375eb9 | 1784 | cxix = dopoptosub_at(ccstack, cxix - 1); |
a0d0e21e | 1785 | } |
2c375eb9 GS |
1786 | |
1787 | cx = &ccstack[cxix]; | |
8dff4fc5 BM |
1788 | if (dbcxp) *dbcxp = cx; |
1789 | ||
7766f137 | 1790 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
f54cb97a | 1791 | const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1); |
2c375eb9 | 1792 | /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the |
06a5b730 | 1793 | field below is defined for any cx. */ |
f2a7f298 | 1794 | /* caller() should not report the automatic calls to &DB::sub */ |
1795 | if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) | |
2c375eb9 | 1796 | cx = &ccstack[dbcxix]; |
06a5b730 PP |
1797 | } |
1798 | ||
8dff4fc5 BM |
1799 | return cx; |
1800 | } | |
1801 | ||
1802 | PP(pp_caller) | |
1803 | { | |
1804 | dVAR; | |
1805 | dSP; | |
eb578fdb | 1806 | const PERL_CONTEXT *cx; |
8dff4fc5 BM |
1807 | const PERL_CONTEXT *dbcx; |
1808 | I32 gimme; | |
d527ce7c | 1809 | const HEK *stash_hek; |
8dff4fc5 | 1810 | I32 count = 0; |
ce0b554b | 1811 | bool has_arg = MAXARG && TOPs; |
8dff4fc5 | 1812 | |
ce0b554b FC |
1813 | if (MAXARG) { |
1814 | if (has_arg) | |
8dff4fc5 | 1815 | count = POPi; |
ce0b554b FC |
1816 | else (void)POPs; |
1817 | } | |
8dff4fc5 | 1818 | |
ce0b554b | 1819 | cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx); |
8dff4fc5 BM |
1820 | if (!cx) { |
1821 | if (GIMME != G_ARRAY) { | |
1822 | EXTEND(SP, 1); | |
1823 | RETPUSHUNDEF; | |
1824 | } | |
1825 | RETURN; | |
1826 | } | |
1827 | ||
fb55feef | 1828 | DEBUG_CX("CALLER"); |
d0279c7c | 1829 | assert(CopSTASH(cx->blk_oldcop)); |
e7886211 FC |
1830 | stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV |
1831 | ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop)) | |
1832 | : NULL; | |
a0d0e21e | 1833 | if (GIMME != G_ARRAY) { |
27d41816 | 1834 | EXTEND(SP, 1); |
d527ce7c | 1835 | if (!stash_hek) |
3280af22 | 1836 | PUSHs(&PL_sv_undef); |
49d8d3a1 MB |
1837 | else { |
1838 | dTARGET; | |
d527ce7c | 1839 | sv_sethek(TARG, stash_hek); |
49d8d3a1 MB |
1840 | PUSHs(TARG); |
1841 | } | |
a0d0e21e LW |
1842 | RETURN; |
1843 | } | |
a0d0e21e | 1844 | |
b3ca2e83 | 1845 | EXTEND(SP, 11); |
27d41816 | 1846 | |
d527ce7c | 1847 | if (!stash_hek) |
3280af22 | 1848 | PUSHs(&PL_sv_undef); |
d527ce7c BF |
1849 | else { |
1850 | dTARGET; | |
1851 | sv_sethek(TARG, stash_hek); | |
1852 | PUSHTARG; | |
1853 | } | |
6e449a3a MHM |
1854 | mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0)); |
1855 | mPUSHi((I32)CopLINE(cx->blk_oldcop)); | |
ce0b554b | 1856 | if (!has_arg) |
a0d0e21e | 1857 | RETURN; |
7766f137 | 1858 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
8dff4fc5 | 1859 | GV * const cvgv = CvGV(dbcx->blk_sub.cv); |
7766f137 | 1860 | /* So is ccstack[dbcxix]. */ |
81ed78b2 | 1861 | if (cvgv && isGV(cvgv)) { |
561b68a9 | 1862 | SV * const sv = newSV(0); |
c445ea15 | 1863 | gv_efullname3(sv, cvgv, NULL); |
6e449a3a | 1864 | mPUSHs(sv); |
bf38a478 | 1865 | PUSHs(boolSV(CxHASARGS(cx))); |
07b8c804 RGS |
1866 | } |
1867 | else { | |
84bafc02 | 1868 | PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP)); |
bf38a478 | 1869 | PUSHs(boolSV(CxHASARGS(cx))); |
07b8c804 | 1870 | } |
a0d0e21e LW |
1871 | } |
1872 | else { | |
84bafc02 | 1873 | PUSHs(newSVpvs_flags("(eval)", SVs_TEMP)); |
6e449a3a | 1874 | mPUSHi(0); |
a0d0e21e | 1875 | } |
54310121 PP |
1876 | gimme = (I32)cx->blk_gimme; |
1877 | if (gimme == G_VOID) | |
3280af22 | 1878 | PUSHs(&PL_sv_undef); |
54310121 | 1879 | else |
98625aca | 1880 | PUSHs(boolSV((gimme & G_WANT) == G_ARRAY)); |
6b35e009 | 1881 | if (CxTYPE(cx) == CXt_EVAL) { |
811a4de9 | 1882 | /* eval STRING */ |
85a64632 | 1883 | if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) { |
19bcb54e FC |
1884 | PUSHs(newSVpvn_flags(SvPVX(cx->blk_eval.cur_text), |
1885 | SvCUR(cx->blk_eval.cur_text)-2, | |
1886 | SvUTF8(cx->blk_eval.cur_text)|SVs_TEMP)); | |
3280af22 | 1887 | PUSHs(&PL_sv_no); |
0f79a09d | 1888 | } |
811a4de9 | 1889 | /* require */ |
0f79a09d | 1890 | else if (cx->blk_eval.old_namesv) { |
6e449a3a | 1891 | mPUSHs(newSVsv(cx->blk_eval.old_namesv)); |
3280af22 | 1892 | PUSHs(&PL_sv_yes); |
06a5b730 | 1893 | } |
811a4de9 GS |
1894 | /* eval BLOCK (try blocks have old_namesv == 0) */ |
1895 | else { | |
1896 | PUSHs(&PL_sv_undef); | |
1897 | PUSHs(&PL_sv_undef); | |
1898 | } | |
4633a7c4 | 1899 | } |
a682de96 GS |
1900 | else { |
1901 | PUSHs(&PL_sv_undef); | |
1902 | PUSHs(&PL_sv_undef); | |
1903 | } | |
bafb2adc | 1904 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx) |
ed094faf | 1905 | && CopSTASH_eq(PL_curcop, PL_debstash)) |
4633a7c4 | 1906 | { |
66a1b24b AL |
1907 | AV * const ary = cx->blk_sub.argarray; |
1908 | const int off = AvARRAY(ary) - AvALLOC(ary); | |
a0d0e21e | 1909 | |
e1a80902 | 1910 | Perl_init_dbargs(aTHX); |
a0d0e21e | 1911 | |
3280af22 NIS |
1912 | if (AvMAX(PL_dbargs) < AvFILLp(ary) + off) |
1913 | av_extend(PL_dbargs, AvFILLp(ary) + off); | |
1914 | Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*); | |
1915 | AvFILLp(PL_dbargs) = AvFILLp(ary) + off; | |
a0d0e21e | 1916 | } |
6e449a3a | 1917 | mPUSHi(CopHINTS_get(cx->blk_oldcop)); |
e476b1b5 GS |
1918 | { |
1919 | SV * mask ; | |
72dc9ed5 | 1920 | STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ; |
114bafba | 1921 | |
f07626ad | 1922 | if (old_warnings == pWARN_NONE) |
e476b1b5 | 1923 | mask = newSVpvn(WARN_NONEstring, WARNsize) ; |
f07626ad FC |
1924 | else if (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0) |
1925 | mask = &PL_sv_undef ; | |
ac27b0f5 | 1926 | else if (old_warnings == pWARN_ALL || |
75b6c4ca RGS |
1927 | (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) { |
1928 | /* Get the bit mask for $warnings::Bits{all}, because | |
1929 | * it could have been extended by warnings::register */ | |
1930 | SV **bits_all; | |
6673a63c | 1931 | HV * const bits = get_hv("warnings::Bits", 0); |
017a3ce5 | 1932 | if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) { |
75b6c4ca RGS |
1933 | mask = newSVsv(*bits_all); |
1934 | } | |
1935 | else { | |
1936 | mask = newSVpvn(WARN_ALLstring, WARNsize) ; | |
1937 | } | |
1938 | } | |
e476b1b5 | 1939 | else |
72dc9ed5 | 1940 | mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]); |
6e449a3a | 1941 | mPUSHs(mask); |
e476b1b5 | 1942 | } |
b3ca2e83 | 1943 | |
c28fe1ec | 1944 | PUSHs(cx->blk_oldcop->cop_hints_hash ? |
20439bc7 | 1945 | sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0)))) |
b3ca2e83 | 1946 | : &PL_sv_undef); |
a0d0e21e LW |
1947 | RETURN; |
1948 | } | |
1949 | ||
a0d0e21e LW |
1950 | PP(pp_reset) |
1951 | { | |
97aff369 | 1952 | dVAR; |
39644a26 | 1953 | dSP; |
ca826051 FC |
1954 | const char * tmps; |
1955 | STRLEN len = 0; | |
1956 | if (MAXARG < 1 || (!TOPs && !POPs)) | |
1957 | tmps = NULL, len = 0; | |
1958 | else | |
1959 | tmps = SvPVx_const(POPs, len); | |
1960 | sv_resetpvn(tmps, len, CopSTASH(PL_curcop)); | |
3280af22 | 1961 | PUSHs(&PL_sv_yes); |
a0d0e21e LW |
1962 | RETURN; |
1963 | } | |
1964 | ||
dd2155a4 DM |
1965 | /* like pp_nextstate, but used instead when the debugger is active */ |
1966 | ||
a0d0e21e LW |
1967 | PP(pp_dbstate) |
1968 | { | |
27da23d5 | 1969 | dVAR; |
533c011a | 1970 | PL_curcop = (COP*)PL_op; |
a0d0e21e | 1971 | TAINT_NOT; /* Each statement is presumed innocent */ |
3280af22 | 1972 | PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp; |
a0d0e21e LW |
1973 | FREETMPS; |
1974 | ||
f410a211 NC |
1975 | PERL_ASYNC_CHECK(); |
1976 | ||
5df8de69 DM |
1977 | if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */ |
1978 | || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace)) | |
a0d0e21e | 1979 | { |
39644a26 | 1980 | dSP; |
eb578fdb | 1981 | PERL_CONTEXT *cx; |
f54cb97a | 1982 | const I32 gimme = G_ARRAY; |
eb160463 | 1983 | U8 hasargs; |
0bd48802 | 1984 | GV * const gv = PL_DBgv; |
432d4561 JL |
1985 | CV * cv = NULL; |
1986 | ||
1987 | if (gv && isGV_with_GP(gv)) | |
1988 | cv = GvCV(gv); | |
a0d0e21e | 1989 | |
c2cb6f77 | 1990 | if (!cv || (!CvROOT(cv) && !CvXSUB(cv))) |
cea2e8a9 | 1991 | DIE(aTHX_ "No DB::DB routine defined"); |
a0d0e21e | 1992 | |
aea4f609 DM |
1993 | if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG)) |
1994 | /* don't do recursive DB::DB call */ | |
a0d0e21e | 1995 | return NORMAL; |
748a9306 | 1996 | |
a57c6685 | 1997 | ENTER; |
4633a7c4 LW |
1998 | SAVETMPS; |
1999 | ||
3280af22 | 2000 | SAVEI32(PL_debug); |
55497cff | 2001 | SAVESTACK_POS(); |
3280af22 | 2002 | PL_debug = 0; |
748a9306 | 2003 | hasargs = 0; |
924508f0 | 2004 | SPAGAIN; |
748a9306 | 2005 | |
aed2304a | 2006 | if (CvISXSUB(cv)) { |
c127bd3a SF |
2007 | PUSHMARK(SP); |
2008 | (void)(*CvXSUB(cv))(aTHX_ cv); | |
c127bd3a | 2009 | FREETMPS; |
a57c6685 | 2010 | LEAVE; |
c127bd3a SF |
2011 | return NORMAL; |
2012 | } | |
2013 | else { | |
2014 | PUSHBLOCK(cx, CXt_SUB, SP); | |
2015 | PUSHSUB_DB(cx); | |
2016 | cx->blk_sub.retop = PL_op->op_next; | |
2017 | CvDEPTH(cv)++; | |
9d976ff5 FC |
2018 | if (CvDEPTH(cv) >= 2) { |
2019 | PERL_STACK_OVERFLOW_CHECK(); | |
2020 | pad_push(CvPADLIST(cv), CvDEPTH(cv)); | |
2021 | } | |
c127bd3a | 2022 | SAVECOMPPAD(); |
9d976ff5 | 2023 | PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv)); |
c127bd3a SF |
2024 | RETURNOP(CvSTART(cv)); |
2025 | } | |
a0d0e21e LW |
2026 | } |
2027 | else | |
2028 | return NORMAL; | |
2029 | } | |
2030 | ||
b9d76716 VP |
2031 | STATIC SV ** |
2032 | S_adjust_stack_on_leave(pTHX_ SV **newsp, SV **sp, SV **mark, I32 gimme, U32 flags) | |
2033 | { | |
9a214eec | 2034 | bool padtmp = 0; |
b9d76716 VP |
2035 | PERL_ARGS_ASSERT_ADJUST_STACK_ON_LEAVE; |
2036 | ||
9a214eec DM |
2037 | if (flags & SVs_PADTMP) { |
2038 | flags &= ~SVs_PADTMP; | |
2039 | padtmp = 1; | |
2040 | } | |
b9d76716 VP |
2041 | if (gimme == G_SCALAR) { |
2042 | if (MARK < SP) | |
9a214eec DM |
2043 | *++newsp = ((SvFLAGS(*SP) & flags) || (padtmp && SvPADTMP(*SP))) |
2044 | ? *SP : sv_mortalcopy(*SP); | |
b9d76716 VP |
2045 | else { |
2046 | /* MEXTEND() only updates MARK, so reuse it instead of newsp. */ | |
2047 | MARK = newsp; | |
2048 | MEXTEND(MARK, 1); | |
2049 | *++MARK = &PL_sv_undef; | |
2050 | return MARK; | |
2051 | } | |
2052 | } | |
2053 | else if (gimme == G_ARRAY) { | |
2054 | /* in case LEAVE wipes old return values */ | |
2055 | while (++MARK <= SP) { | |
9a214eec | 2056 | if ((SvFLAGS(*MARK) & flags) || (padtmp && SvPADTMP(*MARK))) |
b9d76716 VP |
2057 | *++newsp = *MARK; |
2058 | else { | |
2059 | *++newsp = sv_mortalcopy(*MARK); | |
2060 | TAINT_NOT; /* Each item is independent */ | |
2061 | } | |
2062 | } | |
2063 | /* When this function was called with MARK == newsp, we reach this | |
2064 | * point with SP == newsp. */ | |
2065 | } | |
2066 | ||
2067 | return newsp; | |
2068 | } | |
2069 | ||
2b9a6457 VP |
2070 | PP(pp_enter) |
2071 | { | |
2072 | dVAR; dSP; | |
eb578fdb | 2073 | PERL_CONTEXT *cx; |
7c2d9d03 | 2074 | I32 gimme = GIMME_V; |
2b9a6457 VP |
2075 | |
2076 | ENTER_with_name("block"); | |
2077 | ||
2078 | SAVETMPS; | |
2079 | PUSHBLOCK(cx, CXt_BLOCK, SP); | |
2080 | ||
2081 | RETURN; | |
2082 | } | |
2083 | ||
2084 | PP(pp_leave) | |
2085 | { | |
2086 | dVAR; dSP; | |
eb578fdb | 2087 | PERL_CONTEXT *cx; |
2b9a6457 VP |
2088 | SV **newsp; |
2089 | PMOP *newpm; | |
2090 | I32 gimme; | |
2091 | ||
2092 | if (PL_op->op_flags & OPf_SPECIAL) { | |
2093 | cx = &cxstack[cxstack_ix]; | |
2094 | cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */ | |
2095 | } | |
2096 | ||
2097 | POPBLOCK(cx,newpm); | |
2098 | ||
2099 | gimme = OP_GIMME(PL_op, (cxstack_ix >= 0) ? gimme : G_SCALAR); | |
2100 | ||
2101 | TAINT_NOT; | |
f02ea43c | 2102 | SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP); |
2b9a6457 VP |
2103 | PL_curpm = newpm; /* Don't pop $1 et al till now */ |
2104 | ||
2105 | LEAVE_with_name("block"); | |
2106 | ||
2107 | RETURN; | |
2108 | } | |
2109 | ||
a0d0e21e LW |
2110 | PP(pp_enteriter) |
2111 | { | |
27da23d5 | 2112 | dVAR; dSP; dMARK; |
eb578fdb | 2113 | PERL_CONTEXT *cx; |
f54cb97a | 2114 | const I32 gimme = GIMME_V; |
df530c37 | 2115 | void *itervar; /* location of the iteration variable */ |
840fe433 | 2116 | U8 cxtype = CXt_LOOP_FOR; |
a0d0e21e | 2117 | |
d343c3ef | 2118 | ENTER_with_name("loop1"); |
4633a7c4 LW |
2119 | SAVETMPS; |
2120 | ||
aafca525 DM |
2121 | if (PL_op->op_targ) { /* "my" variable */ |
2122 | if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */ | |
14f338dc DM |
2123 | SvPADSTALE_off(PAD_SVl(PL_op->op_targ)); |
2124 | SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ), | |
2125 | SVs_PADSTALE, SVs_PADSTALE); | |
2126 | } | |
09edbca0 | 2127 | SAVEPADSVANDMORTALIZE(PL_op->op_targ); |
89e00a7c | 2128 | #ifdef USE_ITHREADS |
df530c37 | 2129 | itervar = PL_comppad; |
89e00a7c | 2130 | #else |
aafca525 | 2131 | itervar = &PAD_SVl(PL_op->op_targ); |
7766f137 | 2132 | #endif |
54b9620d | 2133 | } |
aafca525 | 2134 | else { /* symbol table variable */ |
159b6efe | 2135 | GV * const gv = MUTABLE_GV(POPs); |
f83b46a0 DM |
2136 | SV** svp = &GvSV(gv); |
2137 | save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV); | |
561b68a9 | 2138 | *svp = newSV(0); |
df530c37 | 2139 | itervar = (void *)gv; |
54b9620d | 2140 | } |
4633a7c4 | 2141 | |
0d863452 RH |
2142 | if (PL_op->op_private & OPpITER_DEF) |
2143 | cxtype |= CXp_FOR_DEF; | |
2144 | ||
d343c3ef | 2145 | ENTER_with_name("loop2"); |
a0d0e21e | 2146 | |
7766f137 | 2147 | PUSHBLOCK(cx, cxtype, SP); |
df530c37 | 2148 | PUSHLOOP_FOR(cx, itervar, MARK); |
533c011a | 2149 | if (PL_op->op_flags & OPf_STACKED) { |
d01136d6 BS |
2150 | SV *maybe_ary = POPs; |
2151 | if (SvTYPE(maybe_ary) != SVt_PVAV) { | |
89ea2908 | 2152 | dPOPss; |
d01136d6 | 2153 | SV * const right = maybe_ary; |
984a4bea RD |
2154 | SvGETMAGIC(sv); |
2155 | SvGETMAGIC(right); | |
4fe3f0fa | 2156 | if (RANGE_IS_NUMERIC(sv,right)) { |
d01136d6 | 2157 | cx->cx_type &= ~CXTYPEMASK; |
c6fdafd0 NC |
2158 | cx->cx_type |= CXt_LOOP_LAZYIV; |
2159 | /* Make sure that no-one re-orders cop.h and breaks our | |
2160 | assumptions */ | |
2161 | assert(CxTYPE(cx) == CXt_LOOP_LAZYIV); | |
a2309040 | 2162 | #ifdef NV_PRESERVES_UV |
f52e41ad FC |
2163 | if ((SvOK(sv) && ((SvNV_nomg(sv) < (NV)IV_MIN) || |
2164 | (SvNV_nomg(sv) > (NV)IV_MAX))) | |
a2309040 | 2165 | || |
f52e41ad FC |
2166 | (SvOK(right) && ((SvNV_nomg(right) > (NV)IV_MAX) || |
2167 | (SvNV_nomg(right) < (NV)IV_MIN)))) | |
a2309040 | 2168 | #else |
f52e41ad | 2169 | if ((SvOK(sv) && ((SvNV_nomg(sv) <= (NV)IV_MIN) |
a2309040 | 2170 | || |
f52e41ad FC |
2171 | ((SvNV_nomg(sv) > 0) && |
2172 | ((SvUV_nomg(sv) > (UV)IV_MAX) || | |
2173 | (SvNV_nomg(sv) > (NV)UV_MAX))))) | |
a2309040 | 2174 | || |
f52e41ad | 2175 | (SvOK(right) && ((SvNV_nomg(right) <= (NV)IV_MIN) |
a2309040 | 2176 | || |
f52e41ad FC |
2177 | ((SvNV_nomg(right) > 0) && |
2178 | ((SvUV_nomg(right) > (UV)IV_MAX) || | |
2179 | (SvNV_nomg(right) > (NV)UV_MAX)) | |
2180 | )))) | |
a2309040 | 2181 | #endif |
076d9a11 | 2182 | DIE(aTHX_ "Range iterator outside integer range"); |
f52e41ad FC |
2183 | cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv); |
2184 | cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right); | |
d4665a05 DM |
2185 | #ifdef DEBUGGING |
2186 | /* for correct -Dstv display */ | |
2187 | cx->blk_oldsp = sp - PL_stack_base; | |
2188 | #endif | |
89ea2908 | 2189 | } |
3f63a782 | 2190 | else { |
d01136d6 BS |
2191 | cx->cx_type &= ~CXTYPEMASK; |
2192 | cx->cx_type |= CXt_LOOP_LAZYSV; | |
2193 | /* Make sure that no-one re-orders cop.h and breaks our | |
2194 | assumptions */ | |
2195 | assert(CxTYPE(cx) == CXt_LOOP_LAZYSV); | |
2196 | cx->blk_loop.state_u.lazysv.cur = newSVsv(sv); | |
2197 | cx->blk_loop.state_u.lazysv.end = right; | |
2198 | SvREFCNT_inc(right); | |
2199 | (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur); | |
267cc4a8 NC |
2200 | /* This will do the upgrade to SVt_PV, and warn if the value |
2201 | is uninitialised. */ | |
10516c54 | 2202 | (void) SvPV_nolen_const(right); |
267cc4a8 NC |
2203 | /* Doing this avoids a check every time in pp_iter in pp_hot.c |
2204 | to replace !SvOK() with a pointer to "". */ | |
2205 | if (!SvOK(right)) { | |
2206 | SvREFCNT_dec(right); | |
d01136d6 | 2207 | cx->blk_loop.state_u.lazysv.end = &PL_sv_no; |
267cc4a8 | 2208 | } |
3f63a782 | 2209 | } |
89ea2908 | 2210 | } |
d01136d6 | 2211 | else /* SvTYPE(maybe_ary) == SVt_PVAV */ { |
502c6561 | 2212 | cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary); |
d01136d6 BS |
2213 | SvREFCNT_inc(maybe_ary); |
2214 | cx->blk_loop.state_u.ary.ix = | |
2215 | (PL_op->op_private & OPpITER_REVERSED) ? | |
2216 | AvFILL(cx->blk_loop.state_u.ary.ary) + 1 : | |
2217 | -1; | |
ef3e5ea9 | 2218 | } |
89ea2908 | 2219 | } |
d01136d6 BS |
2220 | else { /* iterating over items on the stack */ |
2221 | cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */ | |
ef3e5ea9 | 2222 | if (PL_op->op_private & OPpITER_REVERSED) { |
d01136d6 | 2223 | cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1; |
ef3e5ea9 NC |
2224 | } |
2225 | else { | |
d01136d6 | 2226 | cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base; |
ef3e5ea9 | 2227 | } |
4633a7c4 | 2228 | } |
a0d0e21e LW |
2229 | |
2230 | RETURN; | |
2231 | } | |
2232 | ||
2233 | PP(pp_enterloop) | |
2234 | { | |
27da23d5 | 2235 | dVAR; dSP; |
eb578fdb | 2236 | PERL_CONTEXT *cx; |
f54cb97a | 2237 | const I32 gimme = GIMME_V; |
a0d0e21e | 2238 | |
d343c3ef | 2239 | ENTER_with_name("loop1"); |
a0d0e21e | 2240 | SAVETMPS; |
d343c3ef | 2241 | ENTER_with_name("loop2"); |
a0d0e21e | 2242 | |
3b719c58 NC |
2243 | PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP); |
2244 | PUSHLOOP_PLAIN(cx, SP); | |
a0d0e21e LW |
2245 | |
2246 | RETURN; | |
2247 | } | |
2248 | ||
2249 | PP(pp_leaveloop) | |
2250 | { | |
27da23d5 | 2251 | dVAR; dSP; |
eb578fdb | 2252 | PERL_CONTEXT *cx; |
a0d0e21e LW |
2253 | I32 gimme; |
2254 | SV **newsp; | |
2255 | PMOP *newpm; | |
2256 | SV **mark; | |
2257 | ||
2258 | POPBLOCK(cx,newpm); | |
3b719c58 | 2259 | assert(CxTYPE_is_LOOP(cx)); |
4fdae800 | 2260 | mark = newsp; |
a8bba7fa | 2261 | newsp = PL_stack_base + cx->blk_loop.resetsp; |
f86702cc | 2262 | |
a1f49e72 | 2263 | TAINT_NOT; |
b9d76716 | 2264 | SP = adjust_stack_on_leave(newsp, SP, MARK, gimme, 0); |
f86702cc PP |
2265 | PUTBACK; |
2266 | ||
a8bba7fa | 2267 | POPLOOP(cx); /* Stack values are safe: release loop vars ... */ |
3280af22 | 2268 | PL_curpm = newpm; /* ... and pop $1 et al */ |
f86702cc | 2269 | |
d343c3ef GG |
2270 | LEAVE_with_name("loop2"); |
2271 | LEAVE_with_name("loop1"); | |
a0d0e21e | 2272 | |
f86702cc | 2273 | return NORMAL; |
a0d0e21e LW |
2274 | } |
2275 | ||
3bdf583b FC |
2276 | STATIC void |
2277 | S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme, | |
d25b0d7b | 2278 | PERL_CONTEXT *cx, PMOP *newpm) |
3bdf583b | 2279 | { |
80422e24 | 2280 | const bool ref = !!(CxLVAL(cx) & OPpENTERSUB_INARGS); |
3bdf583b | 2281 | if (gimme == G_SCALAR) { |
d25b0d7b FC |
2282 | if (CxLVAL(cx) && !ref) { /* Leave it as it is if we can. */ |
2283 | SV *sv; | |
001de122 | 2284 | const char *what = NULL; |
d25b0d7b FC |
2285 | if (MARK < SP) { |
2286 | assert(MARK+1 == SP); | |
2287 | if ((SvPADTMP(TOPs) || | |
2288 | (SvFLAGS(TOPs) & (SVf_READONLY | SVf_FAKE)) | |
2289 | == SVf_READONLY | |
2290 | ) && | |
2291 | !SvSMAGICAL(TOPs)) { | |
001de122 | 2292 | what = |
d25b0d7b | 2293 | SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef" |
001de122 | 2294 | : "a readonly value" : "a temporary"; |
d25b0d7b | 2295 | } |
001de122 | 2296 | else goto copy_sv; |
d25b0d7b FC |
2297 | } |
2298 | else { | |
2299 | /* sub:lvalue{} will take us here. */ | |
001de122 | 2300 | what = "undef"; |
d25b0d7b | 2301 | } |
001de122 FC |
2302 | LEAVE; |
2303 | cxstack_ix--; | |
2304 | POPSUB(cx,sv); | |
2305 | PL_curpm = newpm; | |
2306 | LEAVESUB(sv); | |
2307 | Perl_croak(aTHX_ | |
2308 | "Can't return %s from lvalue subroutine", what | |
2309 | ); | |
d25b0d7b | 2310 | } |
93905212 | 2311 | if (MARK < SP) { |
a5ad7a5a | 2312 | copy_sv: |
3bdf583b | 2313 | if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) { |
5811c07e | 2314 | if (!SvPADTMP(*SP)) { |
3bdf583b FC |
2315 | *++newsp = SvREFCNT_inc(*SP); |
2316 | FREETMPS; | |
2317 | sv_2mortal(*newsp); | |
5811c07e FC |
2318 | } |
2319 | else { | |
2320 | /* FREETMPS could clobber it */ | |
2321 | SV *sv = SvREFCNT_inc(*SP); | |
2322 | FREETMPS; | |
2323 | *++newsp = sv_mortalcopy(sv); | |
2324 | SvREFCNT_dec(sv); | |
2325 | } | |
3bdf583b FC |
2326 | } |
2327 | else | |
e08be60b | 2328 | *++newsp = |
5811c07e FC |
2329 | SvPADTMP(*SP) |
2330 | ? sv_mortalcopy(*SP) | |
2331 | : !SvTEMP(*SP) | |
e08be60b FC |
2332 | ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP)) |
2333 | : *SP; | |
3bdf583b | 2334 | } |
0d235c77 FC |
2335 | else { |
2336 | EXTEND(newsp,1); | |
3bdf583b | 2337 | *++newsp = &PL_sv_undef; |
0d235c77 | 2338 | } |
0e9700df | 2339 | if (CxLVAL(cx) & OPpDEREF) { |
767eda44 FC |
2340 | SvGETMAGIC(TOPs); |
2341 | if (!SvOK(TOPs)) { | |
0e9700df | 2342 | TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF); |
767eda44 FC |
2343 | } |
2344 | } | |
3bdf583b FC |
2345 | } |
2346 | else if (gimme == G_ARRAY) { | |
0e9700df | 2347 | assert (!(CxLVAL(cx) & OPpDEREF)); |
80422e24 | 2348 | if (ref || !CxLVAL(cx)) |
e08be60b FC |
2349 | while (++MARK <= SP) |
2350 | *++newsp = | |
5811c07e | 2351 | SvFLAGS(*MARK) & SVs_PADTMP |
80422e24 | 2352 | ? sv_mortalcopy(*MARK) |
5811c07e FC |
2353 | : SvTEMP(*MARK) |
2354 | ? *MARK | |
80422e24 | 2355 | : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK)); |
e08be60b | 2356 | else while (++MARK <= SP) { |
d25b0d7b FC |
2357 | if (*MARK != &PL_sv_undef |
2358 | && (SvPADTMP(*MARK) | |
2359 | || (SvFLAGS(*MARK) & (SVf_READONLY|SVf_FAKE)) | |
2360 | == SVf_READONLY | |
2361 | ) | |
2362 | ) { | |
2363 | SV *sv; | |
2364 | /* Might be flattened array after $#array = */ | |
2365 | PUTBACK; | |
2366 | LEAVE; | |
2367 | cxstack_ix--; | |
2368 | POPSUB(cx,sv); | |
2369 | PL_curpm = newpm; | |
2370 | LEAVESUB(sv); | |
ae917476 | 2371 | /* diag_listed_as: Can't return %s from lvalue subroutine */ |
d25b0d7b FC |
2372 | Perl_croak(aTHX_ |
2373 | "Can't return a %s from lvalue subroutine", | |
2374 | SvREADONLY(TOPs) ? "readonly value" : "temporary"); | |
2375 | } | |
2376 | else | |
4bee03f8 FC |
2377 | *++newsp = |
2378 | SvTEMP(*MARK) | |
2379 | ? *MARK | |
2380 | : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK)); | |
3bdf583b FC |
2381 | } |
2382 | } | |
2383 | PL_stack_sp = newsp; | |
2384 | } | |
2385 | ||
a0d0e21e LW |
2386 | PP(pp_return) |
2387 | { | |
27da23d5 | 2388 | dVAR; dSP; dMARK; |
eb578fdb | 2389 | PERL_CONTEXT *cx; |
f86702cc | 2390 | bool popsub2 = FALSE; |
b45de488 | 2391 | bool clear_errsv = FALSE; |
fa1e92c4 | 2392 | bool lval = FALSE; |
a0d0e21e LW |
2393 | I32 gimme; |
2394 | SV **newsp; | |
2395 | PMOP *newpm; | |
2396 | I32 optype = 0; | |
b6494f15 | 2397 | SV *namesv; |
b0d9ce38 | 2398 | SV *sv; |
b263a1ad | 2399 | OP *retop = NULL; |
a0d0e21e | 2400 | |
0bd48802 AL |
2401 | const I32 cxix = dopoptosub(cxstack_ix); |
2402 | ||
9850bf21 RH |
2403 | if (cxix < 0) { |
2404 | if (CxMULTICALL(cxstack)) { /* In this case we must be in a | |
2405 | * sort block, which is a CXt_NULL | |
2406 | * not a CXt_SUB */ | |
2407 | dounwind(0); | |
d7507f74 RH |
2408 | PL_stack_base[1] = *PL_stack_sp; |
2409 | PL_stack_sp = PL_stack_base + 1; | |
a0d0e21e LW |
2410 | return 0; |
2411 | } | |
9850bf21 RH |
2412 | else |
2413 | DIE(aTHX_ "Can't return outside a subroutine"); | |
a0d0e21e | 2414 | } |
a0d0e21e LW |
2415 | if (cxix < cxstack_ix) |
2416 | dounwind(cxix); | |
2417 | ||
d7507f74 RH |
2418 | if (CxMULTICALL(&cxstack[cxix])) { |
2419 | gimme = cxstack[cxix].blk_gimme; | |
2420 | if (gimme == G_VOID) | |
2421 | PL_stack_sp = PL_stack_base; | |
2422 | else if (gimme == G_SCALAR) { | |
2423 | PL_stack_base[1] = *PL_stack_sp; | |
2424 | PL_stack_sp = PL_stack_base + 1; | |
2425 | } | |
9850bf21 | 2426 | return 0; |
d7507f74 | 2427 | } |
9850bf21 | 2428 | |
a0d0e21e | 2429 | POPBLOCK(cx,newpm); |
6b35e009 | 2430 | switch (CxTYPE(cx)) { |
a0d0e21e | 2431 | case CXt_SUB: |
f86702cc | 2432 | popsub2 = TRUE; |
fa1e92c4 | 2433 | lval = !!CvLVALUE(cx->blk_sub.cv); |
f39bc417 | 2434 | retop = cx->blk_sub.retop; |
5dd42e15 | 2435 | cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */ |
a0d0e21e LW |
2436 | break; |
2437 | case CXt_EVAL: | |
b45de488 GS |
2438 | if (!(PL_in_eval & EVAL_KEEPERR)) |
2439 | clear_errsv = TRUE; | |
a0d0e21e | 2440 | POPEVAL(cx); |
b6494f15 | 2441 | namesv = cx->blk_eval.old_namesv; |
f39bc417 | 2442 | retop = cx->blk_eval.retop; |
1d76a5c3 GS |
2443 | if (CxTRYBLOCK(cx)) |
2444 | break; | |
748a9306 LW |
2445 | if (optype == OP_REQUIRE && |
2446 | (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) ) | |
2447 | { | |
54310121 | 2448 | /* Unassume the success we assumed earlier. */ |
b6494f15 | 2449 | (void)hv_delete(GvHVn(PL_incgv), |
ecad31f0 | 2450 | SvPVX_const(namesv), |
c60dbbc3 | 2451 | SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv), |
b6494f15 VP |
2452 | G_DISCARD); |
2453 | DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv)); | |
748a9306 | 2454 | } |
a0d0e21e | 2455 | break; |
7766f137 GS |
2456 | case CXt_FORMAT: |
2457 | POPFORMAT(cx); | |
f39bc417 | 2458 | retop = cx->blk_sub.retop; |
7766f137 | 2459 | break; |
a0d0e21e | 2460 | default: |
5637ef5b | 2461 | DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx)); |
a0d0e21e LW |
2462 | } |
2463 | ||
a1f49e72 | 2464 | TAINT_NOT; |
d25b0d7b | 2465 | if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx, newpm); |
3bdf583b FC |
2466 | else { |
2467 | if (gimme == G_SCALAR) { | |
a29cdaf0 IZ |
2468 | if (MARK < SP) { |
2469 | if (popsub2) { | |
a8bba7fa | 2470 | if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) { |
6f48390a FC |
2471 | if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1 |
2472 | && !SvMAGICAL(TOPs)) { | |
a29cdaf0 IZ |
2473 | *++newsp = SvREFCNT_inc(*SP); |
2474 | FREETMPS; | |
2475 | sv_2mortal(*newsp); | |
959e3673 GS |
2476 | } |
2477 | else { | |
2478 | sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */ | |
a29cdaf0 | 2479 | FREETMPS; |
959e3673 GS |
2480 | *++newsp = sv_mortalcopy(sv); |
2481 | SvREFCNT_dec(sv); | |
a29cdaf0 | 2482 | } |
959e3673 | 2483 | } |
6f48390a FC |
2484 | else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1 |
2485 | && !SvMAGICAL(*SP)) { | |
767eda44 | 2486 | *++newsp = *SP; |
767eda44 | 2487 | } |
959e3673 | 2488 | else |
767eda44 | 2489 | *++newsp = sv_mortalcopy(*SP); |
959e3673 GS |
2490 | } |
2491 | else | |
a29cdaf0 | 2492 | *++newsp = sv_mortalcopy(*SP); |
959e3673 GS |
2493 | } |
2494 | else | |
3280af22 | 2495 | *++newsp = &PL_sv_undef; |
3bdf583b FC |
2496 | } |
2497 | else if (gimme == G_ARRAY) { | |
a1f49e72 | 2498 | while (++MARK <= SP) { |
3ed94dc0 | 2499 | *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1 |
6f48390a | 2500 | && !SvGMAGICAL(*MARK) |
f86702cc | 2501 | ? *MARK : sv_mortalcopy(*MARK); |
a1f49e72 CS |
2502 | TAINT_NOT; /* Each item is independent */ |
2503 | } | |
3bdf583b FC |
2504 | } |
2505 | PL_stack_sp = newsp; | |
a0d0e21e | 2506 | } |
a0d0e21e | 2507 | |
5dd42e15 | 2508 | LEAVE; |
f86702cc PP |
2509 | /* Stack values are safe: */ |
2510 | if (popsub2) { | |
5dd42e15 | 2511 | cxstack_ix--; |
b0d9ce38 | 2512 | POPSUB(cx,sv); /* release CV and @_ ... */ |
f86702cc | 2513 | } |
b0d9ce38 | 2514 | else |
c445ea15 | 2515 | sv = NULL; |
3280af22 | 2516 | PL_curpm = newpm; /* ... and pop $1 et al */ |
f86702cc | 2517 | |
b0d9ce38 | 2518 | LEAVESUB(sv); |
8433848b | 2519 | if (clear_errsv) { |
ab69dbc2 | 2520 | CLEAR_ERRSV(); |
8433848b | 2521 | } |
f39bc417 | 2522 | return retop; |
a0d0e21e LW |
2523 | } |
2524 | ||
4f443c3d FC |
2525 | /* This duplicates parts of pp_leavesub, so that it can share code with |
2526 | * pp_return */ | |
2527 | PP(pp_leavesublv) | |
2528 | { | |
2529 | dVAR; dSP; | |
4f443c3d FC |
2530 | SV **newsp; |
2531 | PMOP *newpm; | |
2532 | I32 gimme; | |
eb578fdb | 2533 | PERL_CONTEXT *cx; |
4f443c3d FC |
2534 | SV *sv; |
2535 | ||
2536 | if (CxMULTICALL(&cxstack[cxstack_ix])) | |
2537 | return 0; | |
2538 | ||
2539 | POPBLOCK(cx,newpm); | |
2540 | cxstack_ix++; /* temporarily protect top context */ | |
4f443c3d FC |
2541 | |
2542 | TAINT_NOT; | |
2543 | ||
0d235c77 | 2544 | S_return_lvalues(aTHX_ newsp, SP, newsp, gimme, cx, newpm); |
4f443c3d FC |
2545 | |
2546 | LEAVE; | |
2547 | cxstack_ix--; | |
2548 | POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */ | |
2549 | PL_curpm = newpm; /* ... and pop $1 et al */ | |
2550 | ||
2551 | LEAVESUB(sv); | |
2552 | return cx->blk_sub.retop; | |
2553 | } | |
2554 | ||
1f039d60 FC |
2555 | static I32 |
2556 | S_unwind_loop(pTHX_ const char * const opname) | |
a0d0e21e | 2557 | { |
1f039d60 | 2558 | dVAR; |
a0d0e21e | 2559 | I32 cxix; |
1f039d60 FC |
2560 | if (PL_op->op_flags & OPf_SPECIAL) { |
2561 | cxix = dopoptoloop(cxstack_ix); | |
2562 | if (cxix < 0) | |
2563 | /* diag_listed_as: Can't "last" outside a loop block */ | |
2564 | Perl_croak(aTHX_ "Can't \"%s\" outside a loop block", opname); | |
2565 | } | |
2566 | else { | |
2567 | dSP; | |
2568 | STRLEN label_len; | |
2569 | const char * const label = | |
2570 | PL_op->op_flags & OPf_STACKED | |
2571 | ? SvPV(TOPs,label_len) | |
2572 | : (label_len = strlen(cPVOP->op_pv), cPVOP->op_pv); | |
2573 | const U32 label_flags = | |
2574 | PL_op->op_flags & OPf_STACKED | |
2575 | ? SvUTF8(POPs) | |
2576 | : (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0; | |
2577 | PUTBACK; | |
2578 | cxix = dopoptolabel(label, label_len, label_flags); | |
2579 | if (cxix < 0) | |
2580 | /* diag_listed_as: Label not found for "last %s" */ | |
2581 | Perl_croak(aTHX_ "Label not found for \"%s %"SVf"\"", | |
2582 | opname, | |
2583 | SVfARG(PL_op->op_flags & OPf_STACKED | |
2584 | && !SvGMAGICAL(TOPp1s) | |
2585 | ? TOPp1s | |
2586 | : newSVpvn_flags(label, | |
2587 | label_len, | |
2588 | label_flags | SVs_TEMP))); | |
2589 | } | |
2590 | if (cxix < cxstack_ix) | |
2591 | dounwind(cxix); | |
2592 | return cxix; | |
2593 | } | |
2594 | ||
2595 | PP(pp_last) | |
2596 | { | |
2597 | dVAR; | |
eb578fdb | 2598 | PERL_CONTEXT *cx; |
f86702cc | 2599 | I32 pop2 = 0; |
a0d0e21e | 2600 | I32 gimme; |
8772537c | 2601 | I32 optype; |
b263a1ad | 2602 | OP *nextop = NULL; |
a0d0e21e LW |
2603 | SV **newsp; |
2604 | PMOP *newpm; | |
a8bba7fa | 2605 | SV **mark; |
c445ea15 | 2606 | SV *sv = NULL; |
9d4ba2ae | 2607 | |
1f039d60 | 2608 | S_unwind_loop(aTHX_ "last"); |
a0d0e21e LW |
2609 | |
2610 | POPBLOCK(cx,newpm); | |
5dd42e15 | 2611 | cxstack_ix++; /* temporarily protect top context */ |
a8bba7fa | 2612 | mark = newsp; |
6b35e009 | 2613 | switch (CxTYPE(cx)) { |
c6fdafd0 | 2614 | case CXt_LOOP_LAZYIV: |
d01136d6 | 2615 | case CXt_LOOP_LAZYSV: |
3b719c58 NC |
2616 | case CXt_LOOP_FOR: |
2617 | case CXt_LOOP_PLAIN: | |
2618 | pop2 = CxTYPE(cx); | |
a8bba7fa | 2619 | newsp = PL_stack_base + cx->blk_loop.resetsp; |
022eaa24 | 2620 | nextop = cx->blk_loop.my_op->op_lastop->op_next; |
a0d0e21e | 2621 | break; |
f86702cc | 2622 | case CXt_SUB: |
f86702cc | 2623 | pop2 = CXt_SUB; |
f39bc417 | 2624 | nextop = cx->blk_sub.retop; |
a0d0e21e | 2625 | break; |
f86702cc PP |
2626 | case CXt_EVAL: |
2627 | POPEVAL(cx); | |
f39bc417 | 2628 | nextop = cx->blk_eval.retop; |
a0d0e21e | 2629 | break; |
7766f137 GS |
2630 | case CXt_FORMAT: |
2631 | POPFORMAT(cx); | |
f39bc417 | 2632 | nextop = cx->blk_sub.retop; |
7766f137 | 2633 | break; |
a0d0e21e | 2634 | default: |
5637ef5b | 2635 | DIE(aTHX_ "panic: last, type=%u", (unsigned) CxTYPE(cx)); |
a0d0e21e LW |
2636 | } |
2637 | ||
a1f49e72 | 2638 | TAINT_NOT; |
1f039d60 | 2639 | PL_stack_sp = adjust_stack_on_leave(newsp, PL_stack_sp, MARK, gimme, |
b9d76716 | 2640 | pop2 == CXt_SUB ? SVs_TEMP : 0); |
f86702cc | 2641 | |
5dd42e15 DM |
2642 | LEAVE; |
2643 | cxstack_ix--; | |
f86702cc PP |
2644 | /* Stack values are safe: */ |
2645 | switch (pop2) { | |
c6fdafd0 | 2646 | case CXt_LOOP_LAZYIV: |
3b719c58 | 2647 | case CXt_LOOP_PLAIN: |
d01136d6 | 2648 | case CXt_LOOP_LAZYSV: |
3b719c58 | 2649 | case CXt_LOOP_FOR: |
a8bba7fa | 2650 | POPLOOP(cx); /* release loop vars ... */ |
4fdae800 | 2651 | LEAVE; |
f86702cc PP |
2652 | break; |
2653 | case CXt_SUB: | |
b0d9ce38 | 2654 | POPSUB(cx,sv); /* release CV and @_ ... */ |
f86702cc | 2655 | break; |
a0d0e21e | 2656 | } |
3280af22 | 2657 | PL_curpm = newpm; /* ... and pop $1 et al */ |
a0d0e21e | 2658 | |
b0d9ce38 | 2659 | LEAVESUB(sv); |
9d4ba2ae AL |
2660 | PERL_UNUSED_VAR(optype); |
2661 | PERL_UNUSED_VAR(gimme); | |
f86702cc | 2662 | return nextop; |
a0d0e21e LW |
2663 | } |
2664 | ||
2665 | PP(pp_next) | |
2666 | { | |
27da23d5 | 2667 | dVAR; |
eb578fdb | 2668 | PERL_CONTEXT *cx; |
1f039d60 | 2669 | const I32 inner = PL_scopestack_ix; |
a0d0e21e | 2670 | |
1f039d60 | 2671 | S_unwind_loop(aTHX_ "next"); |
a0d0e21e | 2672 | |
85538317 GS |
2673 | /* clear off anything above the scope we're re-entering, but |
2674 | * save the rest until after a possible continue block */ | |
1ba6ee2b | 2675 | TOPBLOCK(cx); |
85538317 GS |
2676 | if (PL_scopestack_ix < inner) |
2677 | leave_scope(PL_scopestack[PL_scopestack_ix]); | |
3a1b2b9e | 2678 | PL_curcop = cx->blk_oldcop; |
d57ce4df | 2679 | return (cx)->blk_loop.my_op->op_nextop; |
a0d0e21e LW |
2680 | } |
2681 | ||
2682 | PP(pp_redo) | |
2683 | { | |
27da23d5 | 2684 | dVAR; |
1f039d60 | 2685 | const I32 cxix = S_unwind_loop(aTHX_ "redo"); |
eb578fdb | 2686 | PERL_CONTEXT *cx; |
a0d0e21e | 2687 | I32 oldsave; |
1f039d60 | 2688 | OP* redo_op = cxstack[cxix].blk_loop.my_op->op_redoop; |
a0d0e21e | 2689 | |
a034e688 DM |
2690 | if (redo_op->op_type == OP_ENTER) { |
2691 | /* pop one less context to avoid $x being freed in while (my $x..) */ | |
2692 | cxstack_ix++; | |
2693 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK); | |
2694 | redo_op = redo_op->op_next; | |
2695 | } | |
2696 | ||
a0d0e21e | 2697 | TOPBLOCK(cx); |
3280af22 | 2698 | oldsave = PL_scopestack[PL_scopestack_ix - 1]; |
a0d0e21e | 2699 | LEAVE_SCOPE(oldsave); |
936c78b5 | 2700 | FREETMPS; |
3a1b2b9e | 2701 | PL_curcop = cx->blk_oldcop; |
a034e688 | 2702 | return redo_op; |
a0d0e21e LW |
2703 | } |
2704 | ||
0824fdcb | 2705 | STATIC OP * |
5db1eb8d | 2706 | S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit) |
a0d0e21e | 2707 | { |
97aff369 | 2708 | dVAR; |
a0d0e21e | 2709 | OP **ops = opstack; |
a1894d81 | 2710 | static const char* const too_deep = "Target of goto is too deeply nested"; |
a0d0e21e | 2711 | |
7918f24d NC |
2712 | PERL_ARGS_ASSERT_DOFINDLABEL; |
2713 | ||
fc36a67e | 2714 | if (ops >= oplimit) |
cea2e8a9 | 2715 | Perl_croak(aTHX_ too_deep); |
11343788 MB |
2716 | if (o->op_type == OP_LEAVE || |
2717 | o->op_type == OP_SCOPE || | |
2718 | o->op_type == OP_LEAVELOOP || | |
33d34e4c | 2719 | o->op_type == OP_LEAVESUB || |
11343788 | 2720 | o->op_type == OP_LEAVETRY) |
fc36a67e | 2721 | { |
5dc0d613 | 2722 | *ops++ = cUNOPo->op_first; |
fc36a67e | 2723 | if (ops >= oplimit) |
cea2e8a9 | 2724 | Perl_croak(aTHX_ too_deep); |
fc36a67e | 2725 | } |
c4aa4e48 | 2726 | *ops = 0; |
11343788 | 2727 | if (o->op_flags & OPf_KIDS) { |
aec46f14 | 2728 | OP *kid; |
a0d0e21e | 2729 | /* First try all the kids at this level, since that's likeliest. */ |
11343788 | 2730 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
7e8f1eac | 2731 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { |
5db1eb8d BF |
2732 | STRLEN kid_label_len; |
2733 | U32 kid_label_flags; | |
2734 | const char *kid_label = CopLABEL_len_flags(kCOP, | |
2735 | &kid_label_len, &kid_label_flags); | |
2736 | if (kid_label && ( | |
2737 | ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ? | |
2738 | (flags & SVf_UTF8) | |
2739 | ? (bytes_cmp_utf8( | |
2740 | (const U8*)kid_label, kid_label_len, | |
2741 | (const U8*)label, len) == 0) | |
2742 | : (bytes_cmp_utf8( | |
2743 | (const U8*)label, len, | |
2744 | (const U8*)kid_label, kid_label_len) == 0) | |
eade7155 BF |
2745 | : ( len == kid_label_len && ((kid_label == label) |
2746 | || memEQ(kid_label, label, len))))) | |
7e8f1eac AD |
2747 | return kid; |
2748 | } | |
a0d0e21e | 2749 | } |
11343788 | 2750 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
3280af22 | 2751 | if (kid == PL_lastgotoprobe) |
a0d0e21e | 2752 | continue; |
ed8d0fe2 SM |
2753 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { |
2754 | if (ops == opstack) | |
2755 | *ops++ = kid; | |
2756 | else if (ops[-1]->op_type == OP_NEXTSTATE || | |
2757 | ops[-1]->op_type == OP_DBSTATE) | |
2758 | ops[-1] = kid; | |
2759 | else | |
2760 | *ops++ = kid; | |
2761 | } | |
5db1eb8d | 2762 | if ((o = dofindlabel(kid, label, len, flags, ops, oplimit))) |
11343788 | 2763 | return o; |
a0d0e21e LW |
2764 | } |
2765 | } | |
c4aa4e48 | 2766 | *ops = 0; |
a0d0e21e LW |
2767 | return 0; |
2768 | } | |
2769 | ||
a0d0e21e LW |
2770 | PP(pp_goto) |
2771 | { | |
27da23d5 | 2772 | dVAR; dSP; |
cbbf8932 | 2773 | OP *retop = NULL; |
a0d0e21e | 2774 | I32 ix; |
eb578fdb | 2775 | PERL_CONTEXT *cx; |
fc36a67e PP |
2776 | #define GOTO_DEPTH 64 |
2777 | OP *enterops[GOTO_DEPTH]; | |
cbbf8932 | 2778 | const char *label = NULL; |
5db1eb8d BF |
2779 | STRLEN label_len = 0; |
2780 | U32 label_flags = 0; | |
bfed75c6 | 2781 | const bool do_dump = (PL_op->op_type == OP_DUMP); |
a1894d81 | 2782 | static const char* const must_have_label = "goto must have label"; |
a0d0e21e | 2783 | |
533c011a | 2784 | if (PL_op->op_flags & OPf_STACKED) { |
9d4ba2ae | 2785 | SV * const sv = POPs; |
55b37f1c | 2786 | SvGETMAGIC(sv); |
a0d0e21e LW |
2787 | |
2788 | /* This egregious kludge implements goto &subroutine */ | |
2789 | if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) { | |
2790 | I32 cxix; | |
eb578fdb | 2791 | PERL_CONTEXT *cx; |
ea726b52 | 2792 | CV *cv = MUTABLE_CV(SvRV(sv)); |
049bd5ff | 2793 | AV *arg = GvAV(PL_defgv); |
a0d0e21e LW |
2794 | I32 oldsave; |
2795 | ||
e8f7dd13 | 2796 | retry: |
4aa0a1f7 | 2797 | if (!CvROOT(cv) && !CvXSUB(cv)) { |
7fc63493 | 2798 | const GV * const gv = CvGV(cv); |
e8f7dd13 | 2799 | if (gv) { |
7fc63493 | 2800 | GV *autogv; |
e8f7dd13 GS |
2801 | SV *tmpstr; |
2802 | /* autoloaded stub? */ | |
2803 | if (cv != GvCV(gv) && (cv = GvCV(gv))) | |
2804 | goto retry; | |
c271df94 BF |
2805 | autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), |
2806 | GvNAMELEN(gv), | |
2807 | GvNAMEUTF8(gv) ? SVf_UTF8 : 0); | |
e8f7dd13 GS |
2808 | if (autogv && (cv = GvCV(autogv))) |
2809 | goto retry; | |
2810 | tmpstr = sv_newmortal(); | |
c445ea15 | 2811 | gv_efullname3(tmpstr, gv, NULL); |
be2597df | 2812 | DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr)); |
4aa0a1f7 | 2813 | } |
cea2e8a9 | 2814 | DIE(aTHX_ "Goto undefined subroutine"); |
4aa0a1f7 AD |
2815 | } |
2816 | ||
a0d0e21e | 2817 | /* First do some returnish stuff. */ |
b37c2d43 | 2818 | SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */ |
71fc2216 | 2819 | FREETMPS; |
a0d0e21e | 2820 | cxix = dopoptosub(cxstack_ix); |
8da3792e SM |
2821 | if (cxix < cxstack_ix) { |
2822 | if (cxix < 0) { | |
2823 | SvREFCNT_dec(cv); | |
2824 | DIE(aTHX_ "Can't goto subroutine outside a subroutine"); | |
2825 | } | |
a0d0e21e | 2826 | dounwind(cxix); |
8da3792e | 2827 | } |
a0d0e21e | 2828 | TOPBLOCK(cx); |
2d43a17f | 2829 | SPAGAIN; |
564abe23 | 2830 | /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */ |
2d43a17f | 2831 | if (CxTYPE(cx) == CXt_EVAL) { |
110af908 | 2832 | SvREFCNT_dec(cv); |
c74ace89 | 2833 | if (CxREALEVAL(cx)) |
00455a92 | 2834 | /* diag_listed_as: Can't goto subroutine from an eval-%s */ |
c74ace89 DM |
2835 | DIE(aTHX_ "Can't goto subroutine from an eval-string"); |
2836 | else | |
00455a92 | 2837 | /* diag_listed_as: Can't goto subroutine from an eval-%s */ |
c74ace89 | 2838 | DIE(aTHX_ "Can't goto subroutine from an eval-block"); |
2d43a17f | 2839 | } |
9850bf21 | 2840 | else if (CxMULTICALL(cx)) |
110af908 FC |
2841 | { |
2842 | SvREFCNT_dec(cv); | |
9850bf21 | 2843 | DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)"); |
110af908 | 2844 | } |
bafb2adc | 2845 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { |
a0d0e21e | 2846 | AV* av = cx->blk_sub.argarray; |
bfed75c6 | 2847 | |
049bd5ff FC |
2848 | /* abandon the original @_ if it got reified or if it is |
2849 | the same as the current @_ */ | |
2850 | if (AvREAL(av) || av == arg) { | |
b1464ded | 2851 | SvREFCNT_dec(av); |
d8b46c1b | 2852 | av = newAV(); |
11ca45c0 | 2853 | AvREIFY_only(av); |
ad64d0ec | 2854 | PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av); |
62b1ebc2 | 2855 | } |
049bd5ff | 2856 | else CLEAR_ARGARRAY(av); |
a0d0e21e | 2857 | } |
049bd5ff FC |
2858 | /* We donate this refcount later to the callee’s pad. */ |
2859 | SvREFCNT_inc_simple_void(arg); | |
6b35e009 | 2860 | if (CxTYPE(cx) == CXt_SUB && |
b150fb22 | 2861 | !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) |
a0d0e21e | 2862 | SvREFCNT_dec(cx->blk_sub.cv); |
3280af22 | 2863 | oldsave = PL_scopestack[PL_scopestack_ix - 1]; |
a0d0e21e LW |
2864 | LEAVE_SCOPE(oldsave); |
2865 | ||
1d59c038 FC |
2866 | /* A destructor called during LEAVE_SCOPE could have undefined |
2867 | * our precious cv. See bug #99850. */ | |
2868 | if (!CvROOT(cv) && !CvXSUB(cv)) { | |
2869 | const GV * const gv = CvGV(cv); | |
049bd5ff | 2870 | SvREFCNT_dec(arg); |
1d59c038 FC |
2871 | if (gv) { |
2872 | SV * const tmpstr = sv_newmortal(); | |
2873 | gv_efullname3(tmpstr, gv, NULL); | |
2874 | DIE(aTHX_ "Goto undefined subroutine &%"SVf"", | |
2875 | SVfARG(tmpstr)); | |
2876 | } | |
2877 | DIE(aTHX_ "Goto undefined subroutine"); | |
2878 | } | |
2879 | ||
a0d0e21e LW |
2880 | /* Now do some callish stuff. */ |
2881 | SAVETMPS; | |
5023d17a | 2882 | SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */ |
aed2304a | 2883 | if (CvISXSUB(cv)) { |
b37c2d43 | 2884 | OP* const retop = cx->blk_sub.retop; |
cb65b687 DM |
2885 | SV **newsp; |
2886 | I32 gimme; | |
049bd5ff FC |
2887 | const SSize_t items = AvFILLp(arg) + 1; |
2888 | SV** mark; | |
2889 | ||
cb65b687 DM |
2890 | PERL_UNUSED_VAR(newsp); |
2891 | PERL_UNUSED_VAR(gimme); | |
2892 | ||
049bd5ff FC |
2893 | /* put GvAV(defgv) back onto stack */ |
2894 | EXTEND(SP, items+1); /* @_ could have been extended. */ | |
2895 | Copy(AvARRAY(arg), SP + 1, items, SV*); | |
2896 | mark = SP; | |
2897 | SP += items; | |
2898 | if (AvREAL(arg)) { | |
b1464ded DM |
2899 | I32 index; |
2900 | for (index=0; index<items; index++) | |
049bd5ff FC |
2901 | SvREFCNT_inc_void(sv_2mortal(SP[-index])); |
2902 | } | |
2903 | SvREFCNT_dec(arg); | |
2904 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { | |
2905 | /* Restore old @_ */ | |
2906 | arg = GvAV(PL_defgv); | |
2907 | GvAV(PL_defgv) = cx->blk_sub.savearray; | |
2908 | SvREFCNT_dec(arg); | |
b1464ded | 2909 | } |
1fa4e549 | 2910 | |
b37c2d43 AL |
2911 | /* XS subs don't have a CxSUB, so pop it */ |
2912 | POPBLOCK(cx, PL_curpm); | |
2913 | /* Push a mark for the start of arglist */ | |
2914 | PUSHMARK(mark); | |
2915 | PUTBACK; | |
2916 | (void)(*CvXSUB(cv))(aTHX_ cv); | |
a57c6685 | 2917 | LEAVE; |
5eff7df7 | 2918 | return retop; |
a0d0e21e LW |
2919 | } |
2920 | else { | |
b70d5558 | 2921 | PADLIST * const padlist = CvPADLIST(cv); |
a0d0e21e | 2922 | cx->blk_sub.cv = cv; |
1a5b3db4 | 2923 | cx->blk_sub.olddepth = CvDEPTH(cv); |
dd2155a4 | 2924 | |
a0d0e21e LW |
2925 | CvDEPTH(cv)++; |
2926 | if (CvDEPTH(cv) < 2) | |
74c765eb | 2927 | SvREFCNT_inc_simple_void_NN(cv); |
dd2155a4 | 2928 | else { |
2b9dff67 | 2929 | if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION)) |
44a8e56a | 2930 | sub_crush_depth(cv); |
26019298 | 2931 | pad_push(padlist, CvDEPTH(cv)); |
a0d0e21e | 2932 | } |
426a09cd | 2933 | PL_curcop = cx->blk_oldcop; |
fd617465 DM |
2934 | SAVECOMPPAD(); |
2935 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); | |
bafb2adc | 2936 | if (CxHASARGS(cx)) |
6d4ff0d2 | 2937 | { |
dd2155a4 | 2938 | CX_CURPAD_SAVE(cx->blk_sub); |
a0d0e21e | 2939 | |
049bd5ff FC |
2940 | /* cx->blk_sub.argarray has no reference count, so we |
2941 | need something to hang on to our argument array so | |
2942 | that cx->blk_sub.argarray does not end up pointing | |
2943 | to freed memory as the result of undef *_. So put | |
2944 | it in the callee’s pad, donating our refer- | |
2945 | ence count. */ | |
2946 | SvREFCNT_dec(PAD_SVl(0)); | |
2947 | PAD_SVl(0) = (SV *)(cx->blk_sub.argarray = arg); | |
2948 | ||
2949 | /* GvAV(PL_defgv) might have been modified on scope | |
2950 | exit, so restore it. */ | |
2951 | if (arg != GvAV(PL_defgv)) { | |
2952 | AV * const av = GvAV(PL_defgv); | |
2953 | GvAV(PL_defgv) = (AV *)SvREFCNT_inc_simple(arg); | |
2954 | SvREFCNT_dec(av); | |
a0d0e21e LW |
2955 | } |
2956 | } | |
049bd5ff | 2957 | else SvREFCNT_dec(arg); |
491527d0 | 2958 | if (PERLDB_SUB) { /* Checking curstash breaks DProf. */ |
005a8a35 | 2959 | Perl_get_db_sub(aTHX_ NULL, cv); |
b37c2d43 | 2960 | if (PERLDB_GOTO) { |
b96d8cd9 | 2961 | CV * const gotocv = get_cvs("DB::goto", 0); |
b37c2d43 AL |
2962 | if (gotocv) { |
2963 | PUSHMARK( PL_stack_sp ); | |
ad64d0ec | 2964 | call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG); |
b37c2d43 AL |
2965 | PL_stack_sp--; |
2966 | } | |
491527d0 | 2967 | } |
1ce6579f | 2968 | } |
a0d0e21e LW |
2969 | RETURNOP(CvSTART(cv)); |
2970 | } | |
2971 | } | |
1614b0e3 | 2972 | else { |
55b37f1c | 2973 | label = SvPV_nomg_const(sv, label_len); |
5db1eb8d | 2974 | label_flags = SvUTF8(sv); |
1614b0e3 | 2975 | } |
a0d0e21e | 2976 | } |
2fc690dc | 2977 | else if (!(PL_op->op_flags & OPf_SPECIAL)) { |
5db1eb8d BF |
2978 | label = cPVOP->op_pv; |
2979 | label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0; | |
2980 | label_len = strlen(label); | |
2981 | } | |
2fc690dc | 2982 | if (!(do_dump || label_len)) DIE(aTHX_ must_have_label); |
a0d0e21e | 2983 | |
f410a211 NC |
2984 | PERL_ASYNC_CHECK(); |
2985 | ||
3532f34a | 2986 | if (label_len) { |
cbbf8932 | 2987 | OP *gotoprobe = NULL; |
3b2447bc | 2988 | bool leaving_eval = FALSE; |
33d34e4c | 2989 | bool in_block = FALSE; |
cbbf8932 | 2990 | PERL_CONTEXT *last_eval_cx = NULL; |
a0d0e21e LW |
2991 | |
2992 | /* find label */ | |
2993 | ||
d4c19fe8 | 2994 | PL_lastgotoprobe = NULL; |
a0d0e21e LW |
2995 | *enterops = 0; |
2996 | for (ix = cxstack_ix; ix >= 0; ix--) { | |
2997 | cx = &cxstack[ix]; | |
6b35e009 | 2998 | switch (CxTYPE(cx)) { |
a0d0e21e | 2999 | case CXt_EVAL: |
3b2447bc | 3000 | leaving_eval = TRUE; |
971ecbe6 | 3001 | if (!CxTRYBLOCK(cx)) { |
a4f3a277 RH |
3002 | gotoprobe = (last_eval_cx ? |
3003 | last_eval_cx->blk_eval.old_eval_root : | |
3004 | PL_eval_root); | |
3005 | last_eval_cx = cx; | |
9c5794fe RH |
3006 | break; |
3007 | } | |
3008 | /* else fall through */ | |
c6fdafd0 | 3009 | case CXt_LOOP_LAZYIV: |
d01136d6 | 3010 | case CXt_LOOP_LAZYSV: |
3b719c58 NC |
3011 | case CXt_LOOP_FOR: |
3012 | case CXt_LOOP_PLAIN: | |
bb5aedc1 VP |
3013 | case CXt_GIVEN: |
3014 | case CXt_WHEN: | |
a0d0e21e LW |
3015 | gotoprobe = cx->blk_oldcop->op_sibling; |
3016 | break; | |
3017 | case CXt_SUBST: | |
3018 | continue; | |
3019 | case CXt_BLOCK: | |
33d34e4c | 3020 | if (ix) { |
a0d0e21e | 3021 | gotoprobe = cx->blk_oldcop->op_sibling; |
33d34e4c AE |
3022 | in_block = TRUE; |
3023 | } else | |
3280af22 | 3024 | gotoprobe = PL_main_root; |
a0d0e21e | 3025 | break; |
b3933176 | 3026 | case CXt_SUB: |
9850bf21 | 3027 | if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) { |
b3933176 CS |
3028 | gotoprobe = CvROOT(cx->blk_sub.cv); |
3029 | break; | |
3030 | } | |
3031 | /* FALL THROUGH */ | |
7766f137 | 3032 | case CXt_FORMAT: |
0a753a76 | 3033 | case CXt_NULL: |
a651a37d | 3034 | DIE(aTHX_ "Can't \"goto\" out of a pseudo block"); |
a0d0e21e LW |
3035 | default: |
3036 | if (ix) | |
5637ef5b NC |
3037 | DIE(aTHX_ "panic: goto, type=%u, ix=%ld", |
3038 | CxTYPE(cx), (long) ix); | |
3280af22 | 3039 | gotoprobe = PL_main_root; |
a0d0e21e LW |
3040 | break; |
3041 | } | |
2b597662 | 3042 | if (gotoprobe) { |
5db1eb8d | 3043 | retop = dofindlabel(gotoprobe, label, label_len, label_flags, |
2b597662 GS |
3044 | enterops, enterops + GOTO_DEPTH); |
3045 | if (retop) | |
3046 | break; | |
eae48c89 Z |
3047 | if (gotoprobe->op_sibling && |
3048 | gotoprobe->op_sibling->op_type == OP_UNSTACK && | |
3049 | gotoprobe->op_sibling->op_sibling) { | |
3050 | retop = dofindlabel(gotoprobe->op_sibling->op_sibling, | |
5db1eb8d BF |
3051 | label, label_len, label_flags, enterops, |
3052 | enterops + GOTO_DEPTH); | |
eae48c89 Z |
3053 | if (retop) |
3054 | break; | |
3055 | } | |
2b597662 | 3056 | } |
3280af22 | 3057 | PL_lastgotoprobe = gotoprobe; |
a0d0e21e LW |
3058 | } |
3059 | if (!retop) | |
5db1eb8d BF |
3060 | DIE(aTHX_ "Can't find label %"SVf, |
3061 | SVfARG(newSVpvn_flags(label, label_len, | |
3062 | SVs_TEMP | label_flags))); | |
a0d0e21e | 3063 | |
3b2447bc RH |
3064 | /* if we're leaving an eval, check before we pop any frames |
3065 | that we're not going to punt, otherwise the error | |
3066 | won't be caught */ | |
3067 | ||
3068 | if (leaving_eval && *enterops && enterops[1]) { | |
3069 | I32 i; | |
3070 | for (i = 1; enterops[i]; i++) | |
3071 | if (enterops[i]->op_type == OP_ENTERITER) | |
3072 | DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop"); | |
3073 | } | |
3074 | ||
b500e03b GG |
3075 | if (*enterops && enterops[1]) { |
3076 | I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1; | |
3077 | if (enterops[i]) | |
3078 | deprecate("\"goto\" to jump into a construct"); | |
3079 | } | |
3080 | ||
a0d0e21e LW |
3081 | /* pop unwanted frames */ |
3082 | ||
3083 | if (ix < cxstack_ix) { | |
3084 | I32 oldsave; | |
3085 | ||
3086 | if (ix < 0) | |
3087 | ix = 0; | |
3088 | dounwind(ix); | |
3089 | TOPBLOCK(cx); | |
3280af22 | 3090 | oldsave = PL_scopestack[PL_scopestack_ix]; |
a0d0e21e LW |
3091 | LEAVE_SCOPE(oldsave); |
3092 | } | |
3093 | ||
3094 | /* push wanted frames */ | |
3095 | ||
748a9306 | 3096 | if (*enterops && enterops[1]) { |
0bd48802 | 3097 | OP * const oldop = PL_op; |
33d34e4c AE |
3098 | ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1; |
3099 | for (; enterops[ix]; ix++) { | |
533c011a | 3100 | PL_op = enterops[ix]; |
84902520 TB |
3101 | /* Eventually we may want to stack the needed arguments |
3102 | * for each op. For now, we punt on the hard ones. */ | |
533c011a | 3103 | if (PL_op->op_type == OP_ENTERITER) |
894356b3 | 3104 | DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop"); |
16c91539 | 3105 | PL_op->op_ppaddr(aTHX); |
a0d0e21e | 3106 | } |
533c011a | 3107 | PL_op = oldop; |
a0d0e21e LW |
3108 | } |
3109 | } | |
3110 | ||
3111 | if (do_dump) { | |
a5f75d66 | 3112 | #ifdef VMS |
6b88bc9c | 3113 | if (!retop) retop = PL_main_start; |
a5f75d66 | 3114 | #endif |
3280af22 NIS |
3115 | PL_restartop = retop; |
3116 | PL_do_undump = TRUE; | |
a0d0e21e LW |
3117 | |
3118 | my_unexec(); | |
3119 | ||
3280af22 NIS |
3120 | PL_restartop = 0; /* hmm, must be GNU unexec().. */ |
3121 | PL_do_undump = FALSE; | |
a0d0e21e LW |
3122 | } |
3123 | ||
3124 | RETURNOP(retop); | |
3125 | } | |
3126 | ||
3127 | PP(pp_exit) | |
3128 | { | |
97aff369 | 3129 | dVAR; |
39644a26 | 3130 | dSP; |
a0d0e21e LW |
3131 | I32 anum; |
3132 | ||
3133 | if (MAXARG < 1) | |
3134 | anum = 0; | |
9d3c658e FC |
3135 | else if (!TOPs) { |
3136 | anum = 0; (void)POPs; | |
3137 | } | |
ff0cee69 | 3138 | else { |
a0d0e21e | 3139 | anum = SvIVx(POPs); |
d98f61e7 GS |
3140 | #ifdef VMS |
3141 | if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH)) | |
ff0cee69 | 3142 | anum = 0; |
96e176bf | 3143 | VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH); |
ff0cee69 PP |
3144 | #endif |
3145 | } | |
cc3604b1 | 3146 | PL_exit_flags |= PERL_EXIT_EXPECTED; |
81d86705 NC |
3147 | #ifdef PERL_MAD |
3148 | /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */ | |
3149 | if (anum || !(PL_minus_c && PL_madskills)) | |
3150 | my_exit(anum); | |
3151 | #else | |
a0d0e21e | 3152 | my_exit(anum); |
81d86705 | 3153 | #endif |
3280af22 | 3154 | PUSHs(&PL_sv_undef); |
a0d0e21e LW |
3155 | RETURN; |
3156 | } | |
3157 | ||
a0d0e21e LW |
3158 | /* Eval. */ |
3159 | ||
0824fdcb | 3160 | STATIC void |
cea2e8a9 | 3161 | S_save_lines(pTHX_ AV *array, SV *sv) |
a0d0e21e | 3162 | { |
504618e9 | 3163 | const char *s = SvPVX_const(sv); |
890ce7af | 3164 | const char * const send = SvPVX_const(sv) + SvCUR(sv); |
504618e9 | 3165 | I32 line = 1; |
a0d0e21e | 3166 | |
7918f24d NC |
3167 | PERL_ARGS_ASSERT_SAVE_LINES; |
3168 | ||
a0d0e21e | 3169 | while (s && s < send) { |
f54cb97a | 3170 | const char *t; |
b9f83d2f | 3171 | SV * const tmpstr = newSV_type(SVt_PVMG); |
a0d0e21e | 3172 | |
1d963ff3 | 3173 | t = (const char *)memchr(s, '\n', send - s); |
a0d0e21e LW |
3174 | if (t) |
3175 | t++; | |
3176 | else | |
3177 | t = send; | |
3178 | ||
3179 | sv_setpvn(tmpstr, s, t - s); | |
3180 | av_store(array, line++, tmpstr); | |
3181 | s = t; | |
3182 | } | |
3183 | } | |
3184 | ||
22f16304 RU |
3185 | /* |
3186 | =for apidoc docatch | |
3187 | ||
3188 | Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context. | |
3189 | ||
3190 | 0 is used as continue inside eval, | |
3191 | ||
3192 | 3 is used for a die caught by an inner eval - continue inner loop | |
3193 | ||
3194 | See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must | |
3195 | establish a local jmpenv to handle exception traps. | |
3196 | ||
3197 | =cut | |
3198 | */ | |
0824fdcb | 3199 | STATIC OP * |
cea2e8a9 | 3200 | S_docatch(pTHX_ OP *o) |
1e422769 | 3201 | { |
97aff369 | 3202 | dVAR; |
6224f72b | 3203 | int ret; |
06b5626a | 3204 | OP * const oldop = PL_op; |
db36c5a1 | 3205 | dJMPENV; |
1e422769 | 3206 | |
1e422769 | 3207 | #ifdef DEBUGGING |
54310121 | 3208 | assert(CATCH_GET == TRUE); |
1e422769 | 3209 | #endif |
312caa8e | 3210 | PL_op = o; |
8bffa5f8 | 3211 | |
14dd3ad8 | 3212 | JMPENV_PUSH(ret); |
6224f72b | 3213 | switch (ret) { |
312caa8e | 3214 | case 0: |
abd70938 DM |
3215 | assert(cxstack_ix >= 0); |
3216 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL); | |
3217 | cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env; | |
14dd3ad8 | 3218 | redo_body: |
85aaa934 | 3219 | CALLRUNOPS(aTHX); |
312caa8e CS |
3220 | break; |
3221 | case 3: | |
8bffa5f8 | 3222 | /* die caught by an inner eval - continue inner loop */ |
febb3a6d Z |
3223 | if (PL_restartop && PL_restartjmpenv == PL_top_env) { |
3224 | PL_restartjmpenv = NULL; | |
312caa8e CS |
3225 | PL_op = PL_restartop; |
3226 | PL_restartop = 0; | |
3227 | goto redo_body; | |
3228 | } | |
3229 | /* FALL THROUGH */ | |
3230 | default: | |
14dd3ad8 | 3231 | JMPENV_POP; |
533c011a | 3232 | PL_op = oldop; |
6224f72b | 3233 | JMPENV_JUMP(ret); |
118e2215 | 3234 | assert(0); /* NOTREACHED */ |
1e422769 | 3235 | } |
14dd3ad8 | 3236 | JMPENV_POP; |
533c011a | 3237 | PL_op = oldop; |
5f66b61c | 3238 | return NULL; |
1e422769 PP |
3239 | } |
3240 | ||
a3985cdc DM |
3241 | |
3242 | /* | |
3243 | =for apidoc find_runcv | |
3244 | ||
3245 | Locate the CV corresponding to the currently executing sub or eval. | |
d819b83a DM |
3246 | If db_seqp is non_null, skip CVs that are in the DB package and populate |
3247 | *db_seqp with the cop sequence number at the point that the DB:: code was | |
3248 | entered. (allows debuggers to eval in the scope of the breakpoint rather | |
cf525c36 | 3249 | than in the scope of the debugger itself). |
a3985cdc DM |
3250 | |
3251 | =cut | |
3252 | */ | |
3253 | ||
3254 | CV* | |
d819b83a | 3255 | Perl_find_runcv(pTHX_ U32 *db_seqp) |
a3985cdc | 3256 | { |
db4cf31d | 3257 | return Perl_find_runcv_where(aTHX_ 0, 0, db_seqp); |
70794f7b FC |
3258 | } |
3259 | ||
3260 | /* If this becomes part of the API, it might need a better name. */ | |
3261 | CV * | |
db4cf31d | 3262 | Perl_find_runcv_where(pTHX_ U8 cond, IV arg, U32 *db_seqp) |
70794f7b | 3263 | { |
97aff369 | 3264 | dVAR; |
a3985cdc | 3265 | PERL_SI *si; |
b4b0692a | 3266 | int level = 0; |
a3985cdc | 3267 | |
d819b83a DM |
3268 | if (db_seqp) |
3269 | *db_seqp = PL_curcop->cop_seq; | |
a3985cdc | 3270 | for (si = PL_curstackinfo; si; si = si->si_prev) { |
06b5626a | 3271 | I32 ix; |
a3985cdc | 3272 | for (ix = si->si_cxix; ix >= 0; ix--) { |
06b5626a | 3273 | const PERL_CONTEXT *cx = &(si->si_cxstack[ix]); |
70794f7b | 3274 | CV *cv = NULL; |
d819b83a | 3275 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
70794f7b | 3276 | cv = cx->blk_sub.cv; |
d819b83a DM |
3277 | /* skip DB:: code */ |
3278 | if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) { | |
3279 | *db_seqp = cx->blk_oldcop->cop_seq; | |
3280 | continue; | |
3281 | } | |
d819b83a | 3282 | } |
a3985cdc | 3283 | else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx)) |
70794f7b FC |
3284 | cv = cx->blk_eval.cv; |
3285 | if (cv) { | |
3286 | switch (cond) { | |
db4cf31d FC |
3287 | case FIND_RUNCV_padid_eq: |
3288 | if (!CvPADLIST(cv) | |
a56015b9 | 3289 | || PadlistNAMES(CvPADLIST(cv)) != INT2PTR(PADNAMELIST *, arg)) |
8771da69 | 3290 | continue; |
b4b0692a FC |
3291 | return cv; |
3292 | case FIND_RUNCV_level_eq: | |
db4cf31d | 3293 | if (level++ != arg) continue; |
70794f7b FC |
3294 | /* GERONIMO! */ |
3295 | default: | |
3296 | return cv; | |
3297 | } | |
3298 | } | |
a3985cdc DM |
3299 | } |
3300 | } | |
db4cf31d | 3301 | return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv; |
a3985cdc DM |
3302 | } |
3303 | ||
3304 | ||
27e90453 DM |
3305 | /* Run yyparse() in a setjmp wrapper. Returns: |
3306 | * 0: yyparse() successful | |
3307 | * 1: yyparse() failed | |
3308 | * 3: yyparse() died | |
3309 | */ | |
3310 | STATIC int | |
28ac2b49 | 3311 | S_try_yyparse(pTHX_ int gramtype) |
27e90453 DM |
3312 | { |
3313 | int ret; | |
3314 | dJMPENV; | |
3315 | ||
3316 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL); | |
3317 | JMPENV_PUSH(ret); | |
3318 | switch (ret) { | |
3319 | case 0: | |
28ac2b49 | 3320 | ret = yyparse(gramtype) ? 1 : 0; |
27e90453 DM |
3321 | break; |
3322 | case 3: | |
3323 | break; | |
3324 | default: | |
3325 | JMPENV_POP; | |
3326 | JMPENV_JUMP(ret); | |
118e2215 | 3327 | assert(0); /* NOTREACHED */ |
27e90453 DM |
3328 | } |
3329 | JMPENV_POP; | |
3330 | return ret; | |
3331 | } | |
3332 | ||
3333 | ||
104a8185 DM |
3334 | /* Compile a require/do or an eval ''. |
3335 | * | |
a3985cdc | 3336 | * outside is the lexically enclosing CV (if any) that invoked us. |
104a8185 DM |
3337 | * seq is the current COP scope value. |
3338 | * hh is the saved hints hash, if any. | |
3339 | * | |
410be5db | 3340 | * Returns a bool indicating whether the compile was successful; if so, |
104a8185 DM |
3341 | * PL_eval_start contains the first op of the compiled code; otherwise, |
3342 | * pushes undef. | |
3343 | * | |
3344 | * This function is called from two places: pp_require and pp_entereval. | |
3345 | * These can be distinguished by whether PL_op is entereval. | |
7d116edc FC |
3346 | */ |
3347 | ||
410be5db | 3348 | STATIC bool |
104a8185 | 3349 | S_doeval(pTHX_ int gimme, CV* outside, U32 seq, HV *hh) |
a0d0e21e | 3350 | { |
27da23d5 | 3351 | dVAR; dSP; |
46c461b5 | 3352 | OP * const saveop = PL_op; |
104a8185 | 3353 | bool clear_hints = saveop->op_type != OP_ENTEREVAL; |
f45b078d | 3354 | COP * const oldcurcop = PL_curcop; |
26c9400e | 3355 | bool in_require = (saveop->op_type == OP_REQUIRE); |
27e90453 | 3356 | int yystatus; |
676a678a | 3357 | CV *evalcv; |
a0d0e21e | 3358 | |
27e90453 | 3359 | PL_in_eval = (in_require |
6dc8a9e4 | 3360 | ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL)) |
a1941760 DM |
3361 | : (EVAL_INEVAL | |
3362 | ((PL_op->op_private & OPpEVAL_RE_REPARSING) | |
3363 | ? EVAL_RE_REPARSING : 0))); | |
a0d0e21e | 3364 | |
1ce6579f PP |
3365 | PUSHMARK(SP); |
3366 | ||
676a678a Z |
3367 | evalcv = MUTABLE_CV(newSV_type(SVt_PVCV)); |
3368 | CvEVAL_on(evalcv); | |
2090ab20 | 3369 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL); |
676a678a | 3370 | cxstack[cxstack_ix].blk_eval.cv = evalcv; |
86a64801 | 3371 | cxstack[cxstack_ix].blk_gimme = gimme; |
2090ab20 | 3372 | |
676a678a Z |
3373 | CvOUTSIDE_SEQ(evalcv) = seq; |
3374 | CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); | |
a3985cdc | 3375 | |
dd2155a4 | 3376 | /* set up a scratch pad */ |
a0d0e21e | 3377 | |
676a678a | 3378 | CvPADLIST(evalcv) = pad_new(padnew_SAVE); |
cecbe010 | 3379 | PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */ |
2c05e328 | 3380 | |
07055b4c | 3381 | |
81d86705 | 3382 | if (!PL_madskills) |
676a678a | 3383 | SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */ |
748a9306 | 3384 | |
a0d0e21e LW |
3385 | /* make sure we compile in the right package */ |
3386 | ||
ed094faf | 3387 | if (CopSTASH_ne(PL_curcop, PL_curstash)) { |
03d9f026 FC |
3388 | SAVEGENERICSV(PL_curstash); |
3389 | PL_curstash = (HV *)SvREFCNT_inc_simple(CopSTASH(PL_curcop)); | |
a0d0e21e | 3390 | } |
3c10abe3 | 3391 | /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */ |
3280af22 NIS |
3392 | SAVESPTR(PL_beginav); |
3393 | PL_beginav = newAV(); | |
3394 | SAVEFREESV(PL_beginav); | |
3c10abe3 AG |
3395 | SAVESPTR(PL_unitcheckav); |
3396 | PL_unitcheckav = newAV(); | |
3397 | SAVEFREESV(PL_unitcheckav); | |
a0d0e21e | 3398 | |
81d86705 | 3399 | #ifdef PERL_MAD |
9da243ce | 3400 | SAVEBOOL(PL_madskills); |
81d86705 NC |
3401 | PL_madskills = 0; |
3402 | #endif | |
3403 | ||
104a8185 | 3404 | ENTER_with_name("evalcomp"); |
676a678a Z |
3405 | SAVESPTR(PL_compcv); |
3406 | PL_compcv = evalcv; | |
3407 | ||
a0d0e21e LW |
3408 | /* try to compile it */ |
3409 | ||
5f66b61c | 3410 | PL_eval_root = NULL; |
3280af22 | 3411 | PL_curcop = &PL_compiling; |
26c9400e | 3412 | if ((saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL)) |
faef0170 | 3413 | PL_in_eval |= EVAL_KEEPERR; |
ab69dbc2 RGS |
3414 | else |
3415 | CLEAR_ERRSV(); | |
27e90453 | 3416 | |
377b5421 DM |
3417 | SAVEHINTS(); |
3418 | if (clear_hints) { | |
3419 | PL_hints = 0; | |
3420 | hv_clear(GvHV(PL_hintgv)); | |
3421 | } | |
3422 | else { | |
3423 | PL_hints = saveop->op_private & OPpEVAL_COPHH | |
3424 | ? oldcurcop->cop_hints : saveop->op_targ; | |
4f3e2518 DM |
3425 | |
3426 | /* making 'use re eval' not be in scope when compiling the | |
3427 | * qr/mabye_has_runtime_code_block/ ensures that we don't get | |
3428 | * infinite recursion when S_has_runtime_code() gives a false | |
3429 | * positive: the second time round, HINT_RE_EVAL isn't set so we | |
3430 | * don't bother calling S_has_runtime_code() */ | |
3431 | if (PL_in_eval & EVAL_RE_REPARSING) | |
3432 | PL_hints &= ~HINT_RE_EVAL; | |
3433 | ||
377b5421 DM |
3434 | if (hh) { |
3435 | /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */ | |
3436 | SvREFCNT_dec(GvHV(PL_hintgv)); | |
3437 | GvHV(PL_hintgv) = hh; | |
3438 | } | |
3439 | } | |
3440 | SAVECOMPILEWARNINGS(); | |
3441 | if (clear_hints) { | |
3442 | if (PL_dowarn & G_WARN_ALL_ON) | |
3443 | PL_compiling.cop_warnings = pWARN_ALL ; | |
3444 | else if (PL_dowarn & G_WARN_ALL_OFF) | |
3445 | PL_compiling.cop_warnings = pWARN_NONE ; | |
3446 | else | |
3447 | PL_compiling.cop_warnings = pWARN_STD ; | |
3448 | } | |
3449 | else { | |
3450 | PL_compiling.cop_warnings = | |