| 1 | package Aion::Meta::Subroutine; | ||||||||||||||||||||
| 2 | # Описывает функцию с сигнатурой | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 6 | ▶ | use common::sense; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 6 | ▶ | use Aion::Meta::Util qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 6 | ▶ | use Aion::Types qw/Tuple/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use Scalar::Util qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | ▶ | use Sub::Util qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | Aion::Meta::Util::create_getters(qw/pkg subname signature referent wrapsub/); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 15 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 14 | 15 | my $cls = shift; | |||||||||||||||||||
| 15 | 15 | ▶ | bless {@_}, ref $cls || $cls; | ||||||||||||||||||
|
Condition33%1/3
ref $cls || $cls
MC/DC0%0/2
ref $cls || $cls
ref $cls
$cls
Truth table33%1/3
A || B
A= ref $clsB= $cls
| |||||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 10 | ▶ | sub wrap_sub { | ||||||||||||||||||
|
Subroutine
wrap_sub: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 19 | 10 | my ($self) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 10 | my ($pkg, $subname, $signature, $referent) = @$self{qw/pkg subname signature referent/}; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 10 | my $args_of_meth = "Arguments of method `$subname`"; | |||||||||||||||||||
| 24 | 10 | my $returns_of_meth = "Returns of method `$subname`"; | |||||||||||||||||||
| 25 | 10 | my $return_of_meth = "Return of method `$subname`"; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 10 | my @signature = @$signature; | |||||||||||||||||||
| 28 | 10 | my $ret = pop @signature; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 10 | ▶ | my ($ret_array, $ret_scalar) = exists $ret->{is_wantarray}? @{$ret->{args}}: (Tuple([$ret]), $ret); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 10 | my $args = Tuple(\@signature); | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 8 | ▶ | my $sub = sub { | ||||||||||||||||||
|
Subroutine
sound: called CC=2 SCAR=6.9
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 35 | 8 | $args->validate(\@_, $args_of_meth); | |||||||||||||||||||
| 36 | wantarray? do { | ||||||||||||||||||||
| 37 | 1 | my @returns = $referent->(@_); | |||||||||||||||||||
| 38 | 1 | $ret_array->validate(\@returns, $returns_of_meth); | |||||||||||||||||||
| 39 | @returns | ||||||||||||||||||||
| 40 | 0 | ▶ | }: do { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 41 | 6 | my $return = $referent->(@_); | |||||||||||||||||||
| 42 | 6 | $ret_scalar->validate($return, $return_of_meth); | |||||||||||||||||||
| 43 | 6 | $return | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 10 | }; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 10 | Sub::Util::set_prototype Sub::Util::prototype($referent), $sub; | |||||||||||||||||||
| 48 | 10 | Sub::Util::set_subname Sub::Util::subname($referent), $sub; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 10 | ▶ | *{"$pkg\::$subname"} = $sub if $subname ne '__ANON__'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 10 | $self->{wrapsub} = $sub; | |||||||||||||||||||
| 53 | 10 | $Aion::META{$pkg}{subroutine}{$subname} = $self; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 10 | my $key = pack "J", Scalar::Util::refaddr $sub; | |||||||||||||||||||
| 56 | 10 | $Aion::Isa{$key} = $self; | |||||||||||||||||||
| 57 | 10 | Scalar::Util::weaken $Aion::Isa{$key}; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 10 | $self | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 4 | ▶ | sub compare { | ||||||||||||||||||
|
Subroutine
compare: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||
| 63 | 4 | my ($self, $subroutine) = @_; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | die "Requires subroutine ${\$self->subname}!" unless UNIVERSAL::isa($subroutine, 'Aion::Meta::Subroutine'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 4 | my $i = 0; | |||||||||||||||||||
| 68 | 4 | my $signature = $subroutine->signature; | |||||||||||||||||||
| 69 | 4 | my $fail = 0; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 4 | ▶ | if(@$signature == @{$self->signature}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 4 | for my $type (@{$self->{signature}}) { | |||||||||||||||||||
| 73 | 8 | my $other_type = $signature->[$i++]; | |||||||||||||||||||
| 74 | 8 | ▶ | $fail = 1, last unless $type <= $other_type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||
| 77 | 0 | $fail = 1; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 4 | ▶ | die "Signature mismatch: ${\$self->stringify} <=> ${\$subroutine->stringify}" if $fail; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 3 | ▶ | sub stringify { | ||||||||||||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 84 | 3 | my ($self) = @_; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 3 | my ($pkg, $subname) = @$self{qw/pkg subname/}; | |||||||||||||||||||
| 87 | 3 | my $signature = join " => ", @{$self->signature}; | |||||||||||||||||||
| 88 | 3 | return "$subname($signature) of $pkg"; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 6 | 1; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||