File Coverage

blib/lib/Archive/Libarchive/FFI/SweetLite.pm
Criterion Covered Total %
statement 21 59 35.5
branch 0 8 0.0
condition 0 2 0.0
subroutine 7 26 26.9
pod 0 2 0.0
total 28 97 28.8


line stmt bran cond sub pod time code
1             package
2             Archive::Libarchive::FFI::SweetLite;
3              
4 27     27   557 use strict;
  27         52  
  27         1011  
5 27     27   137 use warnings;
  27         48  
  27         817  
6 27     27   137 use FFI::Raw;
  27         44  
  27         797  
7 27     27   40975 use Text::ParseWords qw( shellwords );
  27         40153  
  27         2299  
8             use Exporter::Tidy
9 27         298 default => [qw(
10             ffi_lib attach_function
11             _void _int _uint _long _ulong _int64 _uint64
12             _short _ushort _char _uchar _float _double _str _ptr
13 27     27   204 )];
  27         53  
14              
15             # This is intended for use with Archive::Libarchive::FFI ONLY until
16             # FFI::Sweet makes it to CPAN
17              
18             my @libs;
19              
20             sub ffi_lib ($)
21             {
22 0     0 0   my $lib = shift;
23 0           push @libs, $$lib;
24             }
25              
26             sub attach_function ($$$;$)
27             {
28 0     0 0   my($name, $arg_types, $rv_type, $wrapper ) = @_;
29 0           my $pkg = caller;
30 0   0       $arg_types //= [];
31 0           my $install_name = $name;
32 0 0         ( $name, $install_name ) = @{ $name } if ref $name;
  0            
33            
34 0           foreach my $lib (@libs)
35             {
36 0           my $ffi = eval { FFI::Raw->new($lib, $name, $rv_type, @$arg_types) };
  0            
37 0 0         next if $@;
38            
39             my $base_sub = sub {
40 0     0     my @args = @_;
41 0           my $ret = eval {
42 0           $ffi->call(@args);
43             };
44 0 0         die "$name: $@" if $@;
45 0           return $ret;
46 0           };
47            
48 27     27   8911 no strict 'refs';
  27         58  
  27         4122  
49 0 0   0     *{join '::', $pkg, $install_name} = $wrapper ? sub { $wrapper->($base_sub, @_) } : $base_sub;
  0            
  0            
50 0           return;
51             }
52            
53 0           die "unable to find $name\n";
54             }
55              
56             foreach my $type (qw( void int uint long ulong int64 uint64 short ushort char uchar float double str ptr ))
57             {
58 27     27   144 no strict 'refs';
  27         50  
  27         1956  
59 0     0     eval qq{ sub _$type { FFI::Raw::$type\() } };
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
  0     0      
60             die $@ if $@;
61             }
62              
63             1;
64              
65             __END__