blib/lib/FFI/Platypus.pm

filter: Statementstmt 97.6% 253 / 259 Branchbran 85.0% 136 / 160 Conditioncond 68.2% 56 / 82 MC/DCmcdc 42.8% 21 / 49 Subroutinesub 100% 40 / 40 Podpod 100% 28 / 28 totaltotal 86.4% 534 / 618 CC 115 SCAR 22.9
CC
115
Cov
89%
function
22.3
new
20.4
find_symbol
14.0
CRAP 9.9
« lib/FFI/Build.pm ↑ Summary blib/lib/FFI/Platypus/Legacy.pm »
1 package FFI::Platypus;
2  
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 ▶use FFI::Platypus::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 56 ▶use FFI::Platypus::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Write Perl bindings to non-Perl libraries with FFI. No XS required.
11 56 our $VERSION = '2.11'; # VERSION
12  
13 # Platypus-Man,
14 # Platypus-Man,
15 # Does Whatever A Platypus Can
16 # Is Mildly Venomous
17 # Hangs Out In Rivers By Caves
18 # Look Out!
19 # Here Comes The Platypus-Man
20  
21 # From the original FFI::Platypus prototype:
22 # Kinda like gluing a duckbill to an adorable mammal
23  
24  
25 56 our @CARP_NOT = qw( FFI::Platypus::Declare FFI::Platypus::Record );
26  
27 56 require XSLoader;
28 56 ▶XSLoader::load(
Condition50%1/2
$FFI::Platypus::VERSION || 0
l!l
560
MC/DC0%0/1
$FFI::Platypus::VERSION || 0
$FFI::Platypus::VERSION
Truth table50%1/2
A
A= $FFI::Platypus::VERSION
Aresult
11
00
29 'FFI::Platypus', $FFI::Platypus::VERSION || 0
30 );
31  
32  
33 sub new
34 {
35 349 ▶ my($class, %args) = @_;
Subroutine new: called CC=16 SCAR=30.2
Pod documented
36 349 my @lib;
37 349 ▶ if(exists $args{lib})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"lib"}) 97 252 349
38 {
39 97 ▶ if(!ref($args{lib}))
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $args{'lib'}) { } 2 95 97
elsif (ref $args{'lib'} eq 'ARRAY') { } 95 0 95
40 {
41 2 push @lib, $args{lib};
42 }
43 elsif(ref($args{lib}) eq 'ARRAY')
44 {
45 95 push @lib, @{$args{lib}};
46 }
47 else
48 {
49 0 croak "lib argument must be a scalar or array reference";
50 }
51 }
52  
53 349 ▶ my $api = $args{api} || 0;
Condition100%2/2
$args{'api'} || 0
l!l
115234
MC/DC100%1/1
$args{'api'} || 0
$args{'api'}
Truth table100%2/2
A
A= $args{'api'}
Aresult
11
00
54 349 ▶ my $experimental = $args{experimental} || 0;
Condition50%1/2
$args{'experimental'} || 0
l!l
0349
MC/DC0%0/1
$args{'experimental'} || 0
$args{'experimental'}
Truth table50%1/2
A
A= $args{'experimental'}
Aresult
11
00
55  
56 349 ▶ if($experimental == 1)
Branch: 2 branches
BranchTrueFalseTotal
if ($experimental == 1) { } 0 349 349
elsif ($experimental == 2) { } 0 349 349
57 {
58 0 Carp::croak("Please do not use the experimental version of api = 1, instead require FFI::Platypus 1.00 or better");
59 }
60 elsif($experimental == 2)
61 {
62 0 Carp::croak("Please do not use the experimental version of api = 2, instead require FFI::Platypus 2.00 or better");
63 }
64  
65 349 ▶ if(defined $api && $api > 2 && $experimental != $api)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api and $api > 2 and $experimental != $api) 0 349 349
Condition33%2/6
defined $api and $api > 2 and $experimental != $api
!ll&&!rl&&r
34900
defined $api and $api > 2
!ll&&!rl&&r
03490
MC/DC0%0/3
defined $api and $api > 2 and $experimental != $api
defined $api $api > 2 $experimental != $api
Truth table25%1/4
A and B and C
A= defined $apiB= $api > 2C= $experimental != $api
ABCresult
0XX0
10X0
1100
1111
66 {
67 0 Carp::cluck("Enabling development API version $api prior to FFI::Platypus $api.00");
68 }
69  
70 349 my $tp;
71  
72 349 ▶ if($api == 0)
Branch: 3 branches
BranchTrueFalseTotal
if ($api == 0) { } 234 115 349
elsif ($api == 1) { } 50 65 115
elsif ($api == 2) { } 65 0 65
73 {
74 234 $tp = 'Version0';
75 }
76 elsif($api == 1)
77 {
78 50 $tp = 'Version1';
79 }
80 elsif($api == 2)
81 {
82 65 $tp = 'Version2';
83 }
84 else
85 {
86 0 Carp::croak("API version $api not (yet) implemented");
87 }
88  
89 349 require "FFI/Platypus/TypeParser/$tp.pm";
90 349 $tp = "FFI::Platypus::TypeParser::$tp";
91  
92 my $self = bless {
93 lib => \@lib,
94 lang => '',
95 handles => {},
96 abi => -1,
97 api => $api,
98 tp => $tp->new,
99 fini => [],
100 349 ▶ ignore_not_found => defined $args{ignore_not_found} ? $args{ignore_not_found} : 0,
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'ignore_not_found'} ? : 3 346 349
101 }, $class;
102  
103 349 ▶ $self->lang($args{lang} || 'C');
Condition100%2/2
$args{'lang'} || 'C'
l!l
12337
MC/DC100%1/1
$args{'lang'} || 'C'
$args{'lang'}
Truth table100%2/2
A
A= $args{'lang'}
Aresult
11
00
104  
105 349 $self;
106 }
107  
108 sub _lang_class ($)
109 {
110 494 ▶ my($lang) = @_;
Subroutine _lang_class: called CC=4 SCAR=13.9
111 494 ▶ my $class = $lang =~ m/^=(.*)$/ ? $1 : "FFI::Platypus::Lang::$lang";
Branch: 1 branch
BranchTrueFalseTotal
$lang =~ /^=(.*)$/ ? : 1 493 494
112 494 ▶ unless($class->can('native_type_map'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("native_type_map")) 53 441 494
113 {
114 53 my $pm = "$class.pm";
115 53 $pm =~ s/::/\//g;
116 53 require $pm;
117 }
118 494 ▶ croak "$class does not provide native_type_map method"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("native_type_map") 0 494 494
119 unless $class->can("native_type_map");
120 494 $class;
121 }
122  
123  
124 sub lib
125 {
126 101 ▶ my($self, @new) = @_;
Subroutine lib: called CC=3 SCAR=11.0
Pod documented
127  
128 101 ▶ if(@new)
Branch: 1 branch
BranchTrueFalseTotal
if (@new) 89 12 101
129 {
130 89 ▶ push @{ $self->{lib} }, map { ref $_ eq 'CODE' ? $_->() : $_ } @new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 1 88 89
131 89 delete $self->{mangler};
132 }
133  
134 101 @{ $self->{lib} };
135 }
136  
137  
138 sub ignore_not_found
139 {
140 66 ▶ my($self, $value) = @_;
Subroutine ignore_not_found: called CC=2 SCAR=6.9
Pod documented
141  
142 66 ▶ if(defined $value)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 63 66
143 {
144 3 $self->{ignore_not_found} = $value;
145 }
146  
147 66 $self->{ignore_not_found};
148 }
149  
150  
151 sub lang
152 {
153 352 ▶ my($self, $value) = @_;
Subroutine lang: called CC=10 SCAR=23.1
Pod documented
154  
155 352 ▶ if(defined $value && $value ne $self->{lang})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne $self->{"lang"}) 352 0 352
Condition33%1/3
defined $value and $value ne $self->{"lang"}
!ll&&!rl&&r
00352
MC/DC0%0/2
defined $value and $value ne $self->{"lang"}
defined $value $value ne $self->{"lang"}
Truth table33%1/3
A and B
A= defined $valueB= $value ne $self->{"lang"}
ABresult
0X0
100
111
156 {
157 352 $self->{lang} = $value;
158 352 my $class = _lang_class($self->{lang});
159 352 ▶ $self->abi($class->abi) if $class->can('abi');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("abi") 1 351 352
160  
161 {
162 352 my %type_map;
163 my $map = $class->native_type_map(
164 $self->{api} > 0
165 ? (api => $self->{api})
166 352 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$self->{'api'} > 0 ? : 116 236 352
167 );
168 352 foreach my $key (keys %$map)
169 {
170 18695 my $value = $map->{$key};
171 18695 ▶ next unless $self->{tp}->have_type($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"tp"}->have_type($value) 338 18357 18695
172 18357 $type_map{$key} = $value;
173 }
174 352 $type_map{$_} = $_ for grep { $self->{tp}->have_type($_) }
175 qw( void sint8 uint8 sint16 uint16 sint32 uint32 sint64 uint64 float double string opaque
176 longdouble complex_float complex_double );
177 352 ▶ $type_map{pointer} = 'opaque' if $self->{tp}->isa('FFI::Platypus::TypeParser::Version0');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tp"}->isa("FFI::Platypus::TypeParser::Version0") 236 116 352
178 352 $self->{tp}->type_map(\%type_map);
179 }
180  
181 352 ▶ $class->load_custom_types($self) if $class->can('load_custom_types');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("load_custom_types") 2 350 352
182 }
183  
184 352 $self->{lang};
185 }
186  
187  
188 3 ▶sub api { shift->{api} }
Subroutine api: called CC=1 SCAR=0.0
Pod documented
189  
190  
191 sub type
192 {
193 275 ▶ my($self, $name, $alias) = @_;
Subroutine type: called CC=5 SCAR=16.5
Pod documented
194 275 ▶ croak "usage: \$ffi->type(name => alias) (alias is optional)" unless defined $self && defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $name 0 275 275
Condition33%1/3
defined $self and defined $name
!ll&&!rl&&r
00275
MC/DC0%0/2
defined $self and defined $name
defined $self defined $name
Truth table33%1/3
A and B
A= defined $selfB= defined $name
ABresult
0X0
100
111
195  
196 275 ▶ $self->{tp}->check_alias($alias) if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 206 69 275
197 275 my $type = $self->{tp}->parse($name);
198 262 ▶ $self->{tp}->set_alias($alias, $type) if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 206 56 262
199  
200 262 $self;
201 }
202  
203  
204 sub custom_type
205 {
206 207 ▶ my($self, $alias, $cb) = @_;
Subroutine custom_type: called CC=8 SCAR=21.7
Pod documented
207  
208 207 ▶ my $argument_count = $cb->{argument_count} || 1;
Condition100%2/2
$cb->{'argument_count'} || 1
l!l
2205
MC/DC100%1/1
$cb->{'argument_count'} || 1
$cb->{'argument_count'}
Truth table100%2/2
A
A= $cb->{'argument_count'}
Aresult
11
00
209  
210 207 ▶ croak "argument_count must be >= 1"
Branch: 1 branch
BranchTrueFalseTotal
unless $argument_count >= 1 0 207 207
211 unless $argument_count >= 1;
212  
213 207 ▶ croak "Usage: \$ffi->custom_type(\$alias, { ... })"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alias and ref $cb eq "HASH" 0 207 207
Condition33%1/3
defined $alias and ref $cb eq "HASH"
!ll&&!rl&&r
00207
MC/DC0%0/2
defined $alias and ref $cb eq "HASH"
defined $alias ref $cb eq "HASH"
Truth table33%1/3
A and B
A= defined $aliasB= ref $cb eq "HASH"
ABresult
0X0
100
111
214 unless defined $alias && ref($cb) eq 'HASH';
215  
216 croak "must define at least one of native_to_perl, perl_to_native, or perl_to_native_post"
217 207 ▶ unless defined $cb->{native_to_perl} || defined $cb->{perl_to_native} || defined $cb->{perl_to_native_post};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb->{"native_to_perl"} or defined $cb->{"perl_to_native"} or defined $cb->{"perl_to_native_post"} 0 207 207
Condition83%5/6
defined $cb->{"native_to_perl"} or defined $cb->{"perl_to_native"} or defined $cb->{"perl_to_native_post"}
l!l&&r!l&&!r
194130
defined $cb->{"native_to_perl"} or defined $cb->{"perl_to_native"}
l!l&&r!l&&!r
1256913
MC/DC0%0/3
defined $cb->{"native_to_perl"} or defined $cb->{"perl_to_native"} or defined $cb->{"perl_to_native_post"}
defined $cb->{"native_to_perl"} defined $cb->{"perl_to_native"} defined $cb->{"perl_to_native_post"}
Truth table75%3/4
A or B or C
A= defined $cb->{"native_to_perl"}B= defined $cb->{"perl_to_native"}C= defined $cb->{"perl_to_native_post"}
ABCresult
1XX1
01X1
0011
0000
218  
219 207 $self->{tp}->check_alias($alias);
220  
221 my $type = $self->{tp}->create_type_custom(
222 $cb->{native_type},
223 $cb->{perl_to_native},
224 $cb->{native_to_perl},
225 $cb->{perl_to_native_post},
226 207 $argument_count,
227 );
228  
229 205 $self->{tp}->set_alias($alias, $type);
230  
231 205 $self;
232 }
233  
234  
235 sub load_custom_type
236 {
237 13 ▶ my($self, $name, $alias, @type_args) = @_;
Subroutine load_custom_type: called CC=8 SCAR=22.2
Pod documented
238  
239 13 ▶ croak "usage: \$ffi->load_custom_type(\$name, \$alias, ...)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $alias 0 13 13
Condition33%1/3
defined $name and defined $alias
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $name and defined $alias
defined $name defined $alias
Truth table33%1/3
A and B
A= defined $nameB= defined $alias
ABresult
0X0
100
111
240 unless defined $name && defined $alias;
241  
242 13 ▶ $name = "FFI::Platypus::Type$name" if $name =~ /^::/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^::/ 13 0 13
243 13 ▶ $name = "FFI::Platypus::Type::$name" unless $name =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /::/ 0 13 13
244  
245 13 ▶ unless($name->can("ffi_custom_type_api_1"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($name->can("ffi_custom_type_api_1")) 3 10 13
246 {
247 3 my $pm = "$name.pm";
248 3 $pm =~ s/::/\//g;
249 3 eval { require $pm };
250 3 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
251 }
252  
253 13 ▶ unless($name->can("ffi_custom_type_api_1"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($name->can("ffi_custom_type_api_1")) 0 13 13
254 {
255 0 croak "$name does not appear to conform to the custom type API";
256 }
257  
258 13 my $cb = $name->ffi_custom_type_api_1($self, @type_args);
259 13 $self->custom_type($alias => $cb);
260  
261 13 $self;
262 }
263  
264  
265 sub types
266 {
267 3 ▶ my($self) = @_;
Subroutine types: called CC=3 SCAR=11.0
Pod documented
268 3 ▶ $self = $self->new unless ref $self && eval { $self->isa('FFI::Platypus') };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and eval { do { $self->isa("FFI::Platypus") } } 1 2 3
Condition66%2/3
ref $self and eval { do { $self->isa("FFI::Platypus") } }
!ll&&!rl&&r
102
MC/DC50%1/2
ref $self and eval { do { $self->isa("FFI::Platypus") } }
ref $self eval { do { $self->isa("FFI::Platypus") } }
Truth table66%2/3
A and B
A= ref $selfB= eval { do { $self->isa("FFI::Platypus") } }
ABresult
0X0
100
111
269 3 sort $self->{tp}->list_types;
270 }
271  
272  
273 sub type_meta
274 {
275 131 ▶ my($self, $name) = @_;
Subroutine type_meta: called CC=3 SCAR=12.0
Pod documented
276 131 ▶ $self = $self->new unless ref $self && eval { $self->isa('FFI::Platypus') };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and eval { do { $self->isa("FFI::Platypus") } } 0 131 131
Condition33%1/3
ref $self and eval { do { $self->isa("FFI::Platypus") } }
!ll&&!rl&&r
00131
MC/DC0%0/2
ref $self and eval { do { $self->isa("FFI::Platypus") } }
ref $self eval { do { $self->isa("FFI::Platypus") } }
Truth table33%1/3
A and B
A= ref $selfB= eval { do { $self->isa("FFI::Platypus") } }
ABresult
0X0
100
111
277 131 $self->{tp}->parse($name)->meta;
278 }
279  
280  
281 sub mangler
282 {
283 12 ▶ my($self, $sub) = @_;
Subroutine mangler: called CC=1 SCAR=0.0
Pod documented
284 12 $self->{mangler} = $self->{mymangler} = $sub;
285 }
286  
287  
288 sub function
289 {
290 1270 ▶ my $wrapper;
Subroutine function: called CC=22 SCAR=31.0
Pod documented
291 1270 ▶ $wrapper = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 32 1238 1270
292  
293 1270 ▶ croak "usage \$ffi->function( \$name, \\\@arguments, [\\\@var_args], [\$return_type])" unless @_ >= 3 && @_ <= 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 and @_ <= 6 0 1270 1270
Condition33%1/3
@_ >= 3 and @_ <= 6
!ll&&!rl&&r
001270
MC/DC0%0/2
@_ >= 3 and @_ <= 6
@_ >= 3 @_ <= 6
Truth table33%1/3
A and B
A= @_ >= 3B= @_ <= 6
ABresult
0X0
100
111
294  
295 1270 my $self = shift;
296 1270 my $name = shift;
297 1270 my $fixed_args = shift;
298 1270 my $var_args;
299 1270 ▶ $var_args = shift if defined $_[0] && ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] eq "ARRAY" 30 1240 1270
Condition100%3/3
defined $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
6123430
MC/DC100%2/2
defined $_[0] and ref $_[0] eq "ARRAY"
defined $_[0] ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= defined $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
300 1270 my $ret = shift;
301 1270 ▶ $ret = 'void' unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 16 1254 1270
302  
303 # special case: treat a single void argument type as an empty list of
304 # arguments, a la olde timey C compilers.
305 1270 ▶ if( (!defined $var_args) && @$fixed_args == 1 && $fixed_args->[0] eq 'void' )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $var_args and @$fixed_args == 1 and $fixed_args->[0] eq "void") 1 1269 1270
Condition100%6/6
not defined $var_args and @$fixed_args == 1 and $fixed_args->[0] eq "void"
!ll&&!rl&&r
6226471
not defined $var_args and @$fixed_args == 1
!ll&&!rl&&r
30592648
MC/DC100%3/3
not defined $var_args and @$fixed_args == 1 and $fixed_args->[0] eq "void"
not defined $var_args @$fixed_args == 1 $fixed_args->[0] eq "void"
Truth table100%4/4
not(A) and B and C
A= not defined $var_argsB= @$fixed_args == 1C= $fixed_args->[0] eq "void"
ABCresult
0XX0
10X0
1100
1111
306 {
307 1 $fixed_args = [];
308 }
309  
310 1270 ▶ my $fixed_arg_count = defined $var_args ? scalar(@$fixed_args) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $var_args ? : 30 1240 1270
311  
312 1270 ▶ my @args = map { $self->{tp}->parse($_) || croak "unknown type: $_" } @$fixed_args;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"tp"}->parse($_) or croak("unknown type: $_") 0 1524 1524
313 1270 ▶ if($var_args)
Branch: 1 branch
BranchTrueFalseTotal
if ($var_args) 30 1240 1270
314 {
315 push @args, map {
316 30 my $type = $self->{tp}->parse($_);
317 # https://github.com/PerlFFI/FFI-Platypus/issues/323
318 127 ▶ $type->type_code == 67 ? $self->{tp}->parse('double') : $type
Branch: 1 branch
BranchTrueFalseTotal
$type->type_code == 67 ? : 7 120 127
319 } @$var_args;
320 }
321  
322 1270 ▶ $ret = $self->{tp}->parse($ret) || croak "unknown type: $ret";
Condition33%1/3
$self->{'tp'}->parse($ret) || croak("unknown type: $ret")
l!l&&r!l&&!r
127000
MC/DC0%0/2
$self->{'tp'}->parse($ret) || croak("unknown type: $ret")
$self->{'tp'}->parse($ret) croak("unknown type: $ret")
Truth table33%1/3
A || B
A= $self->{'tp'}->parse($ret)B= croak("unknown type: $ret")
ABresult
1X1
011
000
323 1270 ▶ my $address = $name =~ /^-?[0-9]+$/ ? $name : $self->find_symbol($name);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^-?[0-9]+$/ ? : 151 1119 1270
324 1270 ▶ croak "unable to find $name" unless defined $address || $self->ignore_not_found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address or $self->ignore_not_found 58 1212 1270
Condition100%3/3
defined $address or $self->ignore_not_found
l!l&&r!l&&!r
1207558
MC/DC100%2/2
defined $address or $self->ignore_not_found
defined $address $self->ignore_not_found
Truth table100%3/3
A or B
A= defined $addressB= $self->ignore_not_found
ABresult
1X1
011
000
325 1212 ▶ return unless defined $address;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 5 1207 1212
326 1207 ▶ $address = @args > 0 ? _cast1() : _cast0() if $address == 0;
Branch: 2 branches
BranchTrueFalseTotal
if $address == 0 150 1057 1207
@args > 0 ? : 149 1 150
327 1207 my $function = FFI::Platypus::Function::Function->new($self, $address, $self->{abi}, $fixed_arg_count, $ret, @args);
328 1206 ▶ $wrapper
Branch: 1 branch
BranchTrueFalseTotal
$wrapper ? : 32 1174 1206
329 ? FFI::Platypus::Function::Wrapper->new($function, $wrapper)
330 : $function;
331 }
332  
333 sub _function_meta
334 {
335 # NOTE: may be upgraded to a documented function one day,
336 # but shouldn't be used externally as we will rename it
337 # if that happens.
338 2 ▶ my($self, $name, $meta, $args, $ret) = @_;
Subroutine _function_meta: called CC=1 SCAR=0.0
339 2 $args = ['opaque','int',@$args];
340 $self->function(
341 $name, $args, $ret, sub {
342 4 ▶ my $xsub = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
343 4 $xsub->($meta, scalar(@_), @_);
344 },
345 2 );
346 }
347  
348  
349 sub attach
350 {
351 880 ▶ my $wrapper;
Subroutine attach: called CC=9 SCAR=22.0
Pod documented
352 880 ▶ $wrapper = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 18 862 880
353  
354 880 my $self = shift;
355 880 my $name = shift;
356 880 my $args = shift;
357 880 my $varargs;
358 880 ▶ $varargs = shift if defined $_[0] && ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] eq "ARRAY" 2 878 880
Condition100%3/3
defined $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
18772
MC/DC100%2/2
defined $_[0] and ref $_[0] eq "ARRAY"
defined $_[0] ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= defined $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
359 880 my $ret = shift;
360 880 my $proto = shift;
361  
362 880 ▶ $ret = 'void' unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 1 879 880
363  
364 880 ▶ my($c_name, $perl_name) = ref($name) ? @$name : ($name, $name);
Branch: 1 branch
BranchTrueFalseTotal
ref $name ? : 658 222 880
365  
366 880 ▶ croak "you tried to provide a perl name that looks like an address"
Branch: 1 branch
BranchTrueFalseTotal
if $perl_name =~ /^-?[0-9]+$/ 0 880 880
367 if $perl_name =~ /^-?[0-9]+$/;
368  
369 880 ▶ my $function = $varargs
Branch: 1 branch
BranchTrueFalseTotal
$varargs ? : 2 878 880
370 ? $self->function($c_name, $args, $varargs, $ret, $wrapper)
371 : $self->function($c_name, $args, $ret, $wrapper);
372  
373 877 ▶ if(defined $function)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function) 875 2 877
374 {
375 875 $function->attach($perl_name, $proto);
376 }
377  
378 877 $self;
379 }
380  
381  
382 sub closure
383 {
384 86 ▶ my($self, $coderef) = @_;
Subroutine closure: called CC=3 SCAR=11.0
Pod documented
385 86 ▶ return undef unless defined $coderef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $coderef 1 85 86
386 85 ▶ croak "not a coderef" unless ref $coderef eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coderef eq "CODE" 0 85 85
387 85 require FFI::Platypus::Closure;
388 85 FFI::Platypus::Closure->new($coderef);
389 }
390  
391  
392 sub cast
393 {
394 137 ▶ $_[0]->function(0 => [$_[1]] => $_[2])->call($_[3]);
Subroutine cast: called CC=1 SCAR=0.0
Pod documented
395 }
396  
397  
398 sub attach_cast
399 {
400 4 ▶ my($self, $name, $type1, $type2, $wrapper) = @_;
Subroutine attach_cast: called CC=4 SCAR=14.0
Pod documented
401 4 my $caller = caller;
402 4 ▶ $name = join '::', $caller, $name unless $name =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /::/ 4 0 4
403 4 ▶ if(defined $wrapper && ref($wrapper) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrapper and ref $wrapper eq 'CODE') { } 1 3 4
Condition66%2/3
defined $wrapper and ref $wrapper eq "CODE"
!ll&&!rl&&r
301
MC/DC50%1/2
defined $wrapper and ref $wrapper eq "CODE"
defined $wrapper ref $wrapper eq "CODE"
Truth table66%2/3
A and B
A= defined $wrapperB= ref $wrapper eq "CODE"
ABresult
0X0
100
111
404 {
405 1 $self->attach([0 => $name] => [$type1] => $type2 => '$', $wrapper);
406 }
407 else
408 {
409 3 $self->attach([0 => $name] => [$type1] => $type2 => '$');
410 }
411 4 $self;
412 }
413  
414  
415 sub sizeof
416 {
417 209 ▶ my($self,$name) = @_;
Subroutine sizeof: called CC=2 SCAR=6.9
Pod documented
418 ref $self
419 209 ▶ ? $self->{tp}->parse($name)->sizeof
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 201 8 209
420 : $self->new->sizeof($name);
421 }
422  
423  
424 sub alignof
425 {
426 74 ▶ my($self, $name) = @_;
Subroutine alignof: called CC=2 SCAR=6.9
Pod documented
427 ref $self
428 74 ▶ ? $self->{tp}->parse($name)->alignof
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 62 12 74
429 : $self->new->alignof($name);
430 }
431  
432  
433 sub kindof
434 {
435 12 ▶ my($self, $name) = @_;
Subroutine kindof: called CC=2 SCAR=6.9
Pod documented
436 ref $self
437 12 ▶ ? $self->{tp}->parse($name)->kindof
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 6 6 12
438 : $self->new->kindof($name);
439 }
440  
441  
442 sub countof
443 {
444 12 ▶ my($self, $name) = @_;
Subroutine countof: called CC=2 SCAR=6.9
Pod documented
445 ref $self
446 12 ▶ ? $self->{tp}->parse($name)->countof
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 6 6 12
447 : $self->new->countof($name);
448 }
449  
450  
451 sub def
452 {
453 10 ▶ my $self = shift;
Subroutine def: called CC=3 SCAR=11.0
Pod documented
454 10 ▶ my $package = shift || caller;
Condition66%2/3
shift() || caller
l!l&&r!l&&!r
640
MC/DC0%0/2
shift() || caller
shift() caller
Truth table66%2/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
455 10 my $type = shift;
456 10 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 6 10
457 {
458 4 $self->type($type);
459 3 $self->{def}->{$package}->{$type} = shift;
460 }
461 9 $self->{def}->{$package}->{$type};
462 }
463  
464  
465 sub unitof
466 {
467 12 ▶ my($self, $name) = @_;
Subroutine unitof: called CC=2 SCAR=6.9
Pod documented
468 ref $self
469 12 ▶ ? $self->{tp}->parse($name)->unitof
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 6 6 12
470 : $self->new->unitof($name);
471 }
472  
473  
474 sub find_lib
475 {
476 7 ▶ my $self = shift;
Subroutine find_lib: called CC=1 SCAR=0.0
Pod documented
477 7 require FFI::CheckLib;
478 7 $self->lib(FFI::CheckLib::find_lib(@_));
479 7 $self;
480 }
481  
482  
483 sub find_symbol
484 {
485 1160 ▶ my($self, $name) = @_;
Subroutine find_symbol: called CC=14 SCAR=26.4
Pod documented
486  
487 1160 ▶ $self->{mangler} ||= $self->{mymangler};
Condition66%2/3
$self->{"mangler"} ||= $self->{"mymangler"}
l!l&&r!l&&!r
10180142
MC/DC50%1/2
$self->{"mangler"} ||= $self->{"mymangler"}
$self->{"mangler"} $self->{"mymangler"}
Truth table66%2/3
A ||= B
A= $self->{"mangler"}B= $self->{"mymangler"}
ABresult
1X1
011
000
488  
489 1160 ▶ unless(defined $self->{mangler})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"mangler"}) 142 1018 1160
490 {
491 142 my $class = _lang_class($self->{lang});
492 142 ▶ if($class->can('mangler'))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('mangler')) { } 2 140 142
493 {
494 2 $self->{mangler} = $class->mangler($self->lib);
495 }
496 else
497 {
498 140 ▶ $self->{mangler} = sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
499 }
500 }
501  
502 1160 foreach my $path (@{ $self->{lib} })
503 {
504 56 ▶ my $handle = do { no warnings; $self->{handles}->{$path||0} } || FFI::Platypus::DL::dlopen($path, FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT());
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition100%5/5
do { no warnings; $self->{'handles'}{$path or 0} } || FFI::Platypus::DL::dlopen($path, &FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT())
l!l&&r!l&&!r
10351542
$path || 0
l!l
1031160
MC/DC100%2/2
do { no warnings; $self->{'handles'}{$path or 0} } || FFI::Platypus::DL::dlopen($path, &FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT())
do { no warnings; $self->{'handles'}{$path or 0} } FFI::Platypus::DL::dlopen($path, &FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT())
MC/DC100%1/1
$path || 0
$path
Truth table100%3/3
A || B
A= do { no warnings; $self->{'handles'}{$path or 0} }B= FFI::Platypus::DL::dlopen($path, &FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT())
ABresult
1X1
011
000
Truth table100%2/2
A
A= $path
Aresult
11
00
505 1191 ▶ unless($handle)
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 2 1189 1191
506 {
507 warn "warning: error loading $path: ", FFI::Platypus::DL::dlerror()
508 2 ▶ if $self->{api} > 0 || $ENV{FFI_PLATYPUS_DLERROR};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"api"} > 0 or $ENV{"FFI_PLATYPUS_DLERROR"} 1 1 2
Condition66%2/3
$self->{"api"} > 0 or $ENV{"FFI_PLATYPUS_DLERROR"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$self->{"api"} > 0 or $ENV{"FFI_PLATYPUS_DLERROR"}
$self->{"api"} > 0 $ENV{"FFI_PLATYPUS_DLERROR"}
Truth table66%2/3
A or B
A= $self->{"api"} > 0B= $ENV{"FFI_PLATYPUS_DLERROR"}
ABresult
1X1
011
000
509 2 next;
510 }
511 1189 my $address = FFI::Platypus::DL::dlsym($handle, $self->{mangler}->($name));
512 1189 ▶ if($address)
Branch: 1 branch
BranchTrueFalseTotal
if ($address) { } 1063 126 1189
513 {
514 1063 ▶ $self->{handles}->{$path||0} = $handle;
Condition100%2/2
$path || 0
l!l
936127
MC/DC100%1/1
$path || 0
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
515 1063 return $address;
516 }
517 else
518 {
519 126 ▶ FFI::Platypus::DL::dlclose($handle) unless $self->{handles}->{$path||0};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handles"}{$path or 0} 32 94 126
Condition100%2/2
$path || 0
l!l
9333
MC/DC100%1/1
$path || 0
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
520 }
521 }
522 97 return;
523 }
524  
525  
526 sub bundle
527 {
528 27 ▶ croak "bundle method only available with api => 1 or better" if $_[0]->{api} < 1;
Subroutine bundle: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"api"} < 1 0 27 27
529 27 require FFI::Platypus::Bundle;
530 27 goto &_bundle;
531 }
532  
533  
534 sub package
535 {
536 3 ▶ croak "package method only available with api => 0" if $_[0]->{api} > 0;
Subroutine package: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"api"} > 0 1 2 3
537 2 require FFI::Platypus::Legacy;
538 2 goto &_package;
539 }
540  
541  
542 sub abis
543 {
544 15 ▶ require FFI::Platypus::ShareConfig;
Subroutine abis: called CC=1 SCAR=0.0
Pod documented
545 15 FFI::Platypus::ShareConfig->get("abi");
546 }
547  
548  
549 sub abi
550 {
551 13 ▶ my($self, $newabi) = @_;
Subroutine abi: called CC=4 SCAR=13.9
Pod documented
552 13 ▶ unless($newabi =~ /^[0-9]+$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($newabi =~ /^[0-9]+$/) 7 6 13
553 {
554 7 ▶ unless(defined $self->abis->{$newabi})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->abis->{$newabi}) 1 6 7
555 {
556 1 croak "no such ABI: $newabi";
557 }
558 6 $newabi = $self->abis->{$newabi};
559 }
560  
561 12 ▶ unless(FFI::Platypus::ABI::verify($newabi))
Branch: 1 branch
BranchTrueFalseTotal
unless (FFI::Platypus::ABI::verify($newabi)) 1 11 12
562 {
563 1 croak "no such ABI: $newabi";
564 }
565  
566 11 $self->{abi} = $newabi;
567 11 $self->{tp}->abi($newabi);
568  
569 11 $self;
570 }
571  
572 sub DESTROY
573 {
574 248 ▶ my($self) = @_;
Subroutine DESTROY: called CC=4 SCAR=13.9
575 248 foreach my $fini (@{ $self->{fini} })
576 {
577 1 $fini->($self);
578 }
579 248 foreach my $handle (values %{ $self->{handles} })
580 {
581 44 ▶ next unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 44 44
582 44 FFI::Platypus::DL::dlclose($handle);
583 }
584 248 delete $self->{handles};
585 }
586  
587 56 1;
588  
589 __END__
« lib/FFI/Build.pm ↑ Summary blib/lib/FFI/Platypus/Legacy.pm »