| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package FFI::Library; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 168663 | use strict; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 88 |  | 
| 4 | 3 |  |  | 3 |  | 18 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 71 |  | 
| 5 | 3 |  |  | 3 |  | 15 | use Carp (); | 
|  | 3 |  |  |  |  | 14 |  | 
|  | 3 |  |  |  |  | 183 |  | 
| 6 | 3 |  |  | 3 |  | 20 | use constant _is_win => $^O =~ /^(MSWin32|cygwin|msys2?)$/; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 2125 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | # ABSTRACT: Perl Access to Dynamically Loaded Libraries | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.08'; # VERSION | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new | 
| 12 |  |  |  |  |  |  | { | 
| 13 | 8 |  |  | 8 | 1 | 38872 | my($class, $libname, $flags) = @_; | 
| 14 | 8 | 100 |  |  |  | 202 | Carp::croak('Usage: $lib = FFI::Library->new($filename [, $flags ])') | 
| 15 |  |  |  |  |  |  | unless @_ <= 3; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 7 |  | 50 |  |  | 50 | $flags ||= 0; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 7 | 100 | 66 |  |  | 128 | if(! defined $libname) | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | { | 
| 21 | 1 |  |  |  |  | 4 | return bless { | 
| 22 |  |  |  |  |  |  | impl => 'null', | 
| 23 |  |  |  |  |  |  | }, $class; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  | elsif(ref $libname and int($libname) == int(\$0)) | 
| 26 |  |  |  |  |  |  | { | 
| 27 | 2 |  |  |  |  | 10 | return $class->_dl_impl(undef, undef); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | elsif(_is_win) | 
| 30 |  |  |  |  |  |  | { | 
| 31 | 0 |  |  |  |  | 0 | return $class->_dl_impl($libname, undef); | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  | elsif(-e $libname) | 
| 34 |  |  |  |  |  |  | { | 
| 35 | 3 | 50 |  |  |  | 24 | return $class->_dl_impl( | 
| 36 |  |  |  |  |  |  | $libname, | 
| 37 |  |  |  |  |  |  | $flags == 0x01 ? FFI::Platypus::Lang::DL::RTLD_GLOBAL() : undef, | 
| 38 |  |  |  |  |  |  | ); | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | else | 
| 41 |  |  |  |  |  |  | { | 
| 42 | 1 |  |  |  |  | 8 | require DynaLoader; | 
| 43 | 1 |  | 33 |  |  | 359 | my $so = DynaLoader::dl_findfile($libname) || $libname; | 
| 44 | 1 |  | 50 |  |  | 234 | my $handle = DynaLoader::dl_load_file($so, $flags || 0); | 
| 45 | 1 | 50 |  |  |  | 10 | return unless $handle; | 
| 46 | 0 |  |  |  |  | 0 | return bless { | 
| 47 |  |  |  |  |  |  | impl => 'dynaloader', | 
| 48 |  |  |  |  |  |  | handle => $handle, | 
| 49 |  |  |  |  |  |  | }, $class; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub _dl_impl | 
| 54 |  |  |  |  |  |  | { | 
| 55 | 5 |  |  | 5 |  | 15 | my($class, $path, $flags) = @_; | 
| 56 | 5 |  |  |  |  | 950 | require FFI::Platypus::DL; | 
| 57 | 5 | 50 |  |  |  | 7836 | $flags = FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT() | 
| 58 |  |  |  |  |  |  | unless defined $flags; | 
| 59 | 5 |  |  |  |  | 366 | my $handle = FFI::Platypus::DL::dlopen($path, $flags); | 
| 60 | 5 | 50 |  |  |  | 25 | return unless defined $handle; | 
| 61 | 5 |  |  |  |  | 50 | bless { impl => 'dl', handle => $handle }, $class; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub address | 
| 65 |  |  |  |  |  |  | { | 
| 66 | 11 |  |  | 11 | 1 | 1367 | my($self, $name) = @_; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 11 | 50 |  |  |  | 43 | if($self->{impl} eq 'dl') | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | { | 
| 70 | 11 |  |  |  |  | 92 | return FFI::Platypus::DL::dlsym($self->{handle}, $name); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | elsif($self->{impl} eq 'dynaloader') | 
| 73 |  |  |  |  |  |  | { | 
| 74 | 0 |  |  |  |  | 0 | return DynaLoader::dl_find_symbol($self->{handle}, $name); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | elsif($self->{impl} eq 'null') | 
| 77 |  |  |  |  |  |  | { | 
| 78 | 0 |  |  |  |  | 0 | return; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | else | 
| 81 |  |  |  |  |  |  | { | 
| 82 | 0 |  |  |  |  | 0 | Carp::croak("Unknown implementaton: @{[ $self->{impl} ]}"); | 
|  | 0 |  |  |  |  | 0 |  | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub function | 
| 87 |  |  |  |  |  |  | { | 
| 88 | 6 |  |  | 6 | 1 | 1769 | my($self, $name, $sig) = @_; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 6 |  |  |  |  | 18 | my $addr = $self->address($name); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 6 | 100 |  |  |  | 217 | Carp::croak("Unknown function $name") | 
| 93 |  |  |  |  |  |  | unless defined $addr; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 4 |  |  |  |  | 529 | require FFI; | 
| 96 | 4 |  |  | 5 |  | 26 | sub { FFI::call($addr, $sig, @_) }; | 
|  | 5 |  |  |  |  | 2387 |  | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub DESTROY | 
| 100 |  |  |  |  |  |  | { | 
| 101 | 6 |  |  | 6 |  | 34263 | my($self) = @_; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 6 | 100 |  |  |  | 29 | if($self->{impl} eq 'dl') | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | { | 
| 105 | 5 |  |  |  |  | 144 | FFI::Platypus::DL::dlclose($self->{handle}); | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | elsif($self->{impl} eq 'dynaloader') | 
| 108 |  |  |  |  |  |  | { | 
| 109 |  |  |  |  |  |  | DynaLoader::dl_free_file($self->{handle}) | 
| 110 | 0 | 0 |  |  |  |  | if defined &DynaLoader::dl_free_file; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | elsif($self->{impl} eq 'null') | 
| 113 |  |  |  |  |  |  | { | 
| 114 |  |  |  |  |  |  | # do nothing | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | else | 
| 117 |  |  |  |  |  |  | { | 
| 118 | 0 |  |  |  |  |  | Carp::croak("Unknown implementaton: @{[ $self->{impl} ]}"); | 
|  | 0 |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | 1; | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | __END__ |