Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ phase = configure
phase = build
phase = runtime
phase = test
skip = Test::Fatal
skip = Test::Warnings
check_dual_life_versions = 0
46 changes: 30 additions & 16 deletions lib/Class/Method/Modifiers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ sub install_modifier {
unshift @{ $cache->{$type} }, $code;
}

require Carp;
my $loc = Carp::short_error_loc();
my ($file, $line, $warnmask) = (caller($loc))[1,2,9];

# wrap the method with another layer of around. much simpler than
# the Moose equivalent. :)
if ($type eq 'around') {
my $method = $cache->{wrapped};
my $attrs = _sub_attrs($code);
my $sig = _sub_sig($code);
# a bare "sub :lvalue {...}" will be parsed as a label and an
# indirect method call. force it to be treated as an expression
# using +
$cache->{wrapped} = eval "package $into; +sub $attrs { \$code->(\$method, \@_); };";
$cache->{wrapped} = eval "package $into; +sub $sig { \$code->(\$method, \@_); };";
}

# install our new method which dispatches the modifiers, but only
Expand All @@ -101,10 +105,15 @@ sub install_modifier {
# to take a reference to it. better a deref than a hash lookup
my $wrapped = \$cache->{"wrapped"};

my $attrs = _sub_attrs($cache->{wrapped});
my $sig = _sub_sig($cache->{wrapped});

my $generated = "package $into;\n";
$generated .= "sub $name $attrs {";
my $generated
= "BEGIN { \${^WARNING_BITS} = \$warnmask }\n"
. "no warnings 'redefine';\n"
. "no warnings 'closure';\n"
. "package $into;\n"
. "#line $line \"$file\"\n"
. "sub $name $sig {";

# before is easy, it doesn't affect the return value(s)
if (@$before) {
Expand Down Expand Up @@ -143,8 +152,6 @@ sub install_modifier {
$generated .= '}';

no strict 'refs';
no warnings 'redefine';
no warnings 'closure';
eval $generated;
};
}
Expand Down Expand Up @@ -198,20 +205,27 @@ sub _fresh {
}
else {
no warnings 'closure'; # for 5.8.x
my $attrs = _sub_attrs($code);
eval "package $into; sub $name $attrs { \$code->(\@_) }";
my $sig = _sub_sig($code);
eval "package $into; sub $name $sig { \$code->(\@_) }";
}
}
}

sub _sub_attrs {
sub _sub_sig {
my ($coderef) = @_;
local *_sub = $coderef;
local $@;
local $SIG{__DIE__};
# this assignment will fail to compile if it isn't an lvalue sub. we
# never want to actually call the sub though, so we return early.
(eval 'return 1; &_sub = 1') ? ':lvalue' : '';
my @sig;
if (defined(my $proto = prototype($coderef))) {
push @sig, "($proto)";
}
if (do {
local *_sub = $coderef;
local $@;
local $SIG{__DIE__};
eval 'return 1; &_sub = 1';
}) {
push @sig, ':lvalue';
}
join ' ', @sig;
}

sub _is_in_package {
Expand Down
96 changes: 96 additions & 0 deletions t/141-prototype.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use strict;
use warnings;
use Test::More 0.88;
use Test::Warnings ($ENV{AUTHOR_TESTING} ? () : ':no_end_test'), 'warnings';
use Test::Fatal;

use Class::Method::Modifiers;

{
sub foo ($) { scalar @_ }

my $after;
after foo => sub { $after = @_ };

is eval q{ foo( @{[10, 20]} ) }, 1,
'after wrapped sub maintains prototype';
is $after, 1,
'after modifier applied';
}

{
my $bar;
my $guff;
sub bar ($) :lvalue { $guff = @_; $bar }

my $after;
after bar => sub { $after = @_ };

eval q{ bar( @{[10, 20]} ) = 5 };
is $guff, 1,
'after wrapped lvalue sub maintains prototype';
is $bar, 5,
'after wrapped lvalue sub maintains lvalue';
is $after, 1,
'after modifier applied';
}

{
sub bog ($) { scalar @_ }
my $min_lineno = __LINE__;
my $around;
my ($warn) = warnings {
around bog => sub ($$) {
my $orig = shift;
$around = @_;
$orig->(@_);
};
};
my $max_lineno = __LINE__;
is eval q{ bog( @{[5, 6]}, @{[10, 11]} ) }, 2,
'around wrapped lvalue sub takes modifier prototype';
is $around, 2,
'around modifier applied';
like $warn, qr/Prototype mismatch/,
'changing prototype throws warning';
like $warn, qr/\Q${\__FILE__}\E/,
'warning is reported from correct location';

my ($lineno) = ($warn =~ qr/\Q${\__FILE__}\E line ([0-9]+)/);
is !!($min_lineno < $lineno && $lineno < $max_lineno), 1,
'line no is within range';
}

{
sub brog ($) { scalar @_ }
no warnings;
my @warn = warnings {
around brog => sub ($$) {
my $orig = shift;
$orig->(@_);
};
};

is 0+@warn, 0,
'warnings controllable via warning pragma';
}

{
require List::Util;
List::Util->import('sum');
my $around;
my @warn = warnings {
around sum => sub :prototype(@) {
my $orig = shift;
$around = @_;
return 2 * $orig->(@_);
};
};
is eval q{sum(11, 1, 4, 5, 9)}, 60,
'result from around xs function';
is $around, 5, 'prototype @ wrapped';
is 0+@warn, 0, 'no warnings';
}

done_testing;
# vim: set ts=8 sts=4 sw=4 tw=115 et :