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