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 | 35 | #include "perl.h" |
9f601cf3 | 36 | #include "feature.h" |
a0d0e21e | 37 | |
5b6f7443 DM |
38 | #define dopopto_cursub() \ |
39 | (PL_curstackinfo->si_cxsubix >= 0 \ | |
40 | ? PL_curstackinfo->si_cxsubix \ | |
41 | : dopoptosub_at(cxstack, cxstack_ix)) | |
42 | ||
94fcd414 NC |
43 | #define dopoptosub(plop) dopoptosub_at(cxstack, (plop)) |
44 | ||
a0d0e21e LW |
45 | PP(pp_wantarray) |
46 | { | |
39644a26 | 47 | dSP; |
a0d0e21e | 48 | I32 cxix; |
93f0bc49 | 49 | const PERL_CONTEXT *cx; |
a0d0e21e LW |
50 | EXTEND(SP, 1); |
51 | ||
93f0bc49 | 52 | if (PL_op->op_private & OPpOFFBYONE) { |
1604cfb0 | 53 | if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF; |
93f0bc49 FC |
54 | } |
55 | else { | |
5b6f7443 | 56 | cxix = dopopto_cursub(); |
93f0bc49 | 57 | if (cxix < 0) |
1604cfb0 | 58 | RETPUSHUNDEF; |
93f0bc49 FC |
59 | cx = &cxstack[cxix]; |
60 | } | |
a0d0e21e | 61 | |
93f0bc49 | 62 | switch (cx->blk_gimme) { |
eb7e169e | 63 | case G_LIST: |
1604cfb0 | 64 | RETPUSHYES; |
54310121 | 65 | case G_SCALAR: |
1604cfb0 | 66 | RETPUSHNO; |
54310121 | 67 | default: |
1604cfb0 | 68 | RETPUSHUNDEF; |
54310121 | 69 | } |
a0d0e21e LW |
70 | } |
71 | ||
2cd61cdb IZ |
72 | PP(pp_regcreset) |
73 | { | |
0b4182de | 74 | TAINT_NOT; |
2cd61cdb IZ |
75 | return NORMAL; |
76 | } | |
77 | ||
b3eb6a9b GS |
78 | PP(pp_regcomp) |
79 | { | |
39644a26 | 80 | dSP; |
cf171702 | 81 | PMOP *pm = cPMOPx(cLOGOP->op_other); |
9f141731 | 82 | SV **args; |
df787a7b | 83 | int nargs; |
84679df5 | 84 | REGEXP *re = NULL; |
9f141731 DM |
85 | REGEXP *new_re; |
86 | const regexp_engine *eng; | |
dbc200c5 | 87 | bool is_bare_re= FALSE; |
bfed75c6 | 88 | |
df787a7b | 89 | if (PL_op->op_flags & OPf_STACKED) { |
1604cfb0 MS |
90 | dMARK; |
91 | nargs = SP - MARK; | |
92 | args = ++MARK; | |
df787a7b DM |
93 | } |
94 | else { | |
1604cfb0 MS |
95 | nargs = 1; |
96 | args = SP; | |
df787a7b DM |
97 | } |
98 | ||
4b5a0d1c | 99 | /* prevent recompiling under /o and ithreads. */ |
3db8f154 | 100 | #if defined(USE_ITHREADS) |
131b3ad0 | 101 | if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) { |
1604cfb0 MS |
102 | SP = args-1; |
103 | RETURN; | |
131b3ad0 | 104 | } |
513629ba | 105 | #endif |
d4b87e75 | 106 | |
9f141731 DM |
107 | re = PM_GETRE(pm); |
108 | assert (re != (REGEXP*) &PL_sv_undef); | |
109 | eng = re ? RX_ENGINE(re) : current_re_engine(); | |
110 | ||
3c13cae6 | 111 | new_re = (eng->op_comp |
1604cfb0 MS |
112 | ? eng->op_comp |
113 | : &Perl_re_op_compile | |
114 | )(aTHX_ args, nargs, pm->op_code_list, eng, re, | |
115 | &is_bare_re, | |
dbc200c5 | 116 | (pm->op_pmflags & RXf_PMf_FLAGCOPYMASK), |
1604cfb0 MS |
117 | pm->op_pmflags | |
118 | (PL_op->op_flags & OPf_SPECIAL ? PMf_USE_RE_EVAL : 0)); | |
dbc200c5 | 119 | |
346d3070 | 120 | if (pm->op_pmflags & PMf_HAS_CV) |
1604cfb0 MS |
121 | ReANY(new_re)->qr_anoncv |
122 | = (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ)); | |
9f141731 DM |
123 | |
124 | if (is_bare_re) { | |
1604cfb0 MS |
125 | REGEXP *tmp; |
126 | /* The match's LHS's get-magic might need to access this op's regexp | |
127 | (e.g. $' =~ /$re/ while foo; see bug 70764). So we must call | |
128 | get-magic now before we replace the regexp. Hopefully this hack can | |
129 | be replaced with the approach described at | |
130 | http://www.nntp.perl.org/group/perl.perl5.porters/2007/03/msg122415.html | |
131 | some day. */ | |
132 | if (pm->op_type == OP_MATCH) { | |
133 | SV *lhs; | |
134 | const bool was_tainted = TAINT_get; | |
135 | if (pm->op_flags & OPf_STACKED) | |
136 | lhs = args[-1]; | |
137 | else if (pm->op_targ) | |
138 | lhs = PAD_SV(pm->op_targ); | |
139 | else lhs = DEFSV; | |
140 | SvGETMAGIC(lhs); | |
141 | /* Restore the previous value of PL_tainted (which may have been | |
142 | modified by get-magic), to avoid incorrectly setting the | |
143 | RXf_TAINTED flag with RX_TAINT_on further down. */ | |
144 | TAINT_set(was_tainted); | |
dc6d7f5c | 145 | #ifdef NO_TAINT_SUPPORT |
9a9b5ec9 DM |
146 | PERL_UNUSED_VAR(was_tainted); |
147 | #endif | |
1604cfb0 MS |
148 | } |
149 | tmp = reg_temp_copy(NULL, new_re); | |
150 | ReREFCNT_dec(new_re); | |
151 | new_re = tmp; | |
df787a7b | 152 | } |
dbc200c5 | 153 | |
9f141731 | 154 | if (re != new_re) { |
1604cfb0 MS |
155 | ReREFCNT_dec(re); |
156 | PM_SETRE(pm, new_re); | |
c277df42 | 157 | } |
d4b87e75 | 158 | |
dbc200c5 | 159 | |
d48c660d DM |
160 | assert(TAINTING_get || !TAINT_get); |
161 | if (TAINT_get) { | |
1604cfb0 | 162 | SvTAINTED_on((SV*)new_re); |
284167a5 | 163 | RX_TAINT_on(new_re); |
72311751 | 164 | } |
72311751 | 165 | |
5585e758 YO |
166 | /* handle the empty pattern */ |
167 | if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm) { | |
168 | if (PL_curpm == PL_reg_curpm) { | |
3cb4cde3 TC |
169 | if (PL_curpm_under && PL_curpm_under == PL_reg_curpm) { |
170 | Perl_croak(aTHX_ "Infinite recursion via empty pattern"); | |
5585e758 | 171 | } |
5585e758 YO |
172 | } |
173 | } | |
174 | ||
c737faaf YO |
175 | #if !defined(USE_ITHREADS) |
176 | /* can't change the optree at runtime either */ | |
177 | /* PMf_KEEP is handled differently under threads to avoid these problems */ | |
a0d0e21e | 178 | if (pm->op_pmflags & PMf_KEEP) { |
1604cfb0 | 179 | cLOGOP->op_first->op_next = PL_op->op_next; |
a0d0e21e | 180 | } |
c737faaf | 181 | #endif |
9f141731 | 182 | |
df787a7b | 183 | SP = args-1; |
a0d0e21e LW |
184 | RETURN; |
185 | } | |
186 | ||
9f141731 | 187 | |
a0d0e21e LW |
188 | PP(pp_substcont) |
189 | { | |
39644a26 | 190 | dSP; |
4ebe6e95 | 191 | PERL_CONTEXT *cx = CX_CUR(); |
cf171702 | 192 | PMOP * const pm = cPMOPx(cLOGOP->op_other); |
eb578fdb KW |
193 | SV * const dstr = cx->sb_dstr; |
194 | char *s = cx->sb_s; | |
195 | char *m = cx->sb_m; | |
a0d0e21e | 196 | char *orig = cx->sb_orig; |
eb578fdb | 197 | REGEXP * const rx = cx->sb_rx; |
c445ea15 | 198 | SV *nsv = NULL; |
988e6e7e | 199 | REGEXP *old = PM_GETRE(pm); |
f410a211 NC |
200 | |
201 | PERL_ASYNC_CHECK(); | |
202 | ||
988e6e7e | 203 | if(old != rx) { |
1604cfb0 MS |
204 | if(old) |
205 | ReREFCNT_dec(old); | |
206 | PM_SETRE(pm,ReREFCNT_inc(rx)); | |
d8f2cf8a AB |
207 | } |
208 | ||
d9f97599 | 209 | rxres_restore(&cx->sb_rxres, rx); |
c90c0ff4 | 210 | |
a0d0e21e | 211 | if (cx->sb_iters++) { |
1604cfb0 MS |
212 | const SSize_t saviters = cx->sb_iters; |
213 | if (cx->sb_iters > cx->sb_maxiters) | |
214 | DIE(aTHX_ "Substitution loop"); | |
a0d0e21e | 215 | |
1604cfb0 | 216 | SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */ |
447ee134 | 217 | |
017533b2 | 218 | /* See "how taint works": pp_subst() in pp_hot.c */ |
1604cfb0 MS |
219 | sv_catsv_nomg(dstr, POPs); |
220 | if (UNLIKELY(TAINT_get)) | |
221 | cx->sb_rxtainted |= SUBST_TAINT_REPL; | |
222 | if (CxONCE(cx) || s < orig || | |
03c83e26 | 223 | !CALLREGEXEC(rx, s, cx->sb_strend, orig, |
1604cfb0 | 224 | (s == m), cx->sb_targ, NULL, |
d5e7783a | 225 | (REXEC_IGNOREPOS|REXEC_NOT_FIRST|REXEC_FAIL_ON_UNDERFLOW))) |
1604cfb0 MS |
226 | { |
227 | SV *targ = cx->sb_targ; | |
228 | ||
229 | assert(cx->sb_strend >= s); | |
230 | if(cx->sb_strend > s) { | |
231 | if (DO_UTF8(dstr) && !SvUTF8(targ)) | |
232 | sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv); | |
233 | else | |
234 | sv_catpvn_nomg(dstr, s, cx->sb_strend - s); | |
235 | } | |
236 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ | |
237 | cx->sb_rxtainted |= SUBST_TAINT_PAT; | |
238 | ||
239 | if (pm->op_pmflags & PMf_NONDESTRUCT) { | |
240 | PUSHs(dstr); | |
241 | /* From here on down we're using the copy, and leaving the | |
242 | original untouched. */ | |
243 | targ = dstr; | |
244 | } | |
245 | else { | |
246 | SV_CHECK_THINKFIRST_COW_DROP(targ); | |
247 | if (isGV(targ)) Perl_croak_no_modify(); | |
248 | SvPV_free(targ); | |
249 | SvPV_set(targ, SvPVX(dstr)); | |
250 | SvCUR_set(targ, SvCUR(dstr)); | |
251 | SvLEN_set(targ, SvLEN(dstr)); | |
252 | if (DO_UTF8(dstr)) | |
253 | SvUTF8_on(targ); | |
254 | SvPV_set(dstr, NULL); | |
255 | ||
256 | PL_tainted = 0; | |
257 | mPUSHi(saviters - 1); | |
258 | ||
259 | (void)SvPOK_only_UTF8(targ); | |
260 | } | |
261 | ||
262 | /* update the taint state of various variables in | |
263 | * preparation for final exit. | |
017533b2 | 264 | * See "how taint works": pp_subst() in pp_hot.c */ |
1604cfb0 MS |
265 | if (TAINTING_get) { |
266 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || | |
267 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
268 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
269 | ) | |
270 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ | |
271 | ||
272 | if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET) | |
273 | && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT)) | |
274 | ) | |
275 | SvTAINTED_on(TOPs); /* taint return value */ | |
276 | /* needed for mg_set below */ | |
277 | TAINT_set( | |
284167a5 | 278 | cBOOL(cx->sb_rxtainted & |
1604cfb0 | 279 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)) |
284167a5 | 280 | ); |
4a69216a DM |
281 | |
282 | /* sv_magic(), when adding magic (e.g.taint magic), also | |
283 | * recalculates any pos() magic, converting any byte offset | |
284 | * to utf8 offset. Make sure pos() is reset before this | |
285 | * happens rather than using the now invalid value (since | |
286 | * we've just replaced targ's pvx buffer with the | |
287 | * potentially shorter dstr buffer). Normally (i.e. in | |
288 | * non-taint cases), pos() gets removed a few lines later | |
289 | * with the SvSETMAGIC(). | |
290 | */ | |
291 | { | |
292 | MAGIC *mg; | |
293 | mg = mg_find_mglob(targ); | |
294 | if (mg) { | |
295 | MgBYTEPOS_set(mg, targ, SvPVX(targ), -1); | |
296 | } | |
297 | } | |
298 | ||
1604cfb0 MS |
299 | SvTAINT(TARG); |
300 | } | |
301 | /* PL_tainted must be correctly set for this mg_set */ | |
302 | SvSETMAGIC(TARG); | |
303 | TAINT_NOT; | |
80d88db0 | 304 | |
1604cfb0 MS |
305 | CX_LEAVE_SCOPE(cx); |
306 | CX_POPSUBST(cx); | |
5da525e9 | 307 | CX_POP(cx); |
80d88db0 | 308 | |
1604cfb0 MS |
309 | PERL_ASYNC_CHECK(); |
310 | RETURNOP(pm->op_next); | |
311 | NOT_REACHED; /* NOTREACHED */ | |
312 | } | |
313 | cx->sb_iters = saviters; | |
a0d0e21e | 314 | } |
07bc277f | 315 | if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) { |
1604cfb0 MS |
316 | m = s; |
317 | s = orig; | |
6502e081 | 318 | assert(!RX_SUBOFFSET(rx)); |
1604cfb0 MS |
319 | cx->sb_orig = orig = RX_SUBBEG(rx); |
320 | s = orig + (m - s); | |
321 | cx->sb_strend = s + (cx->sb_strend - m); | |
a0d0e21e | 322 | } |
07bc277f | 323 | cx->sb_m = m = RX_OFFS(rx)[0].start + orig; |
db79b45b | 324 | if (m > s) { |
1604cfb0 MS |
325 | if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ)) |
326 | sv_catpvn_nomg_utf8_upgrade(dstr, s, m - s, nsv); | |
327 | else | |
328 | sv_catpvn_nomg(dstr, s, m-s); | |
db79b45b | 329 | } |
07bc277f | 330 | cx->sb_s = RX_OFFS(rx)[0].end + orig; |
084916e3 | 331 | { /* Update the pos() information. */ |
1604cfb0 MS |
332 | SV * const sv |
333 | = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ; | |
334 | MAGIC *mg; | |
a911bb25 DM |
335 | |
336 | /* the string being matched against may no longer be a string, | |
337 | * e.g. $_=0; s/.../$_++/ge */ | |
338 | ||
339 | if (!SvPOK(sv)) | |
340 | SvPV_force_nomg_nolen(sv); | |
341 | ||
1604cfb0 MS |
342 | if (!(mg = mg_find_mglob(sv))) { |
343 | mg = sv_magicext_mglob(sv); | |
344 | } | |
345 | MgBYTEPOS_set(mg, sv, SvPVX(sv), m - orig); | |
084916e3 | 346 | } |
988e6e7e | 347 | if (old != rx) |
1604cfb0 | 348 | (void)ReREFCNT_inc(rx); |
a3815e44 | 349 | /* update the taint state of various variables in preparation |
ef07e810 | 350 | * for calling the code block. |
017533b2 | 351 | * See "how taint works": pp_subst() in pp_hot.c */ |
284167a5 | 352 | if (TAINTING_get) { |
1604cfb0 MS |
353 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
354 | cx->sb_rxtainted |= SUBST_TAINT_PAT; | |
20be6587 | 355 | |
1604cfb0 MS |
356 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || |
357 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
358 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
359 | ) | |
360 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ | |
20be6587 | 361 | |
1604cfb0 MS |
362 | if (cx->sb_iters > 1 && (cx->sb_rxtainted & |
363 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))) | |
364 | SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT) | |
365 | ? cx->sb_dstr : cx->sb_targ); | |
366 | TAINT_NOT; | |
20be6587 | 367 | } |
d9f97599 | 368 | rxres_save(&cx->sb_rxres, rx); |
af9838cc | 369 | PL_curpm = pm; |
29f2e912 | 370 | RETURNOP(pm->op_pmstashstartu.op_pmreplstart); |
a0d0e21e LW |
371 | } |
372 | ||
c90c0ff4 | 373 | void |
864dbfa3 | 374 | Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx) |
c90c0ff4 | 375 | { |
376 | UV *p = (UV*)*rsp; | |
377 | U32 i; | |
7918f24d NC |
378 | |
379 | PERL_ARGS_ASSERT_RXRES_SAVE; | |
96a5add6 | 380 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 381 | |
07bc277f | 382 | if (!p || p[1] < RX_NPARENS(rx)) { |
db2c6cb3 | 383 | #ifdef PERL_ANY_COW |
1604cfb0 | 384 | i = 7 + (RX_NPARENS(rx)+1) * 2; |
ed252734 | 385 | #else |
1604cfb0 | 386 | i = 6 + (RX_NPARENS(rx)+1) * 2; |
ed252734 | 387 | #endif |
1604cfb0 MS |
388 | if (!p) |
389 | Newx(p, i, UV); | |
390 | else | |
391 | Renew(p, i, UV); | |
392 | *rsp = (void*)p; | |
c90c0ff4 | 393 | } |
394 | ||
5eabab15 DM |
395 | /* what (if anything) to free on croak */ |
396 | *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL); | |
cf93c79d | 397 | RX_MATCH_COPIED_off(rx); |
6c31ff74 | 398 | *p++ = RX_NPARENS(rx); |
c90c0ff4 | 399 | |
db2c6cb3 | 400 | #ifdef PERL_ANY_COW |
bdd9a1b1 NC |
401 | *p++ = PTR2UV(RX_SAVED_COPY(rx)); |
402 | RX_SAVED_COPY(rx) = NULL; | |
ed252734 NC |
403 | #endif |
404 | ||
07bc277f NC |
405 | *p++ = PTR2UV(RX_SUBBEG(rx)); |
406 | *p++ = (UV)RX_SUBLEN(rx); | |
6502e081 DM |
407 | *p++ = (UV)RX_SUBOFFSET(rx); |
408 | *p++ = (UV)RX_SUBCOFFSET(rx); | |
07bc277f | 409 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
1604cfb0 MS |
410 | *p++ = (UV)RX_OFFS(rx)[i].start; |
411 | *p++ = (UV)RX_OFFS(rx)[i].end; | |
c90c0ff4 | 412 | } |
413 | } | |
414 | ||
9c105995 NC |
415 | static void |
416 | S_rxres_restore(pTHX_ void **rsp, REGEXP *rx) | |
c90c0ff4 | 417 | { |
418 | UV *p = (UV*)*rsp; | |
419 | U32 i; | |
7918f24d NC |
420 | |
421 | PERL_ARGS_ASSERT_RXRES_RESTORE; | |
96a5add6 | 422 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 423 | |
ed252734 | 424 | RX_MATCH_COPY_FREE(rx); |
cf93c79d | 425 | RX_MATCH_COPIED_set(rx, *p); |
c90c0ff4 | 426 | *p++ = 0; |
6c31ff74 | 427 | RX_NPARENS(rx) = *p++; |
c90c0ff4 | 428 | |
db2c6cb3 | 429 | #ifdef PERL_ANY_COW |
bdd9a1b1 | 430 | if (RX_SAVED_COPY(rx)) |
1604cfb0 | 431 | SvREFCNT_dec (RX_SAVED_COPY(rx)); |
bdd9a1b1 | 432 | RX_SAVED_COPY(rx) = INT2PTR(SV*,*p); |
ed252734 NC |
433 | *p++ = 0; |
434 | #endif | |
435 | ||
07bc277f NC |
436 | RX_SUBBEG(rx) = INT2PTR(char*,*p++); |
437 | RX_SUBLEN(rx) = (I32)(*p++); | |
6502e081 DM |
438 | RX_SUBOFFSET(rx) = (I32)*p++; |
439 | RX_SUBCOFFSET(rx) = (I32)*p++; | |
07bc277f | 440 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
1604cfb0 MS |
441 | RX_OFFS(rx)[i].start = (I32)(*p++); |
442 | RX_OFFS(rx)[i].end = (I32)(*p++); | |
c90c0ff4 | 443 | } |
444 | } | |
445 | ||
9c105995 NC |
446 | static void |
447 | S_rxres_free(pTHX_ void **rsp) | |
c90c0ff4 | 448 | { |
44f8325f | 449 | UV * const p = (UV*)*rsp; |
7918f24d NC |
450 | |
451 | PERL_ARGS_ASSERT_RXRES_FREE; | |
96a5add6 | 452 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 453 | |
454 | if (p) { | |
1604cfb0 | 455 | void *tmp = INT2PTR(char*,*p); |
6c31ff74 | 456 | #ifdef PERL_POISON |
db2c6cb3 | 457 | #ifdef PERL_ANY_COW |
1604cfb0 | 458 | U32 i = 9 + p[1] * 2; |
94010e71 | 459 | #else |
1604cfb0 | 460 | U32 i = 8 + p[1] * 2; |
94010e71 | 461 | #endif |
6c31ff74 NC |
462 | #endif |
463 | ||
db2c6cb3 | 464 | #ifdef PERL_ANY_COW |
6c31ff74 | 465 | SvREFCNT_dec (INT2PTR(SV*,p[2])); |
ed252734 | 466 | #endif |
6c31ff74 NC |
467 | #ifdef PERL_POISON |
468 | PoisonFree(p, i, sizeof(UV)); | |
469 | #endif | |
470 | ||
1604cfb0 MS |
471 | Safefree(tmp); |
472 | Safefree(p); | |
473 | *rsp = NULL; | |
c90c0ff4 | 474 | } |
475 | } | |
476 | ||
a701009a DM |
477 | #define FORM_NUM_BLANK (1<<30) |
478 | #define FORM_NUM_POINT (1<<29) | |
479 | ||
a0d0e21e LW |
480 | PP(pp_formline) |
481 | { | |
20b7effb | 482 | dSP; dMARK; dORIGMARK; |
eb578fdb | 483 | SV * const tmpForm = *++MARK; |
086b26f3 | 484 | SV *formsv; /* contains text of original format */ |
eb578fdb KW |
485 | U32 *fpc; /* format ops program counter */ |
486 | char *t; /* current append position in target string */ | |
086b26f3 | 487 | const char *f; /* current position in format string */ |
eb578fdb KW |
488 | I32 arg; |
489 | SV *sv = NULL; /* current item */ | |
086b26f3 | 490 | const char *item = NULL;/* string value of current item */ |
9b4bdfd4 DM |
491 | I32 itemsize = 0; /* length (chars) of item, possibly truncated */ |
492 | I32 itembytes = 0; /* as itemsize, but length in bytes */ | |
086b26f3 DM |
493 | I32 fieldsize = 0; /* width of current field */ |
494 | I32 lines = 0; /* number of lines that have been output */ | |
495 | bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */ | |
496 | const char *chophere = NULL; /* where to chop current item */ | |
f5ada144 | 497 | STRLEN linemark = 0; /* pos of start of line in output */ |
65202027 | 498 | NV value; |
086b26f3 | 499 | bool gotsome = FALSE; /* seen at least one non-blank item on this line */ |
9b4bdfd4 | 500 | STRLEN len; /* length of current sv */ |
732f08da | 501 | STRLEN linemax; /* estimate of output size in bytes */ |
1bd51a4c IH |
502 | bool item_is_utf8 = FALSE; |
503 | bool targ_is_utf8 = FALSE; | |
bd7084a6 | 504 | const char *fmt; |
74e0ddf7 | 505 | MAGIC *mg = NULL; |
4ff700b9 DM |
506 | U8 *source; /* source of bytes to append */ |
507 | STRLEN to_copy; /* how may bytes to append */ | |
ea60cfe8 | 508 | char trans; /* what chars to translate */ |
92ff660b | 509 | bool copied_form = FALSE; /* have we duplicated the form? */ |
74e0ddf7 | 510 | |
3808a683 | 511 | mg = doparseform(tmpForm); |
a0d0e21e | 512 | |
74e0ddf7 | 513 | fpc = (U32*)mg->mg_ptr; |
3808a683 DM |
514 | /* the actual string the format was compiled from. |
515 | * with overload etc, this may not match tmpForm */ | |
516 | formsv = mg->mg_obj; | |
517 | ||
74e0ddf7 | 518 | |
3280af22 | 519 | SvPV_force(PL_formtarget, len); |
3808a683 | 520 | if (SvTAINTED(tmpForm) || SvTAINTED(formsv)) |
1604cfb0 | 521 | SvTAINTED_on(PL_formtarget); |
1bd51a4c | 522 | if (DO_UTF8(PL_formtarget)) |
1604cfb0 | 523 | targ_is_utf8 = TRUE; |
732f08da DM |
524 | /* this is an initial estimate of how much output buffer space |
525 | * to allocate. It may be exceeded later */ | |
526 | linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1); | |
527 | t = SvGROW(PL_formtarget, len + linemax + 1); | |
26e935cf | 528 | /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */ |
a0d0e21e | 529 | t += len; |
3808a683 | 530 | f = SvPV_const(formsv, len); |
a0d0e21e LW |
531 | |
532 | for (;;) { | |
1604cfb0 MS |
533 | DEBUG_f( { |
534 | const char *name = "???"; | |
535 | arg = -1; | |
536 | switch (*fpc) { | |
537 | case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break; | |
538 | case FF_BLANK: arg = fpc[1]; name = "BLANK"; break; | |
539 | case FF_SKIP: arg = fpc[1]; name = "SKIP"; break; | |
540 | case FF_FETCH: arg = fpc[1]; name = "FETCH"; break; | |
541 | case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break; | |
542 | ||
543 | case FF_CHECKNL: name = "CHECKNL"; break; | |
544 | case FF_CHECKCHOP: name = "CHECKCHOP"; break; | |
545 | case FF_SPACE: name = "SPACE"; break; | |
546 | case FF_HALFSPACE: name = "HALFSPACE"; break; | |
547 | case FF_ITEM: name = "ITEM"; break; | |
548 | case FF_CHOP: name = "CHOP"; break; | |
549 | case FF_LINEGLOB: name = "LINEGLOB"; break; | |
550 | case FF_NEWLINE: name = "NEWLINE"; break; | |
551 | case FF_MORE: name = "MORE"; break; | |
552 | case FF_LINEMARK: name = "LINEMARK"; break; | |
553 | case FF_END: name = "END"; break; | |
554 | case FF_0DECIMAL: name = "0DECIMAL"; break; | |
555 | case FF_LINESNGL: name = "LINESNGL"; break; | |
556 | } | |
557 | if (arg >= 0) | |
558 | PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg); | |
559 | else | |
560 | PerlIO_printf(Perl_debug_log, "%-16s\n", name); | |
561 | } ); | |
562 | switch (*fpc++) { | |
563 | case FF_LINEMARK: /* start (or end) of a line */ | |
564 | linemark = t - SvPVX(PL_formtarget); | |
565 | lines++; | |
566 | gotsome = FALSE; | |
567 | break; | |
568 | ||
569 | case FF_LITERAL: /* append <arg> literal chars */ | |
570 | to_copy = *fpc++; | |
571 | source = (U8 *)f; | |
572 | f += to_copy; | |
573 | trans = '~'; | |
82943faa KW |
574 | item_is_utf8 = (targ_is_utf8) |
575 | ? cBOOL(DO_UTF8(formsv)) | |
576 | : cBOOL(SvUTF8(formsv)); | |
1604cfb0 MS |
577 | goto append; |
578 | ||
579 | case FF_SKIP: /* skip <arg> chars in format */ | |
580 | f += *fpc++; | |
581 | break; | |
582 | ||
583 | case FF_FETCH: /* get next item and set field size to <arg> */ | |
584 | arg = *fpc++; | |
585 | f += arg; | |
586 | fieldsize = arg; | |
587 | ||
588 | if (MARK < SP) | |
589 | sv = *++MARK; | |
590 | else { | |
591 | sv = &PL_sv_no; | |
592 | Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments"); | |
593 | } | |
594 | if (SvTAINTED(sv)) | |
595 | SvTAINTED_on(PL_formtarget); | |
596 | break; | |
597 | ||
598 | case FF_CHECKNL: /* find max len of item (up to \n) that fits field */ | |
599 | { | |
600 | const char *s = item = SvPV_const(sv, len); | |
601 | const char *send = s + len; | |
9b4bdfd4 DM |
602 | |
603 | itemsize = 0; | |
1604cfb0 | 604 | item_is_utf8 = DO_UTF8(sv); |
9b4bdfd4 DM |
605 | while (s < send) { |
606 | if (!isCNTRL(*s)) | |
607 | gotsome = TRUE; | |
608 | else if (*s == '\n') | |
609 | break; | |
610 | ||
611 | if (item_is_utf8) | |
612 | s += UTF8SKIP(s); | |
613 | else | |
614 | s++; | |
615 | itemsize++; | |
616 | if (itemsize == fieldsize) | |
617 | break; | |
618 | } | |
619 | itembytes = s - item; | |
62db6ea5 | 620 | chophere = s; |
1604cfb0 MS |
621 | break; |
622 | } | |
a0d0e21e | 623 | |
1604cfb0 MS |
624 | case FF_CHECKCHOP: /* like CHECKNL, but up to highest split point */ |
625 | { | |
626 | const char *s = item = SvPV_const(sv, len); | |
627 | const char *send = s + len; | |
9b4bdfd4 DM |
628 | I32 size = 0; |
629 | ||
630 | chophere = NULL; | |
1604cfb0 | 631 | item_is_utf8 = DO_UTF8(sv); |
9b4bdfd4 DM |
632 | while (s < send) { |
633 | /* look for a legal split position */ | |
634 | if (isSPACE(*s)) { | |
635 | if (*s == '\r') { | |
636 | chophere = s; | |
637 | itemsize = size; | |
638 | break; | |
639 | } | |
640 | if (chopspace) { | |
641 | /* provisional split point */ | |
642 | chophere = s; | |
643 | itemsize = size; | |
644 | } | |
645 | /* we delay testing fieldsize until after we've | |
646 | * processed the possible split char directly | |
647 | * following the last field char; so if fieldsize=3 | |
648 | * and item="a b cdef", we consume "a b", not "a". | |
649 | * Ditto further down. | |
650 | */ | |
651 | if (size == fieldsize) | |
652 | break; | |
653 | } | |
654 | else { | |
5d7d8ffc DM |
655 | if (size == fieldsize) |
656 | break; | |
9b4bdfd4 DM |
657 | if (strchr(PL_chopset, *s)) { |
658 | /* provisional split point */ | |
659 | /* for a non-space split char, we include | |
660 | * the split char; hence the '+1' */ | |
661 | chophere = s + 1; | |
5d7d8ffc | 662 | itemsize = size + 1; |
9b4bdfd4 | 663 | } |
9b4bdfd4 DM |
664 | if (!isCNTRL(*s)) |
665 | gotsome = TRUE; | |
666 | } | |
667 | ||
668 | if (item_is_utf8) | |
669 | s += UTF8SKIP(s); | |
670 | else | |
077dbbf3 | 671 | s++; |
9b4bdfd4 DM |
672 | size++; |
673 | } | |
674 | if (!chophere || s == send) { | |
675 | chophere = s; | |
676 | itemsize = size; | |
677 | } | |
678 | itembytes = chophere - item; | |
679 | ||
1604cfb0 MS |
680 | break; |
681 | } | |
682 | ||
683 | case FF_SPACE: /* append padding space (diff of field, item size) */ | |
684 | arg = fieldsize - itemsize; | |
685 | if (arg) { | |
686 | fieldsize -= arg; | |
687 | while (arg-- > 0) | |
688 | *t++ = ' '; | |
689 | } | |
690 | break; | |
691 | ||
692 | case FF_HALFSPACE: /* like FF_SPACE, but only append half as many */ | |
693 | arg = fieldsize - itemsize; | |
694 | if (arg) { | |
695 | arg /= 2; | |
696 | fieldsize -= arg; | |
697 | while (arg-- > 0) | |
698 | *t++ = ' '; | |
699 | } | |
700 | break; | |
701 | ||
702 | case FF_ITEM: /* append a text item, while blanking ctrl chars */ | |
703 | to_copy = itembytes; | |
704 | source = (U8 *)item; | |
705 | trans = 1; | |
706 | goto append; | |
707 | ||
708 | case FF_CHOP: /* (for ^*) chop the current item */ | |
709 | if (sv != &PL_sv_no) { | |
710 | const char *s = chophere; | |
86191aed TC |
711 | if (!copied_form && |
712 | ((sv == tmpForm || SvSMAGICAL(sv)) | |
713 | || (SvGMAGICAL(tmpForm) && !sv_only_taint_gmagic(tmpForm))) ) { | |
714 | /* sv and tmpForm are either the same SV, or magic might allow modification | |
715 | of tmpForm when sv is modified, so copy */ | |
716 | SV *newformsv = sv_mortalcopy(formsv); | |
717 | U32 *new_compiled; | |
718 | ||
719 | f = SvPV_nolen(newformsv) + (f - SvPV_nolen(formsv)); | |
720 | Newx(new_compiled, mg->mg_len / sizeof(U32), U32); | |
721 | memcpy(new_compiled, mg->mg_ptr, mg->mg_len); | |
722 | SAVEFREEPV(new_compiled); | |
723 | fpc = new_compiled + (fpc - (U32*)mg->mg_ptr); | |
724 | formsv = newformsv; | |
725 | ||
92ff660b | 726 | copied_form = TRUE; |
86191aed | 727 | } |
1604cfb0 MS |
728 | if (chopspace) { |
729 | while (isSPACE(*s)) | |
730 | s++; | |
731 | } | |
9b4bdfd4 DM |
732 | if (SvPOKp(sv)) |
733 | sv_chop(sv,s); | |
734 | else | |
735 | /* tied, overloaded or similar strangeness. | |
736 | * Do it the hard way */ | |
737 | sv_setpvn(sv, s, len - (s-item)); | |
1604cfb0 MS |
738 | SvSETMAGIC(sv); |
739 | break; | |
740 | } | |
2165bd23 | 741 | /* FALLTHROUGH */ |
a0d0e21e | 742 | |
1604cfb0 MS |
743 | case FF_LINESNGL: /* process ^* */ |
744 | chopspace = 0; | |
c67159e1 | 745 | /* FALLTHROUGH */ |
4a73dc0b | 746 | |
1604cfb0 MS |
747 | case FF_LINEGLOB: /* process @* */ |
748 | { | |
749 | const bool oneline = fpc[-1] == FF_LINESNGL; | |
750 | const char *s = item = SvPV_const(sv, len); | |
751 | const char *const send = s + len; | |
752 | ||
753 | item_is_utf8 = DO_UTF8(sv); | |
754 | chophere = s + len; | |
755 | if (!len) | |
756 | break; | |
757 | trans = 0; | |
758 | gotsome = TRUE; | |
759 | source = (U8 *) s; | |
760 | to_copy = len; | |
761 | while (s < send) { | |
762 | if (*s++ == '\n') { | |
763 | if (oneline) { | |
764 | to_copy = s - item - 1; | |
765 | chophere = s; | |
766 | break; | |
767 | } else { | |
768 | if (s == send) { | |
769 | to_copy--; | |
770 | } else | |
771 | lines++; | |
772 | } | |
773 | } | |
774 | } | |
775 | } | |
776 | ||
777 | append: | |
778 | /* append to_copy bytes from source to PL_formstring. | |
779 | * item_is_utf8 implies source is utf8. | |
780 | * if trans, translate certain characters during the copy */ | |
781 | { | |
782 | U8 *tmp = NULL; | |
783 | STRLEN grow = 0; | |
784 | ||
785 | SvCUR_set(PL_formtarget, | |
786 | t - SvPVX_const(PL_formtarget)); | |
787 | ||
788 | if (targ_is_utf8 && !item_is_utf8) { | |
789 | source = tmp = bytes_to_utf8(source, &to_copy); | |
732f08da | 790 | grow = to_copy; |
1604cfb0 MS |
791 | } else { |
792 | if (item_is_utf8 && !targ_is_utf8) { | |
793 | U8 *s; | |
794 | /* Upgrade targ to UTF8, and then we reduce it to | |
795 | a problem we have a simple solution for. | |
796 | Don't need get magic. */ | |
797 | sv_utf8_upgrade_nomg(PL_formtarget); | |
798 | targ_is_utf8 = TRUE; | |
799 | /* re-calculate linemark */ | |
800 | s = (U8*)SvPVX(PL_formtarget); | |
801 | /* the bytes we initially allocated to append the | |
802 | * whole line may have been gobbled up during the | |
803 | * upgrade, so allocate a whole new line's worth | |
804 | * for safety */ | |
805 | grow = linemax; | |
806 | while (linemark--) | |
807 | s += UTF8_SAFE_SKIP(s, | |
40c725d1 | 808 | (U8 *) SvEND(PL_formtarget)); |
1604cfb0 MS |
809 | linemark = s - (U8*)SvPVX(PL_formtarget); |
810 | } | |
811 | /* Easy. They agree. */ | |
812 | assert (item_is_utf8 == targ_is_utf8); | |
813 | } | |
814 | if (!trans) | |
815 | /* @* and ^* are the only things that can exceed | |
816 | * the linemax, so grow by the output size, plus | |
817 | * a whole new form's worth in case of any further | |
818 | * output */ | |
819 | grow = linemax + to_copy; | |
820 | if (grow) | |
821 | SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1); | |
822 | t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget); | |
823 | ||
824 | Copy(source, t, to_copy, char); | |
825 | if (trans) { | |
826 | /* blank out ~ or control chars, depending on trans. | |
827 | * works on bytes not chars, so relies on not | |
828 | * matching utf8 continuation bytes */ | |
829 | U8 *s = (U8*)t; | |
830 | U8 *send = s + to_copy; | |
831 | while (s < send) { | |
832 | const int ch = *s; | |
833 | if (trans == '~' ? (ch == '~') : isCNTRL(ch)) | |
834 | *s = ' '; | |
835 | s++; | |
836 | } | |
837 | } | |
838 | ||
839 | t += to_copy; | |
840 | SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy); | |
841 | if (tmp) | |
842 | Safefree(tmp); | |
843 | break; | |
844 | } | |
845 | ||
846 | case FF_0DECIMAL: /* like FF_DECIMAL but for 0### */ | |
847 | arg = *fpc++; | |
848 | fmt = (const char *) | |
849 | ((arg & FORM_NUM_POINT) ? "%#0*.*" NVff : "%0*.*" NVff); | |
850 | goto ff_dec; | |
851 | ||
852 | case FF_DECIMAL: /* do @##, ^##, where <arg>=(precision|flags) */ | |
853 | arg = *fpc++; | |
854 | fmt = (const char *) | |
855 | ((arg & FORM_NUM_POINT) ? "%#*.*" NVff : "%*.*" NVff); | |
856 | ff_dec: | |
857 | /* If the field is marked with ^ and the value is undefined, | |
858 | blank it out. */ | |
859 | if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) { | |
860 | arg = fieldsize; | |
861 | while (arg--) | |
862 | *t++ = ' '; | |
863 | break; | |
864 | } | |
865 | gotsome = TRUE; | |
866 | value = SvNV(sv); | |
867 | /* overflow evidence */ | |
868 | if (num_overflow(value, fieldsize, arg)) { | |
869 | arg = fieldsize; | |
870 | while (arg--) | |
871 | *t++ = '#'; | |
872 | break; | |
873 | } | |
874 | /* Formats aren't yet marked for locales, so assume "yes". */ | |
875 | { | |
e8549682 JH |
876 | Size_t max = SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)); |
877 | int len; | |
67d796ae KW |
878 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
879 | STORE_LC_NUMERIC_SET_TO_NEEDED(); | |
51f14a05 | 880 | arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
a4eca1d4 JH |
881 | #ifdef USE_QUADMATH |
882 | { | |
a4eca1d4 | 883 | int len; |
065d0f13 | 884 | if (!quadmath_format_valid(fmt)) |
a4eca1d4 | 885 | Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", fmt); |
065d0f13 | 886 | len = quadmath_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value); |
a4eca1d4 | 887 | if (len == -1) |
065d0f13 | 888 | Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", fmt); |
a4eca1d4 JH |
889 | } |
890 | #else | |
b587c0e8 | 891 | /* we generate fmt ourselves so it is safe */ |
7347ee54 | 892 | GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); |
e8549682 | 893 | len = my_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value); |
7347ee54 | 894 | GCC_DIAG_RESTORE_STMT; |
a4eca1d4 JH |
895 | #endif |
896 | PERL_MY_SNPRINTF_POST_GUARD(len, max); | |
a2287a13 | 897 | RESTORE_LC_NUMERIC(); |
1604cfb0 MS |
898 | } |
899 | t += fieldsize; | |
900 | break; | |
901 | ||
902 | case FF_NEWLINE: /* delete trailing spaces, then append \n */ | |
903 | f++; | |
904 | while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ; | |
905 | t++; | |
906 | *t++ = '\n'; | |
907 | break; | |
908 | ||
909 | case FF_BLANK: /* for arg==0: do '~'; for arg>0 : do '~~' */ | |
910 | arg = *fpc++; | |
911 | if (gotsome) { | |
912 | if (arg) { /* repeat until fields exhausted? */ | |
913 | fpc--; | |
914 | goto end; | |
915 | } | |
916 | } | |
917 | else { | |
918 | t = SvPVX(PL_formtarget) + linemark; | |
919 | lines--; | |
920 | } | |
921 | break; | |
922 | ||
923 | case FF_MORE: /* replace long end of string with '...' */ | |
924 | { | |
925 | const char *s = chophere; | |
926 | const char *send = item + len; | |
927 | if (chopspace) { | |
928 | while (isSPACE(*s) && (s < send)) | |
929 | s++; | |
930 | } | |
931 | if (s < send) { | |
932 | char *s1; | |
933 | arg = fieldsize - itemsize; | |
934 | if (arg) { | |
935 | fieldsize -= arg; | |
936 | while (arg-- > 0) | |
937 | *t++ = ' '; | |
938 | } | |
939 | s1 = t - 3; | |
940 | if (strBEGINs(s1," ")) { | |
941 | while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1])) | |
942 | s1--; | |
943 | } | |
944 | *s1++ = '.'; | |
945 | *s1++ = '.'; | |
946 | *s1++ = '.'; | |
947 | } | |
948 | break; | |
949 | } | |
950 | ||
951 | case FF_END: /* tidy up, then return */ | |
952 | end: | |
953 | assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget)); | |
954 | *t = '\0'; | |
955 | SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget)); | |
956 | if (targ_is_utf8) | |
957 | SvUTF8_on(PL_formtarget); | |
958 | FmLINES(PL_formtarget) += lines; | |
959 | SP = ORIGMARK; | |
960 | if (fpc[-1] == FF_BLANK) | |
961 | RETURNOP(cLISTOP->op_first); | |
962 | else | |
963 | RETPUSHYES; | |
964 | } | |
a0d0e21e LW |
965 | } |
966 | } | |
967 | ||
10088f56 | 968 | /* also used for: pp_mapstart() */ |
a0d0e21e LW |
969 | PP(pp_grepstart) |
970 | { | |
20b7effb | 971 | dSP; |
a0d0e21e LW |
972 | SV *src; |
973 | ||
6cae08a8 | 974 | if (PL_stack_base + TOPMARK == SP) { |
1604cfb0 MS |
975 | (void)POPMARK; |
976 | if (GIMME_V == G_SCALAR) | |
977 | XPUSHs(&PL_sv_zero); | |
978 | RETURNOP(PL_op->op_next->op_next); | |
a0d0e21e | 979 | } |
6cae08a8 | 980 | PL_stack_sp = PL_stack_base + TOPMARK + 1; |
897d3989 NC |
981 | Perl_pp_pushmark(aTHX); /* push dst */ |
982 | Perl_pp_pushmark(aTHX); /* push src */ | |
d343c3ef | 983 | ENTER_with_name("grep"); /* enter outer scope */ |
a0d0e21e LW |
984 | |
985 | SAVETMPS; | |
ffd49c98 | 986 | SAVE_DEFSV; |
d343c3ef | 987 | ENTER_with_name("grep_item"); /* enter inner scope */ |
7766f137 | 988 | SAVEVPTR(PL_curpm); |
a0d0e21e | 989 | |
6cae08a8 | 990 | src = PL_stack_base[TOPMARK]; |
60779a30 | 991 | if (SvPADTMP(src)) { |
1604cfb0 MS |
992 | src = PL_stack_base[TOPMARK] = sv_mortalcopy(src); |
993 | PL_tmps_floor++; | |
a0ed822e | 994 | } |
a0d0e21e | 995 | SvTEMP_off(src); |
ffd49c98 | 996 | DEFSV_set(src); |
a0d0e21e LW |
997 | |
998 | PUTBACK; | |
533c011a | 999 | if (PL_op->op_type == OP_MAPSTART) |
1604cfb0 | 1000 | Perl_pp_pushmark(aTHX); /* push top */ |
008ea6fc | 1001 | return cLOGOPx(PL_op->op_next)->op_other; |
a0d0e21e LW |
1002 | } |
1003 | ||
d6b338e1 PE |
1004 | /* pp_grepwhile() lives in pp_hot.c */ |
1005 | ||
a0d0e21e LW |
1006 | PP(pp_mapwhile) |
1007 | { | |
20b7effb | 1008 | dSP; |
1c23e2bd | 1009 | const U8 gimme = GIMME_V; |
6cae08a8 | 1010 | I32 items = (SP - PL_stack_base) - TOPMARK; /* how many new items */ |
a0d0e21e LW |
1011 | I32 count; |
1012 | I32 shift; | |
1013 | SV** src; | |
ac27b0f5 | 1014 | SV** dst; |
a0d0e21e | 1015 | |
544f3153 | 1016 | /* first, move source pointer to the next item in the source list */ |
3280af22 | 1017 | ++PL_markstack_ptr[-1]; |
544f3153 GS |
1018 | |
1019 | /* if there are new items, push them into the destination list */ | |
4c90a460 | 1020 | if (items && gimme != G_VOID) { |
1604cfb0 MS |
1021 | /* might need to make room back there first */ |
1022 | if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) { | |
1023 | /* XXX this implementation is very pessimal because the stack | |
1024 | * is repeatedly extended for every set of items. Is possible | |
1025 | * to do this without any stack extension or copying at all | |
1026 | * by maintaining a separate list over which the map iterates | |
1027 | * (like foreach does). --gsar */ | |
1028 | ||
1029 | /* everything in the stack after the destination list moves | |
1030 | * towards the end the stack by the amount of room needed */ | |
1031 | shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]); | |
1032 | ||
1033 | /* items to shift up (accounting for the moved source pointer) */ | |
1034 | count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1); | |
1035 | ||
1036 | /* This optimization is by Ben Tilly and it does | |
1037 | * things differently from what Sarathy (gsar) | |
1038 | * is describing. The downside of this optimization is | |
1039 | * that leaves "holes" (uninitialized and hopefully unused areas) | |
1040 | * to the Perl stack, but on the other hand this | |
1041 | * shouldn't be a problem. If Sarathy's idea gets | |
1042 | * implemented, this optimization should become | |
1043 | * irrelevant. --jhi */ | |
18ef8bea BT |
1044 | if (shift < count) |
1045 | shift = count; /* Avoid shifting too often --Ben Tilly */ | |
bfed75c6 | 1046 | |
1604cfb0 MS |
1047 | EXTEND(SP,shift); |
1048 | src = SP; | |
1049 | dst = (SP += shift); | |
1050 | PL_markstack_ptr[-1] += shift; | |
1051 | *PL_markstack_ptr += shift; | |
1052 | while (count--) | |
1053 | *dst-- = *src--; | |
1054 | } | |
1055 | /* copy the new items down to the destination list */ | |
1056 | dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1; | |
eb7e169e | 1057 | if (gimme == G_LIST) { |
1604cfb0 MS |
1058 | /* add returned items to the collection (making mortal copies |
1059 | * if necessary), then clear the current temps stack frame | |
1060 | * *except* for those items. We do this splicing the items | |
1061 | * into the start of the tmps frame (so some items may be on | |
1062 | * the tmps stack twice), then moving PL_tmps_floor above | |
1063 | * them, then freeing the frame. That way, the only tmps that | |
1064 | * accumulate over iterations are the return values for map. | |
1065 | * We have to do to this way so that everything gets correctly | |
1066 | * freed if we die during the map. | |
1067 | */ | |
1068 | I32 tmpsbase; | |
1069 | I32 i = items; | |
1070 | /* make space for the slice */ | |
1071 | EXTEND_MORTAL(items); | |
1072 | tmpsbase = PL_tmps_floor + 1; | |
1073 | Move(PL_tmps_stack + tmpsbase, | |
1074 | PL_tmps_stack + tmpsbase + items, | |
1075 | PL_tmps_ix - PL_tmps_floor, | |
1076 | SV*); | |
1077 | PL_tmps_ix += items; | |
1078 | ||
1079 | while (i-- > 0) { | |
1080 | SV *sv = POPs; | |
1081 | if (!SvTEMP(sv)) | |
1082 | sv = sv_mortalcopy(sv); | |
1083 | *dst-- = sv; | |
1084 | PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv); | |
1085 | } | |
1086 | /* clear the stack frame except for the items */ | |
1087 | PL_tmps_floor += items; | |
1088 | FREETMPS; | |
1089 | /* FREETMPS may have cleared the TEMP flag on some of the items */ | |
1090 | i = items; | |
1091 | while (i-- > 0) | |
1092 | SvTEMP_on(PL_tmps_stack[--tmpsbase]); | |
1093 | } | |
1094 | else { | |
1095 | /* scalar context: we don't care about which values map returns | |
1096 | * (we use undef here). And so we certainly don't want to do mortal | |
1097 | * copies of meaningless values. */ | |
1098 | while (items-- > 0) { | |
1099 | (void)POPs; | |
1100 | *dst-- = &PL_sv_undef; | |
1101 | } | |
1102 | FREETMPS; | |
1103 | } | |
a0d0e21e | 1104 | } |
b2a2a901 | 1105 | else { |
1604cfb0 | 1106 | FREETMPS; |
b2a2a901 | 1107 | } |
d343c3ef | 1108 | LEAVE_with_name("grep_item"); /* exit inner scope */ |
a0d0e21e LW |
1109 | |
1110 | /* All done yet? */ | |
6cae08a8 | 1111 | if (PL_markstack_ptr[-1] > TOPMARK) { |
a0d0e21e | 1112 | |
1604cfb0 MS |
1113 | (void)POPMARK; /* pop top */ |
1114 | LEAVE_with_name("grep"); /* exit outer scope */ | |
1115 | (void)POPMARK; /* pop src */ | |
1116 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; | |
1117 | (void)POPMARK; /* pop dst */ | |
1118 | SP = PL_stack_base + POPMARK; /* pop original mark */ | |
1119 | if (gimme == G_SCALAR) { | |
1120 | dTARGET; | |
1121 | XPUSHi(items); | |
1122 | } | |
eb7e169e | 1123 | else if (gimme == G_LIST) |
1604cfb0 MS |
1124 | SP += items; |
1125 | RETURN; | |
a0d0e21e LW |
1126 | } |
1127 | else { | |
1604cfb0 | 1128 | SV *src; |
a0d0e21e | 1129 | |
1604cfb0 MS |
1130 | ENTER_with_name("grep_item"); /* enter inner scope */ |
1131 | SAVEVPTR(PL_curpm); | |
a0d0e21e | 1132 | |
1604cfb0 MS |
1133 | /* set $_ to the new source item */ |
1134 | src = PL_stack_base[PL_markstack_ptr[-1]]; | |
1135 | if (SvPADTMP(src)) { | |
60779a30 DM |
1136 | src = sv_mortalcopy(src); |
1137 | } | |
1604cfb0 MS |
1138 | SvTEMP_off(src); |
1139 | DEFSV_set(src); | |
a0d0e21e | 1140 | |
1604cfb0 | 1141 | RETURNOP(cLOGOP->op_other); |
a0d0e21e LW |
1142 | } |
1143 | } | |
1144 | ||
a0d0e21e LW |
1145 | /* Range stuff. */ |
1146 | ||
1147 | PP(pp_range) | |
1148 | { | |
f4c975aa | 1149 | dTARG; |
eb7e169e | 1150 | if (GIMME_V == G_LIST) |
1604cfb0 | 1151 | return NORMAL; |
f4c975aa DM |
1152 | GETTARGET; |
1153 | if (SvTRUE_NN(targ)) | |
1604cfb0 | 1154 | return cLOGOP->op_other; |
538573f7 | 1155 | else |
1604cfb0 | 1156 | return NORMAL; |
a0d0e21e LW |
1157 | } |
1158 | ||
1159 | PP(pp_flip) | |
1160 | { | |
39644a26 | 1161 | dSP; |
a0d0e21e | 1162 | |
eb7e169e | 1163 | if (GIMME_V == G_LIST) { |
008ea6fc | 1164 | RETURNOP(cLOGOPx(cUNOP->op_first)->op_other); |
a0d0e21e LW |
1165 | } |
1166 | else { | |
1604cfb0 MS |
1167 | dTOPss; |
1168 | SV * const targ = PAD_SV(PL_op->op_targ); | |
1169 | int flip = 0; | |
1170 | ||
1171 | if (PL_op->op_private & OPpFLIP_LINENUM) { | |
1172 | if (GvIO(PL_last_in_gv)) { | |
1173 | flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); | |
1174 | } | |
1175 | else { | |
1176 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); | |
1177 | if (gv && GvSV(gv)) | |
1178 | flip = SvIV(sv) == SvIV(GvSV(gv)); | |
1179 | } | |
1180 | } else { | |
1181 | flip = SvTRUE_NN(sv); | |
1182 | } | |
1183 | if (flip) { | |
1184 | sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1); | |
1185 | if (PL_op->op_flags & OPf_SPECIAL) { | |
1186 | sv_setiv(targ, 1); | |
1187 | SETs(targ); | |
1188 | RETURN; | |
1189 | } | |
1190 | else { | |
1191 | sv_setiv(targ, 0); | |
1192 | SP--; | |
008ea6fc | 1193 | RETURNOP(cLOGOPx(cUNOP->op_first)->op_other); |
1604cfb0 MS |
1194 | } |
1195 | } | |
9aa3b533 | 1196 | SvPVCLEAR(TARG); |
1604cfb0 MS |
1197 | SETs(targ); |
1198 | RETURN; | |
a0d0e21e LW |
1199 | } |
1200 | } | |
1201 | ||
8e9bbdb9 | 1202 | /* This code tries to decide if "$left .. $right" should use the |
d1bc97fe H |
1203 | magical string increment, or if the range is numeric. Initially, |
1204 | an exception was made for *any* string beginning with "0" (see | |
1205 | [#18165], AMS 20021031), but now that is only applied when the | |
1206 | string's length is also >1 - see the rules now documented in | |
1207 | perlop [#133695] */ | |
8e9bbdb9 RGS |
1208 | |
1209 | #define RANGE_IS_NUMERIC(left,right) ( \ | |
1604cfb0 MS |
1210 | SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \ |
1211 | SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \ | |
1212 | (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \ | |
d1bc97fe H |
1213 | looks_like_number(left)) && SvPOKp(left) \ |
1214 | && !(*SvPVX_const(left) == '0' && SvCUR(left)>1 ) )) \ | |
e0ab1c0e | 1215 | && (!SvOK(right) || looks_like_number(right)))) |
8e9bbdb9 | 1216 | |
a0d0e21e LW |
1217 | PP(pp_flop) |
1218 | { | |
20b7effb | 1219 | dSP; |
a0d0e21e | 1220 | |
eb7e169e | 1221 | if (GIMME_V == G_LIST) { |
1604cfb0 MS |
1222 | dPOPPOPssrl; |
1223 | ||
1224 | SvGETMAGIC(left); | |
1225 | SvGETMAGIC(right); | |
1226 | ||
1227 | if (RANGE_IS_NUMERIC(left,right)) { | |
1228 | IV i, j, n; | |
1229 | if ((SvOK(left) && !SvIOK(left) && SvNV_nomg(left) < IV_MIN) || | |
1230 | (SvOK(right) && (SvIOK(right) | |
1231 | ? SvIsUV(right) && SvUV(right) > IV_MAX | |
1232 | : SvNV_nomg(right) > (NV) IV_MAX))) | |
1233 | DIE(aTHX_ "Range iterator outside integer range"); | |
1234 | i = SvIV_nomg(left); | |
1235 | j = SvIV_nomg(right); | |
1236 | if (j >= i) { | |
b262c4c9 JH |
1237 | /* Dance carefully around signed max. */ |
1238 | bool overflow = (i <= 0 && j > SSize_t_MAX + i - 1); | |
1239 | if (!overflow) { | |
1240 | n = j - i + 1; | |
1241 | /* The wraparound of signed integers is undefined | |
1242 | * behavior, but here we aim for count >=1, and | |
1243 | * negative count is just wrong. */ | |
a1e27170 TC |
1244 | if (n < 1 |
1245 | #if IVSIZE > Size_t_size | |
1246 | || n > SSize_t_MAX | |
1247 | #endif | |
1248 | ) | |
b262c4c9 JH |
1249 | overflow = TRUE; |
1250 | } | |
1251 | if (overflow) | |
1252 | Perl_croak(aTHX_ "Out of memory during list extend"); | |
1604cfb0 MS |
1253 | EXTEND_MORTAL(n); |
1254 | EXTEND(SP, n); | |
1255 | } | |
1256 | else | |
1257 | n = 0; | |
1258 | while (n--) { | |
1259 | SV * const sv = sv_2mortal(newSViv(i)); | |
1260 | PUSHs(sv); | |
fc01cab4 DM |
1261 | if (n) /* avoid incrementing above IV_MAX */ |
1262 | i++; | |
1604cfb0 MS |
1263 | } |
1264 | } | |
1265 | else { | |
1266 | STRLEN len, llen; | |
1267 | const char * const lpv = SvPV_nomg_const(left, llen); | |
1268 | const char * const tmps = SvPV_nomg_const(right, len); | |
1269 | ||
1270 | SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP); | |
d6c970c7 AC |
1271 | if (DO_UTF8(right) && IN_UNI_8_BIT) |
1272 | len = sv_len_utf8_nomg(right); | |
1604cfb0 MS |
1273 | while (!SvNIOKp(sv) && SvCUR(sv) <= len) { |
1274 | XPUSHs(sv); | |
1275 | if (strEQ(SvPVX_const(sv),tmps)) | |
1276 | break; | |
1277 | sv = sv_2mortal(newSVsv(sv)); | |
1278 | sv_inc(sv); | |
1279 | } | |
1280 | } | |
a0d0e21e LW |
1281 | } |
1282 | else { | |
1604cfb0 MS |
1283 | dTOPss; |
1284 | SV * const targ = PAD_SV(cUNOP->op_first->op_targ); | |
1285 | int flop = 0; | |
1286 | sv_inc(targ); | |
1287 | ||
1288 | if (PL_op->op_private & OPpFLIP_LINENUM) { | |
1289 | if (GvIO(PL_last_in_gv)) { | |
1290 | flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); | |
1291 | } | |
1292 | else { | |
1293 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); | |
1294 | if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv)); | |
1295 | } | |
1296 | } | |
1297 | else { | |
1298 | flop = SvTRUE_NN(sv); | |
1299 | } | |
1300 | ||
1301 | if (flop) { | |
867a530c | 1302 | sv_setiv(PAD_SV(cUNOPx(cUNOP->op_first)->op_first->op_targ), 0); |
1604cfb0 MS |
1303 | sv_catpvs(targ, "E0"); |
1304 | } | |
1305 | SETs(targ); | |
a0d0e21e LW |
1306 | } |
1307 | ||
1308 | RETURN; | |
1309 | } | |
1310 | ||
1311 | /* Control. */ | |
1312 | ||
27da23d5 | 1313 | static const char * const context_name[] = { |
515afda2 | 1314 | "pseudo-block", |
7896dde7 | 1315 | NULL, /* CXt_WHEN never actually needs "block" */ |
76753e7f | 1316 | NULL, /* CXt_BLOCK never actually needs "block" */ |
7896dde7 | 1317 | NULL, /* CXt_GIVEN never actually needs "block" */ |
76753e7f | 1318 | NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */ |
76753e7f | 1319 | NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */ |
93661e56 DM |
1320 | NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */ |
1321 | NULL, /* CXt_LOOP_LIST never actually needs "loop" */ | |
1322 | NULL, /* CXt_LOOP_ARY never actually needs "loop" */ | |
515afda2 | 1323 | "subroutine", |
76753e7f | 1324 | "format", |
515afda2 | 1325 | "eval", |
515afda2 | 1326 | "substitution", |
f79e2ff9 | 1327 | "defer block", |
515afda2 NC |
1328 | }; |
1329 | ||
76e3520e | 1330 | STATIC I32 |
5db1eb8d | 1331 | S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags) |
a0d0e21e | 1332 | { |
eb578fdb | 1333 | I32 i; |
a0d0e21e | 1334 | |
7918f24d NC |
1335 | PERL_ARGS_ASSERT_DOPOPTOLABEL; |
1336 | ||
a0d0e21e | 1337 | for (i = cxstack_ix; i >= 0; i--) { |
1604cfb0 MS |
1338 | const PERL_CONTEXT * const cx = &cxstack[i]; |
1339 | switch (CxTYPE(cx)) { | |
a1325b90 PE |
1340 | case CXt_EVAL: |
1341 | if(CxTRY(cx)) | |
1342 | continue; | |
1343 | /* FALLTHROUGH */ | |
1604cfb0 MS |
1344 | case CXt_SUBST: |
1345 | case CXt_SUB: | |
1346 | case CXt_FORMAT: | |
1604cfb0 MS |
1347 | case CXt_NULL: |
1348 | /* diag_listed_as: Exiting subroutine via %s */ | |
1349 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", | |
1350 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); | |
1351 | if (CxTYPE(cx) == CXt_NULL) /* sort BLOCK */ | |
1352 | return -1; | |
1353 | break; | |
1354 | case CXt_LOOP_PLAIN: | |
1355 | case CXt_LOOP_LAZYIV: | |
1356 | case CXt_LOOP_LAZYSV: | |
1357 | case CXt_LOOP_LIST: | |
1358 | case CXt_LOOP_ARY: | |
1359 | { | |
5db1eb8d BF |
1360 | STRLEN cx_label_len = 0; |
1361 | U32 cx_label_flags = 0; | |
1604cfb0 MS |
1362 | const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags); |
1363 | if (!cx_label || !( | |
5db1eb8d BF |
1364 | ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ? |
1365 | (flags & SVf_UTF8) | |
1366 | ? (bytes_cmp_utf8( | |
1367 | (const U8*)cx_label, cx_label_len, | |
1368 | (const U8*)label, len) == 0) | |
1369 | : (bytes_cmp_utf8( | |
1370 | (const U8*)label, len, | |
1371 | (const U8*)cx_label, cx_label_len) == 0) | |
eade7155 BF |
1372 | : (len == cx_label_len && ((cx_label == label) |
1373 | || memEQ(cx_label, label, len))) )) { | |
1604cfb0 MS |
1374 | DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n", |
1375 | (long)i, cx_label)); | |
1376 | continue; | |
1377 | } | |
1378 | DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label)); | |
1379 | return i; | |
1380 | } | |
1381 | } | |
a0d0e21e LW |
1382 | } |
1383 | return i; | |
1384 | } | |
1385 | ||
71494cd1 KW |
1386 | /* |
1387 | =for apidoc_section $callback | |
1388 | =for apidoc dowantarray | |
1389 | ||
1390 | Implements the deprecated L<perlapi/C<GIMME>>. | |
0d863452 | 1391 | |
71494cd1 KW |
1392 | =cut |
1393 | */ | |
0d863452 | 1394 | |
1c23e2bd | 1395 | U8 |
864dbfa3 | 1396 | Perl_dowantarray(pTHX) |
e50aee73 | 1397 | { |
1c23e2bd | 1398 | const U8 gimme = block_gimme(); |
54310121 | 1399 | return (gimme == G_VOID) ? G_SCALAR : gimme; |
1400 | } | |
1401 | ||
5b6f7443 DM |
1402 | /* note that this function has mostly been superseded by Perl_gimme_V */ |
1403 | ||
1c23e2bd | 1404 | U8 |
864dbfa3 | 1405 | Perl_block_gimme(pTHX) |
54310121 | 1406 | { |
5b6f7443 | 1407 | const I32 cxix = dopopto_cursub(); |
a05700a8 | 1408 | U8 gimme; |
e50aee73 | 1409 | if (cxix < 0) |
1604cfb0 | 1410 | return G_VOID; |
e50aee73 | 1411 | |
a05700a8 DM |
1412 | gimme = (cxstack[cxix].blk_gimme & G_WANT); |
1413 | if (!gimme) | |
1604cfb0 | 1414 | Perl_croak(aTHX_ "panic: bad gimme: %d\n", gimme); |
a05700a8 | 1415 | return gimme; |
e50aee73 AD |
1416 | } |
1417 | ||
55c87457 KW |
1418 | /* |
1419 | =for apidoc is_lvalue_sub | |
1420 | ||
1421 | Returns non-zero if the sub calling this function is being called in an lvalue | |
1422 | context. Returns 0 otherwise. | |
1423 | ||
1424 | =cut | |
1425 | */ | |
a05700a8 | 1426 | |
78f9721b SM |
1427 | I32 |
1428 | Perl_is_lvalue_sub(pTHX) | |
1429 | { | |
5b6f7443 | 1430 | const I32 cxix = dopopto_cursub(); |
78f9721b SM |
1431 | assert(cxix >= 0); /* We should only be called from inside subs */ |
1432 | ||
bafb2adc | 1433 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) |
1604cfb0 | 1434 | return CxLVAL(cxstack + cxix); |
78f9721b | 1435 | else |
1604cfb0 | 1436 | return 0; |
78f9721b SM |
1437 | } |
1438 | ||
a73d8813 | 1439 | /* only used by cx_pushsub() */ |
777d9014 FC |
1440 | I32 |
1441 | Perl_was_lvalue_sub(pTHX) | |
1442 | { | |
777d9014 FC |
1443 | const I32 cxix = dopoptosub(cxstack_ix-1); |
1444 | assert(cxix >= 0); /* We should only be called from inside subs */ | |
1445 | ||
1446 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) | |
1604cfb0 | 1447 | return CxLVAL(cxstack + cxix); |
777d9014 | 1448 | else |
1604cfb0 | 1449 | return 0; |
777d9014 FC |
1450 | } |
1451 | ||
76e3520e | 1452 | STATIC I32 |
901017d6 | 1453 | S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock) |
2c375eb9 | 1454 | { |
a0d0e21e | 1455 | I32 i; |
7918f24d NC |
1456 | |
1457 | PERL_ARGS_ASSERT_DOPOPTOSUB_AT; | |
81611534 JH |
1458 | #ifndef DEBUGGING |
1459 | PERL_UNUSED_CONTEXT; | |
1460 | #endif | |
7918f24d | 1461 | |
a0d0e21e | 1462 | for (i = startingblock; i >= 0; i--) { |
1604cfb0 MS |
1463 | const PERL_CONTEXT * const cx = &cxstk[i]; |
1464 | switch (CxTYPE(cx)) { | |
1465 | default: | |
1466 | continue; | |
1467 | case CXt_SUB: | |
5fbe8311 DM |
1468 | /* in sub foo { /(?{...})/ }, foo ends up on the CX stack |
1469 | * twice; the first for the normal foo() call, and the second | |
1470 | * for a faked up re-entry into the sub to execute the | |
1471 | * code block. Hide this faked entry from the world. */ | |
1472 | if (cx->cx_type & CXp_SUB_RE_FAKE) | |
1473 | continue; | |
a1325b90 PE |
1474 | DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); |
1475 | return i; | |
1476 | ||
1604cfb0 | 1477 | case CXt_EVAL: |
a1325b90 PE |
1478 | if (CxTRY(cx)) |
1479 | continue; | |
1480 | DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); | |
1481 | return i; | |
1482 | ||
1604cfb0 MS |
1483 | case CXt_FORMAT: |
1484 | DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); | |
1485 | return i; | |
1486 | } | |
a0d0e21e LW |
1487 | } |
1488 | return i; | |
1489 | } | |
1490 | ||
76e3520e | 1491 | STATIC I32 |
cea2e8a9 | 1492 | S_dopoptoeval(pTHX_ I32 startingblock) |
a0d0e21e LW |
1493 | { |
1494 | I32 i; | |
a0d0e21e | 1495 | for (i = startingblock; i >= 0; i--) { |
1604cfb0 MS |
1496 | const PERL_CONTEXT *cx = &cxstack[i]; |
1497 | switch (CxTYPE(cx)) { | |
1498 | default: | |
1499 | continue; | |
1500 | case CXt_EVAL: | |
1501 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i)); | |
1502 | return i; | |
1503 | } | |
a0d0e21e LW |
1504 | } |
1505 | return i; | |
1506 | } | |
1507 | ||
76e3520e | 1508 | STATIC I32 |
cea2e8a9 | 1509 | S_dopoptoloop(pTHX_ I32 startingblock) |
a0d0e21e LW |
1510 | { |
1511 | I32 i; | |
a0d0e21e | 1512 | for (i = startingblock; i >= 0; i--) { |
1604cfb0 MS |
1513 | const PERL_CONTEXT * const cx = &cxstack[i]; |
1514 | switch (CxTYPE(cx)) { | |
a1325b90 PE |
1515 | case CXt_EVAL: |
1516 | if(CxTRY(cx)) | |
1517 | continue; | |
1518 | /* FALLTHROUGH */ | |
1604cfb0 MS |
1519 | case CXt_SUBST: |
1520 | case CXt_SUB: | |
1521 | case CXt_FORMAT: | |
1604cfb0 MS |
1522 | case CXt_NULL: |
1523 | /* diag_listed_as: Exiting subroutine via %s */ | |
1524 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", | |
1525 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); | |
1526 | if ((CxTYPE(cx)) == CXt_NULL) /* sort BLOCK */ | |
1527 | return -1; | |
1528 | break; | |
1529 | case CXt_LOOP_PLAIN: | |
1530 | case CXt_LOOP_LAZYIV: | |
1531 | case CXt_LOOP_LAZYSV: | |
1532 | case CXt_LOOP_LIST: | |
1533 | case CXt_LOOP_ARY: | |
1534 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i)); | |
1535 | return i; | |
1536 | } | |
a0d0e21e LW |
1537 | } |
1538 | return i; | |
1539 | } | |
1540 | ||
7896dde7 Z |
1541 | /* find the next GIVEN or FOR (with implicit $_) loop context block */ |
1542 | ||
1543 | STATIC I32 | |
1544 | S_dopoptogivenfor(pTHX_ I32 startingblock) | |
1545 | { | |
1546 | I32 i; | |
1547 | for (i = startingblock; i >= 0; i--) { | |
1604cfb0 MS |
1548 | const PERL_CONTEXT *cx = &cxstack[i]; |
1549 | switch (CxTYPE(cx)) { | |
1550 | default: | |
1551 | continue; | |
1552 | case CXt_GIVEN: | |
1553 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found given at cx=%ld)\n", (long)i)); | |
1554 | return i; | |
1555 | case CXt_LOOP_PLAIN: | |
7896dde7 | 1556 | assert(!(cx->cx_type & CXp_FOR_DEF)); |
1604cfb0 MS |
1557 | break; |
1558 | case CXt_LOOP_LAZYIV: | |
1559 | case CXt_LOOP_LAZYSV: | |
1560 | case CXt_LOOP_LIST: | |
1561 | case CXt_LOOP_ARY: | |
7896dde7 | 1562 | if (cx->cx_type & CXp_FOR_DEF) { |
1604cfb0 MS |
1563 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found foreach at cx=%ld)\n", (long)i)); |
1564 | return i; | |
1565 | } | |
1566 | } | |
7896dde7 Z |
1567 | } |
1568 | return i; | |
1569 | } | |
1570 | ||
0d863452 | 1571 | STATIC I32 |
7896dde7 | 1572 | S_dopoptowhen(pTHX_ I32 startingblock) |
0d863452 RH |
1573 | { |
1574 | I32 i; | |
1575 | for (i = startingblock; i >= 0; i--) { | |
1604cfb0 MS |
1576 | const PERL_CONTEXT *cx = &cxstack[i]; |
1577 | switch (CxTYPE(cx)) { | |
1578 | default: | |
1579 | continue; | |
1580 | case CXt_WHEN: | |
1581 | DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i)); | |
1582 | return i; | |
1583 | } | |
0d863452 RH |
1584 | } |
1585 | return i; | |
1586 | } | |
1587 | ||
abaf5d5a | 1588 | /* dounwind(): pop all contexts above (but not including) cxix. |
fc6e609e DM |
1589 | * Note that it clears the savestack frame associated with each popped |
1590 | * context entry, but doesn't free any temps. | |
ed8ff0f3 | 1591 | * It does a cx_popblock() of the last frame that it pops, and leaves |
fc6e609e | 1592 | * cxstack_ix equal to cxix. |
abaf5d5a DM |
1593 | */ |
1594 | ||
a0d0e21e | 1595 | void |
864dbfa3 | 1596 | Perl_dounwind(pTHX_ I32 cxix) |
a0d0e21e | 1597 | { |
f144f1e3 | 1598 | if (!PL_curstackinfo) /* can happen if die during thread cloning */ |
1604cfb0 | 1599 | return; |
f144f1e3 | 1600 | |
a0d0e21e | 1601 | while (cxstack_ix > cxix) { |
4ebe6e95 | 1602 | PERL_CONTEXT *cx = CX_CUR(); |
5e691bc6 | 1603 | |
1604cfb0 MS |
1604 | CX_DEBUG(cx, "UNWIND"); |
1605 | /* Note: we don't need to restore the base context info till the end. */ | |
76d10131 DM |
1606 | |
1607 | CX_LEAVE_SCOPE(cx); | |
1608 | ||
1604cfb0 MS |
1609 | switch (CxTYPE(cx)) { |
1610 | case CXt_SUBST: | |
1611 | CX_POPSUBST(cx); | |
7332835e DM |
1612 | /* CXt_SUBST is not a block context type, so skip the |
1613 | * cx_popblock(cx) below */ | |
1614 | if (cxstack_ix == cxix + 1) { | |
1615 | cxstack_ix--; | |
1616 | return; | |
1617 | } | |
1604cfb0 MS |
1618 | break; |
1619 | case CXt_SUB: | |
1620 | cx_popsub(cx); | |
1621 | break; | |
1622 | case CXt_EVAL: | |
1623 | cx_popeval(cx); | |
1624 | break; | |
1625 | case CXt_LOOP_PLAIN: | |
1626 | case CXt_LOOP_LAZYIV: | |
1627 | case CXt_LOOP_LAZYSV: | |
1628 | case CXt_LOOP_LIST: | |
1629 | case CXt_LOOP_ARY: | |
1630 | cx_poploop(cx); | |
1631 | break; | |
1632 | case CXt_WHEN: | |
1633 | cx_popwhen(cx); | |
1634 | break; | |
1635 | case CXt_GIVEN: | |
1636 | cx_popgiven(cx); | |
1637 | break; | |
1638 | case CXt_BLOCK: | |
1639 | case CXt_NULL: | |
f79e2ff9 | 1640 | case CXt_DEFER: |
f217cb3d | 1641 | /* these two don't have a POPFOO() */ |
1604cfb0 MS |
1642 | break; |
1643 | case CXt_FORMAT: | |
1644 | cx_popformat(cx); | |
1645 | break; | |
1646 | } | |
fc6e609e | 1647 | if (cxstack_ix == cxix + 1) { |
ed8ff0f3 | 1648 | cx_popblock(cx); |
fc6e609e | 1649 | } |
1604cfb0 | 1650 | cxstack_ix--; |
a0d0e21e | 1651 | } |
fc6e609e | 1652 | |
a0d0e21e LW |
1653 | } |
1654 | ||
5a844595 GS |
1655 | void |
1656 | Perl_qerror(pTHX_ SV *err) | |
1657 | { | |
7918f24d NC |
1658 | PERL_ARGS_ASSERT_QERROR; |
1659 | ||
6b2fb389 | 1660 | if (PL_in_eval) { |
1604cfb0 MS |
1661 | if (PL_in_eval & EVAL_KEEPERR) { |
1662 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf, | |
ecad31f0 | 1663 | SVfARG(err)); |
1604cfb0 MS |
1664 | } |
1665 | else | |
1666 | sv_catsv(ERRSV, err); | |
6b2fb389 | 1667 | } |
5a844595 | 1668 | else if (PL_errors) |
1604cfb0 | 1669 | sv_catsv(PL_errors, err); |
5a844595 | 1670 | else |
1604cfb0 | 1671 | Perl_warn(aTHX_ "%" SVf, SVfARG(err)); |
eb54d46f YO |
1672 | |
1673 | if (PL_parser) { | |
1674 | STRLEN len; | |
1675 | char *err_pv = SvPV(err,len); | |
1604cfb0 | 1676 | ++PL_parser->error_count; |
eb54d46f YO |
1677 | if (memBEGINs(err_pv,len,"syntax error")) |
1678 | { | |
1679 | PL_parser->error_count |= PERL_PARSE_IS_SYNTAX_ERROR_FLAG; | |
1680 | } | |
1681 | } | |
5a844595 GS |
1682 | } |
1683 | ||
d308a779 DM |
1684 | |
1685 | ||
06a7bc17 DM |
1686 | /* pop a CXt_EVAL context and in addition, if it was a require then |
1687 | * based on action: | |
1688 | * 0: do nothing extra; | |
1689 | * 1: undef $INC{$name}; croak "$name did not return a true value"; | |
1690 | * 2: delete $INC{$name}; croak "$errsv: Compilation failed in require" | |
1691 | */ | |
d308a779 | 1692 | |
8a1fd305 | 1693 | static void |
06a7bc17 | 1694 | S_pop_eval_context_maybe_croak(pTHX_ PERL_CONTEXT *cx, SV *errsv, int action) |
d308a779 | 1695 | { |
51787acf | 1696 | SV *namesv = NULL; /* init to avoid dumb compiler warning */ |
06a7bc17 DM |
1697 | bool do_croak; |
1698 | ||
1699 | CX_LEAVE_SCOPE(cx); | |
1700 | do_croak = action && (CxOLD_OP_TYPE(cx) == OP_REQUIRE); | |
2a1e0dfe DM |
1701 | if (do_croak) { |
1702 | /* keep namesv alive after cx_popeval() */ | |
1703 | namesv = cx->blk_eval.old_namesv; | |
1704 | cx->blk_eval.old_namesv = NULL; | |
1705 | sv_2mortal(namesv); | |
1706 | } | |
06a7bc17 DM |
1707 | cx_popeval(cx); |
1708 | cx_popblock(cx); | |
1709 | CX_POP(cx); | |
1710 | ||
1711 | if (do_croak) { | |
1712 | const char *fmt; | |
1713 | HV *inc_hv = GvHVn(PL_incgv); | |
d308a779 | 1714 | |
06a7bc17 | 1715 | if (action == 1) { |
bffed14d | 1716 | (void)hv_delete_ent(inc_hv, namesv, G_DISCARD, 0); |
147e3846 | 1717 | fmt = "%" SVf " did not return a true value"; |
06a7bc17 DM |
1718 | errsv = namesv; |
1719 | } | |
1720 | else { | |
bffed14d | 1721 | (void)hv_store_ent(inc_hv, namesv, &PL_sv_undef, 0); |
147e3846 | 1722 | fmt = "%" SVf "Compilation failed in require"; |
06a7bc17 DM |
1723 | if (!errsv) |
1724 | errsv = newSVpvs_flags("Unknown error\n", SVs_TEMP); | |
1725 | } | |
1726 | ||
1727 | Perl_croak(aTHX_ fmt, SVfARG(errsv)); | |
1728 | } | |
d308a779 DM |
1729 | } |
1730 | ||
1731 | ||
8c86f023 DM |
1732 | /* die_unwind(): this is the final destination for the various croak() |
1733 | * functions. If we're in an eval, unwind the context and other stacks | |
1734 | * back to the top-most CXt_EVAL and set $@ to msv; otherwise print msv | |
1735 | * to STDERR and initiate an exit. Note that if the CXt_EVAL popped back | |
1736 | * to is a require the exception will be rethrown, as requires don't | |
1737 | * actually trap exceptions. | |
1738 | */ | |
06a7bc17 | 1739 | |
bb4c52e0 | 1740 | void |
c5df3096 | 1741 | Perl_die_unwind(pTHX_ SV *msv) |
a0d0e21e | 1742 | { |
8c86f023 | 1743 | SV *exceptsv = msv; |
96d9b9cd | 1744 | U8 in_eval = PL_in_eval; |
c5df3096 | 1745 | PERL_ARGS_ASSERT_DIE_UNWIND; |
87582a92 | 1746 | |
96d9b9cd | 1747 | if (in_eval) { |
1604cfb0 | 1748 | I32 cxix; |
a0d0e21e | 1749 | |
b66d79a6 DM |
1750 | /* We need to keep this SV alive through all the stack unwinding |
1751 | * and FREETMPSing below, while ensuing that it doesn't leak | |
1752 | * if we call out to something which then dies (e.g. sub STORE{die} | |
1753 | * when unlocalising a tied var). So we do a dance with | |
1754 | * mortalising and SAVEFREEing. | |
1755 | */ | |
042abef7 N |
1756 | if (PL_phase == PERL_PHASE_DESTRUCT) { |
1757 | exceptsv = sv_mortalcopy(exceptsv); | |
1758 | } else { | |
1759 | exceptsv = sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv)); | |
1760 | } | |
8c86f023 | 1761 | |
1604cfb0 MS |
1762 | /* |
1763 | * Historically, perl used to set ERRSV ($@) early in the die | |
1764 | * process and rely on it not getting clobbered during unwinding. | |
1765 | * That sucked, because it was liable to get clobbered, so the | |
1766 | * setting of ERRSV used to emit the exception from eval{} has | |
1767 | * been moved to much later, after unwinding (see just before | |
1768 | * JMPENV_JUMP below). However, some modules were relying on the | |
1769 | * early setting, by examining $@ during unwinding to use it as | |
1770 | * a flag indicating whether the current unwinding was caused by | |
1771 | * an exception. It was never a reliable flag for that purpose, | |
1772 | * being totally open to false positives even without actual | |
1773 | * clobberage, but was useful enough for production code to | |
1774 | * semantically rely on it. | |
1775 | * | |
1776 | * We'd like to have a proper introspective interface that | |
1777 | * explicitly describes the reason for whatever unwinding | |
1778 | * operations are currently in progress, so that those modules | |
1779 | * work reliably and $@ isn't further overloaded. But we don't | |
1780 | * have one yet. In its absence, as a stopgap measure, ERRSV is | |
1781 | * now *additionally* set here, before unwinding, to serve as the | |
1782 | * (unreliable) flag that it used to. | |
1783 | * | |
1784 | * This behaviour is temporary, and should be removed when a | |
1785 | * proper way to detect exceptional unwinding has been developed. | |
1786 | * As of 2010-12, the authors of modules relying on the hack | |
1787 | * are aware of the issue, because the modules failed on | |
1788 | * perls 5.13.{1..7} which had late setting of $@ without this | |
1789 | * early-setting hack. | |
1790 | */ | |
1791 | if (!(in_eval & EVAL_KEEPERR)) { | |
933e3e63 TC |
1792 | /* remove any read-only/magic from the SV, so we don't |
1793 | get infinite recursion when setting ERRSV */ | |
1794 | SANE_ERRSV(); | |
1604cfb0 | 1795 | sv_setsv_flags(ERRSV, exceptsv, |
8c86f023 | 1796 | (SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL)); |
933e3e63 | 1797 | } |
22a30693 | 1798 | |
1604cfb0 MS |
1799 | if (in_eval & EVAL_KEEPERR) { |
1800 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf, | |
1801 | SVfARG(exceptsv)); | |
1802 | } | |
1803 | ||
1804 | while ((cxix = dopoptoeval(cxstack_ix)) < 0 | |
1805 | && PL_curstackinfo->si_prev) | |
1806 | { | |
1807 | dounwind(-1); | |
1808 | POPSTACK; | |
1809 | } | |
1810 | ||
1811 | if (cxix >= 0) { | |
1812 | PERL_CONTEXT *cx; | |
1813 | SV **oldsp; | |
1c23e2bd | 1814 | U8 gimme; |
1604cfb0 MS |
1815 | JMPENV *restartjmpenv; |
1816 | OP *restartop; | |
a0d0e21e | 1817 | |
1604cfb0 MS |
1818 | if (cxix < cxstack_ix) |
1819 | dounwind(cxix); | |
a0d0e21e | 1820 | |
4ebe6e95 | 1821 | cx = CX_CUR(); |
f7d0774b | 1822 | assert(CxTYPE(cx) == CXt_EVAL); |
e17c1e7c DM |
1823 | |
1824 | /* return false to the caller of eval */ | |
f5ddd604 | 1825 | oldsp = PL_stack_base + cx->blk_oldsp; |
f7d0774b | 1826 | gimme = cx->blk_gimme; |
1604cfb0 MS |
1827 | if (gimme == G_SCALAR) |
1828 | *++oldsp = &PL_sv_undef; | |
1829 | PL_stack_sp = oldsp; | |
f7d0774b | 1830 | |
1604cfb0 MS |
1831 | restartjmpenv = cx->blk_eval.cur_top_env; |
1832 | restartop = cx->blk_eval.retop; | |
b66d79a6 DM |
1833 | |
1834 | /* We need a FREETMPS here to avoid late-called destructors | |
1835 | * clobbering $@ *after* we set it below, e.g. | |
1836 | * sub DESTROY { eval { die "X" } } | |
1837 | * eval { my $x = bless []; die $x = 0, "Y" }; | |
1838 | * is($@, "Y") | |
1839 | * Here the clearing of the $x ref mortalises the anon array, | |
1840 | * which needs to be freed *before* $& is set to "Y", | |
1841 | * otherwise it gets overwritten with "X". | |
1842 | * | |
1843 | * However, the FREETMPS will clobber exceptsv, so preserve it | |
1844 | * on the savestack for now. | |
1845 | */ | |
1846 | SAVEFREESV(SvREFCNT_inc_simple_NN(exceptsv)); | |
1847 | FREETMPS; | |
1848 | /* now we're about to pop the savestack, so re-mortalise it */ | |
1849 | sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv)); | |
1850 | ||
06a7bc17 DM |
1851 | /* Note that unlike pp_entereval, pp_require isn't supposed to |
1852 | * trap errors. So if we're a require, after we pop the | |
1853 | * CXt_EVAL that pp_require pushed, rethrow the error with | |
1854 | * croak(exceptsv). This is all handled by the call below when | |
1855 | * action == 2. | |
1856 | */ | |
1857 | S_pop_eval_context_maybe_croak(aTHX_ cx, exceptsv, 2); | |
a0d0e21e | 1858 | |
1604cfb0 | 1859 | if (!(in_eval & EVAL_KEEPERR)) { |
933e3e63 | 1860 | SANE_ERRSV(); |
1604cfb0 | 1861 | sv_setsv(ERRSV, exceptsv); |
933e3e63 | 1862 | } |
1604cfb0 MS |
1863 | PL_restartjmpenv = restartjmpenv; |
1864 | PL_restartop = restartop; | |
1865 | JMPENV_JUMP(3); | |
1866 | NOT_REACHED; /* NOTREACHED */ | |
1867 | } | |
a0d0e21e | 1868 | } |
87582a92 | 1869 | |
96d9b9cd | 1870 | write_to_stderr(exceptsv); |
f86702cc | 1871 | my_failure_exit(); |
e5964223 | 1872 | NOT_REACHED; /* NOTREACHED */ |
a0d0e21e LW |
1873 | } |
1874 | ||
1875 | PP(pp_xor) | |
1876 | { | |
20b7effb | 1877 | dSP; dPOPTOPssrl; |
f4c975aa | 1878 | if (SvTRUE_NN(left) != SvTRUE_NN(right)) |
1604cfb0 | 1879 | RETSETYES; |
a0d0e21e | 1880 | else |
1604cfb0 | 1881 | RETSETNO; |
a0d0e21e LW |
1882 | } |
1883 | ||
8dff4fc5 | 1884 | /* |
dcccc8ff | 1885 | |
3f620621 | 1886 | =for apidoc_section $CV |
dcccc8ff | 1887 | |
8dff4fc5 BM |
1888 | =for apidoc caller_cx |
1889 | ||
72d33970 | 1890 | The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The |
8dff4fc5 | 1891 | returned C<PERL_CONTEXT> structure can be interrogated to find all the |
72d33970 | 1892 | information returned to Perl by C<caller>. Note that XSUBs don't get a |
8dff4fc5 BM |
1893 | stack frame, so C<caller_cx(0, NULL)> will return information for the |
1894 | immediately-surrounding Perl code. | |
1895 | ||
1896 | This function skips over the automatic calls to C<&DB::sub> made on the | |
72d33970 | 1897 | behalf of the debugger. If the stack frame requested was a sub called by |
8dff4fc5 BM |
1898 | C<DB::sub>, the return value will be the frame for the call to |
1899 | C<DB::sub>, since that has the correct line number/etc. for the call | |
72d33970 | 1900 | site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the |
8dff4fc5 BM |
1901 | frame for the sub call itself. |
1902 | ||
1903 | =cut | |
1904 | */ | |
1905 | ||
1906 | const PERL_CONTEXT * | |
1907 | Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp) | |
a0d0e21e | 1908 | { |
5b6f7443 | 1909 | I32 cxix = dopopto_cursub(); |
eb578fdb KW |
1910 | const PERL_CONTEXT *cx; |
1911 | const PERL_CONTEXT *ccstack = cxstack; | |
901017d6 | 1912 | const PERL_SI *top_si = PL_curstackinfo; |
27d41816 | 1913 | |
a0d0e21e | 1914 | for (;;) { |
1604cfb0 MS |
1915 | /* we may be in a higher stacklevel, so dig down deeper */ |
1916 | while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { | |
1917 | top_si = top_si->si_prev; | |
1918 | ccstack = top_si->si_cxstack; | |
1919 | cxix = dopoptosub_at(ccstack, top_si->si_cxix); | |
1920 | } | |
1921 | if (cxix < 0) | |
1922 | return NULL; | |
1923 | /* caller() should not report the automatic calls to &DB::sub */ | |
1924 | if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 && | |
1925 | ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub)) | |
1926 | count++; | |
1927 | if (!count--) | |
1928 | break; | |
1929 | cxix = dopoptosub_at(ccstack, cxix - 1); | |
a0d0e21e | 1930 | } |
2c375eb9 GS |
1931 | |
1932 | cx = &ccstack[cxix]; | |
8dff4fc5 BM |
1933 | if (dbcxp) *dbcxp = cx; |
1934 | ||
7766f137 | 1935 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
f54cb97a | 1936 | const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1); |
1604cfb0 MS |
1937 | /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the |
1938 | field below is defined for any cx. */ | |
1939 | /* caller() should not report the automatic calls to &DB::sub */ | |
1940 | if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) | |
1941 | cx = &ccstack[dbcxix]; | |
06a5b730 | 1942 | } |
1943 | ||
8dff4fc5 BM |
1944 | return cx; |
1945 | } | |
1946 | ||
1947 | PP(pp_caller) | |
1948 | { | |
8dff4fc5 | 1949 | dSP; |
eb578fdb | 1950 | const PERL_CONTEXT *cx; |
8dff4fc5 | 1951 | const PERL_CONTEXT *dbcx; |
1c23e2bd | 1952 | U8 gimme = GIMME_V; |
d527ce7c | 1953 | const HEK *stash_hek; |
8dff4fc5 | 1954 | I32 count = 0; |
ce0b554b | 1955 | bool has_arg = MAXARG && TOPs; |
25502127 | 1956 | const COP *lcop; |
8dff4fc5 | 1957 | |
ce0b554b FC |
1958 | if (MAXARG) { |
1959 | if (has_arg) | |
1604cfb0 | 1960 | count = POPi; |
ce0b554b FC |
1961 | else (void)POPs; |
1962 | } | |
8dff4fc5 | 1963 | |
82943faa | 1964 | cx = caller_cx(count + cBOOL(PL_op->op_private & OPpOFFBYONE), &dbcx); |
8dff4fc5 | 1965 | if (!cx) { |
eb7e169e | 1966 | if (gimme != G_LIST) { |
1604cfb0 MS |
1967 | EXTEND(SP, 1); |
1968 | RETPUSHUNDEF; | |
1969 | } | |
1970 | RETURN; | |
8dff4fc5 BM |
1971 | } |
1972 | ||
5e691bc6 | 1973 | CX_DEBUG(cx, "CALLER"); |
d0279c7c | 1974 | assert(CopSTASH(cx->blk_oldcop)); |
e7886211 FC |
1975 | stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV |
1976 | ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop)) | |
1977 | : NULL; | |
eb7e169e | 1978 | if (gimme != G_LIST) { |
27d41816 | 1979 | EXTEND(SP, 1); |
1604cfb0 MS |
1980 | if (!stash_hek) |
1981 | PUSHs(&PL_sv_undef); | |
1982 | else { | |
1983 | dTARGET; | |
1984 | sv_sethek(TARG, stash_hek); | |
1985 | PUSHs(TARG); | |
1986 | } | |
1987 | RETURN; | |
a0d0e21e | 1988 | } |
a0d0e21e | 1989 | |
b3ca2e83 | 1990 | EXTEND(SP, 11); |
27d41816 | 1991 | |
d527ce7c | 1992 | if (!stash_hek) |
1604cfb0 | 1993 | PUSHs(&PL_sv_undef); |
d527ce7c | 1994 | else { |
1604cfb0 MS |
1995 | dTARGET; |
1996 | sv_sethek(TARG, stash_hek); | |
1997 | PUSHTARG; | |
d527ce7c | 1998 | } |
6e449a3a | 1999 | mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0)); |
e6dae479 | 2000 | lcop = closest_cop(cx->blk_oldcop, OpSIBLING(cx->blk_oldcop), |
1604cfb0 | 2001 | cx->blk_sub.retop, TRUE); |
25502127 | 2002 | if (!lcop) |
1604cfb0 | 2003 | lcop = cx->blk_oldcop; |
e9e9e546 | 2004 | mPUSHu(CopLINE(lcop)); |
ce0b554b | 2005 | if (!has_arg) |
1604cfb0 | 2006 | RETURN; |
7766f137 | 2007 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
1604cfb0 MS |
2008 | /* So is ccstack[dbcxix]. */ |
2009 | if (CvHASGV(dbcx->blk_sub.cv)) { | |
2010 | PUSHs(cv_name(dbcx->blk_sub.cv, 0, 0)); | |
2011 | PUSHs(boolSV(CxHASARGS(cx))); | |
2012 | } | |
2013 | else { | |
2014 | PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP)); | |
2015 | PUSHs(boolSV(CxHASARGS(cx))); | |
2016 | } | |
a0d0e21e LW |
2017 | } |
2018 | else { | |
1604cfb0 MS |
2019 | PUSHs(newSVpvs_flags("(eval)", SVs_TEMP)); |
2020 | PUSHs(&PL_sv_zero); | |
a0d0e21e | 2021 | } |
1c23e2bd | 2022 | gimme = cx->blk_gimme; |
54310121 | 2023 | if (gimme == G_VOID) |
1604cfb0 | 2024 | PUSHs(&PL_sv_undef); |
54310121 | 2025 | else |
eb7e169e | 2026 | PUSHs(boolSV((gimme & G_WANT) == G_LIST)); |
6b35e009 | 2027 | if (CxTYPE(cx) == CXt_EVAL) { |
1604cfb0 MS |
2028 | /* eval STRING */ |
2029 | if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) { | |
78beb4ca TC |
2030 | SV *cur_text = cx->blk_eval.cur_text; |
2031 | if (SvCUR(cur_text) >= 2) { | |
2032 | PUSHs(newSVpvn_flags(SvPVX(cur_text), SvCUR(cur_text)-2, | |
2033 | SvUTF8(cur_text)|SVs_TEMP)); | |
2034 | } | |
2035 | else { | |
2036 | /* I think this is will always be "", but be sure */ | |
2037 | PUSHs(sv_2mortal(newSVsv(cur_text))); | |
2038 | } | |
2039 | ||
1604cfb0 MS |
2040 | PUSHs(&PL_sv_no); |
2041 | } | |
2042 | /* require */ | |
2043 | else if (cx->blk_eval.old_namesv) { | |
2044 | mPUSHs(newSVsv(cx->blk_eval.old_namesv)); | |
2045 | PUSHs(&PL_sv_yes); | |
2046 | } | |
2047 | /* eval BLOCK (try blocks have old_namesv == 0) */ | |
2048 | else { | |
2049 | PUSHs(&PL_sv_undef); | |
2050 | PUSHs(&PL_sv_undef); | |
2051 | } | |
4633a7c4 | 2052 | } |
a682de96 | 2053 | else { |
1604cfb0 MS |
2054 | PUSHs(&PL_sv_undef); |
2055 | PUSHs(&PL_sv_undef); | |
a682de96 | 2056 | } |
bafb2adc | 2057 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx) |
1604cfb0 | 2058 | && CopSTASH_eq(PL_curcop, PL_debstash)) |
4633a7c4 | 2059 | { |
9513529b | 2060 | /* slot 0 of the pad contains the original @_ */ |
1604cfb0 | 2061 | AV * const ary = MUTABLE_AV(AvARRAY(MUTABLE_AV( |
9513529b DM |
2062 | PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[ |
2063 | cx->blk_sub.olddepth+1]))[0]); | |
1604cfb0 | 2064 | const SSize_t off = AvARRAY(ary) - AvALLOC(ary); |
a0d0e21e | 2065 | |
1604cfb0 | 2066 | Perl_init_dbargs(aTHX); |
a0d0e21e | 2067 | |
1604cfb0 MS |
2068 | if (AvMAX(PL_dbargs) < AvFILLp(ary) + off) |
2069 | av_extend(PL_dbargs, AvFILLp(ary) + off); | |
f14cf363 TC |
2070 | if (AvFILLp(ary) + 1 + off) |
2071 | Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*); | |
1604cfb0 | 2072 | AvFILLp(PL_dbargs) = AvFILLp(ary) + off; |
a0d0e21e | 2073 | } |
6e449a3a | 2074 | mPUSHi(CopHINTS_get(cx->blk_oldcop)); |
e476b1b5 | 2075 | { |
1604cfb0 MS |
2076 | SV * mask ; |
2077 | STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ; | |
114bafba | 2078 | |
1604cfb0 | 2079 | if (old_warnings == pWARN_NONE) |
e476b1b5 | 2080 | mask = newSVpvn(WARN_NONEstring, WARNsize) ; |
1604cfb0 | 2081 | else if (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0) |
f07626ad | 2082 | mask = &PL_sv_undef ; |
ac27b0f5 | 2083 | else if (old_warnings == pWARN_ALL || |
1604cfb0 MS |
2084 | (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) { |
2085 | mask = newSVpvn(WARN_ALLstring, WARNsize) ; | |
2086 | } | |
e476b1b5 | 2087 | else |
72dc9ed5 | 2088 | mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]); |
6e449a3a | 2089 | mPUSHs(mask); |
e476b1b5 | 2090 | } |
b3ca2e83 | 2091 | |
c28fe1ec | 2092 | PUSHs(cx->blk_oldcop->cop_hints_hash ? |
1604cfb0 MS |
2093 | sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0)))) |
2094 | : &PL_sv_undef); | |
a0d0e21e LW |
2095 | RETURN; |
2096 | } | |
2097 | ||
a0d0e21e LW |
2098 | PP(pp_reset) |
2099 | { | |
39644a26 | 2100 | dSP; |
ca826051 FC |
2101 | const char * tmps; |
2102 | STRLEN len = 0; | |
2372d073 DM |
2103 | if (MAXARG < 1 || (!TOPs && !POPs)) { |
2104 | EXTEND(SP, 1); | |
1604cfb0 | 2105 | tmps = NULL, len = 0; |
2372d073 | 2106 | } |
ca826051 | 2107 | else |
1604cfb0 | 2108 | tmps = SvPVx_const(POPs, len); |
ca826051 | 2109 | sv_resetpvn(tmps, len, CopSTASH(PL_curcop)); |
3280af22 | 2110 | PUSHs(&PL_sv_yes); |
a0d0e21e LW |
2111 | RETURN; |
2112 | } | |
2113 | ||
dd2155a4 DM |
2114 | /* like pp_nextstate, but used instead when the debugger is active */ |
2115 | ||
a0d0e21e LW |
2116 | PP(pp_dbstate) |
2117 | { | |
533c011a | 2118 | PL_curcop = (COP*)PL_op; |
a0d0e21e | 2119 | TAINT_NOT; /* Each statement is presumed innocent */ |
4ebe6e95 | 2120 | PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp; |
a0d0e21e LW |
2121 | FREETMPS; |
2122 | ||
f410a211 NC |
2123 | PERL_ASYNC_CHECK(); |
2124 | ||
88df5f01 | 2125 | if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */ |
1604cfb0 | 2126 | || PL_DBsingle_iv || PL_DBsignal_iv || PL_DBtrace_iv) |
a0d0e21e | 2127 | { |
1604cfb0 MS |
2128 | dSP; |
2129 | PERL_CONTEXT *cx; | |
eb7e169e | 2130 | const U8 gimme = G_LIST; |
1604cfb0 MS |
2131 | GV * const gv = PL_DBgv; |
2132 | CV * cv = NULL; | |
432d4561 JL |
2133 | |
2134 | if (gv && isGV_with_GP(gv)) | |
2135 | cv = GvCV(gv); | |
a0d0e21e | 2136 | |
1604cfb0 MS |
2137 | if (!cv || (!CvROOT(cv) && !CvXSUB(cv))) |
2138 | DIE(aTHX_ "No DB::DB routine defined"); | |
a0d0e21e | 2139 | |
1604cfb0 MS |
2140 | if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG)) |
2141 | /* don't do recursive DB::DB call */ | |
2142 | return NORMAL; | |
748a9306 | 2143 | |
1604cfb0 | 2144 | if (CvISXSUB(cv)) { |
8ae997c5 DM |
2145 | ENTER; |
2146 | SAVEI32(PL_debug); | |
2147 | PL_debug = 0; | |
2148 | SAVESTACK_POS(); | |
8a44b450 | 2149 | SAVETMPS; |
1604cfb0 MS |
2150 | PUSHMARK(SP); |
2151 | (void)(*CvXSUB(cv))(aTHX_ cv); | |
2152 | FREETMPS; | |
2153 | LEAVE; | |
2154 | return NORMAL; | |
2155 | } | |
2156 | else { | |
2157 | cx = cx_pushblock(CXt_SUB, gimme, SP, PL_savestack_ix); | |
2158 | cx_pushsub(cx, cv, PL_op->op_next, 0); | |
a73d8813 DM |
2159 | /* OP_DBSTATE's op_private holds hint bits rather than |
2160 | * the lvalue-ish flags seen in OP_ENTERSUB. So cancel | |
2161 | * any CxLVAL() flags that have now been mis-calculated */ | |
2162 | cx->blk_u16 = 0; | |
8ae997c5 DM |
2163 | |
2164 | SAVEI32(PL_debug); | |
2165 | PL_debug = 0; | |
2166 | SAVESTACK_POS(); | |
1604cfb0 MS |
2167 | CvDEPTH(cv)++; |
2168 | if (CvDEPTH(cv) >= 2) | |
2169 | pad_push(CvPADLIST(cv), CvDEPTH(cv)); | |
2170 | PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv)); | |
2171 | RETURNOP(CvSTART(cv)); | |
2172 | } | |
a0d0e21e LW |
2173 | } |
2174 | else | |
1604cfb0 | 2175 | return NORMAL; |
a0d0e21e LW |
2176 | } |
2177 | ||
0663a8f8 | 2178 | |
2b9a6457 VP |
2179 | PP(pp_enter) |
2180 | { | |
1c23e2bd | 2181 | U8 gimme = GIMME_V; |
2b9a6457 | 2182 | |
d4ce7588 DM |
2183 | (void)cx_pushblock(CXt_BLOCK, gimme, PL_stack_sp, PL_savestack_ix); |
2184 | return NORMAL; | |
2b9a6457 VP |
2185 | } |
2186 | ||
d4ce7588 | 2187 | |
2b9a6457 VP |
2188 | PP(pp_leave) |
2189 | { | |
eb578fdb | 2190 | PERL_CONTEXT *cx; |
f5ddd604 | 2191 | SV **oldsp; |
1c23e2bd | 2192 | U8 gimme; |
2b9a6457 | 2193 | |
4ebe6e95 | 2194 | cx = CX_CUR(); |
61d3b95a | 2195 | assert(CxTYPE(cx) == CXt_BLOCK); |
4df352a8 DM |
2196 | |
2197 | if (PL_op->op_flags & OPf_SPECIAL) | |
c349b9a0 | 2198 | /* fake block should preserve $1 et al; e.g. /(...)/ while ...; */ |
1604cfb0 | 2199 | cx->blk_oldpm = PL_curpm; |
4df352a8 | 2200 | |
f5ddd604 | 2201 | oldsp = PL_stack_base + cx->blk_oldsp; |
61d3b95a | 2202 | gimme = cx->blk_gimme; |
2b9a6457 | 2203 | |
0663a8f8 | 2204 | if (gimme == G_VOID) |
f5ddd604 | 2205 | PL_stack_sp = oldsp; |
0663a8f8 | 2206 | else |
f5ddd604 | 2207 | leave_adjust_stacks(oldsp, oldsp, gimme, |
75bc488d | 2208 | PL_op->op_private & OPpLVALUE ? 3 : 1); |
67f63db7 | 2209 | |
2f450c1b | 2210 | CX_LEAVE_SCOPE(cx); |
ed8ff0f3 | 2211 | cx_popblock(cx); |
5da525e9 | 2212 | CX_POP(cx); |
2b9a6457 | 2213 | |
0663a8f8 | 2214 | return NORMAL; |
2b9a6457 VP |
2215 | } |
2216 | ||
eaa9f768 JH |
2217 | static bool |
2218 | S_outside_integer(pTHX_ SV *sv) | |
2219 | { | |
2220 | if (SvOK(sv)) { | |
2221 | const NV nv = SvNV_nomg(sv); | |
415b66b2 JH |
2222 | if (Perl_isinfnan(nv)) |
2223 | return TRUE; | |
eaa9f768 JH |
2224 | #ifdef NV_PRESERVES_UV |
2225 | if (nv < (NV)IV_MIN || nv > (NV)IV_MAX) | |
2226 | return TRUE; | |
2227 | #else | |
2228 | if (nv <= (NV)IV_MIN) | |
2229 | return TRUE; | |
2230 | if ((nv > 0) && | |
2231 | ((nv > (NV)UV_MAX || | |
2232 | SvUV_nomg(sv) > (UV)IV_MAX))) | |
2233 | return TRUE; | |
2234 | #endif | |
2235 | } | |
2236 | return FALSE; | |
2237 | } | |
2238 | ||
a0d0e21e LW |
2239 | PP(pp_enteriter) |
2240 | { | |
20b7effb | 2241 | dSP; dMARK; |
eb578fdb | 2242 | PERL_CONTEXT *cx; |
1c23e2bd | 2243 | const U8 gimme = GIMME_V; |
4ad63d70 | 2244 | void *itervarp; /* GV or pad slot of the iteration variable */ |
f0bb9bf7 | 2245 | SV *itersave; /* the old var in the iterator var slot */ |
93661e56 | 2246 | U8 cxflags = 0; |
a0d0e21e | 2247 | |
aafca525 | 2248 | if (PL_op->op_targ) { /* "my" variable */ |
1604cfb0 | 2249 | itervarp = &PAD_SVl(PL_op->op_targ); |
4ad63d70 DM |
2250 | itersave = *(SV**)itervarp; |
2251 | assert(itersave); | |
1604cfb0 | 2252 | if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */ |
fdb8b82b DM |
2253 | /* the SV currently in the pad slot is never live during |
2254 | * iteration (the slot is always aliased to one of the items) | |
2255 | * so it's always stale */ | |
1604cfb0 MS |
2256 | SvPADSTALE_on(itersave); |
2257 | } | |
4ad63d70 | 2258 | SvREFCNT_inc_simple_void_NN(itersave); |
1604cfb0 | 2259 | cxflags = CXp_FOR_PAD; |
54b9620d | 2260 | } |
d39c26a6 | 2261 | else { |
1604cfb0 MS |
2262 | SV * const sv = POPs; |
2263 | itervarp = (void *)sv; | |
d4e2b4d6 | 2264 | if (LIKELY(isGV(sv))) { /* symbol table variable */ |
6d3ca00e DM |
2265 | itersave = GvSV(sv); |
2266 | SvREFCNT_inc_simple_void(itersave); | |
93661e56 | 2267 | cxflags = CXp_FOR_GV; |
7896dde7 Z |
2268 | if (PL_op->op_private & OPpITER_DEF) |
2269 | cxflags |= CXp_FOR_DEF; | |
d4e2b4d6 DM |
2270 | } |
2271 | else { /* LV ref: for \$foo (...) */ | |
2272 | assert(SvTYPE(sv) == SVt_PVMG); | |
2273 | assert(SvMAGIC(sv)); | |
2274 | assert(SvMAGIC(sv)->mg_type == PERL_MAGIC_lvref); | |
2275 | itersave = NULL; | |
93661e56 | 2276 | cxflags = CXp_FOR_LVREF; |
d4e2b4d6 | 2277 | } |
d39c26a6 | 2278 | } |
7896dde7 Z |
2279 | /* OPpITER_DEF (implicit $_) should only occur with a GV iter var */ |
2280 | assert((cxflags & CXp_FOR_GV) || !(PL_op->op_private & OPpITER_DEF)); | |
0d863452 | 2281 | |
75fbe096 DM |
2282 | /* Note that this context is initially set as CXt_NULL. Further on |
2283 | * down it's changed to one of the CXt_LOOP_*. Before it's changed, | |
2284 | * there mustn't be anything in the blk_loop substruct that requires | |
2285 | * freeing or undoing, in case we die in the meantime. And vice-versa. | |
2286 | */ | |
ed8ff0f3 | 2287 | cx = cx_pushblock(cxflags, gimme, MARK, PL_savestack_ix); |
d1b6bf72 | 2288 | cx_pushloop_for(cx, itervarp, itersave); |
2c49879e | 2289 | |
533c011a | 2290 | if (PL_op->op_flags & OPf_STACKED) { |
2c49879e DM |
2291 | /* OPf_STACKED implies either a single array: for(@), with a |
2292 | * single AV on the stack, or a range: for (1..5), with 1 and 5 on | |
2293 | * the stack */ | |
1604cfb0 MS |
2294 | SV *maybe_ary = POPs; |
2295 | if (SvTYPE(maybe_ary) != SVt_PVAV) { | |
2c49879e | 2296 | /* range */ |
1604cfb0 MS |
2297 | dPOPss; |
2298 | SV * const right = maybe_ary; | |
2299 | if (UNLIKELY(cxflags & CXp_FOR_LVREF)) | |
2300 | DIE(aTHX_ "Assigned value is not a reference"); | |
2301 | SvGETMAGIC(sv); | |
2302 | SvGETMAGIC(right); | |
2303 | if (RANGE_IS_NUMERIC(sv,right)) { | |
2304 | cx->cx_type |= CXt_LOOP_LAZYIV; | |
2305 | if (S_outside_integer(aTHX_ sv) || | |
eaa9f768 | 2306 | S_outside_integer(aTHX_ right)) |
1604cfb0 MS |
2307 | DIE(aTHX_ "Range iterator outside integer range"); |
2308 | cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv); | |
2309 | cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right); | |
2310 | } | |
2311 | else { | |
2312 | cx->cx_type |= CXt_LOOP_LAZYSV; | |
2313 | cx->blk_loop.state_u.lazysv.cur = newSVsv(sv); | |
2314 | cx->blk_loop.state_u.lazysv.end = right; | |
2315 | SvREFCNT_inc_simple_void_NN(right); | |
2316 | (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur); | |
2317 | /* This will do the upgrade to SVt_PV, and warn if the value | |
2318 | is uninitialised. */ | |
2319 | (void) SvPV_nolen_const(right); | |
2320 | /* Doing this avoids a check every time in pp_iter in pp_hot.c | |
2321 | to replace !SvOK() with a pointer to "". */ | |
2322 | if (!SvOK(right)) { | |
2323 | SvREFCNT_dec(right); | |
2324 | cx->blk_loop.state_u.lazysv.end = &PL_sv_no; | |
2325 | } | |
2326 | } | |
2327 | } | |
2328 | else /* SvTYPE(maybe_ary) == SVt_PVAV */ { | |
2c49879e | 2329 | /* for (@array) {} */ |
93661e56 | 2330 | cx->cx_type |= CXt_LOOP_ARY; |
1604cfb0 MS |
2331 | cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary); |
2332 | SvREFCNT_inc_simple_void_NN(maybe_ary); | |
2333 | cx->blk_loop.state_u.ary.ix = | |
2334 | (PL_op->op_private & OPpITER_REVERSED) ? | |
2335 | AvFILL(cx->blk_loop.state_u.ary.ary) + 1 : | |
2336 | -1; | |
2337 | } | |
8a1f10dd | 2338 | /* EXTEND(SP, 1) not needed in this branch because we just did POPs */ |
89ea2908 | 2339 | } |
d01136d6 | 2340 | else { /* iterating over items on the stack */ |
93661e56 | 2341 | cx->cx_type |= CXt_LOOP_LIST; |
1bef65a2 | 2342 | cx->blk_oldsp = SP - PL_stack_base; |
1604cfb0 | 2343 | cx->blk_loop.state_u.stack.basesp = MARK - PL_stack_base; |
93661e56 DM |
2344 | cx->blk_loop.state_u.stack.ix = |
2345 | (PL_op->op_private & OPpITER_REVERSED) | |
2346 | ? cx->blk_oldsp + 1 | |
2347 | : cx->blk_loop.state_u.stack.basesp; | |
8a1f10dd DM |
2348 | /* pre-extend stack so pp_iter doesn't have to check every time |
2349 | * it pushes yes/no */ | |
2350 | EXTEND(SP, 1); | |
4633a7c4 | 2351 | } |
a0d0e21e LW |
2352 | |
2353 | RETURN; | |
2354 | } | |
2355 | ||
2356 | PP(pp_enterloop) | |
2357 | { | |
eb578fdb | 2358 | PERL_CONTEXT *cx; |
1c23e2bd | 2359 | const U8 gimme = GIMME_V; |
a0d0e21e | 2360 | |
d4ce7588 | 2361 | cx = cx_pushblock(CXt_LOOP_PLAIN, gimme, PL_stack_sp, PL_savestack_ix); |
d1b6bf72 | 2362 | cx_pushloop_plain(cx); |
d4ce7588 | 2363 | return NORMAL; |
a0d0e21e LW |
2364 | } |
2365 | ||
d4ce7588 | 2366 | |
a0d0e21e LW |
2367 | PP(pp_leaveloop) |
2368 | { | |
eb578fdb | 2369 | PERL_CONTEXT *cx; |
1c23e2bd | 2370 | U8 gimme; |
032736ab | 2371 | SV **base; |
f5ddd604 | 2372 | SV **oldsp; |
a0d0e21e | 2373 | |
4ebe6e95 | 2374 | cx = CX_CUR(); |
3b719c58 | 2375 | assert(CxTYPE_is_LOOP(cx)); |
032736ab DM |
2376 | oldsp = PL_stack_base + cx->blk_oldsp; |
2377 | base = CxTYPE(cx) == CXt_LOOP_LIST | |
1bef65a2 | 2378 | ? PL_stack_base + cx->blk_loop.state_u.stack.basesp |
032736ab | 2379 | : oldsp; |
61d3b95a | 2380 | gimme = cx->blk_gimme; |
f86702cc | 2381 | |
0663a8f8 | 2382 | if (gimme == G_VOID) |
032736ab | 2383 | PL_stack_sp = base; |
0663a8f8 | 2384 | else |
032736ab | 2385 | leave_adjust_stacks(oldsp, base, gimme, |
75bc488d | 2386 | PL_op->op_private & OPpLVALUE ? 3 : 1); |
f86702cc | 2387 | |
2f450c1b | 2388 | CX_LEAVE_SCOPE(cx); |
d1b6bf72 | 2389 | cx_poploop(cx); /* Stack values are safe: release loop vars ... */ |
ed8ff0f3 | 2390 | cx_popblock(cx); |
5da525e9 | 2391 | CX_POP(cx); |
f86702cc | 2392 | |
f86702cc | 2393 | return NORMAL; |
a0d0e21e LW |
2394 | } |
2395 | ||
31ccb4f5 DM |
2396 | |
2397 | /* This duplicates most of pp_leavesub, but with additional code to handle | |
2398 | * return args in lvalue context. It was forked from pp_leavesub to | |
2399 | * avoid slowing down that function any further. | |
2400 | * | |
2401 | * Any changes made to this function may need to be copied to pp_leavesub | |
2402 | * and vice-versa. | |
c349b9a0 DM |
2403 | * |
2404 | * also tail-called by pp_return | |
57486a97 DM |
2405 | */ |
2406 | ||
31ccb4f5 | 2407 | PP(pp_leavesublv) |
3bdf583b | 2408 | { |
1c23e2bd | 2409 | U8 gimme; |
57486a97 | 2410 | PERL_CONTEXT *cx; |
799da9d7 | 2411 | SV **oldsp; |
5da525e9 | 2412 | OP *retop; |
57486a97 | 2413 | |
4ebe6e95 | 2414 | cx = CX_CUR(); |
61d3b95a DM |
2415 | assert(CxTYPE(cx) == CXt_SUB); |
2416 | ||
2417 | if (CxMULTICALL(cx)) { | |
1f0ba93b DM |
2418 | /* entry zero of a stack is always PL_sv_undef, which |
2419 | * simplifies converting a '()' return into undef in scalar context */ | |
2420 | assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef); | |
1604cfb0 | 2421 | return 0; |
1f0ba93b | 2422 | } |
85ecf147 | 2423 | |
61d3b95a | 2424 | gimme = cx->blk_gimme; |
799da9d7 | 2425 | oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */ |
57486a97 | 2426 | |
799da9d7 DM |
2427 | if (gimme == G_VOID) |
2428 | PL_stack_sp = oldsp; | |
2429 | else { | |
2430 | U8 lval = CxLVAL(cx); | |
2431 | bool is_lval = (lval && !(lval & OPpENTERSUB_INARGS)); | |
2432 | const char *what = NULL; | |
2433 | ||
2434 | if (gimme == G_SCALAR) { | |
2435 | if (is_lval) { | |
2436 | /* check for bad return arg */ | |
2437 | if (oldsp < PL_stack_sp) { | |
2438 | SV *sv = *PL_stack_sp; | |
2439 | if ((SvPADTMP(sv) || SvREADONLY(sv))) { | |
2440 | what = | |
2441 | SvREADONLY(sv) ? (sv == &PL_sv_undef) ? "undef" | |
2442 | : "a readonly value" : "a temporary"; | |
2443 | } | |
2444 | else goto ok; | |
2445 | } | |
2446 | else { | |
2447 | /* sub:lvalue{} will take us here. */ | |
2448 | what = "undef"; | |
2449 | } | |
2450 | croak: | |
2451 | Perl_croak(aTHX_ | |
2452 | "Can't return %s from lvalue subroutine", what); | |
2453 | } | |
57486a97 | 2454 | |
799da9d7 | 2455 | ok: |
e02ce34b | 2456 | leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2); |
e80c4acf | 2457 | |
799da9d7 DM |
2458 | if (lval & OPpDEREF) { |
2459 | /* lval_sub()->{...} and similar */ | |
2460 | dSP; | |
2461 | SvGETMAGIC(TOPs); | |
2462 | if (!SvOK(TOPs)) { | |
2463 | TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF); | |
2464 | } | |
2465 | PUTBACK; | |
2466 | } | |
2467 | } | |
2468 | else { | |
eb7e169e | 2469 | assert(gimme == G_LIST); |
799da9d7 DM |
2470 | assert (!(lval & OPpDEREF)); |
2471 | ||
2472 | if (is_lval) { | |
2473 | /* scan for bad return args */ | |
2474 | SV **p; | |
2475 | for (p = PL_stack_sp; p > oldsp; p--) { | |
2476 | SV *sv = *p; | |
2477 | /* the PL_sv_undef exception is to allow things like | |
2478 | * this to work, where PL_sv_undef acts as 'skip' | |
2479 | * placeholder on the LHS of list assigns: | |
2480 | * sub foo :lvalue { undef } | |
2481 | * ($a, undef, foo(), $b) = 1..4; | |
2482 | */ | |
2483 | if (sv != &PL_sv_undef && (SvPADTMP(sv) || SvREADONLY(sv))) | |
2484 | { | |
2485 | /* Might be flattened array after $#array = */ | |
2486 | what = SvREADONLY(sv) | |
2487 | ? "a readonly value" : "a temporary"; | |
2488 | goto croak; | |
2489 | } | |
2490 | } | |
2491 | } | |
2492 | ||
e02ce34b | 2493 | leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2); |
799da9d7 | 2494 | } |
3bdf583b | 2495 | } |
57486a97 | 2496 | |
2f450c1b | 2497 | CX_LEAVE_SCOPE(cx); |
a73d8813 | 2498 | cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */ |
ed8ff0f3 | 2499 | cx_popblock(cx); |
5da525e9 DM |
2500 | retop = cx->blk_sub.retop; |
2501 | CX_POP(cx); | |
57486a97 | 2502 | |
5da525e9 | 2503 | return retop; |
3bdf583b FC |
2504 | } |
2505 | ||
e5e291f5 PE |
2506 | static const char *S_defer_blockname(PERL_CONTEXT *cx) |
2507 | { | |
2508 | return (cx->cx_type & CXp_FINALLY) ? "finally" : "defer"; | |
2509 | } | |
2510 | ||
57486a97 | 2511 | |
a0d0e21e LW |
2512 | PP(pp_return) |
2513 | { | |
20b7effb | 2514 | dSP; dMARK; |
eb578fdb | 2515 | PERL_CONTEXT *cx; |
a1325b90 PE |
2516 | I32 cxix = dopopto_cursub(); |
2517 | ||
d40dc6b1 DM |
2518 | assert(cxstack_ix >= 0); |
2519 | if (cxix < cxstack_ix) { | |
f79e2ff9 PE |
2520 | I32 i; |
2521 | /* Check for defer { return; } */ | |
2522 | for(i = cxstack_ix; i > cxix; i--) { | |
2523 | if(CxTYPE(&cxstack[i]) == CXt_DEFER) | |
e5e291f5 PE |
2524 | /* diag_listed_as: Can't "%s" out of a "defer" block */ |
2525 | /* diag_listed_as: Can't "%s" out of a "finally" block */ | |
2526 | Perl_croak(aTHX_ "Can't \"%s\" out of a \"%s\" block", | |
2527 | "return", S_defer_blockname(&cxstack[i])); | |
f79e2ff9 | 2528 | } |
d40dc6b1 | 2529 | if (cxix < 0) { |
79646418 DM |
2530 | if (!( PL_curstackinfo->si_type == PERLSI_SORT |
2531 | || ( PL_curstackinfo->si_type == PERLSI_MULTICALL | |
2532 | && (cxstack[0].cx_type & CXp_SUB_RE_FAKE)) | |
2533 | ) | |
2534 | ) | |
3089a108 | 2535 | DIE(aTHX_ "Can't return outside a subroutine"); |
79646418 DM |
2536 | /* We must be in: |
2537 | * a sort block, which is a CXt_NULL not a CXt_SUB; | |
2538 | * or a /(?{...})/ block. | |
2539 | * Handle specially. */ | |
2540 | assert(CxTYPE(&cxstack[0]) == CXt_NULL | |
2541 | || ( CxTYPE(&cxstack[0]) == CXt_SUB | |
2542 | && (cxstack[0].cx_type & CXp_SUB_RE_FAKE))); | |
3089a108 DM |
2543 | if (cxstack_ix > 0) { |
2544 | /* See comment below about context popping. Since we know | |
2545 | * we're scalar and not lvalue, we can preserve the return | |
2546 | * value in a simpler fashion than there. */ | |
2547 | SV *sv = *SP; | |
d40dc6b1 | 2548 | assert(cxstack[0].blk_gimme == G_SCALAR); |
3089a108 DM |
2549 | if ( (sp != PL_stack_base) |
2550 | && !(SvFLAGS(sv) & (SVs_TEMP|SVs_PADTMP)) | |
2551 | ) | |
2552 | *SP = sv_mortalcopy(sv); | |
2553 | dounwind(0); | |
d40dc6b1 | 2554 | } |
3089a108 DM |
2555 | /* caller responsible for popping cxstack[0] */ |
2556 | return 0; | |
d40dc6b1 | 2557 | } |
3089a108 DM |
2558 | |
2559 | /* There are contexts that need popping. Doing this may free the | |
c349b9a0 | 2560 | * return value(s), so preserve them first: e.g. popping the plain |
3089a108 DM |
2561 | * loop here would free $x: |
2562 | * sub f { { my $x = 1; return $x } } | |
2563 | * We may also need to shift the args down; for example, | |
2564 | * for (1,2) { return 3,4 } | |
c349b9a0 DM |
2565 | * leaves 1,2,3,4 on the stack. Both these actions will be done by |
2566 | * leave_adjust_stacks(), along with freeing any temps. Note that | |
2567 | * whoever we tail-call (e.g. pp_leaveeval) will also call | |
2568 | * leave_adjust_stacks(); however, the second call is likely to | |
2569 | * just see a bunch of SvTEMPs with a ref count of 1, and so just | |
2570 | * pass them through, rather than copying them again. So this | |
2571 | * isn't as inefficient as it sounds. | |
3089a108 DM |
2572 | */ |
2573 | cx = &cxstack[cxix]; | |
3089a108 | 2574 | PUTBACK; |
e02ce34b DM |
2575 | if (cx->blk_gimme != G_VOID) |
2576 | leave_adjust_stacks(MARK, PL_stack_base + cx->blk_oldsp, | |
fc6e609e DM |
2577 | cx->blk_gimme, |
2578 | CxTYPE(cx) == CXt_SUB && CvLVALUE(cx->blk_sub.cv) | |
2579 | ? 3 : 0); | |
0663a8f8 | 2580 | SPAGAIN; |
1604cfb0 | 2581 | dounwind(cxix); |
42831ce4 | 2582 | cx = &cxstack[cxix]; /* CX stack may have been realloced */ |
d40dc6b1 | 2583 | } |
3089a108 DM |
2584 | else { |
2585 | /* Like in the branch above, we need to handle any extra junk on | |
2586 | * the stack. But because we're not also popping extra contexts, we | |
2587 | * don't have to worry about prematurely freeing args. So we just | |
2588 | * need to do the bare minimum to handle junk, and leave the main | |
2589 | * arg processing in the function we tail call, e.g. pp_leavesub. | |
2590 | * In list context we have to splice out the junk; in scalar | |
2591 | * context we can leave as-is (pp_leavesub will later return the | |
2592 | * top stack element). But for an empty arg list, e.g. | |
2593 | * for (1,2) { return } | |
2594 | * we need to set sp = oldsp so that pp_leavesub knows to push | |
2595 | * &PL_sv_undef onto the stack. | |
2596 | */ | |
3ebe7c5a DM |
2597 | SV **oldsp; |
2598 | cx = &cxstack[cxix]; | |
2599 | oldsp = PL_stack_base + cx->blk_oldsp; | |
2600 | if (oldsp != MARK) { | |
2601 | SSize_t nargs = SP - MARK; | |
2602 | if (nargs) { | |
eb7e169e | 2603 | if (cx->blk_gimme == G_LIST) { |
3ebe7c5a DM |
2604 | /* shift return args to base of call stack frame */ |
2605 | Move(MARK + 1, oldsp + 1, nargs, SV*); | |
2606 | PL_stack_sp = oldsp + nargs; | |
2607 | } | |
6228a1e1 | 2608 | } |
3ebe7c5a DM |
2609 | else |
2610 | PL_stack_sp = oldsp; | |
13929c4c | 2611 | } |
3089a108 | 2612 | } |
617a4f41 DM |
2613 | |
2614 | /* fall through to a normal exit */ | |
2615 | switch (CxTYPE(cx)) { | |
2616 | case CXt_EVAL: | |
99dbf645 | 2617 | return CxEVALBLOCK(cx) |
617a4f41 DM |
2618 | ? Perl_pp_leavetry(aTHX) |
2619 | : Perl_pp_leaveeval(aTHX); | |
2620 | case CXt_SUB: | |
13929c4c | 2621 | return CvLVALUE(cx->blk_sub.cv) |
31ccb4f5 | 2622 | ? Perl_pp_leavesublv(aTHX) |
13929c4c | 2623 | : Perl_pp_leavesub(aTHX); |
7766f137 | 2624 | case CXt_FORMAT: |
617a4f41 | 2625 | return Perl_pp_leavewrite(aTHX); |
a0d0e21e | 2626 | default: |
1604cfb0 | 2627 | DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx)); |
a0d0e21e | 2628 | } |
a0d0e21e LW |
2629 | } |
2630 | ||
42b5eca0 | 2631 | /* find the enclosing loop or labelled loop and dounwind() back to it. */ |
4f443c3d | 2632 | |
31705cda | 2633 | static PERL_CONTEXT * |
42b5eca0 | 2634 | S_unwind_loop(pTHX) |
a0d0e21e | 2635 | { |
a0d0e21e | 2636 | I32 cxix; |
1f039d60 | 2637 | if (PL_op->op_flags & OPf_SPECIAL) { |
1604cfb0 MS |
2638 | cxix = dopoptoloop(cxstack_ix); |
2639 | if (cxix < 0) | |
2640 | /* diag_listed_as: Can't "last" outside a loop block */ | |
2641 | Perl_croak(aTHX_ "Can't \"%s\" outside a loop block", | |
42b5eca0 | 2642 | OP_NAME(PL_op)); |
1f039d60 FC |
2643 | } |
2644 | else { | |
1604cfb0 MS |
2645 | dSP; |
2646 | STRLEN label_len; | |
2647 | const char * const label = | |
2648 | PL_op->op_flags & OPf_STACKED | |
2649 | ? SvPV(TOPs,label_len) | |
2650 | : (label_len = strlen(cPVOP->op_pv), cPVOP->op_pv); | |
2651 | const U32 label_flags = | |
2652 | PL_op->op_flags & OPf_STACKED | |
2653 | ? SvUTF8(POPs) | |
2654 | : (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0; | |
2655 | PUTBACK; | |
1f039d60 | 2656 | cxix = dopoptolabel(label, label_len, label_flags); |
1604cfb0 MS |
2657 | if (cxix < 0) |
2658 | /* diag_listed_as: Label not found for "last %s" */ | |
2659 | Perl_croak(aTHX_ "Label not found for \"%s %" SVf "\"", | |
2660 | OP_NAME(PL_op), | |
1f039d60 FC |
2661 | SVfARG(PL_op->op_flags & OPf_STACKED |
2662 | && !SvGMAGICAL(TOPp1s) | |
2663 | ? TOPp1s | |
2664 | : newSVpvn_flags(label, | |
2665 | label_len, | |
2666 | label_flags | SVs_TEMP))); | |
2667 | } | |
f79e2ff9 PE |
2668 | if (cxix < cxstack_ix) { |
2669 | I32 i; | |
2670 | /* Check for defer { last ... } etc */ | |
2671 | for(i = cxstack_ix; i > cxix; i--) { | |
2672 | if(CxTYPE(&cxstack[i]) == CXt_DEFER) | |
e5e291f5 PE |
2673 | /* diag_listed_as: Can't "%s" out of a "defer" block */ |
2674 | /* diag_listed_as: Can't "%s" out of a "finally" block */ | |
2675 | Perl_croak(aTHX_ "Can't \"%s\" out of a \"%s\" block", | |
2676 | OP_NAME(PL_op), S_defer_blockname(&cxstack[i])); | |
f79e2ff9 | 2677 | } |
1604cfb0 | 2678 | dounwind(cxix); |
f79e2ff9 | 2679 | } |
dc7f00ca | 2680 | return &cxstack[cxix]; |
1f039d60 FC |
2681 | } |
2682 | ||
dc7f00ca | 2683 | |
1f039d60 FC |
2684 | PP(pp_last) |
2685 | { | |
eb578fdb | 2686 | PERL_CONTEXT *cx; |
5da525e9 | 2687 | OP* nextop; |
9d4ba2ae | 2688 | |
42b5eca0 | 2689 | cx = S_unwind_loop(aTHX); |
4df352a8 | 2690 | |
93661e56 | 2691 | assert(CxTYPE_is_LOOP(cx)); |
1bef65a2 DM |
2692 | PL_stack_sp = PL_stack_base |
2693 | + (CxTYPE(cx) == CXt_LOOP_LIST | |
2694 | ? cx->blk_loop.state_u.stack.basesp | |
2695 | : cx->blk_oldsp | |
2696 | ); | |
a0d0e21e | 2697 | |
a1f49e72 | 2698 | TAINT_NOT; |
f86702cc | 2699 | |
2700 | /* Stack values are safe: */ | |
2f450c1b | 2701 | CX_LEAVE_SCOPE(cx); |
d1b6bf72 | 2702 | cx_poploop(cx); /* release loop vars ... */ |
ed8ff0f3 | 2703 | cx_popblock(cx); |
5da525e9 DM |
2704 | nextop = cx->blk_loop.my_op->op_lastop->op_next; |
2705 | CX_POP(cx); | |
a0d0e21e | 2706 | |
5da525e9 | 2707 | return nextop; |
a0d0e21e LW |
2708 | } |
2709 | ||
2710 | PP(pp_next) | |
2711 | { | |
eb578fdb | 2712 | PERL_CONTEXT *cx; |
a0d0e21e | 2713 | |
cd97dc8d DM |
2714 | /* if not a bare 'next' in the main scope, search for it */ |
2715 | cx = CX_CUR(); | |
2716 | if (!((PL_op->op_flags & OPf_SPECIAL) && CxTYPE_is_LOOP(cx))) | |
2717 | cx = S_unwind_loop(aTHX); | |
a0d0e21e | 2718 | |
ed8ff0f3 | 2719 | cx_topblock(cx); |
3a1b2b9e | 2720 | PL_curcop = cx->blk_oldcop; |
47c9d59f | 2721 | PERL_ASYNC_CHECK(); |
d57ce4df | 2722 | return (cx)->blk_loop.my_op->op_nextop; |
a0d0e21e LW |
2723 | } |
2724 | ||
2725 | PP(pp_redo) | |
2726 | { | |
42b5eca0 | 2727 | PERL_CONTEXT *cx = S_unwind_loop(aTHX); |
dc7f00ca | 2728 | OP* redo_op = cx->blk_loop.my_op->op_redoop; |
a0d0e21e | 2729 | |
a034e688 | 2730 | if (redo_op->op_type == OP_ENTER) { |
1604cfb0 MS |
2731 | /* pop one less context to avoid $x being freed in while (my $x..) */ |
2732 | cxstack_ix++; | |
dc7f00ca | 2733 | cx = CX_CUR(); |
1604cfb0 MS |
2734 | assert(CxTYPE(cx) == CXt_BLOCK); |
2735 | redo_op = redo_op->op_next; | |
a034e688 DM |
2736 | } |
2737 | ||
936c78b5 | 2738 | FREETMPS; |
ef588991 | 2739 | CX_LEAVE_SCOPE(cx); |
ed8ff0f3 | 2740 | cx_topblock(cx); |
3a1b2b9e | 2741 | PL_curcop = cx->blk_oldcop; |
47c9d59f | 2742 | PERL_ASYNC_CHECK(); |
a034e688 | 2743 | return redo_op; |
a0d0e21e LW |
2744 | } |
2745 | ||
6d90e983 | 2746 | #define UNENTERABLE (OP *)1 |
4bfb5532 | 2747 | #define GOTO_DEPTH 64 |
6d90e983 | 2748 | |
0824fdcb | 2749 | STATIC OP * |
5db1eb8d | 2750 | S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit) |
a0d0e21e | 2751 | { |
a0d0e21e | 2752 | OP **ops = opstack; |
a1894d81 | 2753 | static const char* const too_deep = "Target of goto is too deeply nested"; |
a0d0e21e | 2754 | |
7918f24d NC |
2755 | PERL_ARGS_ASSERT_DOFINDLABEL; |
2756 | ||
fc36a67e | 2757 | if (ops >= oplimit) |
1604cfb0 | 2758 | Perl_croak(aTHX_ "%s", too_deep); |
11343788 | 2759 | if (o->op_type == OP_LEAVE || |
1604cfb0 MS |
2760 | o->op_type == OP_SCOPE || |
2761 | o->op_type == OP_LEAVELOOP || | |
2762 | o->op_type == OP_LEAVESUB || | |
2763 | o->op_type == OP_LEAVETRY || | |
2764 | o->op_type == OP_LEAVEGIVEN) | |
fc36a67e | 2765 | { |
1604cfb0 | 2766 | *ops++ = cUNOPo->op_first; |
fc36a67e | 2767 | } |
4bfb5532 FC |
2768 | else if (oplimit - opstack < GOTO_DEPTH) { |
2769 | if (o->op_flags & OPf_KIDS | |
1604cfb0 MS |
2770 | && cUNOPo->op_first->op_type == OP_PUSHMARK) { |
2771 | *ops++ = UNENTERABLE; | |
4bfb5532 FC |
2772 | } |
2773 | else if (o->op_flags & OPf_KIDS && PL_opargs[o->op_type] | |
1604cfb0 MS |
2774 | && OP_CLASS(o) != OA_LOGOP |
2775 | && o->op_type != OP_LINESEQ | |
2776 | && o->op_type != OP_SREFGEN | |
2777 | && o->op_type != OP_ENTEREVAL | |
2778 | && o->op_type != OP_GLOB | |
2779 | && o->op_type != OP_RV2CV) { | |
2780 | OP * const kid = cUNOPo->op_first; | |
2781 | if (OP_GIMME(kid, 0) != G_SCALAR || OpHAS_SIBLING(kid)) | |
2782 | *ops++ = UNENTERABLE; | |
4bfb5532 | 2783 | } |
6d90e983 FC |
2784 | } |
2785 | if (ops >= oplimit) | |
1604cfb0 | 2786 | Perl_croak(aTHX_ "%s", too_deep); |
c4aa4e48 | 2787 | *ops = 0; |
11343788 | 2788 | if (o->op_flags & OPf_KIDS) { |
1604cfb0 MS |
2789 | OP *kid; |
2790 | OP * const kid1 = cUNOPo->op_first; | |
2791 | /* First try all the kids at this level, since that's likeliest. */ | |
2792 | for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { | |
2793 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { | |
5db1eb8d BF |
2794 | STRLEN kid_label_len; |
2795 | U32 kid_label_flags; | |
1604cfb0 | 2796 | const char *kid_label = CopLABEL_len_flags(kCOP, |
5db1eb8d | 2797 | &kid_label_len, &kid_label_flags); |
1604cfb0 | 2798 | if (kid_label && ( |
5db1eb8d BF |
2799 | ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ? |
2800 | (flags & SVf_UTF8) | |
2801 | ? (bytes_cmp_utf8( | |
2802 | (const U8*)kid_label, kid_label_len, | |
2803 | (const U8*)label, len) == 0) | |
2804 | : (bytes_cmp_utf8( | |
2805 | (const U8*)label, len, | |
2806 | (const U8*)kid_label, kid_label_len) == 0) | |
eade7155 BF |
2807 | : ( len == kid_label_len && ((kid_label == label) |
2808 | || memEQ(kid_label, label, len))))) | |
1604cfb0 MS |
2809 | return kid; |
2810 | } | |
2811 | } | |
2812 | for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { | |
2813 | bool first_kid_of_binary = FALSE; | |
2814 | if (kid == PL_lastgotoprobe) | |
2815 | continue; | |
2816 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { | |
2817 | if (ops == opstack) | |
2818 | *ops++ = kid; | |
2819 | else if (ops[-1] != UNENTERABLE | |
2820 | && (ops[-1]->op_type == OP_NEXTSTATE || | |
2821 | ops[-1]->op_type == OP_DBSTATE)) | |
2822 | ops[-1] = kid; | |
2823 | else | |
2824 | *ops++ = kid; | |
2825 | } | |
2826 | if (kid == kid1 && ops != opstack && ops[-1] == UNENTERABLE) { | |
2827 | first_kid_of_binary = TRUE; | |
2828 | ops--; | |
2829 | } | |
315aa462 PE |
2830 | if ((o = dofindlabel(kid, label, len, flags, ops, oplimit))) { |
2831 | if (kid->op_type == OP_PUSHDEFER) | |
483cd949 | 2832 | Perl_croak(aTHX_ "Can't \"goto\" into a \"defer\" block"); |
1604cfb0 | 2833 | return o; |
315aa462 | 2834 | } |
1604cfb0 MS |
2835 | if (first_kid_of_binary) |
2836 | *ops++ = UNENTERABLE; | |
2837 | } | |
a0d0e21e | 2838 | } |
c4aa4e48 | 2839 | *ops = 0; |
a0d0e21e LW |
2840 | return 0; |
2841 | } | |
2842 | ||
b1c05ba5 | 2843 | |
b5377742 FC |
2844 | static void |
2845 | S_check_op_type(pTHX_ OP * const o) | |
2846 | { | |
2847 | /* Eventually we may want to stack the needed arguments | |
2848 | * for each op. For now, we punt on the hard ones. */ | |
2849 | /* XXX This comment seems to me like wishful thinking. --sprout */ | |
6d90e983 | 2850 | if (o == UNENTERABLE) |
1604cfb0 | 2851 | Perl_croak(aTHX_ |
6d90e983 | 2852 | "Can't \"goto\" into a binary or list expression"); |
b5377742 FC |
2853 | if (o->op_type == OP_ENTERITER) |
2854 | Perl_croak(aTHX_ | |
2855 | "Can't \"goto\" into the middle of a foreach loop"); | |
a01f4640 FC |
2856 | if (o->op_type == OP_ENTERGIVEN) |
2857 | Perl_croak(aTHX_ | |
2858 | "Can't \"goto\" into a \"given\" block"); | |
b5377742 FC |
2859 | } |
2860 | ||
b1c05ba5 DM |
2861 | /* also used for: pp_dump() */ |
2862 | ||
2863 | PP(pp_goto) | |
a0d0e21e | 2864 | { |
c91f661c | 2865 | dSP; |
cbbf8932 | 2866 | OP *retop = NULL; |
a0d0e21e | 2867 | I32 ix; |
eb578fdb | 2868 | PERL_CONTEXT *cx; |
fc36a67e | 2869 | OP *enterops[GOTO_DEPTH]; |
cbbf8932 | 2870 | const char *label = NULL; |
5db1eb8d BF |
2871 | STRLEN label_len = 0; |
2872 | U32 label_flags = 0; | |
bfed75c6 | 2873 | const bool do_dump = (PL_op->op_type == OP_DUMP); |
a1894d81 | 2874 | static const char* const must_have_label = "goto must have label"; |
a0d0e21e | 2875 | |
533c011a | 2876 | if (PL_op->op_flags & OPf_STACKED) { |
7d1d69cb DM |
2877 | /* goto EXPR or goto &foo */ |
2878 | ||
1604cfb0 MS |
2879 | SV * const sv = POPs; |
2880 | SvGETMAGIC(sv); | |
a0d0e21e | 2881 | |
1604cfb0 | 2882 | if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) { |
0fa3d31d | 2883 | /* This egregious kludge implements goto &subroutine */ |
1604cfb0 MS |
2884 | I32 cxix; |
2885 | PERL_CONTEXT *cx; | |
2886 | CV *cv = MUTABLE_CV(SvRV(sv)); | |
2887 | AV *arg = GvAV(PL_defgv); | |
f4cc8ab9 | 2888 | CV *old_cv = NULL; |
1604cfb0 MS |
2889 | |
2890 | while (!CvROOT(cv) && !CvXSUB(cv)) { | |
2891 | const GV * const gv = CvGV(cv); | |
2892 | if (gv) { | |
2893 | GV *autogv; | |
2894 | SV *tmpstr; | |
2895 | /* autoloaded stub? */ | |
2896 | if (cv != GvCV(gv) && (cv = GvCV(gv))) | |
2897 | continue; | |
2898 | autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), | |
2899 | GvNAMELEN(gv), | |
c271df94 | 2900 | GvNAMEUTF8(gv) ? SVf_UTF8 : 0); |
1604cfb0 MS |
2901 | if (autogv && (cv = GvCV(autogv))) |
2902 | continue; | |
2903 | tmpstr = sv_newmortal(); | |
2904 | gv_efullname3(tmpstr, gv, NULL); | |
2905 | DIE(aTHX_ "Goto undefined subroutine &%" SVf, SVfARG(tmpstr)); | |
2906 | } | |
2907 | DIE(aTHX_ "Goto undefined subroutine"); | |
2908 | } | |
2909 | ||
2910 | cxix = dopopto_cursub(); | |
d338c0c2 DM |
2911 | if (cxix < 0) { |
2912 | DIE(aTHX_ "Can't goto subroutine outside a subroutine"); | |
8da3792e | 2913 | } |
d338c0c2 | 2914 | cx = &cxstack[cxix]; |
1604cfb0 MS |
2915 | /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */ |
2916 | if (CxTYPE(cx) == CXt_EVAL) { | |
2917 | if (CxREALEVAL(cx)) | |
2918 | /* diag_listed_as: Can't goto subroutine from an eval-%s */ | |
2919 | DIE(aTHX_ "Can't goto subroutine from an eval-string"); | |
2920 | else | |
2921 | /* diag_listed_as: Can't goto subroutine from an eval-%s */ | |
2922 | DIE(aTHX_ "Can't goto subroutine from an eval-block"); | |
2923 | } | |
2924 | else if (CxMULTICALL(cx)) | |
2925 | DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)"); | |
2926 | ||
f79e2ff9 PE |
2927 | /* Check for defer { goto &...; } */ |
2928 | for(ix = cxstack_ix; ix > cxix; ix--) { | |
2929 | if(CxTYPE(&cxstack[ix]) == CXt_DEFER) | |
e5e291f5 PE |
2930 | /* diag_listed_as: Can't "%s" out of a "defer" block */ |
2931 | Perl_croak(aTHX_ "Can't \"%s\" out of a \"%s\" block", | |
2932 | "goto", S_defer_blockname(&cxstack[ix])); | |
f79e2ff9 PE |
2933 | } |
2934 | ||
1604cfb0 MS |
2935 | /* First do some returnish stuff. */ |
2936 | ||
2937 | SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */ | |
2938 | FREETMPS; | |
2939 | if (cxix < cxstack_ix) { | |
2940 | dounwind(cxix); | |
d338c0c2 | 2941 | } |
7e637ba4 | 2942 | cx = CX_CUR(); |
1604cfb0 MS |
2943 | cx_topblock(cx); |
2944 | SPAGAIN; | |
39de75fd | 2945 | |
8ae997c5 DM |
2946 | /* protect @_ during save stack unwind. */ |
2947 | if (arg) | |
2948 | SvREFCNT_inc_NN(sv_2mortal(MUTABLE_SV(arg))); | |
2949 | ||
1604cfb0 | 2950 | assert(PL_scopestack_ix == cx->blk_oldscopesp); |
dfe0f39b | 2951 | CX_LEAVE_SCOPE(cx); |
8ae997c5 | 2952 | |
1604cfb0 | 2953 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { |
a73d8813 | 2954 | /* this is part of cx_popsub_args() */ |
1604cfb0 | 2955 | AV* av = MUTABLE_AV(PAD_SVl(0)); |
e2657e18 DM |
2956 | assert(AvARRAY(MUTABLE_AV( |
2957 | PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[ | |
2958 | CvDEPTH(cx->blk_sub.cv)])) == PL_curpad); | |
2959 | ||
f72bdec3 DM |
2960 | /* we are going to donate the current @_ from the old sub |
2961 | * to the new sub. This first part of the donation puts a | |
2962 | * new empty AV in the pad[0] slot of the old sub, | |
2963 | * unless pad[0] and @_ differ (e.g. if the old sub did | |
2964 | * local *_ = []); in which case clear the old pad[0] | |
2965 | * array in the usual way */ | |
1604cfb0 | 2966 | if (av == arg || AvREAL(av)) |
95b2f486 | 2967 | clear_defarray(av, av == arg); |
1604cfb0 MS |
2968 | else CLEAR_ARGARRAY(av); |
2969 | } | |
88c11d84 | 2970 | |
b1e25d05 DM |
2971 | /* don't restore PL_comppad here. It won't be needed if the |
2972 | * sub we're going to is non-XS, but restoring it early then | |
2973 | * croaking (e.g. the "Goto undefined subroutine" below) | |
2974 | * means the CX block gets processed again in dounwind, | |
2975 | * but this time with the wrong PL_comppad */ | |
88c11d84 | 2976 | |
1604cfb0 MS |
2977 | /* A destructor called during LEAVE_SCOPE could have undefined |
2978 | * our precious cv. See bug #99850. */ | |
2979 | if (!CvROOT(cv) && !CvXSUB(cv)) { | |
2980 | const GV * const gv = CvGV(cv); | |
2981 | if (gv) { | |
2982 | SV * const tmpstr = sv_newmortal(); | |
2983 | gv_efullname3(tmpstr, gv, NULL); | |
2984 | DIE(aTHX_ "Goto undefined subroutine &%" SVf, | |
2985 | SVfARG(tmpstr)); | |
2986 | } | |
2987 | DIE(aTHX_ "Goto undefined subroutine"); | |
2988 | } | |
2989 | ||
2990 | if (CxTYPE(cx) == CXt_SUB) { | |
2991 | CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth; | |
f4cc8ab9 DM |
2992 | /*on XS calls defer freeing the old CV as it could |
2993 | * prematurely set PL_op to NULL, which could cause | |
2994 | * e..g XS subs using GIMME_V to SEGV */ | |
2995 | if (CvISXSUB(cv)) | |
2996 | old_cv = cx->blk_sub.cv; | |
2997 | else | |
2998 | SvREFCNT_dec_NN(cx->blk_sub.cv); | |
cd17cc2e DM |
2999 | } |
3000 | ||
1604cfb0 MS |
3001 | /* Now do some callish stuff. */ |
3002 | if (CvISXSUB(cv)) { | |
3003 | const SSize_t items = arg ? AvFILL(arg) + 1 : 0; | |
3004 | const bool m = arg ? cBOOL(SvRMAGICAL(arg)) : 0; | |
3005 | SV** mark; | |
58cf0419 | 3006 | UNOP fake_goto_op; |
049bd5ff | 3007 | |
8ae997c5 | 3008 | ENTER; |
80774f05 DM |
3009 | SAVETMPS; |
3010 | SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */ | |
f4cc8ab9 DM |
3011 | if (old_cv) |
3012 | SAVEFREESV(old_cv); /* ditto, deferred freeing of old CV */ | |
80774f05 | 3013 | |
1604cfb0 MS |
3014 | /* put GvAV(defgv) back onto stack */ |
3015 | if (items) { | |
3016 | EXTEND(SP, items+1); /* @_ could have been extended. */ | |
3017 | } | |
3018 | mark = SP; | |
3019 | if (items) { | |
3020 | SSize_t index; | |
3021 | bool r = cBOOL(AvREAL(arg)); | |
3022 | for (index=0; index<items; index++) | |
3023 | { | |
3024 | SV *sv; | |
3025 | if (m) { | |
3026 | SV ** const svp = av_fetch(arg, index, 0); | |
3027 | sv = svp ? *svp : NULL; | |
3028 | } | |
3029 | else sv = AvARRAY(arg)[index]; | |
3030 | SP[index+1] = sv | |
3031 | ? r ? SvREFCNT_inc_NN(sv_2mortal(sv)) : sv | |
3032 | : sv_2mortal(newSVavdefelem(arg, index, 1)); | |
3033 | } | |
3034 | } | |
3035 | SP += items; | |
3036 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { | |
3037 | /* Restore old @_ */ | |
b405d38b | 3038 | CX_POP_SAVEARRAY(cx); |
1604cfb0 | 3039 | } |
1fa4e549 | 3040 | |
1604cfb0 | 3041 | retop = cx->blk_sub.retop; |
b1e25d05 DM |
3042 | PL_comppad = cx->blk_sub.prevcomppad; |
3043 | PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL; | |
8ae997c5 | 3044 | |
58cf0419 DM |
3045 | /* Make a temporary a copy of the current GOTO op on the C |
3046 | * stack, but with a modified gimme (we can't modify the | |
3047 | * real GOTO op as that's not thread-safe). This allows XS | |
3048 | * users of GIMME_V to get the correct calling context, | |
3049 | * even though there is no longer a CXt_SUB frame to | |
3050 | * provide that information. | |
3051 | */ | |
3052 | Copy(PL_op, &fake_goto_op, 1, UNOP); | |
3053 | fake_goto_op.op_flags = | |
3054 | (fake_goto_op.op_flags & ~OPf_WANT) | |
3055 | | (cx->blk_gimme & G_WANT); | |
58cf0419 DM |
3056 | PL_op = (OP*)&fake_goto_op; |
3057 | ||
1604cfb0 | 3058 | /* XS subs don't have a CXt_SUB, so pop it; |
ed8ff0f3 DM |
3059 | * this is a cx_popblock(), less all the stuff we already did |
3060 | * for cx_topblock() earlier */ | |
8ae997c5 | 3061 | PL_curcop = cx->blk_oldcop; |
843fe1ca DM |
3062 | /* this is cx_popsub, less all the stuff we already did */ |
3063 | PL_curstackinfo->si_cxsubix = cx->blk_sub.old_cxsubix; | |
3064 | ||
5da525e9 | 3065 | CX_POP(cx); |
8ae997c5 | 3066 | |
1604cfb0 MS |
3067 | /* Push a mark for the start of arglist */ |
3068 | PUSHMARK(mark); | |
3069 | PUTBACK; | |
3070 | (void)(*CvXSUB(cv))(aTHX_ cv); | |
3071 | LEAVE; | |
3072 | goto _return; | |
3073 | } | |
3074 | else { | |
3075 | PADLIST * const padlist = CvPADLIST(cv); | |
39de75fd | 3076 | |
80774f05 DM |
3077 | SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */ |
3078 | ||
a73d8813 | 3079 | /* partial unrolled cx_pushsub(): */ |
39de75fd | 3080 | |
1604cfb0 MS |
3081 | cx->blk_sub.cv = cv; |
3082 | cx->blk_sub.olddepth = CvDEPTH(cv); | |
dd2155a4 | 3083 | |
1604cfb0 | 3084 | CvDEPTH(cv)++; |
2c50b7ed | 3085 | SvREFCNT_inc_simple_void_NN(cv); |
1604cfb0 MS |
3086 | if (CvDEPTH(cv) > 1) { |
3087 | if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION)) | |
3088 | sub_crush_depth(cv); | |
3089 | pad_push(padlist, CvDEPTH(cv)); | |
3090 | } | |
3091 | PL_curcop = cx->blk_oldcop; | |
3092 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); | |
3093 | if (CxHASARGS(cx)) | |
3094 | { | |
f72bdec3 DM |
3095 | /* second half of donating @_ from the old sub to the |
3096 | * new sub: abandon the original pad[0] AV in the | |
3097 | * new sub, and replace it with the donated @_. | |
3098 | * pad[0] takes ownership of the extra refcount | |
3099 | * we gave arg earlier */ | |
1604cfb0 MS |
3100 | if (arg) { |
3101 | SvREFCNT_dec(PAD_SVl(0)); | |
3102 | PAD_SVl(0) = (SV *)arg; | |
13122036 | 3103 | SvREFCNT_inc_simple_void_NN(arg); |
1604cfb0 MS |
3104 | } |
3105 | ||
3106 | /* GvAV(PL_defgv) might have been modified on scope | |
3107 | exit, so point it at arg again. */ | |
3108 | if (arg != GvAV(PL_defgv)) { | |
3109 | AV * const av = GvAV(PL_defgv); | |
3110 | GvAV(PL_defgv) = (AV *)SvREFCNT_inc_simple(arg); | |
3111 | SvREFCNT_dec(av); | |
3112 | } | |
3113 | } | |
3114 | ||
3115 | if (PERLDB_SUB) { /* Checking curstash breaks DProf. */ | |
3116 | Perl_get_db_sub(aTHX_ NULL, cv); | |
3117 | if (PERLDB_GOTO) { | |
3118 | CV * const gotocv = get_cvs("DB::goto", 0); | |
3119 | if (gotocv) { | |
3120 | PUSHMARK( PL_stack_sp ); | |
3121 | call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG); | |
3122 | PL_stack_sp--; | |
3123 | } | |
3124 | } | |
3125 | } | |
3126 | retop = CvSTART(cv); | |
3127 | goto putback_return; | |
3128 | } | |
3129 | } | |
3130 | else { | |
7d1d69cb | 3131 | /* goto EXPR */ |
1604cfb0 | 3132 | label = SvPV_nomg_const(sv, label_len); |
5db1eb8d | 3133 | label_flags = SvUTF8(sv); |
1604cfb0 | 3134 | } |
a0d0e21e | 3135 | } |
2fc690dc | 3136 | else if (!(PL_op->op_flags & OPf_SPECIAL)) { |
7d1d69cb | 3137 | /* goto LABEL or dump LABEL */ |
1604cfb0 | 3138 | label = cPVOP->op_pv; |
5db1eb8d BF |
3139 | label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0; |
3140 | label_len = strlen(label); | |
3141 | } | |
0157ef98 | 3142 | if (!(do_dump || label_len)) DIE(aTHX_ "%s", must_have_label); |
a0d0e21e | 3143 | |
f410a211 NC |
3144 | PERL_ASYNC_CHECK(); |
3145 | ||
3532f34a | 3146 | if (label_len) { |
1604cfb0 MS |
3147 | OP *gotoprobe = NULL; |
3148 | bool leaving_eval = FALSE; | |
3149 | bool in_block = FALSE; | |
3150 | bool pseudo_block = FALSE; | |
3151 | PERL_CONTEXT *last_eval_cx = NULL; | |
3152 | ||
3153 | /* find label */ | |
3154 | ||
3155 | PL_lastgotoprobe = NULL; | |
3156 | *enterops = 0; | |
3157 | for (ix = cxstack_ix; ix >= 0; ix--) { | |
3158 | cx = &cxstack[ix]; | |
3159 | switch (CxTYPE(cx)) { | |
3160 | case CXt_EVAL: | |
3161 | leaving_eval = TRUE; | |
99dbf645 | 3162 | if (!CxEVALBLOCK(cx)) { |
1604cfb0 MS |
3163 | gotoprobe = (last_eval_cx ? |
3164 | last_eval_cx->blk_eval.old_eval_root : | |
3165 | PL_eval_root); | |
3166 | last_eval_cx = cx; | |
3167 | break; | |
9c5794fe RH |
3168 | } |
3169 | /* else fall through */ | |
93661e56 DM |
3170 | case CXt_LOOP_PLAIN: |
3171 | case CXt_LOOP_LAZYIV: | |
3172 | case CXt_LOOP_LAZYSV: | |
3173 | case CXt_LOOP_LIST: | |
3174 | case CXt_LOOP_ARY: | |
1604cfb0 MS |
3175 | case CXt_GIVEN: |
3176 | case CXt_WHEN: | |
3177 | gotoprobe = OpSIBLING(cx->blk_oldcop); | |
3178 | break; | |
3179 | case CXt_SUBST: | |
3180 | continue; | |
3181 | case CXt_BLOCK: | |
3182 | if (ix) { | |
3183 | gotoprobe = OpSIBLING(cx->blk_oldcop); | |
3184 | in_block = TRUE; | |
3185 | } else | |
3186 | gotoprobe = PL_main_root; | |
3187 | break; | |
3188 | case CXt_SUB: | |
3189 | gotoprobe = CvROOT(cx->blk_sub.cv); | |
3190 | pseudo_block = cBOOL(CxMULTICALL(cx)); | |
3191 | break; | |
3192 | case CXt_FORMAT: | |
3193 | case CXt_NULL: | |
3194 | DIE(aTHX_ "Can't \"goto\" out of a pseudo block"); | |
f79e2ff9 | 3195 | case CXt_DEFER: |
e5e291f5 PE |
3196 | /* diag_listed_as: Can't "%s" out of a "defer" block */ |
3197 | DIE(aTHX_ "Can't \"%s\" out of a \"%s\" block", "goto", S_defer_blockname(cx)); | |
1604cfb0 MS |
3198 | default: |
3199 | if (ix) | |
3200 | DIE(aTHX_ "panic: goto, type=%u, ix=%ld", | |
3201 | CxTYPE(cx), (long) ix); | |
3202 | gotoprobe = PL_main_root; | |
3203 | break; | |
3204 | } | |
3205 | if (gotoprobe) { | |
29e61fd9 DM |
3206 | OP *sibl1, *sibl2; |
3207 | ||
1604cfb0 MS |
3208 | retop = dofindlabel(gotoprobe, label, label_len, label_flags, |
3209 | enterops, enterops + GOTO_DEPTH); | |
3210 | if (retop) | |
3211 | break; | |
3212 | if ( (sibl1 = OpSIBLING(gotoprobe)) && | |
3213 | sibl1->op_type == OP_UNSTACK && | |
3214 | (sibl2 = OpSIBLING(sibl1))) | |
29e61fd9 | 3215 | { |
1604cfb0 MS |
3216 | retop = dofindlabel(sibl2, |
3217 | label, label_len, label_flags, enterops, | |
3218 | enterops + GOTO_DEPTH); | |
3219 | if (retop) | |
3220 | break; | |
3221 | } | |
3222 | } | |
3223 | if (pseudo_block) | |
3224 | DIE(aTHX_ "Can't \"goto\" out of a pseudo block"); | |
3225 | PL_lastgotoprobe = gotoprobe; | |
3226 | } | |
3227 | if (!retop) | |
3228 | DIE(aTHX_ "Can't find label %" UTF8f, | |
3229 | UTF8fARG(label_flags, label_len, label)); | |
3230 | ||
3231 | /* if we're leaving an eval, check before we pop any frames | |
3b2447bc | 3232 | that we're not going to punt, otherwise the error |
1604cfb0 | 3233 | won't be caught */ |
3b2447bc | 3234 | |
1604cfb0 MS |
3235 | if (leaving_eval && *enterops && enterops[1]) { |
3236 | I32 i; | |
3b2447bc | 3237 | for (i = 1; enterops[i]; i++) |
b5377742 | 3238 | S_check_op_type(aTHX_ enterops[i]); |
1604cfb0 MS |
3239 | } |
3240 | ||
3241 | if (*enterops && enterops[1]) { | |
3242 | I32 i = enterops[1] != UNENTERABLE | |
3243 | && enterops[1]->op_type == OP_ENTER && in_block | |
3244 | ? 2 | |
3245 | : 1; | |
3246 | if (enterops[i]) | |
3247 | deprecate("\"goto\" to jump into a construct"); | |
3248 | } | |
3249 | ||
3250 | /* pop unwanted frames */ | |
3251 | ||
3252 | if (ix < cxstack_ix) { | |
3253 | if (ix < 0) | |
3254 | DIE(aTHX_ "panic: docatch: illegal ix=%ld", (long)ix); | |
3255 | dounwind(ix); | |
7e637ba4 | 3256 | cx = CX_CUR(); |
1604cfb0 MS |
3257 | cx_topblock(cx); |
3258 | } | |
3259 | ||
3260 | /* push wanted frames */ | |
3261 | ||
3262 | if (*enterops && enterops[1]) { | |
3263 | OP * const oldop = PL_op; | |
3264 | ix = enterops[1] != UNENTERABLE | |
3265 | && enterops[1]->op_type == OP_ENTER && in_block | |
3266 | ? 2 | |
3267 | : 1; | |
3268 | for (; enterops[ix]; ix++) { | |
3269 | PL_op = enterops[ix]; | |
3270 | S_check_op_type(aTHX_ PL_op); | |
3271 | DEBUG_l( Perl_deb(aTHX_ "pp_goto: Entering %s\n", | |
3272 | OP_NAME(PL_op))); | |
3273 | PL_op->op_ppaddr(aTHX); | |
3274 | } | |
3275 | PL_op = oldop; | |
3276 | } | |
a0d0e21e LW |
3277 | } |
3278 | ||
2631bbca | 3279 | if (do_dump) { |
a5f75d66 | 3280 | #ifdef VMS |
1604cfb0 | 3281 | if (!retop) retop = PL_main_start; |
a5f75d66 | 3282 | #endif |
1604cfb0 MS |
3283 | PL_restartop = retop; |
3284 | PL_do_undump = TRUE; | |
a0d0e21e | 3285 | |
1604cfb0 | 3286 | my_unexec(); |
a0d0e21e | 3287 | |
1604cfb0 MS |
3288 | PL_restartop = 0; /* hmm, must be GNU unexec().. */ |
3289 | PL_do_undump = FALSE; | |
a0d0e21e LW |
3290 | } |
3291 | ||
51eb35b5 DD |
3292 | putback_return: |
3293 | PL_stack_sp = sp; | |
3294 | _return: | |
47c9d59f | 3295 | PERL_ASYNC_CHECK(); |
51eb35b5 | 3296 | return retop; |
a0d0e21e LW |
3297 | } |
3298 | ||
3299 | PP(pp_exit) | |
3300 | { | |
39644a26 | 3301 | dSP; |
a0d0e21e LW |
3302 | I32 anum; |
3303 | ||
3304 | if (MAXARG < 1) | |
1604cfb0 | 3305 | anum = 0; |
9d3c658e | 3306 | else if (!TOPs) { |
1604cfb0 | 3307 | anum = 0; (void)POPs; |
9d3c658e | 3308 | } |
ff0cee69 | 3309 | else { |
1604cfb0 | 3310 | anum = SvIVx(POPs); |
d98f61e7 | 3311 | #ifdef VMS |
1604cfb0 MS |
3312 | if (anum == 1 |
3313 | && SvTRUE(cop_hints_fetch_pvs(PL_curcop, "vmsish_exit", 0))) | |
3314 | anum = 0; | |
97124ef6 FC |
3315 | VMSISH_HUSHED = |
3316 | VMSISH_HUSHED || (PL_curcop->op_private & OPpHUSH_VMSISH); | |
ff0cee69 | 3317 | #endif |
3318 | } | |
cc3604b1 | 3319 | PL_exit_flags |= PERL_EXIT_EXPECTED; |
a0d0e21e | 3320 | my_exit(anum); |
3280af22 | 3321 | PUSHs(&PL_sv_undef); |
a0d0e21e LW |
3322 | RETURN; |
3323 | } | |
3324 | ||
a0d0e21e LW |
3325 | /* Eval. */ |
3326 | ||
0824fdcb | 3327 | STATIC void |
cea2e8a9 | 3328 | S_save_lines(pTHX_ AV *array, SV *sv) |
a0d0e21e | 3329 | { |
504618e9 | 3330 | const char *s = SvPVX_const(sv); |
890ce7af | 3331 | const char * const send = SvPVX_const(sv) + SvCUR(sv); |
504618e9 | 3332 | I32 line = 1; |
a0d0e21e | 3333 | |
7918f24d NC |
3334 | PERL_ARGS_ASSERT_SAVE_LINES; |
3335 | ||
a0d0e21e | 3336 | while (s && s < send) { |
1604cfb0 MS |
3337 | const char *t; |
3338 | SV * const tmpstr = newSV_type(SVt_PVMG); | |
a0d0e21e | 3339 | |
1604cfb0 MS |
3340 | t = (const char *)memchr(s, '\n', send - s); |
3341 | if (t) | |
3342 | t++; | |
3343 | else | |
3344 | t = send; | |
a0d0e21e | 3345 | |
f7f919a0 | 3346 | sv_setpvn_fresh(tmpstr, s, t - s); |
1604cfb0 MS |
3347 | av_store(array, line++, tmpstr); |
3348 | s = t; | |
a0d0e21e LW |
3349 | } |
3350 | } | |
3351 | ||
22f16304 RU |
3352 | /* |
3353 | =for apidoc docatch | |
3354 | ||
a6ceaeb9 DM |
3355 | Interpose, for the current op and RUNOPS loop, |
3356 | ||
3357 | - a new JMPENV stack catch frame, and | |
3358 | - an inner RUNOPS loop to run all the remaining ops following the | |
3359 | current PL_op. | |
3360 | ||
3361 | Then handle any exceptions raised while in that loop. | |
3362 | For a caught eval at this level, re-enter the loop with the specified | |
3363 | restart op (i.e. the op following the OP_LEAVETRY etc); otherwise re-throw | |
3364 | the exception. | |
22f16304 | 3365 | |
a6ceaeb9 | 3366 | docatch() is intended to be used like this: |
22f16304 | 3367 | |
a6ceaeb9 DM |
3368 | PP(pp_entertry) |
3369 | { | |
3370 | if (CATCH_GET) | |
3371 | return docatch(Perl_pp_entertry); | |
22f16304 | 3372 | |
a6ceaeb9 DM |
3373 | ... rest of function ... |
3374 | return PL_op->op_next; | |
3375 | } | |
3376 | ||
3377 | If a new catch frame isn't needed, the op behaves normally. Otherwise it | |
3378 | calls docatch(), which recursively calls pp_entertry(), this time with | |
3379 | CATCH_GET() false, so the rest of the body of the entertry is run. Then | |
3380 | docatch() calls CALLRUNOPS() which executes all the ops following the | |
3381 | entertry. When the loop finally finishes, control returns to docatch(), | |
3382 | which pops the JMPENV and returns to the parent pp_entertry(), which | |
3383 | itself immediately returns. Note that *all* subsequent ops are run within | |
3384 | the inner RUNOPS loop, not just the body of the eval. For example, in | |
3385 | ||
3386 | sub TIEARRAY { eval {1}; my $x } | |
3387 | tie @a, "main"; | |
3388 | ||
3389 | at the point the 'my' is executed, the C stack will look something like: | |
3390 | ||
3391 | #10 main() | |
3392 | #9 perl_run() # JMPENV_PUSH level 1 here | |
3393 | #8 S_run_body() | |
3394 | #7 Perl_runops_standard() # main RUNOPS loop | |
3395 | #6 Perl_pp_tie() | |
3396 | #5 Perl_call_sv() | |
3397 | #4 Perl_runops_standard() # unguarded RUNOPS loop: no new JMPENV | |
3398 | #3 Perl_pp_entertry() | |
3399 | #2 S_docatch() # JMPENV_PUSH level 2 here | |
3400 | #1 Perl_runops_standard() # docatch()'s RUNOPs loop | |
3401 | #0 Perl_pp_padsv() | |
3402 | ||
3403 | Basically, any section of the perl core which starts a RUNOPS loop may | |
3404 | make a promise that it will catch any exceptions and restart the loop if | |
3405 | necessary. If it's not prepared to do that (like call_sv() isn't), then | |
3406 | it sets CATCH_GET() to true, so that any later eval-like code knows to | |
3407 | set up a new handler and loop (via docatch()). | |
3408 | ||
3409 | See L<perlinterp/"Exception handing"> for further details. | |
22f16304 RU |
3410 | |
3411 | =cut | |
3412 | */ | |
a6ceaeb9 | 3413 | |
0824fdcb | 3414 | STATIC OP * |
d7e3f70f | 3415 | S_docatch(pTHX_ Perl_ppaddr_t firstpp) |
1e422769 | 3416 | { |
6224f72b | 3417 | int ret; |
06b5626a | 3418 | OP * const oldop = PL_op; |
db36c5a1 | 3419 | dJMPENV; |
1e422769 | 3420 | |
a6ceaeb9 | 3421 | assert(CATCH_GET); |
14dd3ad8 | 3422 | JMPENV_PUSH(ret); |
a6ceaeb9 DM |
3423 | assert(!CATCH_GET); |
3424 | ||
6224f72b | 3425 | switch (ret) { |
a6ceaeb9 DM |
3426 | case 0: /* normal flow-of-control return from JMPENV_PUSH */ |
3427 | ||
3428 | /* re-run the current op, this time executing the full body of the | |
3429 | * pp function */ | |
1604cfb0 | 3430 | PL_op = firstpp(aTHX); |
14dd3ad8 | 3431 | redo_body: |
16e43efd DM |
3432 | if (PL_op) { |
3433 | CALLRUNOPS(aTHX); | |
3434 | } | |
1604cfb0 | 3435 | break; |
a6ceaeb9 DM |
3436 | |
3437 | case 3: /* an exception raised within an eval */ | |
5fd637ce DM |
3438 | if (PL_restartjmpenv == PL_top_env) { |
3439 | /* die caught by an inner eval - continue inner loop */ | |
3440 | ||
3441 | if (!PL_restartop) | |
3442 | break; | |
1604cfb0 MS |
3443 | PL_restartjmpenv = NULL; |
3444 | PL_op = PL_restartop; | |
3445 | PL_restartop = 0; | |
3446 | goto redo_body; | |
3447 | } | |
3448 | /* FALLTHROUGH */ | |
a6ceaeb9 | 3449 | |
312caa8e | 3450 | default: |
1604cfb0 MS |
3451 | JMPENV_POP; |
3452 | PL_op = oldop; | |
a6ceaeb9 | 3453 | JMPENV_JUMP(ret); /* re-throw the exception */ |
1604cfb0 | 3454 | NOT_REACHED; /* NOTREACHED */ |
1e422769 | 3455 | } |
14dd3ad8 | 3456 | JMPENV_POP; |
533c011a | 3457 | PL_op = oldop; |
5f66b61c | 3458 | return NULL; |
1e422769 | 3459 | } |
3460 | ||
a3985cdc DM |
3461 | |
3462 | /* | |
3463 | =for apidoc find_runcv | |
3464 | ||
3465 | Locate the CV corresponding to the currently executing sub or eval. | |
796b6530 KW |
3466 | If C<db_seqp> is non_null, skip CVs that are in the DB package and populate |
3467 | C<*db_seqp> with the cop sequence number at the point that the DB:: code was | |
72d33970 FC |
3468 | entered. (This allows debuggers to eval in the scope of the breakpoint |
3469 | rather than in the scope of the debugger itself.) | |
a3985cdc DM |
3470 | |
3471 | =cut | |
3472 | */ | |
3473 | ||
3474 | CV* | |
d819b83a | 3475 | Perl_find_runcv(pTHX_ U32 *db_seqp) |
a3985cdc | 3476 | { |
db4cf31d | 3477 | return Perl_find_runcv_where(aTHX_ 0, 0, db_seqp); |
70794f7b FC |
3478 | } |
3479 | ||
3480 | /* If this becomes part of the API, it might need a better name. */ | |
3481 | CV * | |
db4cf31d | 3482 | Perl_find_runcv_where(pTHX_ U8 cond, IV arg, U32 *db_seqp) |
70794f7b | 3483 | { |
a3985cdc | 3484 | PERL_SI *si; |
b4b0692a | 3485 | int level = 0; |
a3985cdc | 3486 | |
d819b83a | 3487 | if (db_seqp) |
1604cfb0 | 3488 | *db_seqp = |
c3923c33 DM |
3489 | PL_curcop == &PL_compiling |
3490 | ? PL_cop_seqmax | |
3491 | : PL_curcop->cop_seq; | |
3492 | ||
a3985cdc | 3493 | for (si = PL_curstackinfo; si; si = si->si_prev) { |
06b5626a | 3494 | I32 ix; |
1604cfb0 MS |
3495 | for (ix = si->si_cxix; ix >= 0; ix--) { |
3496 | const PERL_CONTEXT *cx = &(si->si_cxstack[ix]); | |
3497 | CV *cv = NULL; | |
3498 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { | |
3499 | cv = cx->blk_sub.cv; | |
3500 | /* skip DB:: code */ | |
3501 | if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) { | |
3502 | *db_seqp = cx->blk_oldcop->cop_seq; | |
3503 | continue; | |
3504 | } | |
a453e28a DM |
3505 | if (cx->cx_type & CXp_SUB_RE) |
3506 | continue; | |
1604cfb0 | 3507 | } |
99dbf645 | 3508 | else if (CxTYPE(cx) == CXt_EVAL && !CxEVALBLOCK(cx)) |
1604cfb0 MS |
3509 | cv = cx->blk_eval.cv; |
3510 | if (cv) { | |
3511 | switch (cond) { | |
3512 | case FIND_RUNCV_padid_eq: | |
3513 | if (!CvPADLIST(cv) | |
3514 | || CvPADLIST(cv)->xpadl_id != (U32)arg) | |
3515 | continue; | |
3516 | return cv; | |
3517 | case FIND_RUNCV_level_eq: | |
3518 | if (level++ != arg) continue; | |
2165bd23 | 3519 | /* FALLTHROUGH */ |
1604cfb0 MS |
3520 | default: |
3521 | return cv; | |
3522 | } | |
3523 | } | |
3524 | } | |
a3985cdc | 3525 | } |
db4cf31d | 3526 | return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv; |
a3985cdc DM |
3527 | } |
3528 | ||
3529 | ||
27e90453 DM |
3530 | /* Run yyparse() in a setjmp wrapper. Returns: |
3531 | * 0: yyparse() successful | |
3532 | * 1: yyparse() failed | |
3533 | * 3: yyparse() died | |
3534 | */ | |
3535 | STATIC int | |
28ac2b49 | 3536 | S_try_yyparse(pTHX_ int gramtype) |
27e90453 DM |
3537 | { |
3538 | int ret; | |
3539 | dJMPENV; | |
3540 | ||
4ebe6e95 | 3541 | assert(CxTYPE(CX_CUR()) == CXt_EVAL); |
27e90453 DM |
3542 | JMPENV_PUSH(ret); |
3543 | switch (ret) { | |
3544 | case 0: | |
1604cfb0 MS |
3545 | ret = yyparse(gramtype) ? 1 : 0; |
3546 | break; | |
27e90453 | 3547 | case 3: |
1604cfb0 | 3548 | break; |
27e90453 | 3549 | default: |
1604cfb0 MS |
3550 | JMPENV_POP; |
3551 | JMPENV_JUMP(ret); | |
3552 | NOT_REACHED; /* NOTREACHED */ | |
27e90453 DM |
3553 | } |
3554 | JMPENV_POP; | |
3555 | return ret; | |
3556 | } | |
3557 | ||
cb809451 YO |
3558 | /* Run PL_unitcheckav in a setjmp wrapper via call_list. |
3559 | * Returns: | |
3560 | * 0: unitcheck blocks ran without error | |
3561 | * 3: a unitcheck block died | |
3562 | */ | |
3563 | STATIC int | |
3564 | S_try_run_unitcheck(pTHX) | |
3565 | { | |
3566 | int ret; | |
3567 | dJMPENV; | |
3568 | JMPENV_PUSH(ret); | |
3569 | switch (ret) { | |
3570 | case 0: | |
3571 | call_list(PL_scopestack_ix, PL_unitcheckav); | |
3572 | break; | |
3573 | case 3: | |
5429d8b1 | 3574 | /* call_list died */ |
cb809451 YO |
3575 | break; |
3576 | default: | |
3577 | JMPENV_POP; | |
3578 | JMPENV_JUMP(ret); | |
3579 | NOT_REACHED; /* NOTREACHED */ | |
3580 | } | |
3581 | JMPENV_POP; | |
3582 | return ret; | |
3583 | } | |
27e90453 | 3584 | |
104a8185 DM |
3585 | /* Compile a require/do or an eval ''. |
3586 | * | |
a3985cdc | 3587 | * outside is the lexically enclosing CV (if any) that invoked us. |
104a8185 DM |
3588 | * seq is the current COP scope value. |
3589 | * hh is the saved hints hash, if any. | |
3590 | * | |
410be5db | 3591 | * Returns a bool indicating whether the compile was successful; if so, |
104a8185 DM |
3592 | * PL_eval_start contains the first op of the compiled code; otherwise, |
3593 | * pushes undef. | |
3594 | * | |
3595 | * This function is called from two places: pp_require and pp_entereval. | |
3596 | * These can be distinguished by whether PL_op is entereval. | |
7d116edc FC |
3597 | */ |
3598 | ||
410be5db | 3599 | STATIC bool |
1c23e2bd | 3600 | S_doeval_compile(pTHX_ U8 gimme, CV* outside, U32 seq, HV *hh) |
a0d0e21e | 3601 | { |
20b7effb | 3602 | dSP; |
46c461b5 | 3603 | OP * const saveop = PL_op; |
104a8185 | 3604 | bool clear_hints = saveop->op_type != OP_ENTEREVAL; |
f45b078d | 3605 | COP * const oldcurcop = PL_curcop; |
26c9400e | 3606 | bool in_require = (saveop->op_type == OP_REQUIRE); |
27e90453 | 3607 | int yystatus; |
676a678a | 3608 | CV *evalcv; |
a0d0e21e | 3609 | |
27e90453 | 3610 | PL_in_eval = (in_require |
1604cfb0 MS |
3611 | ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL)) |
3612 | : (EVAL_INEVAL | | |
a1941760 DM |
3613 | ((PL_op->op_private & OPpEVAL_RE_REPARSING) |
3614 | ? EVAL_RE_REPARSING : 0))); | |
a0d0e21e | 3615 | |
1ce6579f | 3616 | PUSHMARK(SP); |
3617 | ||
676a678a Z |
3618 | evalcv = MUTABLE_CV(newSV_type(SVt_PVCV)); |
3619 | CvEVAL_on(evalcv); | |
4ebe6e95 DM |
3620 | assert(CxTYPE(CX_CUR()) == CXt_EVAL); |
3621 | CX_CUR()->blk_eval.cv = evalcv; | |
3622 | CX_CUR()->blk_gimme = gimme; | |
2090ab20 | 3623 | |
676a678a Z |
3624 | CvOUTSIDE_SEQ(evalcv) = seq; |
3625 | CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); | |
a3985cdc | 3626 | |
dd2155a4 | 3627 | /* set up a scratch pad */ |
a0d0e21e | 3628 | |
eacbb379 | 3629 | CvPADLIST_set(evalcv, pad_new(padnew_SAVE)); |
cecbe010 | 3630 | PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */ |
2c05e328 | 3631 | |
07055b4c | 3632 | |
b5bbe64a | 3633 | SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */ |
748a9306 | 3634 | |
a0d0e21e LW |
3635 | /* make sure we compile in the right package */ |
3636 | ||
ed094faf | 3637 | if (CopSTASH_ne(PL_curcop, PL_curstash)) { |
1604cfb0 MS |
3638 | SAVEGENERICSV(PL_curstash); |
3639 | PL_curstash = (HV *)CopSTASH(PL_curcop); | |
3640 | if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL; | |
3641 | else { | |
3642 | SvREFCNT_inc_simple_void(PL_curstash); | |
3643 | save_item(PL_curstname); | |
3644 | sv_sethek(PL_curstname, HvNAME_HEK(PL_curstash)); | |
3645 | } | |
a0d0e21e | 3646 | } |
3c10abe3 | 3647 | /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */ |
3280af22 NIS |
3648 | SAVESPTR(PL_beginav); |
3649 | PL_beginav = newAV(); | |
3650 | SAVEFREESV(PL_beginav); | |
3c10abe3 AG |
3651 | SAVESPTR(PL_unitcheckav); |
3652 | PL_unitcheckav = newAV(); | |
3653 | SAVEFREESV(PL_unitcheckav); | |
a0d0e21e | 3654 | |
81d86705 | 3655 | |
104a8185 | 3656 | ENTER_with_name("evalcomp"); |
676a678a Z |
3657 | SAVESPTR(PL_compcv); |
3658 | PL_compcv = evalcv; | |
3659 | ||
a0d0e21e LW |
3660 | /* try to compile it */ |
3661 | ||
5f66b61c | 3662 | PL_eval_root = NULL; |
3280af22 | 3663 | PL_curcop = &PL_compiling; |
26c9400e | 3664 | if ((saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL)) |
1604cfb0 | 3665 | PL_in_eval |= EVAL_KEEPERR; |
ab69dbc2 | 3666 | else |
1604cfb0 | 3667 | CLEAR_ERRSV(); |
27e90453 | 3668 | |
377b5421 DM |
3669 | SAVEHINTS(); |
3670 | if (clear_hints) { | |
1604cfb0 | 3671 | PL_hints = HINTS_DEFAULT; |
78efaf03 | 3672 | PL_prevailing_version = 0; |
1604cfb0 | 3673 | hv_clear(GvHV(PL_hintgv)); |
9f601cf3 | 3674 | CLEARFEATUREBITS(); |
377b5421 DM |
3675 | } |
3676 | else { | |
1604cfb0 MS |
3677 | PL_hints = saveop->op_private & OPpEVAL_COPHH |
3678 | ? oldcurcop->cop_hints : (U32)saveop->op_targ; | |
4f3e2518 DM |
3679 | |
3680 | /* making 'use re eval' not be in scope when compiling the | |
3681 | * qr/mabye_has_runtime_code_block/ ensures that we don't get | |
3682 | * infinite recursion when S_has_runtime_code() gives a false | |
3683 | * positive: the second time round, HINT_RE_EVAL isn't set so we | |
3684 | * don't bother calling S_has_runtime_code() */ | |
3685 | if (PL_in_eval & EVAL_RE_REPARSING) | |
3686 | PL_hints &= ~HINT_RE_EVAL; | |
3687 | ||
1604cfb0 MS |
3688 | if (hh) { |
3689 | /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */ | |
3690 | SvREFCNT_dec(GvHV(PL_hintgv)); | |
3691 | GvHV(PL_hintgv) = hh; | |
9f601cf3 | 3692 | FETCHFEATUREBITSHH(hh); |
1604cfb0 | 3693 | } |
377b5421 DM |
3694 | } |
3695 | SAVECOMPILEWARNINGS(); | |
3696 | if (clear_hints) { | |
1604cfb0 MS |
3697 | if (PL_dowarn & G_WARN_ALL_ON) |
3698 | PL_compiling.cop_warnings = pWARN_ALL ; | |
3699 | else if (PL_dowarn & G_WARN_ALL_OFF) | |
3700 | PL_compiling.cop_warnings = pWARN_NONE ; | |
3701 | else | |
3702 | PL_compiling.cop_warnings = pWARN_STD ; | |
377b5421 DM |
3703 | } |
3704 | else { | |
1604cfb0 MS |
3705 | PL_compiling.cop_warnings = |
3706 | DUP_WARNINGS(oldcurcop->cop_warnings); | |
3707 | cophh_free(CopHINTHASH_get(&PL_compiling)); | |
3708 | if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) { | |
3709 | /* The label, if present, is the first entry on the chain. So rather | |
3710 | than writing a blank label in front of it (which involves an | |
3711 | allocation), just use the next entry in the chain. */ | |
3712 | PL_compiling.cop_hints_hash | |
3713 | = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next); | |
3714 | /* Check the assumption that this removed the label. */ | |
3715 | assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL); | |
3716 | } | |
3717 | else | |
3718 | PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash); | |
377b5421 | 3719 | } |
f45b078d | 3720 | |
a88d97bf | 3721 | CALL_BLOCK_HOOKS(bhk_eval, saveop); |
52db365a | 3722 | |
c304acb4 YO |
3723 | /* we should never be CATCH_GET true here, as our immediate callers should |
3724 | * always handle that case. */ | |
3725 | assert(!CATCH_GET); | |
fc69996c | 3726 | /* compile the code */ |
c304acb4 YO |
3727 | yystatus = (!in_require) |
3728 | ? S_try_yyparse(aTHX_ GRAMPROG) | |
3729 | : yyparse(GRAMPROG); | |
27e90453 DM |
3730 | |
3731 | if (yystatus || PL_parser->error_count || !PL_eval_root) { | |
1604cfb0 | 3732 | PERL_CONTEXT *cx; |
d308a779 | 3733 | SV *errsv; |
bfed75c6 | 3734 | |
1604cfb0 MS |
3735 | PL_op = saveop; |
3736 | /* note that if yystatus == 3, then the require/eval died during | |
fc69996c DM |
3737 | * compilation, so the EVAL CX block has already been popped, and |
3738 | * various vars restored */ | |
1604cfb0 MS |
3739 | if (yystatus != 3) { |
3740 | if (PL_eval_root) { | |
3741 | op_free(PL_eval_root); | |
3742 | PL_eval_root = NULL; | |
3743 | } | |
3744 | SP = PL_stack_base + POPMARK; /* pop original mark */ | |
4ebe6e95 | 3745 | cx = CX_CUR(); |
06a7bc17 DM |
3746 | assert(CxTYPE(cx) == CXt_EVAL); |
3747 | /* pop the CXt_EVAL, and if was a require, croak */ | |
3748 | S_pop_eval_context_maybe_croak(aTHX_ cx, ERRSV, 2); | |
1604cfb0 | 3749 | } |
d308a779 | 3750 | |
03e81cd3 DM |
3751 | /* die_unwind() re-croaks when in require, having popped the |
3752 | * require EVAL context. So we should never catch a require | |
3753 | * exception here */ | |
1604cfb0 | 3754 | assert(!in_require); |
03e81cd3 | 3755 | |
1604cfb0 | 3756 | errsv = ERRSV; |
d308a779 DM |
3757 | if (!*(SvPV_nolen_const(errsv))) |
3758 | sv_setpvs(errsv, "Compilation error"); | |
3759 | ||
eb7e169e | 3760 | if (gimme != G_LIST) PUSHs(&PL_sv_undef); |
1604cfb0 MS |
3761 | PUTBACK; |
3762 | return FALSE; | |
a0d0e21e | 3763 | } |
fc69996c DM |
3764 | |
3765 | /* Compilation successful. Now clean up */ | |
3766 | ||
3767 | LEAVE_with_name("evalcomp"); | |
104a8185 | 3768 | |
57843af0 | 3769 | CopLINE_set(&PL_compiling, 0); |
104a8185 | 3770 | SAVEFREEOP(PL_eval_root); |
8be227ab | 3771 | cv_forget_slab(evalcv); |
0c58d367 | 3772 | |
a0d0e21e LW |
3773 | DEBUG_x(dump_eval()); |
3774 | ||
55497cff | 3775 | /* Register with debugger: */ |
26c9400e | 3776 | if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) { |
1604cfb0 MS |
3777 | CV * const cv = get_cvs("DB::postponed", 0); |
3778 | if (cv) { | |
3779 | dSP; | |
3780 | PUSHMARK(SP); | |
3781 | XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); | |
3782 | PUTBACK; | |
3783 | call_sv(MUTABLE_SV(cv), G_DISCARD); | |
3784 | } | |
55497cff | 3785 | } |
3786 | ||
59e36a28 | 3787 | if (PL_unitcheckav && av_count(PL_unitcheckav)>0) { |
1604cfb0 | 3788 | OP *es = PL_eval_start; |
cb809451 YO |
3789 | /* TODO: are we sure we shouldn't do S_try_run_unitcheck() |
3790 | * when `in_require` is true? */ | |
d1a6746d YO |
3791 | if (in_require) { |
3792 | call_list(PL_scopestack_ix, PL_unitcheckav); | |
cb809451 YO |
3793 | } |
3794 | else if (S_try_run_unitcheck(aTHX)) { | |
3795 | /* there was an error! */ | |
d1a6746d | 3796 | |
cb809451 YO |
3797 | /* Restore PL_OP */ |
3798 | PL_op = saveop; | |
d1a6746d | 3799 | |
cb809451 YO |
3800 | SV *errsv = ERRSV; |
3801 | if (!*(SvPV_nolen_const(errsv))) { | |
3802 | /* This happens when using: | |
3803 | * eval qq# UNITCHECK { die "\x00"; } #; | |
3804 | */ | |
3805 | sv_setpvs(errsv, "Unit check error"); | |
d1a6746d | 3806 | } |
cb809451 YO |
3807 | |
3808 | if (gimme != G_LIST) PUSHs(&PL_sv_undef); | |
3809 | PUTBACK; | |
3810 | return FALSE; | |
d1a6746d | 3811 | } |
1604cfb0 | 3812 | PL_eval_start = es; |
8ed49485 | 3813 | } |
3c10abe3 | 3814 | |
676a678a | 3815 | CvDEPTH(evalcv) = 1; |
3280af22 | 3816 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
533c011a | 3817 | PL_op = saveop; /* The caller may need it. */ |
bc177e6b | 3818 | PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */ |
5dc0d613 | 3819 | |
410be5db DM |
3820 | PUTBACK; |
3821 | return TRUE; | |
a0d0e21e LW |
3822 | } |
3823 | ||
f0dea69c DM |
3824 | /* Return NULL if the file doesn't exist or isn't a file; |
3825 | * else return PerlIO_openn(). | |
3826 | */ | |
fc69996c | 3827 | |
a6c40364 | 3828 | STATIC PerlIO * |
282b29ee | 3829 | S_check_type_and_open(pTHX_ SV *name) |
ce8abf5f SP |
3830 | { |
3831 | Stat_t st; | |
41188aa0 | 3832 | STRLEN len; |
d345f487 | 3833 | PerlIO * retio; |
41188aa0 | 3834 | const char *p = SvPV_const(name, len); |
c8028aa6 | 3835 | int st_rc; |
df528165 | 3836 | |
7918f24d NC |
3837 | PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN; |
3838 | ||
c8028aa6 TC |
3839 | /* checking here captures a reasonable error message when |
3840 | * PERL_DISABLE_PMC is true, but when PMC checks are enabled, the | |
3841 | * user gets a confusing message about looking for the .pmc file | |
1e777496 DD |
3842 | * rather than for the .pm file so do the check in S_doopen_pm when |
3843 | * PMC is on instead of here. S_doopen_pm calls this func. | |
c8028aa6 TC |
3844 | * This check prevents a \0 in @INC causing problems. |
3845 | */ | |
1e777496 | 3846 | #ifdef PERL_DISABLE_PMC |
41188aa0 | 3847 | if (!IS_SAFE_PATHNAME(p, len, "require")) |
c8028aa6 | 3848 | return NULL; |
1e777496 | 3849 | #endif |
c8028aa6 | 3850 | |
d345f487 DD |
3851 | /* on Win32 stat is expensive (it does an open() and close() twice and |
3852 | a couple other IO calls), the open will fail with a dir on its own with | |
3853 | errno EACCES, so only do a stat to separate a dir from a real EACCES | |
3854 | caused by user perms */ | |
3855 | #ifndef WIN32 | |
c8028aa6 TC |
3856 | st_rc = PerlLIO_stat(p, &st); |
3857 | ||
d1ac83c4 | 3858 | if (st_rc < 0) |
1604cfb0 | 3859 | return NULL; |
d1ac83c4 | 3860 | else { |
1604cfb0 MS |
3861 | int eno; |
3862 | if(S_ISBLK(st.st_mode)) { | |
3863 | eno = EINVAL; | |
3864 | goto not_file; | |
3865 | } | |
3866 | else if(S_ISDIR(st.st_mode)) { | |
3867 | eno = EISDIR; | |
3868 | not_file: | |
3869 | errno = eno; | |
3870 | return NULL; | |
3871 | } | |
ce8abf5f | 3872 | } |
d345f487 | 3873 | #endif |
ce8abf5f | 3874 | |
d345f487 | 3875 | retio = PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name); |
d345f487 DD |
3876 | #ifdef WIN32 |
3877 | /* EACCES stops the INC search early in pp_require to implement | |
3878 | feature RT #113422 */ | |
3879 | if(!retio && errno == EACCES) { /* exists but probably a directory */ | |
1604cfb0 MS |
3880 | int eno; |
3881 | st_rc = PerlLIO_stat(p, &st); | |
3882 | if (st_rc >= 0) { | |
3883 | if(S_ISDIR(st.st_mode)) | |
3884 | eno = EISDIR; | |
3885 | else if(S_ISBLK(st.st_mode)) | |
3886 | eno = EINVAL; | |
3887 | else | |
3888 | eno = EACCES; | |
3889 | errno = eno; | |
3890 | } | |
d345f487 | 3891 | } |
ccb84406 | 3892 | #endif |
d345f487 | 3893 | return retio; |
ce8abf5f SP |
3894 | } |
3895 | ||
f0dea69c DM |
3896 | /* doopen_pm(): return the equivalent of PerlIO_openn() on the given name, |
3897 | * but first check for bad names (\0) and non-files. | |
3898 | * Also if the filename ends in .pm and unless PERL_DISABLE_PMC, | |
3899 | * try loading Foo.pmc first. | |
3900 | */ | |
75c20bac | 3901 | #ifndef PERL_DISABLE_PMC |
ce8abf5f | 3902 | STATIC PerlIO * |
282b29ee | 3903 | S_doopen_pm(pTHX_ SV *name) |
b295d113 | 3904 | { |
282b29ee NC |
3905 | STRLEN namelen; |
3906 | const char *p = SvPV_const(name, namelen); | |
b295d113 | 3907 | |
7918f24d NC |
3908 | PERL_ARGS_ASSERT_DOOPEN_PM; |
3909 | ||
c8028aa6 TC |
3910 | /* check the name before trying for the .pmc name to avoid the |
3911 | * warning referring to the .pmc which the user probably doesn't | |
3912 | * know or care about | |
3913 | */ | |
41188aa0 | 3914 | if (!IS_SAFE_PATHNAME(p, namelen, "require")) |
c8028aa6 TC |
3915 | return NULL; |
3916 | ||
b80f8424 | 3917 | if (memENDPs(p, namelen, ".pm")) { |
1604cfb0 MS |
3918 | SV *const pmcsv = sv_newmortal(); |
3919 | PerlIO * pmcio; | |
50b8ed39 | 3920 | |
1604cfb0 MS |
3921 | SvSetSV_nosteal(pmcsv,name); |
3922 | sv_catpvs(pmcsv, "c"); | |
50b8ed39 | 3923 | |
1604cfb0 MS |
3924 | pmcio = check_type_and_open(pmcsv); |
3925 | if (pmcio) | |
3926 | return pmcio; | |
a6c40364 | 3927 | } |
282b29ee | 3928 | return check_type_and_open(name); |
75c20bac | 3929 | } |
7925835c | 3930 | #else |
282b29ee | 3931 | # define doopen_pm(name) check_type_and_open(name) |
7925835c | 3932 | #endif /* !PERL_DISABLE_PMC */ |
b295d113 | 3933 | |
f0dea69c DM |
3934 | /* require doesn't search in @INC for absolute names, or when the name is |
3935 | explicitly relative the current directory: i.e. ./, ../ */ | |
511712dc TC |
3936 | PERL_STATIC_INLINE bool |
3937 | S_path_is_searchable(const char *name) | |
3938 | { | |
3939 | PERL_ARGS_ASSERT_PATH_IS_SEARCHABLE; | |
3940 | ||
3941 | if (PERL_FILE_IS_ABSOLUTE(name) | |
3942 | #ifdef WIN32 | |
1604cfb0 MS |
3943 | || (*name == '.' && ((name[1] == '/' || |
3944 | (name[1] == '.' && name[2] == '/')) | |
3945 | || (name[1] == '\\' || | |
3946 | ( name[1] == '.' && name[2] == '\\'))) | |
3947 | ) | |
511712dc | 3948 | #else |
1604cfb0 MS |
3949 | || (*name == '.' && (name[1] == '/' || |
3950 | (name[1] == '.' && name[2] == '/'))) | |
511712dc | 3951 | #endif |
1604cfb0 | 3952 | ) |
511712dc | 3953 | { |
1604cfb0 | 3954 | return FALSE; |
511712dc TC |
3955 | } |
3956 | else | |
1604cfb0 | 3957 | return TRUE; |
511712dc TC |
3958 | } |
3959 | ||
b1c05ba5 | 3960 | |
5fb41388 | 3961 | /* implement 'require 5.010001' */ |
b1c05ba5 | 3962 | |
5fb41388 DM |
3963 | static OP * |
3964 | S_require_version(pTHX_ SV *sv) | |
a0d0e21e | 3965 | { |
c91f661c | 3966 | dSP; |
a0d0e21e | 3967 | |
9cdec136 DM |
3968 | sv = sv_2mortal(new_version(sv)); |
3969 | if (!Perl_sv_derived_from_pvn(aTHX_ PL_patchlevel, STR_WITH_LEN("version"), 0)) | |
3970 | upg_version(PL_patchlevel, TRUE); | |
3971 | if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) { | |
3972 | if ( vcmp(sv,PL_patchlevel) <= 0 ) | |
147e3846 | 3973 | DIE(aTHX_ "Perls since %" SVf " too modern--this is %" SVf ", stopped", |
9cdec136 DM |
3974 | SVfARG(sv_2mortal(vnormal(sv))), |
3975 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) | |
3976 | ); | |
3977 | } | |
3978 | else { | |
3979 | if ( vcmp(sv,PL_patchlevel) > 0 ) { | |
3980 | I32 first = 0; | |
3981 | AV *lav; | |
3982 | SV * const req = SvRV(sv); | |
3983 | SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE); | |
3984 | ||
3985 | /* get the left hand term */ | |
3986 | lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE))); | |
3987 | ||
3988 | first = SvIV(*av_fetch(lav,0,0)); | |
3989 | if ( first > (int)PERL_REVISION /* probably 'use 6.0' */ | |
3990 | || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */ | |
a35255b7 | 3991 | || av_count(lav) > 2 /* FP with > 3 digits */ |
9cdec136 DM |
3992 | || strstr(SvPVX(pv),".0") /* FP with leading 0 */ |
3993 | ) { | |
147e3846 KW |
3994 | DIE(aTHX_ "Perl %" SVf " required--this is only " |
3995 | "%" SVf ", stopped", | |
9cdec136 DM |
3996 | SVfARG(sv_2mortal(vnormal(req))), |
3997 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) | |
3998 | ); | |
3999 | } | |
4000 | else { /* probably 'use 5.10' or 'use 5.8' */ | |
4001 | SV *hintsv; | |
4002 | I32 second = 0; | |
4003 | ||
a35255b7 | 4004 | if (av_count(lav) > 1) |
9cdec136 DM |
4005 | second = SvIV(*av_fetch(lav,1,0)); |
4006 | ||
4007 | second /= second >= 600 ? 100 : 10; | |
4008 | hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0", | |
4009 | (int)first, (int)second); | |
4010 | upg_version(hintsv, TRUE); | |
4011 | ||
147e3846 KW |
4012 | DIE(aTHX_ "Perl %" SVf " required (did you mean %" SVf "?)" |
4013 | "--this is only %" SVf ", stopped", | |
9cdec136 DM |
4014 | SVfARG(sv_2mortal(vnormal(req))), |
4015 | SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))), | |
4016 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) | |
4017 | ); | |
4018 | } | |
4019 | } | |
4020 | } | |
d7aa5382 | 4021 | |
9cdec136 | 4022 | RETPUSHYES; |
5fb41388 DM |
4023 | } |
4024 | ||
4025 | /* Handle C<require Foo::Bar>, C<require "Foo/Bar.pm"> and C<do "Foo.pm">. | |
4026 | * The first form will have already been converted at compile time to | |
4027 | * the second form */ | |
4028 | ||
4029 | static OP * | |
2a0461a3 | 4030 | S_require_file(pTHX_ SV *sv) |
5fb41388 | 4031 | { |
c91f661c | 4032 | dSP; |
5fb41388 DM |
4033 | |
4034 | PERL_CONTEXT *cx; | |
4035 | const char *name; | |
4036 | STRLEN len; | |
4037 | char * unixname; | |
4038 | STRLEN unixlen; | |
4039 | #ifdef VMS | |
4040 | int vms_unixname = 0; | |
4041 | char *unixdir; | |
4042 | #endif | |
f0dea69c DM |
4043 | /* tryname is the actual pathname (with @INC prefix) which was loaded. |
4044 | * It's stored as a value in %INC, and used for error messages */ | |
5fb41388 | 4045 | const char *tryname = NULL; |
f0dea69c | 4046 | SV *namesv = NULL; /* SV equivalent of tryname */ |
5fb41388 DM |
4047 | const U8 gimme = GIMME_V; |
4048 | int filter_has_file = 0; | |
4049 | PerlIO *tryrsfp = NULL; | |
4050 | SV *filter_cache = NULL; | |
4051 | SV *filter_state = NULL; | |
4052 | SV *filter_sub = NULL; | |
4053 | SV *hook_sv = NULL; | |
4054 | OP *op; | |
4055 | int saved_errno; | |
4056 | bool path_searchable; | |
4057 | I32 old_savestack_ix; | |
33fe1955 LM |
4058 | const bool op_is_require = PL_op->op_type == OP_REQUIRE; |
4059 | const char *const op_name = op_is_require ? "require" : "do"; | |
0cbfaef6 | 4060 | SV ** svp_cached = NULL; |
33fe1955 LM |
4061 | |
4062 | assert(op_is_require || PL_op->op_type == OP_DOFILE); | |
5fb41388 | 4063 | |
f04d2c34 | 4064 | if (!SvOK(sv)) |
33fe1955 | 4065 | DIE(aTHX_ "Missing or undefined argument to %s", op_name); |
672794ca | 4066 | name = SvPV_nomg_const(sv, len); |
6132ea6c | 4067 | if (!(name && len > 0 && *name)) |
33fe1955 | 4068 | DIE(aTHX_ "Missing or undefined argument to %s", op_name); |
f04d2c34 | 4069 | |
0cbfaef6 | 4070 | #ifndef VMS |
1604cfb0 MS |
4071 | /* try to return earlier (save the SAFE_PATHNAME check) if INC already got the name */ |
4072 | if (op_is_require) { | |
4073 | /* can optimize to only perform one single lookup */ | |
4074 | svp_cached = hv_fetch(GvHVn(PL_incgv), (char*) name, len, 0); | |
4075 | if ( svp_cached && (SvGETMAGIC(*svp_cached), SvOK(*svp_cached)) ) RETPUSHYES; | |
4076 | } | |
0cbfaef6 N |
4077 | #endif |
4078 | ||
33fe1955 LM |
4079 | if (!IS_SAFE_PATHNAME(name, len, op_name)) { |
4080 | if (!op_is_require) { | |
a1b60c8d LM |
4081 | CLEAR_ERRSV(); |
4082 | RETPUSHUNDEF; | |
4083 | } | |
c8028aa6 | 4084 | DIE(aTHX_ "Can't locate %s: %s", |
08f800f8 FC |
4085 | pv_escape(newSVpvs_flags("",SVs_TEMP),name,len,len*2, |
4086 | NULL, SvUTF8(sv)?PERL_PV_ESCAPE_UNI:0), | |
c8028aa6 TC |
4087 | Strerror(ENOENT)); |
4088 | } | |
33fe1955 | 4089 | TAINT_PROPER(op_name); |
4492be7a | 4090 | |
511712dc | 4091 | path_searchable = path_is_searchable(name); |
4492be7a JM |
4092 | |
4093 | #ifdef VMS | |
4094 | /* The key in the %ENV hash is in the syntax of file passed as the argument | |
4095 | * usually this is in UNIX format, but sometimes in VMS format, which | |
4096 | * can result in a module being pulled in more than once. | |
4097 | * To prevent this, the key must be stored in UNIX format if the VMS | |
4098 | * name can be translated to UNIX. | |
4099 | */ | |
155f4c25 | 4100 | |
8de90695 | 4101 | if ((unixname = |
1604cfb0 MS |
4102 | tounixspec(name, SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1))))) |
4103 | != NULL) { | |
4104 | unixlen = strlen(unixname); | |
4105 | vms_unixname = 1; | |
4492be7a JM |
4106 | } |
4107 | else | |
4108 | #endif | |
4109 | { | |
4110 | /* if not VMS or VMS name can not be translated to UNIX, pass it | |
1604cfb0 MS |
4111 | * through. |
4112 | */ | |
4113 | unixname = (char *) name; | |
4114 | unixlen = len; | |
4492be7a | 4115 | } |
33fe1955 | 4116 | if (op_is_require) { |
1604cfb0 MS |
4117 | /* reuse the previous hv_fetch result if possible */ |
4118 | SV * const * const svp = svp_cached ? svp_cached : hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0); | |
4119 | if ( svp ) { | |
4b004c43 TC |
4120 | /* we already did a get magic if this was cached */ |
4121 | if (!svp_cached) | |
4122 | SvGETMAGIC(*svp); | |
1604cfb0 MS |
4123 | if (SvOK(*svp)) |
4124 | RETPUSHYES; | |
4125 | else | |
4126 | DIE(aTHX_ "Attempt to reload %s aborted.\n" | |
4127 | "Compilation failed in require", unixname); | |
4128 | } | |
a52f2cce | 4129 | |
f0dea69c | 4130 | /*XXX OPf_KIDS should always be true? -dapm 4/2017 */ |
a52f2cce | 4131 | if (PL_op->op_flags & OPf_KIDS) { |
d69d9fd4 | 4132 | SVOP * const kid = cSVOPx(cUNOP->op_first); |
a52f2cce NC |
4133 | |
4134 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { | |
f0dea69c DM |
4135 | /* Make sure that a bareword module name (e.g. ::Foo::Bar) |
4136 | * doesn't map to a naughty pathname like /Foo/Bar.pm. | |
4137 | * Note that the parser will normally detect such errors | |
4138 | * at compile time before we reach here, but | |
4139 | * Perl_load_module() can fake up an identical optree | |
4140 | * without going near the parser, and being able to put | |
4141 | * anything as the bareword. So we include a duplicate set | |
4142 | * of checks here at runtime. | |
4143 | */ | |
a52f2cce NC |
4144 | const STRLEN package_len = len - 3; |
4145 | const char slashdot[2] = {'/', '.'}; | |
4146 | #ifdef DOSISH | |
4147 | const char backslashdot[2] = {'\\', '.'}; | |
4148 | #endif | |
4149 | ||
4150 | /* Disallow *purported* barewords that map to absolute | |
4151 | filenames, filenames relative to the current or parent | |
4152 | directory, or (*nix) hidden filenames. Also sanity check | |
4153 | that the generated filename ends .pm */ | |
4154 | if (!path_searchable || len < 3 || name[0] == '.' | |
b59bf0b2 | 4155 | || !memEQs(name + package_len, len - package_len, ".pm")) |
147e3846 | 4156 | DIE(aTHX_ "Bareword in require maps to disallowed filename \"%" SVf "\"", sv); |
a52f2cce NC |
4157 | if (memchr(name, 0, package_len)) { |
4158 | /* diag_listed_as: Bareword in require contains "%s" */ | |
4159 | DIE(aTHX_ "Bareword in require contains \"\\0\""); | |
4160 | } | |
4161 | if (ninstr(name, name + package_len, slashdot, | |
4162 | slashdot + sizeof(slashdot))) { | |
4163 | /* diag_listed_as: Bareword in require contains "%s" */ | |
4164 | DIE(aTHX_ "Bareword in require contains \"/.\""); | |
4165 | } | |
4166 | #ifdef DOSISH | |
4167 | if (ninstr(name, name + package_len, backslashdot, | |
4168 | backslashdot + sizeof(backslashdot))) { | |
4169 | /* diag_listed_as: Bareword in require contains "%s" */ | |
4170 | DIE(aTHX_ "Bareword in require contains \"\\.\""); | |
4171 | } | |
4172 | #endif | |
4173 | } | |
4174 | } | |
4d8b06f1 | 4175 | } |
a0d0e21e | 4176 | |
3f6bd23a | 4177 | PERL_DTRACE_PROBE_FILE_LOADING(unixname); |
32aeab29 | 4178 | |
f0dea69c | 4179 | /* Try to locate and open a file, possibly using @INC */ |
a0d0e21e | 4180 | |
f0dea69c DM |
4181 | /* with "/foo/bar.pm", "./foo.pm" and "../foo/bar.pm", try to load |
4182 | * the file directly rather than via @INC ... */ | |
511712dc | 4183 | if (!path_searchable) { |
1604cfb0 MS |
4184 | /* At this point, name is SvPVX(sv) */ |
4185 | tryname = name; | |
4186 | tryrsfp = doopen_pm(sv); | |
bf4acbe4 | 4187 | } |
f0dea69c DM |
4188 | |
4189 | /* ... but if we fail, still search @INC for code references; | |
a3815e44 | 4190 | * these are applied even on non-searchable paths (except |
f0dea69c DM |
4191 | * if we got EACESS). |
4192 | * | |
4193 | * For searchable paths, just search @INC normally | |
4194 | */ | |
511712dc | 4195 | if (!tryrsfp && !(errno == EACCES && !path_searchable)) { |
1604cfb0 MS |
4196 | AV * const ar = GvAVn(PL_incgv); |
4197 | SSize_t i; | |
748a9306 | 4198 | #ifdef VMS |
1604cfb0 | 4199 | if (vms_unixname) |
46fc3d4c | 4200 | #endif |
1604cfb0 MS |
4201 | { |
4202 | SV *nsv = sv; | |
4203 | namesv = newSV_type(SVt_PV); | |
4204 | for (i = 0; i <= AvFILL(ar); i++) { | |
4205 | SV * const dirsv = *av_fetch(ar, i, TRUE); | |
4206 | ||
4207 | SvGETMAGIC(dirsv); | |
4208 | if (SvROK(dirsv)) { | |
4209 | int count; | |
4210 | SV **svp; | |
4211 | SV *loader = dirsv; | |
4212 | ||
4213 | if (SvTYPE(SvRV(loader)) == SVt_PVAV | |
4214 | && !SvOBJECT(SvRV(loader))) | |
4215 | { | |
4216 | loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE); | |
4217 | SvGETMAGIC(loader); | |
4218 | } | |
4219 | ||
4220 | Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%" UVxf "/%s", | |
4221 | PTR2UV(SvRV(dirsv)), name); | |
4222 | tryname = SvPVX_const(namesv); | |
4223 | tryrsfp = NULL; | |
4224 | ||
4225 | if (SvPADTMP(nsv)) { | |
4226 | nsv = sv_newmortal(); | |
4227 | SvSetSV_nosteal(nsv,sv); | |
4228 | } | |
4229 | ||
4230 | ENTER_with_name("call_INC"); | |
4231 | SAVETMPS; | |
4232 | EXTEND(SP, 2); | |
4233 | ||
4234 | PUSHMARK(SP); | |
4235 | PUSHs(dirsv); | |
4236 | PUSHs(nsv); | |
4237 | PUTBACK; | |
4238 | if (SvGMAGICAL(loader)) { | |
4239 | SV *l = sv_newmortal(); | |
4240 | sv_setsv_nomg(l, loader); | |
4241 | loader = l; | |
4242 | } | |
4243 | if (sv_isobject(loader)) | |
eb7e169e | 4244 | count = call_method("INC", G_LIST); |
1604cfb0 | 4245 | else |
eb7e169e | 4246 | count = call_sv(loader, G_LIST); |
1604cfb0 MS |
4247 | SPAGAIN; |
4248 | ||
4249 | if (count > 0) { | |
4250 | int i = 0; | |
4251 | SV *arg; | |
4252 | ||
4253 | SP -= count - 1; | |
4254 | arg = SP[i++]; | |
4255 | ||
4256 | if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV) | |
4257 | && !isGV_with_GP(SvRV(arg))) { | |
4258 | filter_cache = SvRV(arg); | |
4259 | ||
4260 | if (i < count) { | |
4261 | arg = SP[i++]; | |
4262 | } | |
4263 | } | |
4264 | ||
4265 | if (SvROK(arg) && isGV_with_GP(SvRV(arg))) { | |
4266 | arg = SvRV(arg); | |
4267 | } | |
4268 | ||
4269 | if (isGV_with_GP(arg)) { | |
4270 | IO * const io = GvIO((const GV *)arg); | |
4271 | ||
4272 | ++filter_has_file; | |
4273 | ||
4274 | if (io) { | |
4275 | tryrsfp = IoIFP(io); | |
4276 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { | |
4277 | PerlIO_close(IoOFP(io)); | |
4278 | } | |
4279 | IoIFP(io) = NULL; | |
4280 | IoOFP(io) = NULL; | |
4281 | } | |
4282 | ||
4283 | if (i < count) { | |
4284 | arg = SP[i++]; | |
4285 | } | |
4286 | } | |
4287 | ||
4288 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) { | |
4289 | filter_sub = arg; | |
4290 | SvREFCNT_inc_simple_void_NN(filter_sub); | |
4291 | ||
4292 | if (i < count) { | |
4293 | filter_state = SP[i]; | |
4294 | SvREFCNT_inc_simple_void(filter_state); | |
4295 | } | |
4296 | } | |
4297 | ||
4298 | if (!tryrsfp && (filter_cache || filter_sub)) { | |
4299 | tryrsfp = PerlIO_open(BIT_BUCKET, | |
4300 | PERL_SCRIPT_MODE); | |
4301 | } | |
4302 | SP--; | |
4303 | } | |
4304 | ||
4305 | /* FREETMPS may free our filter_cache */ | |
4306 | SvREFCNT_inc_simple_void(filter_cache); | |
4307 | ||
4308 | PUTBACK; | |
4309 | FREETMPS; | |
4310 | LEAVE_with_name("call_INC"); | |
4311 | ||
4312 | /* Now re-mortalize it. */ | |
4313 | sv_2mortal(filter_cache); | |
4314 | ||
4315 | /* Adjust file name if the hook has set an %INC entry. | |
4316 | This needs to happen after the FREETMPS above. */ | |
4317 | svp = hv_fetch(GvHVn(PL_incgv), name, len, 0); | |
4318 | if (svp) | |
4319 | tryname = SvPV_nolen_const(*svp); | |
4320 | ||
4321 | if (tryrsfp) { | |
4322 | hook_sv = dirsv; | |
4323 | break; | |
4324 | } | |
4325 | ||
4326 | filter_has_file = 0; | |
4327 | filter_cache = NULL; | |
4328 | if (filter_state) { | |
4329 | SvREFCNT_dec_NN(filter_state); | |
4330 | filter_state = NULL; | |
4331 | } | |
4332 | if (filter_sub) { | |
4333 | SvREFCNT_dec_NN(filter_sub); | |
4334 | filter_sub = NULL; | |
4335 | } | |
4336 | } | |
4337 | else if (path_searchable) { | |
13e8e866 DM |
4338 | /* match against a plain @INC element (non-searchable |
4339 | * paths are only matched against refs in @INC) */ | |
1604cfb0 MS |
4340 | const char *dir; |
4341 | STRLEN dirlen; | |
4342 | ||
4343 | if (SvOK(dirsv)) { | |
4344 | dir = SvPV_nomg_const(dirsv, dirlen); | |
4345 | } else { | |
4346 | dir = ""; | |
4347 | dirlen = 0; | |
4348 | } | |
4349 | ||
4350 | if (!IS_SAFE_SYSCALL(dir, dirlen, "@INC entry", op_name)) | |
4351 | continue; | |
e37778c2 | 4352 | #ifdef VMS |
1604cfb0 MS |
4353 | if ((unixdir = |
4354 | tounixpath(dir, SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1))))) | |
4355 | == NULL) | |
4356 | continue; | |
4357 | sv_setpv(namesv, unixdir); | |
4358 | sv_catpv(namesv, unixname); | |
4fda7c0c | 4359 | #else |
1604cfb0 MS |
4360 | /* The equivalent of |
4361 | Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name); | |
4362 | but without the need to parse the format string, or | |
4363 | call strlen on either pointer, and with the correct | |
4364 | allocation up front. */ | |
4365 | { | |
4366 | char *tmp = SvGROW(namesv, dirlen + len + 2); | |
4367 | ||
4368 | memcpy(tmp, dir, dirlen); | |
4369 | tmp +=dirlen; | |
4370 | ||
4371 | /* Avoid '<dir>//<file>' */ | |
4372 | if (!dirlen || *(tmp-1) != '/') { | |
4373 | *tmp++ = '/'; | |
4374 | } else { | |
4375 | /* So SvCUR_set reports the correct length below */ | |
4376 | dirlen--; | |
4377 | } | |
4378 | ||
4379 | /* name came from an SV, so it will have a '\0' at the | |
4380 | end that we can copy as part of this memcpy(). */ | |
4381 | memcpy(tmp, name, len + 1); | |
4382 | ||
4383 | SvCUR_set(namesv, dirlen + len + 1); | |
4384 | SvPOK_on(namesv); | |
4385 | } | |
bf4acbe4 | 4386 | #endif |
1604cfb0 MS |
4387 | TAINT_PROPER(op_name); |
4388 | tryname = SvPVX_const(namesv); | |
4389 | tryrsfp = doopen_pm(namesv); | |
4390 | if (tryrsfp) { | |
4391 | if (tryname[0] == '.' && tryname[1] == '/') { | |
4392 | ++tryname; | |
4393 | while (*++tryname == '/') {} | |
4394 | } | |
4395 | break; | |
4396 | } | |
2433d39e BF |
4397 | else if (errno == EMFILE || errno == EACCES) { |
4398 | /* no point in trying other paths if out of handles; | |
4399 | * on the other hand, if we couldn't open one of the | |
4400 | * files, then going on with the search could lead to | |
4401 | * unexpected results; see perl #113422 | |
4402 | */ | |
4403 | break; | |
4404 | } | |
1604cfb0 MS |
4405 | } |
4406 | } | |
4407 | } | |
a0d0e21e | 4408 | } |
f0dea69c DM |
4409 | |
4410 | /* at this point we've ether opened a file (tryrsfp) or set errno */ | |
4411 | ||
83b195e4 | 4412 | saved_errno = errno; /* sv_2mortal can realloc things */ |
b2ef6d44 | 4413 | sv_2mortal(namesv); |
a0d0e21e | 4414 | if (!tryrsfp) { |
f0dea69c | 4415 | /* we failed; croak if require() or return undef if do() */ |
1604cfb0 MS |
4416 | if (op_is_require) { |
4417 | if(saved_errno == EMFILE || saved_errno == EACCES) { | |
4418 | /* diag_listed_as: Can't locate %s */ | |
4419 | DIE(aTHX_ "Can't locate %s: %s: %s", | |
4420 | name, tryname, Strerror(saved_errno)); | |
4421 | } else { | |
4422 | if (path_searchable) { /* did we lookup @INC? */ | |
4423 | AV * const ar = GvAVn(PL_incgv); | |
4424 | SSize_t i; | |
4425 | SV *const msg = newSVpvs_flags("", SVs_TEMP); | |
4426 | SV *const inc = newSVpvs_flags("", SVs_TEMP); | |
4427 | for (i = 0; i <= AvFILL(ar); i++) { | |
4428 | sv_catpvs(inc, " "); | |
4429 | sv_catsv(inc, *av_fetch(ar, i, TRUE)); | |
4430 | } | |
4431 | if (memENDPs(name, len, ".pm")) { | |
b80f8424 | 4432 | const char *e = name + len - (sizeof(".pm") - 1); |
1604cfb0 | 4433 | const char *c; |
d31614f5 DM |
4434 | bool utf8 = cBOOL(SvUTF8(sv)); |
4435 | ||
4436 | /* if the filename, when converted from "Foo/Bar.pm" | |
4437 | * form back to Foo::Bar form, makes a valid | |
4438 | * package name (i.e. parseable by C<require | |
4439 | * Foo::Bar>), then emit a hint. | |
4440 | * | |
4441 | * this loop is modelled after the one in | |
4442 | S_parse_ident */ | |
1604cfb0 | 4443 | c = name; |
d31614f5 DM |
4444 | while (c < e) { |
4445 | if (utf8 && isIDFIRST_utf8_safe(c, e)) { | |
4446 | c += UTF8SKIP(c); | |
4447 | while (c < e && isIDCONT_utf8_safe( | |
4448 | (const U8*) c, (const U8*) e)) | |
4449 | c += UTF8SKIP(c); | |
4450 | } | |
4451 | else if (isWORDCHAR_A(*c)) { | |
4452 | while (c < e && isWORDCHAR_A(*c)) | |
4453 | c++; | |
4454 | } | |
1604cfb0 | 4455 | else if (*c == '/') |
d31614f5 DM |
4456 | c++; |
4457 | else | |
4458 | break; | |
4459 | } | |
4460 | ||
4461 | if (c == e && isIDFIRST_lazy_if_safe(name, e, utf8)) { | |
f8db7d5b | 4462 | sv_catpvs(msg, " (you may need to install the "); |
d31614f5 DM |
4463 | for (c = name; c < e; c++) { |
4464 | if (*c == '/') { | |
4465 | sv_catpvs(msg, "::"); | |
4466 | } | |
4467 | else { | |
4468 | sv_catpvn(msg, c, 1); | |
4469 | } | |
4470 | } | |
f8db7d5b | 4471 | sv_catpvs(msg, " module)"); |
d31614f5 | 4472 | } |
1604cfb0 MS |
4473 | } |
4474 | else if (memENDs(name, len, ".h")) { | |
4475 | sv_catpvs(msg, " (change .h to .ph maybe?) (did you run h2ph?)"); | |
4476 | } | |
4477 | else if (memENDs(name, len, ".ph")) { | |
4478 | sv_catpvs(msg, " (did you run h2ph?)"); | |
4479 | } | |
4480 | ||
4481 | /* diag_listed_as: Can't locate %s */ | |
4482 | DIE(aTHX_ | |
4483 | "Can't locate %s in @INC%" SVf " (@INC contains:%" SVf ")", | |
4484 | name, msg, inc); | |
4485 | } | |
4486 | } | |
4487 | DIE(aTHX_ "Can't locate %s", name); | |
4488 | } | |
2a0461a3 TC |
4489 | else { |
4490 | #ifdef DEFAULT_INC_EXCLUDES_DOT | |
4491 | Stat_t st; | |
4492 | PerlIO *io = NULL; | |
4493 | dSAVE_ERRNO; | |
f0dea69c DM |
4494 | /* the complication is to match the logic from doopen_pm() so |
4495 | * we don't treat do "sda1" as a previously successful "do". | |
2a0461a3 TC |
4496 | */ |
4497 | bool do_warn = namesv && ckWARN_d(WARN_DEPRECATED) | |
4498 | && PerlLIO_stat(name, &st) == 0 && !S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode) | |
4499 | && (io = PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &sv)) != NULL; | |
4500 | if (io) | |
4501 | PerlIO_close(io); | |
4502 | ||
4503 | RESTORE_ERRNO; | |
4504 | if (do_warn) { | |
1c99110e DM |
4505 | Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), |
4506 | "do \"%s\" failed, '.' is no longer in @INC; " | |
4507 | "did you mean do \"./%s\"?", | |
4508 | name, name); | |
2a0461a3 TC |
4509 | } |
4510 | #endif | |
4511 | CLEAR_ERRSV(); | |
4512 | RETPUSHUNDEF; | |
4513 | } | |
a0d0e21e | 4514 | } |
d8bfb8bd | 4515 | else |
1604cfb0 | 4516 | SETERRNO(0, SS_NORMAL); |
a0d0e21e | 4517 | |
f0dea69c | 4518 | /* Update %INC. Assume success here to prevent recursive requirement. */ |
238d24b4 | 4519 | /* name is never assigned to again, so len is still strlen(name) */ |
d3a4e64e | 4520 | /* Check whether a hook in @INC has already filled %INC */ |
44f8325f | 4521 | if (!hook_sv) { |
1604cfb0 MS |
4522 | (void)hv_store(GvHVn(PL_incgv), |
4523 | unixname, unixlen, newSVpv(tryname,0),0); | |
44f8325f | 4524 | } else { |
1604cfb0 MS |
4525 | SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0); |
4526 | if (!svp) | |
4527 | (void)hv_store(GvHVn(PL_incgv), | |
4528 | unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 ); | |
d3a4e64e | 4529 | } |
a0d0e21e | 4530 | |
f0dea69c DM |
4531 | /* Now parse the file */ |
4532 | ||
adcbf118 | 4533 | old_savestack_ix = PL_savestack_ix; |
b2ef6d44 FC |
4534 | SAVECOPFILE_FREE(&PL_compiling); |
4535 | CopFILE_set(&PL_compiling, tryname); | |
8eaa0acf | 4536 | lex_start(NULL, tryrsfp, 0); |
e50aee73 | 4537 | |
34113e50 | 4538 | if (filter_sub || filter_cache) { |
1604cfb0 MS |
4539 | /* We can use the SvPV of the filter PVIO itself as our cache, rather |
4540 | than hanging another SV from it. In turn, filter_add() optionally | |
4541 | takes the SV to use as the filter (or creates a new SV if passed | |
4542 | NULL), so simply pass in whatever value filter_cache has. */ | |
8fcb2425 | 4543 | SV * const fc = filter_cache ? newSV_type(SVt_NULL) : NULL; |
1604cfb0 MS |
4544 | SV *datasv; |
4545 | if (fc) sv_copypv(fc, filter_cache); | |
4546 | datasv = filter_add(S_run_user_filter, fc); | |
4547 | IoLINES(datasv) = filter_has_file; | |
4548 | IoTOP_GV(datasv) = MUTABLE_GV(filter_state); | |
4549 | IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub); | |
bbed91b5 KF |
4550 | } |
4551 | ||
4552 | /* switch to eval mode */ | |
d7e3f70f | 4553 | assert(!CATCH_GET); |
ed8ff0f3 | 4554 | cx = cx_pushblock(CXt_EVAL, gimme, SP, old_savestack_ix); |
13febba5 | 4555 | cx_pusheval(cx, PL_op->op_next, newSVpv(name, 0)); |
a0d0e21e | 4556 | |
57843af0 GS |
4557 | SAVECOPLINE(&PL_compiling); |
4558 | CopLINE_set(&PL_compiling, 0); | |
a0d0e21e LW |
4559 | |
4560 | PUTBACK; | |
6ec9efec | 4561 | |
9aba0c93 | 4562 | if (doeval_compile(gimme, NULL, PL_curcop->cop_seq, NULL)) |
1604cfb0 | 4563 | op = PL_eval_start; |
410be5db | 4564 | else |
1604cfb0 | 4565 | op = PL_op->op_next; |
bfed75c6 | 4566 | |
3f6bd23a | 4567 | PERL_DTRACE_PROBE_FILE_LOADED(unixname); |
32aeab29 | 4568 | |
6ec9efec | 4569 | return op; |
a0d0e21e LW |
4570 | } |
4571 | ||
5fb41388 DM |
4572 | |
4573 | /* also used for: pp_dofile() */ | |
4574 | ||
4575 | PP(pp_require) | |
4576 | { | |
a7304a68 | 4577 | /* If a suitable JMPENV catch frame isn't present, call docatch(), |
a6ceaeb9 DM |
4578 | * which will: |
4579 | * - add such a frame, and | |
4580 | * - start a new RUNOPS loop, which will (as the first op to run), | |
4581 | * recursively call this pp function again. | |
4582 | * The main body of this function is then executed by the inner call. | |
4583 | */ | |
4584 | if (CATCH_GET) | |
4585 | return docatch(Perl_pp_require); | |
d7e3f70f Z |
4586 | |
4587 | { | |
1604cfb0 MS |
4588 | dSP; |
4589 | SV *sv = POPs; | |
4590 | SvGETMAGIC(sv); | |
4591 | PUTBACK; | |
4592 | return ((SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) | |
4593 | ? S_require_version(aTHX_ sv) | |
4594 | : S_require_file(aTHX_ sv); | |
d7e3f70f | 4595 | } |
5fb41388 DM |
4596 | } |
4597 | ||
4598 | ||
996c9baa VP |
4599 | /* This is a op added to hold the hints hash for |
4600 | pp_entereval. The hash can be modified by the code | |
4601 | being eval'ed, so we return a copy instead. */ | |
4602 | ||
4603 | PP(pp_hintseval) | |
4604 | { | |
996c9baa | 4605 | dSP; |
defdfed5 | 4606 | mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv)))); |
996c9baa VP |
4607 | RETURN; |
4608 | } | |
4609 | ||
4610 | ||
a0d0e21e LW |
4611 | PP(pp_entereval) |
4612 | { | |
20b7effb | 4613 | dSP; |
eb578fdb | 4614 | PERL_CONTEXT *cx; |
0d863452 | 4615 | SV *sv; |
d7e3f70f Z |
4616 | U8 gimme; |
4617 | U32 was; | |
83ee9e09 | 4618 | char tbuf[TYPE_DIGITS(long) + 12]; |
d7e3f70f Z |
4619 | bool saved_delete; |
4620 | char *tmpbuf; | |
a0d0e21e | 4621 | STRLEN len; |
a3985cdc | 4622 | CV* runcv; |
d7e3f70f Z |
4623 | U32 seq, lex_flags; |
4624 | HV *saved_hh; | |
4625 | bool bytes; | |
adcbf118 | 4626 | I32 old_savestack_ix; |
e389bba9 | 4627 | |
a7304a68 | 4628 | /* If a suitable JMPENV catch frame isn't present, call docatch(), |
a6ceaeb9 DM |
4629 | * which will: |
4630 | * - add such a frame, and | |
4631 | * - start a new RUNOPS loop, which will (as the first op to run), | |
4632 | * recursively call this pp function again. | |
4633 | * The main body of this function is then executed by the inner call. | |
4634 | */ | |
4635 | if (CATCH_GET) | |
4636 | return docatch(Perl_pp_entereval); | |
d7e3f70f | 4637 | |
c304acb4 YO |
4638 | assert(!CATCH_GET); |
4639 | ||
d7e3f70f Z |
4640 | gimme = GIMME_V; |
4641 | was = PL_breakable_sub_gen; | |
4642 | saved_delete = FALSE; | |
4643 | tmpbuf = tbuf; | |
4644 | lex_flags = 0; | |
4645 | saved_hh = NULL; | |
4646 | bytes = PL_op->op_private & OPpEVAL_BYTES; | |
4647 | ||
0d863452 | 4648 | if (PL_op->op_private & OPpEVAL_HAS_HH) { |
1604cfb0 | 4649 | saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs)); |
0d863452 | 4650 | } |
bc344123 | 4651 | else if (PL_hints & HINT_LOCALIZE_HH || ( |
1604cfb0 MS |
4652 | PL_op->op_private & OPpEVAL_COPHH |
4653 | && PL_curcop->cop_hints & HINT_LOCALIZE_HH | |
4654 | )) { | |
4655 | saved_hh = cop_hints_2hv(PL_curcop, 0); | |
4656 | hv_magic(saved_hh, NULL, PERL_MAGIC_hints); | |
7d789282 | 4657 | } |
0d863452 | 4658 | sv = POPs; |
895b760f | 4659 | if (!SvPOK(sv)) { |
1604cfb0 MS |
4660 | /* make sure we've got a plain PV (no overload etc) before testing |
4661 | * for taint. Making a copy here is probably overkill, but better | |
4662 | * safe than sorry */ | |
4663 | STRLEN len; | |
4664 | const char * const p = SvPV_const(sv, len); | |
0479a84a | 4665 | |
1604cfb0 MS |
4666 | sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv)); |
4667 | lex_flags |= LEX_START_COPIED; | |
7d789282 | 4668 | |
1604cfb0 MS |
4669 | if (bytes && SvUTF8(sv)) |
4670 | SvPVbyte_force(sv, len); | |
7d789282 | 4671 | } |
60d63348 | 4672 | else if (bytes && SvUTF8(sv)) { |
1604cfb0 MS |
4673 | /* Don't modify someone else's scalar */ |
4674 | STRLEN len; | |
4675 | sv = newSVsv(sv); | |
4676 | (void)sv_2mortal(sv); | |
4677 | SvPVbyte_force(sv,len); | |
4678 | lex_flags |= LEX_START_COPIED; | |
895b760f | 4679 | } |
a0d0e21e | 4680 | |
af2d3def | 4681 | TAINT_IF(SvTAINTED(sv)); |
748a9306 | 4682 | TAINT_PROPER("eval"); |
a0d0e21e | 4683 | |
adcbf118 DM |
4684 | old_savestack_ix = PL_savestack_ix; |
4685 | ||
0abcdfa4 | 4686 | lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE |
1604cfb0 MS |
4687 | ? LEX_IGNORE_UTF8_HINTS |
4688 | : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER | |
4689 | ) | |
4690 | ); | |
ac27b0f5 | 4691 | |
a0d0e21e LW |
4692 | /* switch to eval mode */ |
4693 | ||
83ee9e09 | 4694 | if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) { |
1604cfb0 MS |
4695 | SV * const temp_sv = sv_newmortal(); |
4696 | Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%" IVdf "]", | |
4697 | (unsigned long)++PL_evalseq, | |
4698 | CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); | |
4699 | tmpbuf = SvPVX(temp_sv); | |
4700 | len = SvCUR(temp_sv); | |
83ee9e09 GS |
4701 | } |
4702 | else | |
1604cfb0 | 4703 | len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq); |
f4dd75d9 | 4704 | SAVECOPFILE_FREE(&PL_compiling); |
57843af0 | 4705 | CopFILE_set(&PL_compiling, tmpbuf+2); |
f4dd75d9 | 4706 | SAVECOPLINE(&PL_compiling); |
57843af0 | 4707 | CopLINE_set(&PL_compiling, 1); |
d819b83a DM |
4708 | /* special case: an eval '' executed within the DB package gets lexically |
4709 | * placed in the first non-DB CV rather than the current CV - this | |
4710 | * allows the debugger to execute code, find lexicals etc, in the | |
4711 | * scope of the code being debugged. Passing &seq gets find_runcv | |
4712 | * to do the dirty work for us */ | |
4713 | runcv = find_runcv(&seq); | |
a0d0e21e | 4714 | |
d7e3f70f | 4715 | assert(!CATCH_GET); |
ed8ff0f3 | 4716 | cx = cx_pushblock((CXt_EVAL|CXp_REAL), gimme, SP, old_savestack_ix); |
13febba5 | 4717 | cx_pusheval(cx, PL_op->op_next, NULL); |
a0d0e21e LW |
4718 | |
4719 | /* prepare to compile string */ | |
4720 | ||
c7a622b3 | 4721 | if (PERLDB_LINE_OR_SAVESRC && PL_curstash != PL_debstash) |
1604cfb0 | 4722 | save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr); |
78da7625 | 4723 | else { |
1604cfb0 MS |
4724 | /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up |
4725 | deleting the eval's FILEGV from the stash before gv_check() runs | |
4726 | (i.e. before run-time proper). To work around the coredump that | |
4727 | ensues, we always turn GvMULTI_on for any globals that were | |
4728 | introduced within evals. See force_ident(). GSAR 96-10-12 */ | |
4729 | char *const safestr = savepvn(tmpbuf, len); | |
4730 | SAVEDELETE(PL_defstash, safestr, len); | |
4731 | saved_delete = TRUE; | |
78da7625 FC |
4732 | } |
4733 | ||
a0d0e21e | 4734 | PUTBACK; |
f9bddea7 | 4735 | |
9aba0c93 | 4736 | if (doeval_compile(gimme, runcv, seq, saved_hh)) { |
1604cfb0 MS |
4737 | if (was != PL_breakable_sub_gen /* Some subs defined here. */ |
4738 | ? PERLDB_LINE_OR_SAVESRC | |
4739 | : PERLDB_SAVESRC_NOSUBS) { | |
4740 | /* Retain the filegv we created. */ | |
4741 | } else if (!saved_delete) { | |
4742 | char *const safestr = savepvn(tmpbuf, len); | |
4743 | SAVEDELETE(PL_defstash, safestr, len); | |
4744 | } | |
4745 | return PL_eval_start; | |
f9bddea7 | 4746 | } else { |
1604cfb0 MS |
4747 | /* We have already left the scope set up earlier thanks to the LEAVE |
4748 | in doeval_compile(). */ | |
4749 | if (was != PL_breakable_sub_gen /* Some subs defined here. */ | |
4750 | ? PERLDB_LINE_OR_SAVESRC | |
4751 | : PERLDB_SAVESRC_INVALID) { | |
4752 | /* Retain the filegv we created. */ | |
4753 | } else if (!saved_delete) { | |
4754 | (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD); | |
4755 | } | |
4756 | return PL_op->op_next; | |
f9bddea7 | 4757 | } |
a0d0e21e LW |
4758 | } |
4759 | ||
c349b9a0 DM |
4760 | |
4761 | /* also tail-called by pp_return */ | |
4762 | ||
a0d0e21e LW |
4763 | PP(pp_leaveeval) |
4764 | { | |
f5ddd604 | 4765 | SV **oldsp; |
1c23e2bd | 4766 | U8 gimme; |
eb578fdb | 4767 | PERL_CONTEXT *cx; |
a0d0e21e | 4768 | OP *retop; |
06a7bc17 | 4769 | int failed; |
676a678a | 4770 | CV *evalcv; |
06a7bc17 | 4771 | bool keep; |
a0d0e21e | 4772 | |
011c3814 | 4773 | PERL_ASYNC_CHECK(); |
61d3b95a | 4774 | |
4ebe6e95 | 4775 | cx = CX_CUR(); |
61d3b95a | 4776 | assert(CxTYPE(cx) == CXt_EVAL); |
2aabfe8a | 4777 | |
f5ddd604 | 4778 | oldsp = PL_stack_base + cx->blk_oldsp; |
61d3b95a DM |
4779 | gimme = cx->blk_gimme; |
4780 | ||
2aabfe8a | 4781 | /* did require return a false value? */ |
06a7bc17 DM |
4782 | failed = CxOLD_OP_TYPE(cx) == OP_REQUIRE |
4783 | && !(gimme == G_SCALAR | |
f4c975aa | 4784 | ? SvTRUE_NN(*PL_stack_sp) |
06a7bc17 | 4785 | : PL_stack_sp > oldsp); |
2aabfe8a | 4786 | |
b66d79a6 | 4787 | if (gimme == G_VOID) { |
f5ddd604 | 4788 | PL_stack_sp = oldsp; |
b66d79a6 DM |
4789 | /* free now to avoid late-called destructors clobbering $@ */ |
4790 | FREETMPS; | |
4791 | } | |
2aabfe8a | 4792 | else |
f5ddd604 | 4793 | leave_adjust_stacks(oldsp, oldsp, gimme, 0); |
2aabfe8a | 4794 | |
13febba5 | 4795 | /* the cx_popeval does a leavescope, which frees the optree associated |
4df352a8 DM |
4796 | * with eval, which if it frees the nextstate associated with |
4797 | * PL_curcop, sets PL_curcop to NULL. Which can mess up freeing a | |
4798 | * regex when running under 'use re Debug' because it needs PL_curcop | |
4799 | * to get the current hints. So restore it early. | |
4800 | */ | |
4801 | PL_curcop = cx->blk_oldcop; | |
2aabfe8a | 4802 | |
06a7bc17 DM |
4803 | /* grab this value before cx_popeval restores the old PL_in_eval */ |
4804 | keep = cBOOL(PL_in_eval & EVAL_KEEPERR); | |
f39bc417 | 4805 | retop = cx->blk_eval.retop; |
676a678a | 4806 | evalcv = cx->blk_eval.cv; |
4fdae800 | 4807 | #ifdef DEBUGGING |
676a678a | 4808 | assert(CvDEPTH(evalcv) == 1); |
4fdae800 | 4809 | #endif |
676a678a | 4810 | CvDEPTH(evalcv) = 0; |
4fdae800 | 4811 | |
06a7bc17 DM |
4812 | /* pop the CXt_EVAL, and if a require failed, croak */ |
4813 | S_pop_eval_context_maybe_croak(aTHX_ cx, NULL, failed); | |
a0d0e21e | 4814 | |
d308a779 DM |
4815 | if (!keep) |
4816 | CLEAR_ERRSV(); | |
4817 | ||
2aabfe8a | 4818 | return retop; |
a0d0e21e LW |
4819 | } |
4820 | ||
383bf72f PE |
4821 | /* Ops that implement try/catch syntax |
4822 | * Note the asymmetry here: | |
4823 | * pp_entertrycatch does two pushblocks | |
4824 | * pp_leavetrycatch pops only the outer one; the inner one is popped by | |
4825 | * pp_poptry or by stack-unwind of die within the try block | |
4826 | */ | |
4827 | ||
4828 | PP(pp_entertrycatch) | |
4829 | { | |
4830 | PERL_CONTEXT *cx; | |
4831 | const U8 gimme = GIMME_V; | |
4832 | ||
a7304a68 | 4833 | /* If a suitable JMPENV catch frame isn't present, call docatch(), |
a6ceaeb9 DM |
4834 | * which will: |
4835 | * - add such a frame, and | |
4836 | * - start a new RUNOPS loop, which will (as the first op to run), | |
4837 | * recursively call this pp function again. | |
4838 | * The main body of this function is then executed by the inner call. | |
4839 | */ | |
4840 | if (CATCH_GET) | |
4841 | return docatch(Perl_pp_entertrycatch); | |
383bf72f PE |
4842 | |
4843 | assert(!CATCH_GET); | |
4844 | ||
4845 | Perl_pp_enter(aTHX); /* performs cx_pushblock(CXt_BLOCK, ...) */ | |
4846 | ||
4847 | save_scalar(PL_errgv); | |
4848 | CLEAR_ERRSV(); | |
4849 | ||
4850 | cx = cx_pushblock((CXt_EVAL|CXp_EVALBLOCK|CXp_TRY), gimme, | |
4851 | PL_stack_sp, PL_savestack_ix); | |
6b729d24 | 4852 | cx_pushtry(cx, cLOGOP->op_other); |
383bf72f PE |
4853 | |
4854 | PL_in_eval = EVAL_INEVAL; | |
4855 | ||
4856 | return NORMAL; | |
4857 | } | |
4858 | ||
4859 | PP(pp_leavetrycatch) | |
4860 | { | |
4861 | /* leavetrycatch is leave */ | |
4862 | return Perl_pp_leave(aTHX); | |
4863 | } | |
4864 | ||
4865 | PP(pp_poptry) | |
4866 | { | |
4867 | /* poptry is leavetry */ | |
4868 | return Perl_pp_leavetry(aTHX); | |
4869 | } | |
4870 | ||
a1325b90 PE |
4871 | PP(pp_catch) |
4872 | { | |
4873 | dTARGET; | |
4874 | ||
a1325b90 PE |
4875 | save_clearsv(&(PAD_SVl(PL_op->op_targ))); |
4876 | sv_setsv(TARG, ERRSV); | |
4877 | CLEAR_ERRSV(); | |
4878 | ||
4879 | return cLOGOP->op_other; | |
4880 | } | |
4881 | ||
edb2152a NC |
4882 | /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it |
4883 | close to the related Perl_create_eval_scope. */ | |
4884 | void | |
4885 | Perl_delete_eval_scope(pTHX) | |
a0d0e21e | 4886 | { |
eb578fdb | 4887 | PERL_CONTEXT *cx; |
1604cfb0 | 4888 | |
4ebe6e95 | 4889 | cx = CX_CUR(); |
2f450c1b | 4890 | CX_LEAVE_SCOPE(cx); |
13febba5 | 4891 | cx_popeval(cx); |
ed8ff0f3 | 4892 | cx_popblock(cx); |
5da525e9 | 4893 | CX_POP(cx); |
edb2152a | 4894 | } |
a0d0e21e | 4895 | |
edb2152a NC |
4896 | /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was |
4897 | also needed by Perl_fold_constants. */ | |
274ed8ae DM |
4898 | void |
4899 | Perl_create_eval_scope(pTHX_ OP *retop, U32 flags) | |
edb2152a NC |
4900 | { |
4901 | PERL_CONTEXT *cx; | |
1c23e2bd | 4902 | const U8 gimme = GIMME_V; |
1604cfb0 | 4903 | |
99dbf645 | 4904 | cx = cx_pushblock((CXt_EVAL|CXp_EVALBLOCK), gimme, |
490576d1 | 4905 | PL_stack_sp, PL_savestack_ix); |
13febba5 | 4906 | cx_pusheval(cx, retop, NULL); |
a0d0e21e | 4907 | |
faef0170 | 4908 | PL_in_eval = EVAL_INEVAL; |
edb2152a | 4909 | if (flags & G_KEEPERR) |
1604cfb0 | 4910 | PL_in_eval |= EVAL_KEEPERR; |
ab69dbc2 | 4911 | else |
1604cfb0 | 4912 | CLEAR_ERRSV(); |
edb2152a | 4913 | if (flags & G_FAKINGEVAL) { |
1604cfb0 | 4914 | PL_eval_root = PL_op; /* Only needed so that goto works right. */ |
edb2152a | 4915 | } |
edb2152a NC |
4916 | } |
4917 | ||
4918 | PP(pp_entertry) | |
4919 | { | |
a1325b90 PE |
4920 | OP *retop = cLOGOP->op_other->op_next; |
4921 | ||
a7304a68 | 4922 | /* If a suitable JMPENV catch frame isn't present, call docatch(), |
a6ceaeb9 DM |
4923 | * which will: |
4924 | * - add such a frame, and | |
4925 | * - start a new RUNOPS loop, which will (as the first op to run), | |
4926 | * recursively call this pp function again. | |
4927 | * The main body of this function is then executed by the inner call. | |
4928 | */ | |
4929 | if (CATCH_GET) | |
4930 | return docatch(Perl_pp_entertry); | |
d7e3f70f Z |
4931 | |
4932 | assert(!CATCH_GET); | |
a1325b90 | 4933 | |
383bf72f | 4934 | create_eval_scope(retop, 0); |
a1325b90 | 4935 | |
d7e3f70f | 4936 | return PL_op->op_next; |
a0d0e21e LW |
4937 | } |
4938 | ||
c349b9a0 DM |
4939 | |
4940 | /* also tail-called by pp_return */ | |
4941 | ||
a0d0e21e LW |
4942 | PP(pp_leavetry) |
4943 | { | |
f5ddd604 | 4944 | SV **oldsp; |
1c23e2bd | 4945 | U8 gimme; |
eb578fdb | 4946 | PERL_CONTEXT *cx; |
334ea179 | 4947 | OP *retop; |
a0d0e21e | 4948 | |
011c3814 | 4949 | PERL_ASYNC_CHECK(); |
61d3b95a | 4950 | |
4ebe6e95 | 4951 | cx = CX_CUR(); |
61d3b95a | 4952 | assert(CxTYPE(cx) == CXt_EVAL); |
f5ddd604 | 4953 | oldsp = PL_stack_base + cx->blk_oldsp; |
61d3b95a DM |
4954 | gimme = cx->blk_gimme; |
4955 | ||
b66d79a6 | 4956 | if (gimme == G_VOID) { |
f5ddd604 | 4957 | PL_stack_sp = oldsp; |
b66d79a6 DM |
4958 | /* free now to avoid late-called destructors clobbering $@ */ |
4959 | FREETMPS; | |
4960 | } | |
0663a8f8 | 4961 | else |
f5ddd604 | 4962 | leave_adjust_stacks(oldsp, oldsp, gimme, 1); |
2f450c1b | 4963 | CX_LEAVE_SCOPE(cx); |
13febba5 | 4964 | cx_popeval(cx); |
ed8ff0f3 | 4965 | cx_popblock(cx); |
383bf72f | 4966 | retop = CxTRY(cx) ? PL_op->op_next : cx->blk_eval.retop; |
5da525e9 | 4967 | CX_POP(cx); |
67f63db7 | 4968 | |
ab69dbc2 | 4969 | CLEAR_ERRSV(); |
0663a8f8 | 4970 | return retop; |
a0d0e21e LW |
4971 | } |
4972 | ||
0d863452 RH |
4973 | PP(pp_entergiven) |
4974 | { | |
20b7effb | 4975 | dSP; |
eb578fdb | 4976 | PERL_CONTEXT *cx; |
1c23e2bd | 4977 | const U8 gimme = GIMME_V; |
b95eccd3 DM |
4978 | SV *origsv = DEFSV; |
4979 | SV *newsv = POPs; | |
0d863452 | 4980 | |
5d051ee0 | 4981 | assert(!PL_op->op_targ); /* used to be set for lexical $_ */ |
b95eccd3 | 4982 | GvSV(PL_defgv) = SvREFCNT_inc(newsv); |
0d863452 | 4983 | |
7896dde7 Z |
4984 | cx = cx_pushblock(CXt_GIVEN, gimme, SP, PL_savestack_ix); |
4985 | cx_pushgiven(cx, origsv); | |
0d863452 RH |
4986 | |
4987 | RETURN; | |
4988 | } | |
4989 | ||
7896dde7 Z |
4990 | PP(pp_leavegiven) |
4991 | { | |
4992 | PERL_CONTEXT *cx; | |
4993 | U8 gimme; | |
4994 | SV **oldsp; | |
4995 | PERL_UNUSED_CONTEXT; | |
4996 | ||
4997 | cx = CX_CUR(); | |
4998 | assert(CxTYPE(cx) == CXt_GIVEN); | |
4999 | oldsp = PL_stack_base + cx->blk_oldsp; | |
5000 | gimme = cx->blk_gimme; | |
5001 | ||
5002 | if (gimme == G_VOID) | |
5003 | PL_stack_sp = oldsp; | |
5004 | else | |
5005 | leave_adjust_stacks(oldsp, oldsp, gimme, 1); | |
5006 | ||
5007 | CX_LEAVE_SCOPE(cx); | |
5008 | cx_popgiven(cx); | |
5009 | cx_popblock(cx); | |
5010 | CX_POP(cx); | |
5011 | ||
5012 | return NORMAL; | |
5013 | } | |
5014 | ||
5015 | /* Helper routines used by pp_smartmatch */ | |
5016 | STATIC PMOP * | |
5017 | S_make_matcher(pTHX_ REGEXP *re) | |
5018 | { | |
cf171702 | 5019 | PMOP *matcher = cPMOPx(newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED)); |
7896dde7 Z |
5020 | |
5021 | PERL_ARGS_ASSERT_MAKE_MATCHER; | |
5022 | ||
5023 | PM_SETRE(matcher, ReREFCNT_inc(re)); | |
5024 | ||
5025 | SAVEFREEOP((OP *) matcher); | |
5026 | ENTER_with_name("matcher"); SAVETMPS; | |
5027 | SAVEOP(); | |
5028 | return matcher; | |
5029 | } | |
5030 | ||
5031 | STATIC bool | |
5032 | S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv) | |
5033 | { | |
5034 | dSP; | |
5035 | bool result; | |
5036 | ||
5037 | PERL_ARGS_ASSERT_MATCHER_MATCHES_SV; | |
5038 | ||
5039 | PL_op = (OP *) matcher; | |
5040 | XPUSHs(sv); | |
5041 | PUTBACK; | |
5042 | (void) Perl_pp_match(aTHX); | |
5043 | SPAGAIN; | |
5044 | result = SvTRUEx(POPs); | |
5045 | PUTBACK; | |
5046 | ||
5047 | return result; | |
5048 | } | |
5049 | ||
5050 | STATIC void | |
5051 | S_destroy_matcher(pTHX_ PMOP *matcher) | |
5052 | { | |
5053 | PERL_ARGS_ASSERT_DESTROY_MATCHER; | |
5054 | PERL_UNUSED_ARG(matcher); | |
5055 | ||
5056 | FREETMPS; | |
5057 | LEAVE_with_name("matcher"); | |
5058 | } | |
5059 | ||
5060 | /* Do a smart match */ | |
0d863452 RH |
5061 | PP(pp_smartmatch) |
5062 | { | |
7896dde7 Z |
5063 | DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n")); |
5064 | return do_smartmatch(NULL, NULL, 0); | |
5065 | } | |
5066 | ||
5067 | /* This version of do_smartmatch() implements the | |
5068 | * table of smart matches that is found in perlsyn. | |
5069 | */ | |
5070 | STATIC OP * | |
5071 | S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied) | |
5072 | { | |
0d863452 | 5073 | dSP; |
7896dde7 Z |
5074 | |
5075 | bool object_on_left = FALSE; | |
5076 | SV *e = TOPs; /* e is for 'expression' */ | |
5077 | SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */ | |
5078 | ||
5079 | /* Take care only to invoke mg_get() once for each argument. | |
5080 | * Currently we do this by copying the SV if it's magical. */ | |
5081 | if (d) { | |
1604cfb0 MS |
5082 | if (!copied && SvGMAGICAL(d)) |
5083 | d = sv_mortalcopy(d); | |
7896dde7 Z |
5084 | } |
5085 | else | |
1604cfb0 | 5086 | d = &PL_sv_undef; |
6f1401dc | 5087 | |
7896dde7 Z |
5088 | assert(e); |
5089 | if (SvGMAGICAL(e)) | |
1604cfb0 | 5090 | e = sv_mortalcopy(e); |
7896dde7 Z |
5091 | |
5092 | /* First of all, handle overload magic of the rightmost argument */ | |
5093 | if (SvAMAGIC(e)) { | |
1604cfb0 MS |
5094 | SV * tmpsv; |
5095 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n")); | |
5096 | DEBUG_M(Perl_deb(aTHX_ " attempting overload\n")); | |
7896dde7 | 5097 | |
1604cfb0 MS |
5098 | tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft); |
5099 | if (tmpsv) { | |
5100 | SPAGAIN; | |
5101 | (void)POPs; | |
5102 | SETs(tmpsv); | |
5103 | RETURN; | |
5104 | } | |
5105 | DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n")); | |
7896dde7 Z |
5106 | } |
5107 | ||
5108 | SP -= 2; /* Pop the values */ | |
e8fe1b7c | 5109 | PUTBACK; |
7896dde7 Z |
5110 | |
5111 | /* ~~ undef */ | |
5112 | if (!SvOK(e)) { | |
1604cfb0 MS |
5113 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n")); |
5114 | if (SvOK(d)) | |
5115 | RETPUSHNO; | |
5116 | else | |
5117 | RETPUSHYES; | |
7896dde7 Z |
5118 | } |
5119 | ||
5120 | if (SvROK(e) && SvOBJECT(SvRV(e)) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) { | |
1604cfb0 MS |
5121 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n")); |
5122 | Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation"); | |
7896dde7 Z |
5123 | } |
5124 | if (SvROK(d) && SvOBJECT(SvRV(d)) && (SvTYPE(SvRV(d)) != SVt_REGEXP)) | |
1604cfb0 | 5125 | object_on_left = TRUE; |
7896dde7 Z |
5126 | |
5127 | /* ~~ sub */ | |
5128 | if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) { | |
1604cfb0 MS |
5129 | I32 c; |
5130 | if (object_on_left) { | |
5131 | goto sm_any_sub; /* Treat objects like scalars */ | |
5132 | } | |
5133 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
5134 | /* Test sub truth for each key */ | |
5135 | HE *he; | |
5136 | bool andedresults = TRUE; | |
5137 | HV *hv = (HV*) SvRV(d); | |
5138 | I32 numkeys = hv_iterinit(hv); | |
5139 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n")); | |
5140 | if (numkeys == 0) | |
5141 | RETPUSHYES; | |
5142 | while ( (he = hv_iternext(hv)) ) { | |
5143 | DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n")); | |
5144 | ENTER_with_name("smartmatch_hash_key_test"); | |
5145 | SAVETMPS; | |
5146 | PUSHMARK(SP); | |
5147 | PUSHs(hv_iterkeysv(he)); | |
5148 | PUTBACK; | |
5149 | c = call_sv(e, G_SCALAR); | |
5150 | SPAGAIN; | |
5151 | if (c == 0) | |
5152 | andedresults = FALSE; | |
5153 | else | |
5154 | andedresults = SvTRUEx(POPs) && andedresults; | |
5155 | FREETMPS; | |
5156 | LEAVE_with_name("smartmatch_hash_key_test"); | |
5157 | } | |
5158 | if (andedresults) | |
5159 | RETPUSHYES; | |
5160 | else | |
5161 | RETPUSHNO; | |
5162 | } | |
5163 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
5164 | /* Test sub truth for each element */ | |
5165 | Size_t i; | |
5166 | bool andedresults = TRUE; | |
5167 | AV *av = (AV*) SvRV(d); | |
5168 | const Size_t len = av_count(av); | |
5169 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n")); | |
5170 | if (len == 0) | |
5171 | RETPUSHYES; | |
5172 | for (i = 0; i < len; ++i) { | |
5173 | SV * const * const svp = av_fetch(av, i, FALSE); | |
5174 | DEBUG_M(Perl_deb(aTHX_ " testing array element...\n")); | |
5175 | ENTER_with_name("smartmatch_array_elem_test"); | |
5176 | SAVETMPS; | |
5177 | PUSHMARK(SP); | |
5178 | if (svp) | |
5179 | PUSHs(*svp); | |
5180 | PUTBACK; | |
5181 | c = call_sv(e, G_SCALAR); | |
5182 | SPAGAIN; | |
5183 | if (c == 0) | |
5184 | andedresults = FALSE; | |
5185 | else | |
5186 | andedresults = SvTRUEx(POPs) && andedresults; | |
5187 | FREETMPS; | |
5188 | LEAVE_with_name("smartmatch_array_elem_test"); | |
5189 | } | |
5190 | if (andedresults) | |
5191 | RETPUSHYES; | |
5192 | else | |
5193 | RETPUSHNO; | |
5194 | } | |
5195 | else { | |
5196 | sm_any_sub: | |
5197 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n")); | |
5198 | ENTER_with_name("smartmatch_coderef"); | |
5199 | SAVETMPS; | |
5200 | PUSHMARK(SP); | |
5201 | PUSHs(d); | |
5202 | PUTBACK; | |
5203 | c = call_sv(e, G_SCALAR); | |
5204 | SPAGAIN; | |
5205 | if (c == 0) | |
5206 | PUSHs(&PL_sv_no); | |
5207 | else if (SvTEMP(TOPs)) | |
5208 | SvREFCNT_inc_void(TOPs); | |
5209 | FREETMPS; | |
5210 | LEAVE_with_name("smartmatch_coderef"); | |
5211 | RETURN; | |
5212 | } | |
7896dde7 Z |
5213 | } |
5214 | /* ~~ %hash */ | |
5215 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { | |
1604cfb0 MS |
5216 | if (object_on_left) { |
5217 | goto sm_any_hash; /* Treat objects like scalars */ | |
5218 | } | |
5219 | else if (!SvOK(d)) { | |
5220 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n")); | |
5221 | RETPUSHNO; | |
5222 | } | |
5223 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
5224 | /* Check that the key-sets are identical */ | |
5225 | HE *he; | |
5226 | HV *other_hv = MUTABLE_HV(SvRV(d)); | |
5227 | bool tied; | |
5228 | bool other_tied; | |
5229 | U32 this_key_count = 0, | |
5230 | other_key_count = 0; | |
5231 | HV *hv = MUTABLE_HV(SvRV(e)); | |
5232 | ||
5233 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n")); | |
5234 | /* Tied hashes don't know how many keys they have. */ | |
5235 | tied = cBOOL(SvTIED_mg((SV*)hv, PERL_MAGIC_tied)); | |
5236 | other_tied = cBOOL(SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)); | |
5237 | if (!tied ) { | |
5238 | if(other_tied) { | |
5239 | /* swap HV sides */ | |
5240 | HV * const temp = other_hv; | |
5241 | other_hv = hv; | |
5242 | hv = temp; | |
5243 | tied = TRUE; | |
5244 | other_tied = FALSE; | |
5245 | } | |
5246 | else if(HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv)) | |
5247 | RETPUSHNO; | |
5248 | } | |
5249 | ||
5250 | /* The hashes have the same number of keys, so it suffices | |
5251 | to check that one is a subset of the other. */ | |
5252 | (void) hv_iterinit(hv); | |
5253 | while ( (he = hv_iternext(hv)) ) { | |
5254 | SV *key = hv_iterkeysv(he); | |
5255 | ||
5256 | DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n")); | |
5257 | ++ this_key_count; | |
5258 | ||
5259 | if(!hv_exists_ent(other_hv, key, 0)) { | |
5260 | (void) hv_iterinit(hv); /* reset iterator */ | |
5261 | RETPUSHNO; | |
5262 | } | |
5263 | } | |
5264 | ||
5265 | if (other_tied) { | |
5266 | (void) hv_iterinit(other_hv); | |
5267 | while ( hv_iternext(other_hv) ) | |
5268 | ++other_key_count; | |
5269 | } | |
5270 | else | |
5271 | other_key_count = HvUSEDKEYS(other_hv); | |
5272 | ||
5273 | if (this_key_count != other_key_count) | |
5274 | RETPUSHNO; | |
5275 | else | |
5276 | RETPUSHYES; | |
5277 | } | |
5278 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
5279 | AV * const other_av = MUTABLE_AV(SvRV(d)); | |
5280 | const Size_t other_len = av_count(other_av); | |
5281 | Size_t i; | |
5282 | HV *hv = MUTABLE_HV(SvRV(e)); | |
5283 | ||
5284 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n")); | |
5285 | for (i = 0; i < other_len; ++i) { | |
5286 | SV ** const svp = av_fetch(other_av, i, FALSE); | |
5287 | DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n")); | |
5288 | if (svp) { /* ??? When can this not happen? */ | |
5289 | if (hv_exists_ent(hv, *svp, 0)) | |
5290 | RETPUSHYES; | |
5291 | } | |
5292 | } | |
5293 | RETPUSHNO; | |
5294 | } | |
5295 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) { | |
5296 | DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n")); | |
5297 | sm_regex_hash: | |
5298 | { | |
5299 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(d)); | |
5300 | HE *he; | |
5301 | HV *hv = MUTABLE_HV(SvRV(e)); | |
5302 | ||
5303 | (void) hv_iterinit(hv); | |
5304 | while ( (he = hv_iternext(hv)) ) { | |
5305 | DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n")); | |
7896dde7 | 5306 | PUTBACK; |
1604cfb0 | 5307 | if (matcher_matches_sv(matcher, hv_iterkeysv(he))) { |
7896dde7 | 5308 | SPAGAIN; |
1604cfb0 MS |
5309 | (void) hv_iterinit(hv); |
5310 | destroy_matcher(matcher); | |
5311 | RETPUSHYES; | |
5312 | } | |
7896dde7 | 5313 | SPAGAIN; |
1604cfb0 MS |
5314 | } |
5315 | destroy_matcher(matcher); | |
5316 | RETPUSHNO; | |
5317 | } | |
5318 | } | |
5319 | else { | |
5320 | sm_any_hash: | |
5321 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n")); | |
5322 | if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0)) | |
5323 | RETPUSHYES; | |
5324 | else | |
5325 | RETPUSHNO; | |
5326 | } | |
7896dde7 Z |
5327 | } |
5328 | /* ~~ @array */ | |
5329 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) { | |
1604cfb0 MS |
5330 | if (object_on_left) { |
5331 | goto sm_any_array; /* Treat objects like scalars */ | |
5332 | } | |
5333 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
5334 | AV * const other_av = MUTABLE_AV(SvRV(e)); | |
5335 | const Size_t other_len = av_count(other_av); | |
5336 | Size_t i; | |
5337 | ||
5338 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n")); | |
5339 | for (i = 0; i < other_len; ++i) { | |
5340 | SV ** const svp = av_fetch(other_av, i, FALSE); | |
5341 | ||
5342 | DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n")); | |
5343 | if (svp) { /* ??? When can this not happen? */ | |
5344 | if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0)) | |
5345 | RETPUSHYES; | |
5346 | } | |
5347 | } | |
5348 | RETPUSHNO; | |
5349 | } | |
5350 | if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
5351 | AV *other_av = MUTABLE_AV(SvRV(d)); | |
5352 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n")); | |
5353 | if (av_count(MUTABLE_AV(SvRV(e))) != av_count(other_av)) | |
5354 | RETPUSHNO; | |
5355 | else { | |
a35255b7 KW |
5356 | Size_t i; |
5357 | const Size_t other_len = av_count(other_av); | |
7896dde7 | 5358 | |
1604cfb0 | 5359 | if (NULL == seen_this) { |
7ea8b04b | 5360 | seen_this = (HV*)newSV_type_mortal(SVt_PVHV); |
1604cfb0 MS |
5361 | } |
5362 | if (NULL == seen_other) { | |
7ea8b04b | 5363 | seen_other = (HV*)newSV_type_mortal(SVt_PVHV); |
1604cfb0 MS |
5364 | } |
5365 | for(i = 0; i < other_len; ++i) { | |
5366 | SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5367 | SV * const * const other_elem = av_fetch(other_av, i, FALSE); | |
5368 | ||
5369 | if (!this_elem || !other_elem) { | |
5370 | if ((this_elem && SvOK(*this_elem)) | |
5371 | || (other_elem && SvOK(*other_elem))) | |
5372 | RETPUSHNO; | |
5373 | } | |
5374 | else if (hv_exists_ent(seen_this, | |
5375 | sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) || | |
5376 | hv_exists_ent(seen_other, | |
5377 | sv_2mortal(newSViv(PTR2IV(*other_elem))), 0)) | |
5378 | { | |
5379 | if (*this_elem != *other_elem) | |
5380 | RETPUSHNO; | |
5381 | } | |
5382 | else { | |
5383 | (void)hv_store_ent(seen_this, | |
5384 | sv_2mortal(newSViv(PTR2IV(*this_elem))), | |
5385 | &PL_sv_undef, 0); | |
5386 | (void)hv_store_ent(seen_other, | |
5387 | sv_2mortal(newSViv(PTR2IV(*other_elem))), | |
5388 | &PL_sv_undef, 0); | |
5389 | PUSHs(*other_elem); | |
5390 | PUSHs(*this_elem); | |
5391 | ||
5392 | PUTBACK; | |
5393 | DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n")); | |
5394 | (void) do_smartmatch(seen_this, seen_other, 0); | |
5395 | SPAGAIN; | |
5396 | DEBUG_M(Perl_deb(aTHX_ " recursion finished\n")); | |
5397 | ||
5398 | if (!SvTRUEx(POPs)) | |
5399 | RETPUSHNO; | |
5400 | } | |
5401 | } | |
5402 | RETPUSHYES; | |
5403 | } | |
5404 | } | |
5405 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) { | |
5406 | DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n")); | |
5407 | sm_regex_array: | |
5408 | { | |
5409 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(d)); | |
5410 | const Size_t this_len = av_count(MUTABLE_AV(SvRV(e))); | |
5411 | Size_t i; | |
5412 | ||
5413 | for(i = 0; i < this_len; ++i) { | |
5414 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5415 | DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n")); | |
7896dde7 | 5416 | PUTBACK; |
1604cfb0 | 5417 | if (svp && matcher_matches_sv(matcher, *svp)) { |
7896dde7 | 5418 | SPAGAIN; |
1604cfb0 MS |
5419 | destroy_matcher(matcher); |
5420 | RETPUSHYES; | |
5421 | } | |
5422 | SPAGAIN; | |
5423 | } | |
5424 | destroy_matcher(matcher); | |
5425 | RETPUSHNO; | |
5426 | } | |
5427 | } | |
5428 | else if (!SvOK(d)) { | |
5429 | /* undef ~~ array */ | |
5430 | const Size_t this_len = av_count(MUTABLE_AV(SvRV(e))); | |
5431 | Size_t i; | |
5432 | ||
5433 | DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n")); | |
5434 | for (i = 0; i < this_len; ++i) { | |
5435 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5436 | DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n")); | |
5437 | if (!svp || !SvOK(*svp)) | |
5438 | RETPUSHYES; | |
5439 | } | |
5440 | RETPUSHNO; | |
5441 | } | |
5442 | else { | |
5443 | sm_any_array: | |
5444 | { | |
5445 | Size_t i; | |
5446 | const Size_t this_len = av_count(MUTABLE_AV(SvRV(e))); | |
5447 | ||
5448 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n")); | |
5449 | for (i = 0; i < this_len; ++i) { | |
5450 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5451 | if (!svp) | |
5452 | continue; | |
5453 | ||
5454 | PUSHs(d); | |
5455 | PUSHs(*svp); | |
5456 | PUTBACK; | |
5457 | /* infinite recursion isn't supposed to happen here */ | |
5458 | DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n")); | |
5459 | (void) do_smartmatch(NULL, NULL, 1); | |
7896dde7 | 5460 | SPAGAIN; |
1604cfb0 MS |
5461 | DEBUG_M(Perl_deb(aTHX_ " recursion finished\n")); |
5462 | if (SvTRUEx(POPs)) | |
5463 | RETPUSHYES; | |
5464 | } | |
5465 | RETPUSHNO; | |
5466 | } | |
5467 | } | |
7896dde7 Z |
5468 | } |
5469 | /* ~~ qr// */ | |
5470 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) { | |
1604cfb0 MS |
5471 | if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { |
5472 | SV *t = d; d = e; e = t; | |
5473 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n")); | |
5474 | goto sm_regex_hash; | |
5475 | } | |
5476 | else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
5477 | SV *t = d; d = e; e = t; | |
5478 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n")); | |
5479 | goto sm_regex_array; | |
5480 | } | |
5481 | else { | |
5482 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(e)); | |
7896dde7 Z |
5483 | bool result; |
5484 | ||
1604cfb0 MS |
5485 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n")); |
5486 | PUTBACK; | |
5487 | result = matcher_matches_sv(matcher, d); | |
7896dde7 | 5488 | SPAGAIN; |
1604cfb0 MS |
5489 | PUSHs(result ? &PL_sv_yes : &PL_sv_no); |
5490 | destroy_matcher(matcher); | |
5491 | RETURN; | |
5492 | } | |
7896dde7 Z |
5493 | } |
5494 | /* ~~ scalar */ | |
5495 | /* See if there is overload magic on left */ | |
5496 | else if (object_on_left && SvAMAGIC(d)) { | |
1604cfb0 MS |
5497 | SV *tmpsv; |
5498 | DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n")); | |
5499 | DEBUG_M(Perl_deb(aTHX_ " attempting overload\n")); | |
5500 | PUSHs(d); PUSHs(e); | |
5501 | PUTBACK; | |
5502 | tmpsv = amagic_call(d, e, smart_amg, AMGf_noright); | |
5503 | if (tmpsv) { | |
5504 | SPAGAIN; | |
5505 | (void)POPs; | |
5506 | SETs(tmpsv); | |
5507 | RETURN; | |
5508 | } | |
5509 | SP -= 2; | |
5510 | DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n")); | |
5511 | goto sm_any_scalar; | |
7896dde7 Z |
5512 | } |
5513 | else if (!SvOK(d)) { | |
1604cfb0 MS |
5514 | /* undef ~~ scalar ; we already know that the scalar is SvOK */ |
5515 | DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n")); | |
5516 | RETPUSHNO; | |
7896dde7 Z |
5517 | } |
5518 | else | |
5519 | sm_any_scalar: | |
5520 | if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) { | |
1604cfb0 MS |
5521 | DEBUG_M(if (SvNIOK(e)) |
5522 | Perl_deb(aTHX_ " applying rule Any-Num\n"); | |
5523 | else | |
5524 | Perl_deb(aTHX_ " applying rule Num-numish\n"); | |
5525 | ); | |
5526 | /* numeric comparison */ | |
5527 | PUSHs(d); PUSHs(e); | |
5528 | PUTBACK; | |
5529 | if (CopHINTS_get(PL_curcop) & HINT_INTEGER) | |
5530 | (void) Perl_pp_i_eq(aTHX); | |
5531 | else | |
5532 | (void) Perl_pp_eq(aTHX); | |
5533 | SPAGAIN; | |
5534 | if (SvTRUEx(POPs)) | |
5535 | RETPUSHYES; | |
5536 | else | |
5537 | RETPUSHNO; | |
0d863452 | 5538 | } |
7896dde7 Z |
5539 | |
5540 | /* As a last resort, use string comparison */ | |
5541 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n")); | |
5542 | PUSHs(d); PUSHs(e); | |
5543 | PUTBACK; | |
5544 | return Perl_pp_seq(aTHX); | |
0d863452 RH |
5545 | } |
5546 | ||
7896dde7 | 5547 | PP(pp_enterwhen) |
0d863452 | 5548 | { |
20b7effb | 5549 | dSP; |
eb578fdb | 5550 | PERL_CONTEXT *cx; |
1c23e2bd | 5551 | const U8 gimme = GIMME_V; |
0d863452 RH |
5552 | |
5553 | /* This is essentially an optimization: if the match | |
5554 | fails, we don't want to push a context and then | |
5555 | pop it again right away, so we skip straight | |
7896dde7 | 5556 | to the op that follows the leavewhen. |
25b991bf | 5557 | RETURNOP calls PUTBACK which restores the stack pointer after the POPs. |
0d863452 | 5558 | */ |
7896dde7 | 5559 | if (!(PL_op->op_flags & OPf_SPECIAL) && !SvTRUEx(POPs)) { |
1604cfb0 MS |
5560 | if (gimme == G_SCALAR) |
5561 | PUSHs(&PL_sv_undef); | |
5562 | RETURNOP(cLOGOP->op_other->op_next); | |
b98da25d | 5563 | } |
0d863452 | 5564 | |
7896dde7 Z |
5565 | cx = cx_pushblock(CXt_WHEN, gimme, SP, PL_savestack_ix); |
5566 | cx_pushwhen(cx); | |
0d863452 RH |
5567 | |
5568 | RETURN; | |
5569 | } | |
5570 | ||
7896dde7 | 5571 | PP(pp_leavewhen) |
0d863452 | 5572 | { |
c08f093b | 5573 | I32 cxix; |
eb578fdb | 5574 | PERL_CONTEXT *cx; |
1c23e2bd | 5575 | U8 gimme; |
f5ddd604 | 5576 | SV **oldsp; |
8aef2117 | 5577 | |
4ebe6e95 | 5578 | cx = CX_CUR(); |
7896dde7 | 5579 | assert(CxTYPE(cx) == CXt_WHEN); |
8aef2117 | 5580 | gimme = cx->blk_gimme; |
0d863452 | 5581 | |
7896dde7 | 5582 | cxix = dopoptogivenfor(cxstack_ix); |
c08f093b | 5583 | if (cxix < 0) |
1604cfb0 MS |
5584 | /* diag_listed_as: Can't "when" outside a topicalizer */ |
5585 | DIE(aTHX_ "Can't \"%s\" outside a topicalizer", | |
5586 | PL_op->op_flags & OPf_SPECIAL ? "default" : "when"); | |
c08f093b | 5587 | |
f5ddd604 | 5588 | oldsp = PL_stack_base + cx->blk_oldsp; |
0663a8f8 | 5589 | if (gimme == G_VOID) |
f5ddd604 | 5590 | PL_stack_sp = oldsp; |
0663a8f8 | 5591 | else |
f5ddd604 | 5592 | leave_adjust_stacks(oldsp, oldsp, gimme, 1); |
75bc488d | 5593 | |
7896dde7 | 5594 | /* pop the WHEN, BLOCK and anything else before the GIVEN/FOR */ |
8aef2117 DM |
5595 | assert(cxix < cxstack_ix); |
5596 | dounwind(cxix); | |
c08f093b VP |
5597 | |
5598 | cx = &cxstack[cxix]; | |
5599 | ||
7896dde7 | 5600 | if (CxFOREACH(cx)) { |
590529d8 DM |
5601 | /* emulate pp_next. Note that any stack(s) cleanup will be |
5602 | * done by the pp_unstack which op_nextop should point to */ | |
7e637ba4 | 5603 | cx = CX_CUR(); |
1604cfb0 MS |
5604 | cx_topblock(cx); |
5605 | PL_curcop = cx->blk_oldcop; | |
5606 | return cx->blk_loop.my_op->op_nextop; | |
c08f093b | 5607 | } |
47c9d59f | 5608 | else { |
1604cfb0 | 5609 | PERL_ASYNC_CHECK(); |
7896dde7 | 5610 | assert(cx->blk_givwhen.leave_op->op_type == OP_LEAVEGIVEN); |
1604cfb0 | 5611 | return cx->blk_givwhen.leave_op; |
47c9d59f | 5612 | } |
0d863452 RH |
5613 | } |
5614 | ||
5615 | PP(pp_continue) | |
5616 | { | |
0d863452 | 5617 | I32 cxix; |
eb578fdb | 5618 | PERL_CONTEXT *cx; |
5da525e9 | 5619 | OP *nextop; |
0d863452 | 5620 | |
7896dde7 | 5621 | cxix = dopoptowhen(cxstack_ix); |
0d863452 | 5622 | if (cxix < 0) |
1604cfb0 | 5623 | DIE(aTHX_ "Can't \"continue\" outside a when block"); |
c08f093b | 5624 | |
0d863452 RH |
5625 | if (cxix < cxstack_ix) |
5626 | dounwind(cxix); | |
5627 | ||
4ebe6e95 | 5628 | cx = CX_CUR(); |
7896dde7 | 5629 | assert(CxTYPE(cx) == CXt_WHEN); |
4df352a8 | 5630 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; |
2f450c1b | 5631 | CX_LEAVE_SCOPE(cx); |
7896dde7 | 5632 | cx_popwhen(cx); |
ed8ff0f3 | 5633 | cx_popblock(cx); |
7896dde7 | 5634 | nextop = cx->blk_givwhen.leave_op->op_next; |
5da525e9 | 5635 | CX_POP(cx); |
c08f093b | 5636 | |
5da525e9 | 5637 | return nextop; |
0d863452 RH |
5638 | } |
5639 | ||
7896dde7 Z |
5640 | PP(pp_break) |
5641 | { | |
5642 | I32 cxix; | |
5643 | PERL_CONTEXT *cx; | |
5644 | ||
5645 | cxix = dopoptogivenfor(cxstack_ix); | |
5646 | if (cxix < 0) | |
1604cfb0 | 5647 | DIE(aTHX_ "Can't \"break\" outside a given block"); |
7896dde7 Z |
5648 | |
5649 | cx = &cxstack[cxix]; | |
5650 | if (CxFOREACH(cx)) | |
1604cfb0 | 5651 | DIE(aTHX_ "Can't \"break\" in a loop topicalizer"); |
7896dde7 Z |
5652 | |
5653 | if (cxix < cxstack_ix) | |
5654 | dounwind(cxix); | |
5655 | ||
5656 | /* Restore the sp at the time we entered the given block */ | |
5657 | cx = CX_CUR(); | |
5658 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; | |
5659 | ||
5660 | return cx->blk_givwhen.leave_op; | |
5661 | } | |
5662 | ||
f79e2ff9 | 5663 | static void |
e5e291f5 | 5664 | _invoke_defer_block(pTHX_ U8 type, void *_arg) |
f79e2ff9 PE |
5665 | { |
5666 | OP *start = (OP *)_arg; | |
5667 | #ifdef DEBUGGING | |
5668 | I32 was_cxstack_ix = cxstack_ix; | |
5669 | #endif | |
5670 | ||
e5e291f5 | 5671 | cx_pushblock(type, G_VOID, PL_stack_sp, PL_savestack_ix); |
f79e2ff9 PE |
5672 | ENTER; |
5673 | SAVETMPS; | |
5674 | ||
5675 | SAVEOP(); | |
5676 | PL_op = start; | |
5677 | ||
5678 | CALLRUNOPS(aTHX); | |
5679 | ||
5680 | FREETMPS; | |
5681 | LEAVE; | |
5682 | ||
5683 | { | |
5684 | PERL_CONTEXT *cx; | |
5685 | ||
5686 | cx = CX_CUR(); | |
5687 | assert(CxTYPE(cx) == CXt_DEFER); | |
5688 | ||
5689 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; | |
5690 | ||
5691 | CX_LEAVE_SCOPE(cx); | |
5692 | cx_popblock(cx); | |
5693 | CX_POP(cx); | |
5694 | } | |
5695 | ||
5696 | assert(cxstack_ix == was_cxstack_ix); | |
5697 | } | |
5698 | ||
e5e291f5 PE |
5699 | static void |
5700 | invoke_defer_block(pTHX_ void *_arg) | |
5701 | { | |
5702 | _invoke_defer_block(aTHX_ CXt_DEFER, _arg); | |
5703 | } | |
5704 | ||
5705 | static void | |
5706 | invoke_finally_block(pTHX_ void *_arg) | |
5707 | { | |
5708 | _invoke_defer_block(aTHX_ CXt_DEFER|CXp_FINALLY, _arg); | |
5709 | } | |
5710 | ||
f79e2ff9 PE |
5711 | PP(pp_pushdefer) |
5712 | { | |
e5e291f5 PE |
5713 | if(PL_op->op_private & OPpDEFER_FINALLY) |
5714 | SAVEDESTRUCTOR_X(invoke_finally_block, cLOGOP->op_other); | |
5715 | else | |
5716 | SAVEDESTRUCTOR_X(invoke_defer_block, cLOGOP->op_other); | |
f79e2ff9 PE |
5717 | |
5718 | return NORMAL; | |
5719 | } | |
5720 | ||
74e0ddf7 | 5721 | static MAGIC * |
cea2e8a9 | 5722 | S_doparseform(pTHX_ SV *sv) |
a0d0e21e LW |
5723 | { |
5724 | STRLEN len; | |
eb578fdb KW |
5725 | char *s = SvPV(sv, len); |
5726 | char *send; | |
5727 | char *base = NULL; /* start of current field */ | |
5728 | I32 skipspaces = 0; /* number of contiguous spaces seen */ | |
086b26f3 DM |
5729 | bool noblank = FALSE; /* ~ or ~~ seen on this line */ |
5730 | bool repeat = FALSE; /* ~~ seen on this line */ | |
5731 | bool postspace = FALSE; /* a text field may need right padding */ | |
dea28490 | 5732 | U32 *fops; |
eb578fdb | 5733 | U32 *fpc; |
086b26f3 | 5734 | U32 *linepc = NULL; /* position of last FF_LINEMARK */ |
eb578fdb | 5735 | I32 arg; |
086b26f3 DM |
5736 | bool ischop; /* it's a ^ rather than a @ */ |
5737 | bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */ | |
a1b95068 | 5738 | int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */ |
3808a683 DM |
5739 | MAGIC *mg = NULL; |
5740 | SV *sv_copy; | |
a0d0e21e | 5741 | |
7918f24d NC |
5742 | PERL_ARGS_ASSERT_DOPARSEFORM; |
5743 | ||
55497cff | 5744 | if (len == 0) |
1604cfb0 | 5745 | Perl_croak(aTHX_ "Null picture in formline"); |
ac27b0f5 | 5746 | |
3808a683 | 5747 | if (SvTYPE(sv) >= SVt_PVMG) { |
1604cfb0 MS |
5748 | /* This might, of course, still return NULL. */ |
5749 | mg = mg_find(sv, PERL_MAGIC_fm); | |
3808a683 | 5750 | } else { |
1604cfb0 | 5751 | sv_upgrade(sv, SVt_PVMG); |
3808a683 DM |
5752 | } |
5753 | ||
5754 | if (mg) { | |
1604cfb0 MS |
5755 | /* still the same as previously-compiled string? */ |
5756 | SV *old = mg->mg_obj; | |
82943faa KW |
5757 | if ( ! (cBOOL(SvUTF8(old)) ^ cBOOL(SvUTF8(sv))) |
5758 | && len == SvCUR(old) | |
5759 | && strnEQ(SvPVX(old), s, len) | |
1604cfb0 MS |
5760 | ) { |
5761 | DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n")); | |
5762 | return mg; | |
5763 | } | |
3808a683 | 5764 | |
1604cfb0 MS |
5765 | DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n")); |
5766 | Safefree(mg->mg_ptr); | |
5767 | mg->mg_ptr = NULL; | |
5768 | SvREFCNT_dec(old); | |
5769 | mg->mg_obj = NULL; | |
3808a683 | 5770 | } |
b57b1734 | 5771 | else { |
1604cfb0 MS |
5772 | DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n")); |
5773 | mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0); | |
b57b1734 | 5774 | } |
3808a683 DM |
5775 | |
5776 | sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv)); | |
5777 | s = SvPV(sv_copy, len); /* work on the copy, not the original */ | |
5778 | send = s + len; | |
5779 | ||
5780 | ||
815f25c6 DM |
5781 | /* estimate the buffer size needed */ |
5782 | for (base = s; s <= send; s++) { | |
1604cfb0 MS |
5783 | if (*s == '\n' || *s == '@' || *s == '^') |
5784 | maxops += 10; | |
815f25c6 DM |
5785 | } |
5786 | s = base; | |
c445ea15 | 5787 | base = NULL; |
815f25c6 | 5788 | |
a02a5408 | 5789 | Newx(fops, maxops, U32); |
a0d0e21e LW |
5790 | fpc = fops; |
5791 | ||
5792 | if (s < send) { | |
1604cfb0 MS |
5793 | linepc = fpc; |
5794 | *fpc++ = FF_LINEMARK; | |
5795 | noblank = repeat = FALSE; | |
5796 | base = s; | |
a0d0e21e LW |
5797 | } |
5798 | ||
5799 | while (s <= send) { | |
1604cfb0 MS |
5800 | switch (*s++) { |
5801 | default: | |
5802 | skipspaces = 0; | |
5803 | continue; | |
5804 | ||
5805 | case '~': | |
5806 | if (*s == '~') { | |
5807 | repeat = TRUE; | |
5808 | skipspaces++; | |
5809 | s++; | |
5810 | } | |
5811 | noblank = TRUE; | |
5812 | /* FALLTHROUGH */ | |
5813 | case ' ': case '\t': | |
5814 | skipspaces++; | |
5815 | continue; | |
a1b95068 | 5816 | case 0: |
1604cfb0 MS |
5817 | if (s < send) { |
5818 | skipspaces = 0; | |
a1b95068 | 5819 | continue; |
2165bd23 LM |
5820 | } |
5821 | /* FALLTHROUGH */ | |
1604cfb0 MS |
5822 | case '\n': |
5823 | arg = s - base; | |
5824 | skipspaces++; | |
5825 | arg -= skipspaces; | |
5826 | if (arg) { | |
5827 | if (postspace) | |
5828 | *fpc++ = FF_SPACE; | |
5829 | *fpc++ = FF_LITERAL; | |
5830 | *fpc++ = (U32)arg; | |
5831 | } | |
5832 | postspace = FALSE; | |
5833 | if (s <= send) | |
5834 | skipspaces--; | |
5835 | if (skipspaces) { | |
5836 | *fpc++ = FF_SKIP; | |
5837 | *fpc++ = (U32)skipspaces; | |
5838 | } | |
5839 | skipspaces = 0; | |
5840 | if (s <= send) | |
5841 | *fpc++ = FF_NEWLINE; | |
5842 | if (noblank) { | |
5843 | *fpc++ = FF_BLANK; | |
5844 | if (repeat) | |
5845 | arg = fpc - linepc + 1; | |
5846 | else | |
5847 | arg = 0; | |
5848 | *fpc++ = (U32)arg; | |
5849 | } | |
5850 | if (s < send) { | |
5851 | linepc = fpc; | |
5852 | *fpc++ = FF_LINEMARK; | |
5853 | noblank = repeat = FALSE; | |
5854 | base = s; | |
5855 | } | |
5856 | else | |
5857 | s++; | |
5858 | continue; | |
5859 | ||
5860 | case '@': | |
5861 | case '^': | |
5862 | ischop = s[-1] == '^'; | |
5863 | ||
5864 | if (postspace) { | |
5865 | *fpc++ = FF_SPACE; | |
5866 | postspace = FALSE; | |
5867 | } | |
5868 | arg = (s - base) - 1; | |
5869 | if (arg) { | |
5870 | *fpc++ = FF_LITERAL; | |
5871 | *fpc++ = (U32)arg; | |
5872 | } | |
5873 | ||
5874 | base = s - 1; | |
5875 | *fpc++ = FF_FETCH; | |
5876 | if (*s == '*') { /* @* or ^* */ | |
5877 | s++; | |
5878 | *fpc++ = 2; /* skip the @* or ^* */ | |
5879 | if (ischop) { | |
5880 | *fpc++ = FF_LINESNGL; | |
5881 | *fpc++ = FF_CHOP; | |
5882 | } else | |
5883 | *fpc++ = FF_LINEGLOB; | |
5884 | } | |
5885 | else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */ | |
5886 | arg = ischop ? FORM_NUM_BLANK : 0; | |
5887 | base = s - 1; | |
5888 | while (*s == '#') | |
5889 | s++; | |
5890 | if (*s == '.') { | |
06b5626a | 5891 | const char * const f = ++s; |
1604cfb0 MS |
5892 | while (*s == '#') |
5893 | s++; | |
5894 | arg |= FORM_NUM_POINT + (s - f); | |
5895 | } | |
5896 | *fpc++ = s - base; /* fieldsize for FETCH */ | |
5897 | *fpc++ = FF_DECIMAL; | |
76912796 | 5898 | *fpc++ = (U32)arg; |
a1b95068 | 5899 | unchopnum |= ! ischop; |
784707d5 JP |
5900 | } |
5901 | else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */ | |
a701009a | 5902 | arg = ischop ? FORM_NUM_BLANK : 0; |
1604cfb0 | 5903 | base = s - 1; |
784707d5 JP |
5904 | s++; /* skip the '0' first */ |
5905 | while (*s == '#') | |
5906 | s++; | |
5907 | if (*s == '.') { | |
06b5626a | 5908 | const char * const f = ++s; |
784707d5 JP |
5909 | while (*s == '#') |
5910 | s++; | |
a701009a | 5911 | arg |= FORM_NUM_POINT + (s - f); |
784707d5 JP |
5912 | } |
5913 | *fpc++ = s - base; /* fieldsize for FETCH */ | |
5914 | *fpc++ = FF_0DECIMAL; | |
1604cfb0 | 5915 | *fpc++ = (U32)arg; |
a1b95068 | 5916 | unchopnum |= ! ischop; |
1604cfb0 MS |
5917 | } |
5918 | else { /* text field */ | |
5919 | I32 prespace = 0; | |
5920 | bool ismore = FALSE; | |
5921 | ||
5922 | if (*s == '>') { | |
5923 | while (*++s == '>') ; | |
5924 | prespace = FF_SPACE; | |
5925 | } | |
5926 | else if (*s == '|') { | |
5927 | while (*++s == '|') ; | |
5928 | prespace = FF_HALFSPACE; | |
5929 | postspace = TRUE; | |
5930 | } | |
5931 | else { | |
5932 | if (*s == '<') | |
5933 | while (*++s == '<') ; | |
5934 | postspace = TRUE; | |
5935 | } | |
5936 | if (*s == '.' && s[1] == '.' && s[2] == '.') { | |
5937 | s += 3; | |
5938 | ismore = TRUE; | |
5939 | } | |
5940 | *fpc++ = s - base; /* fieldsize for FETCH */ | |
5941 | ||
5942 | *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL; | |
5943 | ||
5944 | if (prespace) | |
5945 | *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */ | |
5946 | *fpc++ = FF_ITEM; | |
5947 | if (ismore) | |
5948 | *fpc++ = FF_MORE; | |
5949 | if (ischop) | |
5950 | *fpc++ = FF_CHOP; | |
5951 | } | |
5952 | base = s; | |
5953 | skipspaces = 0; | |
5954 | continue; | |
5955 | } | |
a0d0e21e LW |
5956 | } |
5957 | *fpc++ = FF_END; | |
5958 | ||
815f25c6 | 5959 | assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */ |
a0d0e21e | 5960 | arg = fpc - fops; |
74e0ddf7 | 5961 | |
3808a683 | 5962 | mg->mg_ptr = (char *) fops; |
74e0ddf7 | 5963 | mg->mg_len = arg * sizeof(U32); |
3808a683 DM |
5964 | mg->mg_obj = sv_copy; |
5965 | mg->mg_flags |= MGf_REFCOUNTED; | |
a1b95068 | 5966 | |
bfed75c6 | 5967 | if (unchopnum && repeat) |
75f63940 | 5968 | Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)"); |
74e0ddf7 NC |
5969 | |
5970 | return mg; | |
a1b95068 WL |
5971 | } |
5972 | ||
5973 | ||
5974 | STATIC bool | |
5975 | S_num_overflow(NV value, I32 fldsize, I32 frcsize) | |
5976 | { | |
5977 | /* Can value be printed in fldsize chars, using %*.*f ? */ | |
5978 | NV pwr = 1; | |
5979 | NV eps = 0.5; | |
5980 | bool res = FALSE; | |
5981 | int intsize = fldsize - (value < 0 ? 1 : 0); | |
5982 | ||
a701009a | 5983 | if (frcsize & FORM_NUM_POINT) |
a1b95068 | 5984 | intsize--; |
a701009a | 5985 | frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
a1b95068 WL |
5986 | intsize -= frcsize; |
5987 | ||
5988 | while (intsize--) pwr *= 10.0; | |
5989 | while (frcsize--) eps /= 10.0; | |
5990 | ||
5991 | if( value >= 0 ){ | |
5992 | if (value + eps >= pwr) | |
1604cfb0 | 5993 | res = TRUE; |
a1b95068 WL |
5994 | } else { |
5995 | if (value - eps <= -pwr) | |
1604cfb0 | 5996 | res = TRUE; |
a1b95068 WL |
5997 | } |
5998 | return res; | |
a0d0e21e | 5999 | } |
4e35701f | 6000 | |
bbed91b5 | 6001 | static I32 |
0bd48802 | 6002 | S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen) |
bbed91b5 | 6003 | { |
0bd48802 | 6004 | SV * const datasv = FILTER_DATA(idx); |
504618e9 | 6005 | const int filter_has_file = IoLINES(datasv); |
ad64d0ec NC |
6006 | SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv)); |
6007 | SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv)); | |
941a98a0 | 6008 | int status = 0; |
ec0b63d7 | 6009 | SV *upstream; |
941a98a0 | 6010 | STRLEN got_len; |
162177c1 Z |
6011 | char *got_p = NULL; |
6012 | char *prune_from = NULL; | |
34113e50 | 6013 | bool read_from_cache = FALSE; |
bb7a0f54 | 6014 | STRLEN umaxlen; |
d60d2019 | 6015 | SV *err = NULL; |
bb7a0f54 | 6016 | |
7918f24d NC |
6017 | PERL_ARGS_ASSERT_RUN_USER_FILTER; |
6018 | ||
bb7a0f54 MHM |
6019 | assert(maxlen >= 0); |
6020 | umaxlen = maxlen; | |
5675696b | 6021 | |
bbed91b5 | 6022 | /* I was having segfault trouble under Linux 2.2.5 after a |
f6bab5f6 | 6023 | parse error occurred. (Had to hack around it with a test |
13765c85 | 6024 | for PL_parser->error_count == 0.) Solaris doesn't segfault -- |
bbed91b5 KF |
6025 | not sure where the trouble is yet. XXX */ |
6026 | ||
4464f08e | 6027 | { |
1604cfb0 MS |
6028 | SV *const cache = datasv; |
6029 | if (SvOK(cache)) { | |
6030 | STRLEN cache_len; | |
6031 | const char *cache_p = SvPV(cache, cache_len); | |
6032 | STRLEN take = 0; | |
6033 | ||
6034 | if (umaxlen) { | |
6035 | /* Running in block mode and we have some cached data already. | |
6036 | */ | |
6037 | if (cache_len >= umaxlen) { | |
6038 | /* In fact, so much data we don't even need to call | |
6039 | filter_read. */ | |
6040 | take = umaxlen; | |
6041 | } | |
6042 | } else { | |
6043 | const char *const first_nl = | |
6044 | (const char *)memchr(cache_p, '\n', cache_len); | |
6045 | if (first_nl) { | |
6046 | take = first_nl + 1 - cache_p; | |
6047 | } | |
6048 | } | |
6049 | if (take) { | |
6050 | sv_catpvn(buf_sv, cache_p, take); | |
6051 | sv_chop(cache, cache_p + take); | |
6052 | /* Definitely not EOF */ | |
6053 | return 1; | |
6054 | } | |
6055 | ||
6056 | sv_catsv(buf_sv, cache); | |
6057 | if (umaxlen) { | |
6058 | umaxlen -= cache_len; | |
6059 | } | |
6060 | SvOK_off(cache); | |
6061 | read_from_cache = TRUE; | |
6062 | } | |
937b367d | 6063 | } |
ec0b63d7 | 6064 | |
34113e50 NC |
6065 | /* Filter API says that the filter appends to the contents of the buffer. |
6066 | Usually the buffer is "", so the details don't matter. But if it's not, | |
6067 | then clearly what it contains is already filtered by this filter, so we | |
6068 | don't want to pass it in a second time. | |
6069 | I'm going to use a mortal in case the upstream filter croaks. */ | |
ec0b63d7 | 6070 | upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv)) |
7ea8b04b | 6071 | ? newSV_type_mortal(SVt_PV) : buf_sv; |
ec0b63d7 | 6072 | SvUPGRADE(upstream, SVt_PV); |
1604cfb0 | 6073 | |
bbed91b5 | 6074 | if (filter_has_file) { |
1604cfb0 | 6075 | status = FILTER_READ(idx+1, upstream, 0); |
bbed91b5 KF |
6076 | } |
6077 | ||
34113e50 | 6078 | if (filter_sub && status >= 0) { |
1604cfb0 MS |
6079 | dSP; |
6080 | int count; | |
6081 | ||
6082 | ENTER_with_name("call_filter_sub"); | |
6083 | SAVE_DEFSV; | |
6084 | SAVETMPS; | |
6085 | EXTEND(SP, 2); | |
6086 | ||
6087 | DEFSV_set(upstream); | |
6088 | PUSHMARK(SP); | |
6089 | PUSHs(&PL_sv_zero); | |
6090 | if (filter_state) { | |
6091 | PUSHs(filter_state); | |
6092 | } | |
6093 | PUTBACK; | |
6094 | count = call_sv(filter_sub, G_SCALAR|G_EVAL); | |
6095 | SPAGAIN; | |
6096 | ||
6097 | if (count > 0) { | |
6098 | SV *out = POPs; | |
6099 | SvGETMAGIC(out); | |
6100 | if (SvOK(out)) { | |
6101 | status = SvIV(out); | |
6102 | } | |
eed484f9 DD |
6103 | else { |
6104 | SV * const errsv = ERRSV; | |
6105 | if (SvTRUE_NN(errsv)) | |
6106 | err = newSVsv(errsv); | |
d60d2019 | 6107 | } |
1604cfb0 | 6108 | } |
bbed91b5 | 6109 | |
1604cfb0 MS |
6110 | PUTBACK; |
6111 | FREETMPS; | |
6112 | LEAVE_with_name("call_filter_sub"); | |
bbed91b5 KF |
6113 | } |
6114 | ||
536ac391 | 6115 | if (SvGMAGICAL(upstream)) { |
1604cfb0 MS |
6116 | mg_get(upstream); |
6117 | if (upstream == buf_sv) mg_free(buf_sv); | |
536ac391 | 6118 | } |
b68108d9 | 6119 | if (SvIsCOW(upstream)) sv_force_normal(upstream); |
d60d2019 | 6120 | if(!err && SvOK(upstream)) { |
1604cfb0 MS |
6121 | got_p = SvPV_nomg(upstream, got_len); |
6122 | if (umaxlen) { | |
6123 | if (got_len > umaxlen) { | |
6124 | prune_from = got_p + umaxlen; | |
6125 | } | |
6126 | } else { | |
6127 | char *const first_nl = (char *)memchr(got_p, '\n', got_len); | |
6128 | if (first_nl && first_nl + 1 < got_p + got_len) { | |
6129 | /* There's a second line here... */ | |
6130 | prune_from = first_nl + 1; | |
6131 | } | |
6132 | } | |
937b367d | 6133 | } |
d60d2019 | 6134 | if (!err && prune_from) { |
1604cfb0 MS |
6135 | /* Oh. Too long. Stuff some in our cache. */ |
6136 | STRLEN cached_len = got_p + got_len - prune_from; | |
6137 | SV *const cache = datasv; | |
6138 | ||
6139 | if (SvOK(cache)) { | |
6140 | /* Cache should be empty. */ | |
6141 | assert(!SvCUR(cache)); | |
6142 | } | |
6143 | ||
6144 | sv_setpvn(cache, prune_from, cached_len); | |
6145 | /* If you ask for block mode, you may well split UTF-8 characters. | |
6146 | "If it breaks, you get to keep both parts" | |
6147 | (Your code is broken if you don't put them back together again | |
6148 | before something notices.) */ | |
6149 | if (SvUTF8(upstream)) { | |
6150 | SvUTF8_on(cache); | |
6151 | } | |
6152 | if (SvPOK(upstream)) SvCUR_set(upstream, got_len - cached_len); | |
6153 | else | |
6154 | /* Cannot just use sv_setpvn, as that could free the buffer | |
6155 | before we have a chance to assign it. */ | |
6156 | sv_usepvn(upstream, savepvn(got_p, got_len - cached_len), | |
6157 | got_len - cached_len); | |
6158 | *prune_from = 0; | |
6159 | /* Can't yet be EOF */ | |
6160 | if (status == 0) | |
6161 | status = 1; | |
941a98a0 | 6162 | } |
937b367d | 6163 | |
34113e50 NC |
6164 | /* If they are at EOF but buf_sv has something in it, then they may never |
6165 | have touched the SV upstream, so it may be undefined. If we naively | |
6166 | concatenate it then we get a warning about use of uninitialised value. | |
6167 | */ | |
d60d2019 | 6168 | if (!err && upstream != buf_sv && |
dc423e96 | 6169 | SvOK(upstream)) { |
1604cfb0 | 6170 | sv_catsv_nomg(buf_sv, upstream); |
937b367d | 6171 | } |
ae2c96ed | 6172 | else if (SvOK(upstream)) (void)SvPV_force_nolen(buf_sv); |
937b367d | 6173 | |
941a98a0 | 6174 | if (status <= 0) { |
1604cfb0 MS |
6175 | IoLINES(datasv) = 0; |
6176 | if (filter_state) { | |
6177 | SvREFCNT_dec(filter_state); | |
6178 | IoTOP_GV(datasv) = NULL; | |
6179 | } | |
6180 | if (filter_sub) { | |
6181 | SvREFCNT_dec(filter_sub); | |
6182 | IoBOTTOM_GV(datasv) = NULL; | |
6183 | } | |
6184 | filter_del(S_run_user_filter); | |
bbed91b5 | 6185 | } |
d60d2019 JL |
6186 | |
6187 | if (err) | |
6188 | croak_sv(err); | |
6189 | ||
34113e50 | 6190 | if (status == 0 && read_from_cache) { |
1604cfb0 MS |
6191 | /* If we read some data from the cache (and by getting here it implies |
6192 | that we emptied the cache) then we aren't yet at EOF, and mustn't | |
6193 | report that to our caller. */ | |
6194 | return 1; | |
34113e50 | 6195 | } |
941a98a0 | 6196 | return status; |
bbed91b5 | 6197 | } |
84d4ea48 | 6198 | |
241d1a3b | 6199 | /* |
14d04a33 | 6200 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 6201 | */ |