| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use v5.14; |
| 4 |
use warnings; |
| 5 |
use utf8; |
| 6 |
|
| 7 |
use Test2::V0; |
| 8 |
|
| 9 |
use B qw( svref_2object walkoptree ); |
| 10 |
|
| 11 |
use B::Deparse; |
| 12 |
my $deparser = B::Deparse->new(); |
| 13 |
|
| 14 |
use lib "t"; |
| 15 |
use testcase "t::infix"; |
| 16 |
|
| 17 |
BEGIN { $^H{"t::infix/permit"} = 1; } |
| 18 |
|
| 19 |
# Newer perls generate OP_SREFGEN directly; older perls see only OP_REFGEN |
| 20 |
use constant REFGEN => $] >= 5.022 ? "srefgen" : "refgen"; |
| 21 |
|
| 22 |
{ |
| 23 |
my $result = t::infix::addfunc( 10, 20 ); |
| 24 |
is( $result, 30, 'add wrapper func' ); |
| 25 |
|
| 26 |
my $aref = [ t::infix::interspersefunc( "Z", "a", "b" ) ]; |
| 27 |
is( $aref, [qw( a Z b )], 'intersperse wrapper func' ); |
| 28 |
|
| 29 |
is( [ t::infix::addpairsfunc( [ 1, 2 ], [ 3, 4 ] ) ], |
| 30 |
[ 4, 6 ], 'addpairs wrapper func' ); |
| 31 |
} |
| 32 |
|
| 33 |
sub count_ops |
| 34 |
{ |
| 35 |
my ( $code ) = @_; |
| 36 |
my %opcounts; |
| 37 |
|
| 38 |
# B::walkoptree() is stupid |
| 39 |
# https://github.com/Perl/perl5/issues/19101 |
| 40 |
no warnings 'once'; |
| 41 |
local *B::OP::collect_opnames = sub { |
| 42 |
my ( $op ) = @_; |
| 43 |
$opcounts{ $op->name }++ unless $op->name eq "null"; |
| 44 |
}; |
| 45 |
walkoptree( svref_2object( $code )->ROOT, "collect_opnames" ); |
| 46 |
|
| 47 |
return %opcounts; |
| 48 |
} |
| 49 |
|
| 50 |
# callchecker for scalar/scalar ops |
| 51 |
{ |
| 52 |
my %opcounts; |
| 53 |
|
| 54 |
%opcounts = count_ops sub { t::infix::addfunc( $_[0], $_[1] ) }; |
| 55 |
|
| 56 |
# If the callchecker ran correctly we should see one 'custom' op and no |
| 57 |
# 'entersub's |
| 58 |
ok( (scalar grep { m/^infix_add_0x/ } keys %opcounts), |
| 59 |
'callchecker generated an OP_CUSTOM call' ); |
| 60 |
ok( !$opcounts{entersub}, 'callchecker removed an OP_ENTERSUB call' ); |
| 61 |
|
| 62 |
# Opchecker should ignore non-scalar args |
| 63 |
%opcounts = count_ops sub { t::infix::addfunc( @_, "more" ) }; |
| 64 |
ok( !$opcounts{custom}, 'No OP_CUSTOM call for DEFAV' ); |
| 65 |
|
| 66 |
%opcounts = count_ops sub { t::infix::addfunc( lhs(), rhs() ) }; |
| 67 |
ok( !$opcounts{custom}, 'No OP_CUSTOM call for list ENTERSUB' ); |
| 68 |
|
| 69 |
# Opchecker still permits scalar entersub calls |
| 70 |
%opcounts = count_ops sub { t::infix::addfunc( scalar lhs(), scalar rhs() ) }; |
| 71 |
ok( (scalar grep { m/^infix_add_0x/ } keys %opcounts), |
| 72 |
'OP_CUSTOM call for scalar ENTERSUB' ); |
| 73 |
} |
| 74 |
|
| 75 |
# callchecker for list/list ops |
| 76 |
{ |
| 77 |
my $code; |
| 78 |
my %opcounts; |
| 79 |
|
| 80 |
my $aref = [1,2,3]; |
| 81 |
%opcounts = count_ops $code = sub { t::infix::addpairsfunc( $aref, $aref ) }; |
| 82 |
ok( (scalar grep { m/^infix_addpairs_0x/ } keys %opcounts), |
| 83 |
'callchecker generated an OP_CUSTOM call for list/list' ); |
| 84 |
ok( !$opcounts{entersub}, 'callchecker removed an OP_ENTERSUB call for list/list' ); |
| 85 |
is( $opcounts{rv2av}, 2, 'callchecker made two OP_RV2AV' ); |
| 86 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list' ); |
| 87 |
|
| 88 |
my @padav = (1,2,3); |
| 89 |
|
| 90 |
%opcounts = count_ops $code = sub { t::infix::addpairsfunc( \@padav, \@padav ) }; |
| 91 |
ok( !$opcounts{srefgen}, 'callchecker made no OP_SREFGEN for \@padav' ); |
| 92 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list on \@padav' ); |
| 93 |
|
| 94 |
our @pkgav = (1,2,3); |
| 95 |
|
| 96 |
%opcounts = count_ops $code = sub { t::infix::addpairsfunc( \@pkgav, \@pkgav ) }; |
| 97 |
ok( !$opcounts{srefgen}, 'callchecker made no OP_SREFGEN for \@pkgav' ); |
| 98 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list on \@pkgav' ); |
| 99 |
|
| 100 |
# stress-test it |
| 101 |
|
| 102 |
%opcounts = count_ops $code = sub { t::infix::addpairsfunc( \@{ \@{ \@padav } }, \@{ \@{ \@padav } } ) }; |
| 103 |
# Preserve the two sets of inner ones but remove the outer ones |
| 104 |
is( $opcounts{+REFGEN}, 4, 'callchecker removed one layer of OP_SREFGEN for stress-test' ); |
| 105 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list on stress-test' ); |
| 106 |
|
| 107 |
package OneTwoThree { |
| 108 |
use overload '@{}' => sub { return [1, 2, 3] }; |
| 109 |
} |
| 110 |
|
| 111 |
$code = sub { t::infix::addpairsfunc( bless( {}, "OneTwoThree" ), \@padav ) }; |
| 112 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list on blessed object' ); |
| 113 |
|
| 114 |
# anonlist remains on LHS |
| 115 |
%opcounts = count_ops $code = sub { t::infix::addpairsfunc( [1,2,3], \@padav ) }; |
| 116 |
ok( $opcounts{anonlist}, 'callchecker left OP_ANONLIST on LHS' ); |
| 117 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list on anonlist' ); |
| 118 |
|
| 119 |
# anonlist is unwrapped on RHS |
| 120 |
%opcounts = count_ops $code = sub { t::infix::addpairsfunc( \@padav, [1,2,3] ) }; |
| 121 |
ok( !$opcounts{anonlist}, 'callchecker removed OP_ANONLIST on RHS' ); |
| 122 |
is( [ $code->() ], [ 2, 4, 6 ], 'result of callcheckered code for list/list on anonlist' ); |
| 123 |
} |
| 124 |
|
| 125 |
# wrapper func by coderef |
| 126 |
{ |
| 127 |
my $wrapper = \&t::infix::addfunc; |
| 128 |
is( $wrapper->( 30, 40 ), 70, 'add wrapper func by CODE reference' ); |
| 129 |
} |
| 130 |
|
| 131 |
# argument checking |
| 132 |
{ |
| 133 |
ok( !eval { t::infix::addfunc( 10, 20, 30 ) }, |
| 134 |
'Wrapper func fails for too many args' ); |
| 135 |
like( $@, qr/^Too many arguments for subroutine 't::infix::addfunc'/, |
| 136 |
'Failure message for too many args' ); |
| 137 |
|
| 138 |
ok( !eval { t::infix::addfunc( 60 ) }, |
| 139 |
'Wrapper func fails for too few args' ); |
| 140 |
like( $@, qr/^Too few arguments for subroutine 't::infix::addfunc'/, |
| 141 |
'Failure message for too few args' ); |
| 142 |
} |
| 143 |
|
| 144 |
sub is_deparsed |
| 145 |
{ |
| 146 |
my ( $sub, $exp, $name ) = @_; |
| 147 |
|
| 148 |
my $got = $deparser->coderef2text( $sub ); |
| 149 |
|
| 150 |
# Deparsed output is '{ ... }'-wrapped |
| 151 |
$got = ( $got =~ m/^{\n(.*)\n}$/s )[0]; |
| 152 |
|
| 153 |
# Deparsed output will have a lot of pragmata and so on; just grab the |
| 154 |
# final line |
| 155 |
$got = ( split m/\n/, $got )[-1]; |
| 156 |
$got =~ s/^\s+//; |
| 157 |
|
| 158 |
is( $got, $exp, $name ); |
| 159 |
} |
| 160 |
|
| 161 |
{ |
| 162 |
# We need to ensure the wrapper function doesn't deparse to the actual |
| 163 |
# infix operator syntax in order to test this one |
| 164 |
BEGIN { delete $^H{"t::infix/permit"} } |
| 165 |
|
| 166 |
is_deparsed sub { t::infix::addfunc( $_[0], $_[1] ) }, |
| 167 |
't::infix::addfunc($_[0], $_[1]);', |
| 168 |
'deparsed call to wrapper func'; |
| 169 |
|
| 170 |
my @padav; |
| 171 |
our @pkgav; |
| 172 |
|
| 173 |
is_deparsed sub { t::infix::addpairsfunc( $_[0], $_[1] ) }, |
| 174 |
't::infix::addpairsfunc($_[0], $_[1]);', |
| 175 |
'deparsed call to list/list wrapper func on slugs'; |
| 176 |
is_deparsed sub { t::infix::addpairsfunc( \@padav, \@padav ) }, |
| 177 |
't::infix::addpairsfunc(\@padav, \@padav);', |
| 178 |
'deparsed call to list/list wrapper func on padav'; |
| 179 |
is_deparsed sub { t::infix::addpairsfunc( \@pkgav, \@pkgav ) }, |
| 180 |
't::infix::addpairsfunc(\@pkgav, \@pkgav);', |
| 181 |
'deparsed call to list/list wrapper func on pkgav'; |
| 182 |
is_deparsed sub { t::infix::addpairsfunc( [1,2], [3,4] ) }, |
| 183 |
't::infix::addpairsfunc([1, 2], [3, 4]);', |
| 184 |
'deparsed call to list/list wrapper func on anonlist'; |
| 185 |
} |
| 186 |
|
| 187 |
# list-associative |
| 188 |
{ |
| 189 |
# wrapper by direct call |
| 190 |
is( t::infix::catfunc( "a", "b", "c" ), "^abc^", |
| 191 |
'List-associative wrapper function by direct call' ); |
| 192 |
|
| 193 |
# wrapper by direct call non-convertable |
| 194 |
my @args = qw( a b c ); |
| 195 |
is( t::infix::catfunc( @args ), "^abc^", |
| 196 |
'List-associative wrapper function by non-convertable direct call' ); |
| 197 |
|
| 198 |
my $wrapper = \&t::infix::catfunc; |
| 199 |
is( $wrapper->( "d", "e", "f" ), "^def^", |
| 200 |
'List-associative wrapper function by CODE reference' ); |
| 201 |
} |
| 202 |
|
| 203 |
# call-checker for list-associative ops |
| 204 |
{ |
| 205 |
my $code; |
| 206 |
my %opcounts; |
| 207 |
|
| 208 |
# scalars |
| 209 |
%opcounts = count_ops $code = sub { t::infix::catfunc "X", "Y" }; |
| 210 |
ok( (scalar grep { m/^infix_cat_0x/ } keys %opcounts), |
| 211 |
'callchecker generated an OP_CUSTOM call for listassoc scalars' ); |
| 212 |
ok( !$opcounts{entersub}, 'callchecker removed an OP_ENTERSUB call for listassoc scalars' ); |
| 213 |
is( $code->(), "^XY^", 'result of callcheckered code for listassoc scalars' ); |
| 214 |
|
| 215 |
# lists |
| 216 |
%opcounts = count_ops $code = sub { t::infix::LLfunc ["X"], ["Y"] }; |
| 217 |
ok( (scalar grep { m/^infix_LL_0x/ } keys %opcounts), |
| 218 |
'callchecker generated an OP_CUSTOM call for listassoc lists' ); |
| 219 |
ok( !$opcounts{entersub}, 'callchecker removed an OP_ENTERSUB call for listassoc lists' ); |
| 220 |
is( $code->(), "([X][Y])", 'result of callcheckered code for listassoc lists' ); |
| 221 |
|
| 222 |
# RT153244 |
| 223 |
$code = sub { t::infix::catfunc() }; |
| 224 |
pass( 'Compiling a zero argument listassoc scalars wrapper did not crash' ); |
| 225 |
|
| 226 |
$code = sub { t::infix::LLfunc() }; |
| 227 |
pass( 'Compiling a zero argument listassoc scalars wrapper did not crash' ); |
| 228 |
} |
| 229 |
|
| 230 |
done_testing; |