| 1 | package FFI::Platypus::Bundle; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use 5.008004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Bundle foreign code with your Perl module | ||||||||||||||||||||||||||||
| 9 | 19 | our $VERSION = '2.12'; # VERSION | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package FFI::Platypus; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub _bundle | ||||||||||||||||||||||||||||
| 15 | 27 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_bundle: called CC=16 SCAR=28.0
| |||||||||||||||||||||||||||||
| 16 | 27 | my @arg_ptrs; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 27 | ▶ | if(defined $_[-1] && ref($_[-1]) eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_[-1] and ref $_[-1] eq "ARRAY"
MC/DC50%1/2
defined $_[-1] and ref $_[-1] eq "ARRAY"
defined $_[-1]
ref $_[-1] eq "ARRAY"
Truth table66%2/3
A and B
A= defined $_[-1]B= ref $_[-1] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||
| 20 | 1 | @arg_ptrs = @{ pop @_ }; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 27 | push @arg_ptrs, undef; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 27 | my($self, $package) = @_; | |||||||||||||||||||||||||||
| 26 | 27 | ▶ | $package = caller unless defined $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 27 | require List::Util; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 27 | my($pm) = do { | |||||||||||||||||||||||||||
| 31 | 27 | my $pm = "$package.pm"; | |||||||||||||||||||||||||||
| 32 | 27 | $pm =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 33 | # if the module is already loaded, we can use %INC | ||||||||||||||||||||||||||||
| 34 | # otherwise we can go through @INC and find the first .pm | ||||||||||||||||||||||||||||
| 35 | # this doesn't handle all edge cases, but probably enough | ||||||||||||||||||||||||||||
| 36 | 27 | ▶ | List::Util::first(sub { (defined $_) && (-f $_) }, ($INC{$pm}, map { "$_/$pm" } @INC)); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 27 | ▶ | Carp::croak "unable to find module $package" unless $pm; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 27 | my @parts = split /::/, $package; | |||||||||||||||||||||||||||
| 42 | 27 | my $incroot = $pm; | |||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 27 | my $c = @parts; | |||||||||||||||||||||||||||
| 45 | 27 | ▶ | $incroot =~ s![\\/][^\\/]+$!! while $c--; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 27 | ▶ | my $txtfn = List::Util::first(sub { -f $_ }, do { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 27 | my $dir = join '/', @parts; | |||||||||||||||||||||||||||
| 50 | 27 | my $file = $parts[-1] . ".txt"; | |||||||||||||||||||||||||||
| 51 | ( | ||||||||||||||||||||||||||||
| 52 | 27 | "$incroot/auto/$dir/$file", | |||||||||||||||||||||||||||
| 53 | "$incroot/../arch/auto/$dir/$file", | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | }); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 27 | my $lib; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 27 | ▶ | if($txtfn) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 24 | $lib = do { | |||||||||||||||||||||||||||
| 62 | 24 | my $fh; | |||||||||||||||||||||||||||
| 63 | 24 | ▶ | open($fh, '<', $txtfn) or die "unable to read $txtfn $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 24 | my $line = <$fh>; | |||||||||||||||||||||||||||
| 65 | 24 | close $fh; | |||||||||||||||||||||||||||
| 66 | 24 | ▶ | $line =~ /^FFI::Build\@(.*)$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | ? "$incroot/$1" | ||||||||||||||||||||||||||||
| 68 | : Carp::croak "bad format $txtfn"; | ||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | 24 | ▶ | Carp::croak "bundle code is missing: $lib" unless -f $lib; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | elsif(-d "$incroot/../ffi") | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 3 | require FFI::Build::MM; | |||||||||||||||||||||||||||
| 75 | 3 | require Capture::Tiny; | |||||||||||||||||||||||||||
| 76 | 3 | require Cwd; | |||||||||||||||||||||||||||
| 77 | 3 | require File::Spec; | |||||||||||||||||||||||||||
| 78 | 3 | my $save = Cwd::getcwd(); | |||||||||||||||||||||||||||
| 79 | 3 | chdir "$incroot/.."; | |||||||||||||||||||||||||||
| 80 | 3 | ▶ | my($output, $error) = Capture::Tiny::capture_merged(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 3 | $lib = eval { | |||||||||||||||||||||||||||
| 82 | 3 | my $dist_name = $package; | |||||||||||||||||||||||||||
| 83 | 3 | $dist_name =~ s/::/-/g; | |||||||||||||||||||||||||||
| 84 | 3 | my $fbmm = FFI::Build::MM->new( save => 0 ); | |||||||||||||||||||||||||||
| 85 | 3 | $fbmm->mm_args( DISTNAME => $dist_name ); | |||||||||||||||||||||||||||
| 86 | 3 | my $build = $fbmm->load_build('ffi', undef, 'ffi/_build'); | |||||||||||||||||||||||||||
| 87 | 3 | $build->build; | |||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||
| 89 | 3 | $@; | |||||||||||||||||||||||||||
| 90 | 3 | }); | |||||||||||||||||||||||||||
| 91 | 3 | ▶ | if($error) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | chdir $save; | |||||||||||||||||||||||||||
| 94 | 0 | print STDERR $output; | |||||||||||||||||||||||||||
| 95 | 0 | die $error; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 3 | $lib = File::Spec->rel2abs($lib); | |||||||||||||||||||||||||||
| 100 | 3 | chdir $save; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else | ||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | Carp::croak "unable to find bundle code for $package"; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 27 | ▶ | my $handle = FFI::Platypus::DL::dlopen($lib, FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | or Carp::croak "error loading bundle code: $lib @{[ FFI::Platypus::DL::dlerror() ]}"; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 27 | $self->{handles}->{$lib} = $handle; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 27 | $self->lib($lib); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 27 | ▶ | if(my $init = eval { $self->function( 'ffi_pl_bundle_init' => [ 'string', 'sint32', 'opaque[]' ] => 'void' ) }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 1 | $init->call($package, scalar(@arg_ptrs)-1, \@arg_ptrs); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 27 | ▶ | if(my $init = eval { $self->function( 'ffi_pl_bundle_constant' => [ 'string', 'opaque' ] => 'void' ) }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 1 | require FFI::Platypus::Constant; | |||||||||||||||||||||||||||
| 123 | 1 | my $api = FFI::Platypus::Constant->new($package); | |||||||||||||||||||||||||||
| 124 | 1 | $init->call($package, $api->ptr); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 27 | ▶ | if(my $address = $self->find_symbol( 'ffi_pl_bundle_fini' )) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 1 | ▶ | push @{ $self->{fini} }, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 1 | $self->function( $address => [ 'string' ] => 'void' ) | |||||||||||||||||||||||||||
| 132 | ->call( $package ); | ||||||||||||||||||||||||||||
| 133 | 1 | }; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 27 | $self; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 19 | 1; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||