FFI/Library.pm

filter: Statementstmt 82.6% 43 / 52 Branchbran 58.8% 20 / 34 Conditioncond 50.0% 5 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 71.3% 82 / 115 CC 22 SCAR 21.8
CC
22
Cov
71%
new
14.8
address
9.3
DESTROY
8.1
CRAP 8.8
↑ Summary FFI.pm »
1 package FFI::Library;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use constant _is_win => $^O =~ /^(MSWin32|cygwin|msys2?)$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Perl Access to Dynamically Loaded Libraries
9 3 our $VERSION = '1.15'; # VERSION
10  
11 sub new
12 8 ▶{
Subroutine new: called CC=12 SCAR=27.0
Pod documented
13 8 my($class, $libname, $flags) = @_;
14 8 ▶ Carp::croak('Usage: $lib = FFI::Library->new($filename [, $flags ])')
Branch: 1 branch
BranchTrueFalseTotal
unless @_ <= 3 1 7 8
15 unless @_ <= 3;
16  
17 7 ▶ $flags ||= 0;
Condition50%1/2
$flags ||= 0
l!l
07
MC/DC0%0/1
$flags ||= 0
$flags
Truth table50%1/2
A
A= $flags
Aresult
11
00
18  
19 7 ▶ if(! defined $libname)
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $libname) { } 1 6 7
elsif (ref $libname and int $libname == int \$0) { } 2 4 6
elsif (&_is_win()) { } 0 4 4
elsif (-e $libname) { } 3 1 4
20 {
21 1 return bless {
22 impl => 'null',
23 }, $class;
24 }
25 ▶ elsif(ref $libname and int($libname) == int(\$0))
Condition66%2/3
ref $libname and int $libname == int \$0
!ll&&!rl&&r
402
MC/DC50%1/2
ref $libname and int $libname == int \$0
ref $libname int $libname == int \$0
Truth table66%2/3
A and B
A= ref $libnameB= int $libname == int \$0
ABresult
0X0
100
111
26 {
27 2 return $class->_dl_impl(undef, undef);
28 }
29 elsif(_is_win)
30 {
31 0 return $class->_dl_impl($libname, undef);
32 }
33 elsif(-e $libname)
34 {
35 3 ▶ return $class->_dl_impl(
Branch: 1 branch
BranchTrueFalseTotal
$flags == 1 ? : 0 3 3
36 $libname,
37 $flags == 0x01 ? FFI::Platypus::Lang::DL::RTLD_GLOBAL() : undef,
38 );
39 }
40 else
41 {
42 1 require DynaLoader;
43 1 ▶ my $so = DynaLoader::dl_findfile($libname) || $libname;
Condition33%1/3
DynaLoader::dl_findfile($libname) || $libname
l!l&&r!l&&!r
010
MC/DC0%0/2
DynaLoader::dl_findfile($libname) || $libname
DynaLoader::dl_findfile($libname) $libname
Truth table33%1/3
A || B
A= DynaLoader::dl_findfile($libname)B= $libname
ABresult
1X1
011
000
44 1 ▶ my $handle = DynaLoader::dl_load_file($so, $flags || 0);
Condition50%1/2
$flags || 0
l!l
01
MC/DC0%0/1
$flags || 0
$flags
Truth table50%1/2
A
A= $flags
Aresult
11
00
45 1 ▶ return unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 1 0 1
46 0 return bless {
47 impl => 'dynaloader',
48 handle => $handle,
49 }, $class;
50 }
51 }
52  
53 sub _dl_impl
54 5 ▶{
Subroutine _dl_impl: called CC=3 SCAR=11.2
55 5 my($class, $path, $flags) = @_;
56 5 require FFI::Platypus::DL;
57 5 ▶ $flags = FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 5 0 5
58 unless defined $flags;
59 5 my $handle = FFI::Platypus::DL::dlopen($path, $flags);
60 5 ▶ return unless defined $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 5 5
61 5 bless { impl => 'dl', handle => $handle }, $class;
62 }
63  
64 sub address
65 11 ▶{
Subroutine address: called CC=4 SCAR=22.3
Pod documented
66 11 my($self, $name) = @_;
67  
68 11 ▶ if($self->{impl} eq 'dl')
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'impl'} eq 'dl') { } 11 0 11
elsif ($self->{'impl'} eq 'dynaloader') { } 0 0 0
elsif ($self->{'impl'} eq 'null') { } 0 0 0
69 {
70 11 return FFI::Platypus::DL::dlsym($self->{handle}, $name);
71 }
72 elsif($self->{impl} eq 'dynaloader')
73 {
74 0 return DynaLoader::dl_find_symbol($self->{handle}, $name);
75 }
76 elsif($self->{impl} eq 'null')
77 {
78 0 return;
79 }
80 else
81 {
82 0 Carp::croak("Unknown implementaton: @{[ $self->{impl} ]}");
83 }
84 }
85  
86 sub function
87 6 ▶{
Subroutine function: called CC=2 SCAR=6.9
Pod documented
88 6 my($self, $name, $sig) = @_;
89  
90 6 my $addr = $self->address($name);
91  
92 6 ▶ Carp::croak("Unknown function $name")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 2 4 6
93 unless defined $addr;
94  
95 4 require FFI;
96 4 ▶ sub { FFI::call($addr, $sig, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
97 }
98  
99 sub DESTROY
100 6 ▶{
Subroutine DESTROY: called CC=5 SCAR=20.9
101 6 my($self) = @_;
102  
103 6 ▶ if($self->{impl} eq 'dl')
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'impl'} eq 'dl') { } 5 1 6
elsif ($self->{'impl'} eq 'dynaloader') { } 0 1 1
elsif ($self->{'impl'} eq 'null') { } 1 0 1
104 {
105 5 FFI::Platypus::DL::dlclose($self->{handle});
106 }
107 elsif($self->{impl} eq 'dynaloader')
108 {
109 DynaLoader::dl_free_file($self->{handle})
110 0 ▶ if defined &DynaLoader::dl_free_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined &DynaLoader::dl_free_file 0 0 0
111 }
112 elsif($self->{impl} eq 'null')
113 {
114 # do nothing
115 }
116 else
117 {
118 0 Carp::croak("Unknown implementaton: @{[ $self->{impl} ]}");
119 }
120 }
121  
122 3 1;
123  
124 __END__
↑ Summary FFI.pm »