Commit a2f2c1535a for perl
commit a2f2c1535a954e12480fc029f3062e5446c098d0
Author: David Mitchell <davem@iabyn.nospamdeletethisbit.com>
Date: Tue Oct 6 16:54:29 2026 +0100
regmatch: refactor EVAL return value processing
The return value from EVAL patterns such as /(?{...})/ and /(??{...})/
potentially undergoes two phases of processing. The first occurs before
restoring scope, parens etc. Doing it before ensures that code like
/(??{ $1 })/ does the right thing.
The return value after the first phase is potentially stored in two
variables: the original (but possibly stringified etc) SV in ret, and
potentially a REGEXP SV extracted from the return value stored in re_sv.
This distinguishes these two cases:
/(??{ "abc" })/ // ret contains "abc"
/(??{ qr/abc/ })/ // re_sv is a REGEX sv
The second phase of processing for /(??{...})/ looks like:
if (re_sv)
// use the pre-compiled RE in re_sv
else
// compile the string in ret into an SV
In a few commits' time, some of the code in the EVAL branch (including
the phase 1 return processing) will be moved into its own static
function. To prepare for this, this commit switches from storing the
return value in two variables, into storing it in a single variable
(which will eventually be the return value of the new C function).
With this change, the second-phase processing code now looks like:
if (SvTYPE(re_sv) == SVt_REGEXP)
// use the pre-compiled RE in re_sv
else
// compile the string in re_sv into an SV
I think this also makes the code clearer.
Also to aid moving the code into separate function, for the logical == 1
case where the return value is treated as a boolean, instead of setting
the sw boolean local var directly, re_sv is now set to NULL if it has a
false value. Then in the second phase, its NULLness (or otherwise) is
used to set sw.
In theory there should be no functional changes.
diff --git a/regexec.c b/regexec.c
index baab4a7561..cbb5f4966a 100644
--- a/regexec.c
+++ b/regexec.c
@@ -8931,7 +8931,6 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
#define CUR_EVAL cur_eval->u.eval
{
- SV *ret;
REGEXP *re_sv;
regexp *re;
regexp_internal *rei;
@@ -9021,6 +9020,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
COP * const ocurcop = PL_curcop;
OP *nop;
CV *newcv;
+ SV *ret;
U32 n = EVAL_CODE_DATA_SLOT(scan);
if (!reginfo->caller_cv)
@@ -9172,15 +9172,14 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
PL_op = NULL;
- re_sv = NULL;
if (logical == 0) { /* /(?{ ... })/ and /(*{ ... })/ */
SV *replsv = save_scalar(PL_replgv);
sv_setsv(replsv, ret); /* $^R */
SvSETMAGIC(replsv);
}
else if (logical == 1) { /* /(?(?{...})X|Y)/ */
- sw = cBOOL(SvTRUE_NN(ret));
- logical = 0;
+ if (!SvTRUE_NN(ret))
+ ret = NULL;
}
else { /* /(??{ ... }) */
/* if its overloaded, let the regex compiler handle
@@ -9188,26 +9187,27 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
if (SvGMAGICAL(ret))
ret = sv_mortalcopy(ret);
if (!SvAMAGIC(ret)) {
+ MAGIC *mg;
SV *sv = ret;
if (SvROK(sv))
sv = SvRV(sv);
+
if (SvTYPE(sv) == SVt_REGEXP)
- re_sv = (REGEXP*) sv;
- else if (SvSMAGICAL(ret)) {
- MAGIC *mg = mg_find(ret, PERL_MAGIC_qr);
- if (mg)
- re_sv = (REGEXP *) mg->mg_obj;
- }
+ ret = sv;
+ else if ( SvSMAGICAL(ret)
+ && ((mg = mg_find(ret, PERL_MAGIC_qr))))
- /* force any undef warnings here */
- if (!re_sv && !SvPOK(ret) && !SvNIOK(ret)) {
+ ret = mg->mg_obj;
+ else if (!SvPOK(ret) && !SvNIOK(ret)) {
+ /* force any undef warnings here */
ret = sv_mortalcopy(ret);
(void) SvPV_force_nolen(ret);
}
}
-
}
+ re_sv = (REGEXP*)ret;
+
/* *** Note that at this point we don't restore
* PL_comppad, (or pop the CxSUB) on the assumption it may
* be used again soon. This is safe as long as nothing
@@ -9220,6 +9220,10 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
PL_curpm = PL_reg_curpm;
if (logical != 2) {
+ if (logical == 1) {
+ sw = !!re_sv;
+ logical = 0;
+ }
PUSH_STATE_GOTO(EVAL_B, next, locinput, loceol,
script_run_begin);
/* NOTREACHED */
@@ -9232,11 +9236,12 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
/* extract RE object from returned value; compiling if
* necessary */
- if (re_sv) {
+ if (SvTYPE(re_sv) == SVt_REGEXP) {
re_sv = reg_temp_copy(NULL, re_sv);
}
else {
U32 pm_flags = 0;
+ SV *ret = (SV*)re_sv;
if (SvUTF8(ret) && IN_BYTES) {
/* In use 'bytes': make a copy of the octet
@@ -9270,6 +9275,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
SAVEFREESV(re_sv);
re = ReANY(re_sv);
}
+
RXp_MATCH_COPIED_off(re);
RXp_SUBBEG(re) = RXp_SUBBEG(rex);
RXp_SUBLEN(re) = RXp_SUBLEN(rex);