Skip to content

Commit

Permalink
Also implement the negated versions of the flagged operators; ne:u an…
Browse files Browse the repository at this point in the history
…d !=:u
  • Loading branch information
leonerd committed Jan 27, 2025
1 parent 7aeda2d commit ac020e6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/B/Op_private.pm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions op.c
Original file line number Diff line number Diff line change
Expand Up @@ -16200,8 +16200,10 @@ Perl_apply_opflags(pTHX_ U32 opcode, char *flagstr)

for(char flag; (flag = *flagstr); flagstr++) {
switch(opcode_base) {
case OP_SEQ:
case OP_EQ:
case OP_SEQ: /* eq */
case OP_SNE: /* ne */
case OP_EQ: /* == */
case OP_NE: /* != */
switch(flag) {
case 'u':
priv |= OPpEQ_UNDEF;
Expand Down
12 changes: 6 additions & 6 deletions opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,15 @@ PP(pp_ne)
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];

if(UNLIKELY(PL_op->op_private & OPpEQ_UNDEF)) {
bool lundef = !SvOK(left), rundef = !SvOK(right);

if(lundef || rundef) {
rpp_replace_2_IMM_NN(boolSV(!(lundef && rundef)));
return NORMAL;
}
}

U32 flags_and = SvFLAGS(left) & SvFLAGS(right);
U32 flags_or = SvFLAGS(left) | SvFLAGS(right);

Expand Down Expand Up @@ -2473,6 +2482,15 @@ PP(pp_sne)
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];

if(PL_op->op_private & OPpEQ_UNDEF) {
bool lundef = !SvOK(left), rundef = !SvOK(right);

if(lundef || rundef) {
rpp_replace_2_IMM_NN(boolSV(!(lundef && rundef)));
return NORMAL;
}
}

rpp_replace_2_IMM_NN(boolSV(!sv_eq_flags(left, right, 0)));
return NORMAL;
}
Expand Down
2 changes: 1 addition & 1 deletion regen/op_private
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ addbits('emptyavhv',

addbits($_,
7 => qw(OPpEQ_UNDEF UNDEF),
) for qw( eq seq );
) for qw( eq ne seq sne );

addbits('argdefelem',
7 => qw(OPpARG_IF_UNDEF IF_UNDEF),
Expand Down
10 changes: 10 additions & 0 deletions t/op/equ.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ ok(not(123 ==:u 456), '==:u on different values');
is($warnings, 0, 'no warnings were produced by use of undef');
}

foreach (["abc", "abc"], ["abc", "def"], ["", undef], [undef, undef]) {
my ($left, $right) = @$_;
is(not($left ne:u $right), ($left eq:u $right), 'ne:u is a synonym for not(eq:u)');
}

# ==:u treats undef as distinct, equal to itself, with no warnings
{
my $warnings = 0;
Expand All @@ -71,6 +76,11 @@ ok(not(123 ==:u 456), '==:u on different values');
is($warnings, 0, 'no warnings were produced by use of undef');
}

foreach ([123, 123], [123, 456], [0, undef], [undef, undef]) {
my ($left, $right) = @$_;
is(not($left !=:u $right), ($left ==:u $right), '!=:u is a synonym for not(==:u)');
}

# performs GETMAGIC
{
"abc" =~ m/(\d+)/;
Expand Down

0 comments on commit ac020e6

Please sign in to comment.