Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ->methods to work with constants (and perl 5.28) #299

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lib/perl5i/2/HASH.pm
lib/perl5i/2/Meta.pm
lib/perl5i/2/Meta/Class.pm
lib/perl5i/2/Meta/Instance.pm
lib/perl5i/2/MethodInfo.pm
lib/perl5i/2/RequireMessage.pm
lib/perl5i/2/SCALAR.pm
lib/perl5i/2/Signature.pm
Expand Down
12 changes: 12 additions & 0 deletions lib/perl5i/2/CODE.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ sub signature {
return $Signatures{$_[0]};
}

sub is_constant {
require B;
# Use eval to take advantage of compile-time constants.
eval '
no warnings "redefine";
sub is_constant {
not not B::svref_2object($_[0])->CvFLAGS & B::CVf_CONST
}
';
goto &is_constant;
}

1;
27 changes: 23 additions & 4 deletions lib/perl5i/2/Meta.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ sub linear_isa {
}

sub methods {
require perl5i::2::MethodInfo;

my $self = shift;
my $opts = shift // {};
my $top = $self->class;
Expand All @@ -70,15 +72,32 @@ sub methods {
my $sym_table = $class->mc->symbol_table;
for my $name (keys %$sym_table) {
my $glob = $sym_table->{$name};
next unless ref \$glob eq "GLOB";
next unless my $code = *{$glob}{CODE};
my $code;
if (ref $glob) {
if (ref $glob ne 'CODE') { # constant
$all_methods{$name} =
perl5i::2::MethodInfo->new($class, $name,
\$sym_table->{$name});
next;
}
$code = $glob;
}
else {
next unless ref \$glob eq "GLOB";
next unless $code = *{$glob}{CODE};
}
my $sig = $code->signature;
next if $sig and !$sig->is_method;
$all_methods{$name} = $class;
$all_methods{$name} =
perl5i::2::MethodInfo->new($class, $name,
\$sym_table->{$name});
}
}

return wantarray ? keys %all_methods : [keys %all_methods];
my @ret = map perl5i::2::MethodInfo->new($_, $all_methods{$_}),
keys %all_methods;

return wantarray ? values %all_methods : [values %all_methods];
}

sub symbol_table {
Expand Down
28 changes: 28 additions & 0 deletions lib/perl5i/2/MethodInfo.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package perl5i::2::MethodInfo;

use strict;
use warnings;
use 5.010_000;
use perl5i::2::autobox;
use overload '""' => sub { shift->{name} }, fallback => 1;

sub new {
my ($this_class, $that_class, $meth, $stashelem) = @_;
return bless { name => $meth,
package => $that_class,
stashelem => $stashelem }, $this_class;
}

# Delegate these methods to the CODE autobox class.
for my $method (qw( is_constant )) {
no strict 'refs';
*$method = sub {
my $self = shift;
my $sub = ref $self->{stashelem} eq 'GLOB'
? *{$self->{stashelem}}{CODE}
: *{"$self->{package}:\:$self->{name}"}{CODE}; # reify
$sub->$method
}
}

1;
13 changes: 13 additions & 0 deletions t/Meta/methods.t
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,17 @@ note "func gets filtered out of methods list"; {
can_ok( $class, 'as_func'); # sanity check
}

note "constants count as methods, regardless of how they came to be"; {
{
package Foo;
use constant a => 1;
sub b () { 2 }
sub c () { 3 } () = \&c;
}
is_deeply
scalar "Foo"->mc->methods->sort,
scalar [qw[ a b c ]]->sort,
"constants count as methods";
}

done_testing;