| 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
| |||||||||||||||||||||||
| 15 | unless @_ <= 3; | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 7 | ▶ | $flags ||= 0; | ||||||||||||||||||||
|
Condition50%1/2
$flags ||= 0
MC/DC0%0/1
$flags ||= 0
$flags
Truth table50%1/2
A
A= $flags
| |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 7 | ▶ | if(! defined $libname) | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||
| 44 | 1 | ▶ | my $handle = DynaLoader::dl_load_file($so, $flags || 0); | ||||||||||||||||||||
|
Condition50%1/2
$flags || 0
MC/DC0%0/1
$flags || 0
$flags
Truth table50%1/2
A
A= $flags
| |||||||||||||||||||||||
| 45 | 1 | ▶ | return unless $handle; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 58 | unless defined $flags; | ||||||||||||||||||||||
| 59 | 5 | my $handle = FFI::Platypus::DL::dlopen($path, $flags); | |||||||||||||||||||||
| 60 | 5 | ▶ | return unless defined $handle; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||