blib/lib/FFI/Platypus.pm

filter: Statementstmt 97.6% 253 / 259 Branchbran 85.4% 135 / 158 Conditioncond 68.6% 57 / 83 MC/DCmcdc 42.8% 21 / 49 Subroutinesub 100% 40 / 40 Podpod 100% 28 / 28 totaltotal 86.5% 534 / 617 CC 115 SCAR 22.9
CC
115
Cov
89%
function
22.2
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 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 57 ▶use FFI::Platypus::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 57 ▶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 57 our $VERSION = '2.12'; # 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 57 our @CARP_NOT = qw( FFI::Platypus::Declare FFI::Platypus::Record );
26  
27 57 require XSLoader;
28 57 ▶XSLoader::load(
Condition50%1/2
$FFI::Platypus::VERSION || 0
l!l
570
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 358 ▶{
Subroutine new: called CC=16 SCAR=30.2
Pod documented
35 358 my($class, %args) = @_;
36 358 my @lib;
37 358 ▶ if(exists $args{lib})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"lib"}) 105 253 358
38 {
39 105 ▶ if(!ref($args{lib}))
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $args{'lib'}) { } 2 103 105
elsif (ref $args{'lib'} eq 'ARRAY') { } 103 0 103
40 {
41 2 push @lib, $args{lib};
42 }
43 elsif(ref($args{lib}) eq 'ARRAY')
44 {
45 103 push @lib, @{$args{lib}};
46 }
47 else
48 {
49 0 croak "lib argument must be a scalar or array reference";
50 }
51 }
52  
53 358 ▶ my $api = $args{api} || 0;
Condition100%2/2
$args{'api'} || 0
l!l
123235
MC/DC100%1/1
$args{'api'} || 0
$args{'api'}
Truth table100%2/2
A
A= $args{'api'}
Aresult
11
00
54 358 ▶ my $experimental = $args{experimental} || 0;
Condition50%1/2
$args{'experimental'} || 0
l!l
0358
MC/DC0%0/1
$args{'experimental'} || 0
$args{'experimental'}
Truth table50%1/2
A
A= $args{'experimental'}
Aresult
11
00
55  
56 358 ▶ if($experimental == 1)
Branch: 2 branches
BranchTrueFalseTotal
if ($experimental == 1) { } 0 358 358
elsif ($experimental == 2) { } 0 358 358
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 358 ▶ if(defined $api && $api > 2 && $experimental != $api)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api and $api > 2 and $experimental != $api) 0 358 358
Condition33%2/6
defined $api and $api > 2 and $experimental != $api
!ll&&!rl&&r
35800
defined $api and $api > 2
!ll&&!rl&&r
03580
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 358 my $tp;
71  
72 358 ▶ if($api == 0)
Branch: 3 branches
BranchTrueFalseTotal
if ($api == 0) { } 235 123 358
elsif ($api == 1) { } 50 73 123
elsif ($api == 2) { } 73 0 73
73 {
74 235 $tp = 'Version0';
75 }
76 elsif($api == 1)
77 {
78 50 $tp = 'Version1';
79 }
80 elsif($api == 2)
81 {
82 73 $tp = 'Version2';
83 }
84 else
85 {
86 0 Carp::croak("API version $api not (yet) implemented");
87 }
88  
89 358 require "FFI/Platypus/TypeParser/$tp.pm";
90 358 $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 358 ▶ ignore_not_found => defined $args{ignore_not_found} ? $args{ignore_not_found} : 0,
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'ignore_not_found'} ? : 3 355 358
101 }, $class;
102  
103 358 ▶ $self->lang($args{lang} || 'C');
Condition100%2/2
$args{'lang'} || 'C'
l!l
12346
MC/DC100%1/1
$args{'lang'} || 'C'
$args{'lang'}
Truth table100%2/2
A
A= $args{'lang'}
Aresult
11
00
104  
105 358 $self;
106 }
107  
108 sub _lang_class ($)
109 512 ▶{
Subroutine _lang_class: called CC=4 SCAR=13.9
110 512 my($lang) = @_;
111 512 ▶ my $class = $lang =~ m/^=(.*)$/ ? $1 : "FFI::Platypus::Lang::$lang";
Branch: 1 branch
BranchTrueFalseTotal
$lang =~ /^=(.*)$/ ? : 1 511 512
112 512 ▶ unless($class->can('native_type_map'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("native_type_map")) 54 458 512
113 {
114 54 my $pm = "$class.pm";
115 54 $pm =~ s/::/\//g;
116 54 require $pm;
117 }
118 512 ▶ croak "$class does not provide native_type_map method"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("native_type_map") 0 512 512
119 unless $class->can("native_type_map");
120 512 $class;
121 }
122  
123  
124 sub lib
125 102 ▶{
Subroutine lib: called CC=3 SCAR=11.0
Pod documented
126 102 my($self, @new) = @_;
127  
128 102 ▶ if(@new)
Branch: 1 branch
BranchTrueFalseTotal
if (@new) 90 12 102
129 {
130 90 ▶ push @{ $self->{lib} }, map { ref $_ eq 'CODE' ? $_->() : $_ } @new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 1 89 90
131 90 delete $self->{mangler};
132 }
133  
134 102 @{ $self->{lib} };
135 }
136  
137  
138 sub ignore_not_found
139 66 ▶{
Subroutine ignore_not_found: called CC=2 SCAR=6.9
Pod documented
140 66 my($self, $value) = @_;
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 361 ▶{
Subroutine lang: called CC=10 SCAR=23.1
Pod documented
153 361 my($self, $value) = @_;
154  
155 361 ▶ if(defined $value && $value ne $self->{lang})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne $self->{"lang"}) 361 0 361
Condition33%1/3
defined $value and $value ne $self->{"lang"}
!ll&&!rl&&r
00361
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 361 $self->{lang} = $value;
158 361 my $class = _lang_class($self->{lang});
159 361 ▶ $self->abi($class->abi) if $class->can('abi');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("abi") 1 360 361
160  
161 {
162 361 my %type_map;
163 my $map = $class->native_type_map(
164 $self->{api} > 0
165 ? (api => $self->{api})
166 361 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$self->{'api'} > 0 ? : 124 237 361
167 );
168 361 foreach my $key (keys %$map)
169 {
170 19190 my $value = $map->{$key};
171 19190 ▶ next unless $self->{tp}->have_type($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"tp"}->have_type($value) 347 18843 19190
172 18843 $type_map{$key} = $value;
173 }
174 361 $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 361 ▶ $type_map{pointer} = 'opaque' if $self->{tp}->isa('FFI::Platypus::TypeParser::Version0');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tp"}->isa("FFI::Platypus::TypeParser::Version0") 237 124 361
178 361 $self->{tp}->type_map(\%type_map);
179 }
180  
181 361 ▶ $class->load_custom_types($self) if $class->can('load_custom_types');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("load_custom_types") 2 359 361
182 }
183  
184 361 $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 275 ▶{
Subroutine type: called CC=5 SCAR=16.5
Pod documented
193 275 my($self, $name, $alias) = @_;
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 207 ▶{
Subroutine custom_type: called CC=8 SCAR=21.7
Pod documented
206 207 my($self, $alias, $cb) = @_;
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 13 ▶{
Subroutine load_custom_type: called CC=8 SCAR=22.2
Pod documented
237 13 my($self, $name, $alias, @type_args) = @_;
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 3 ▶{
Subroutine types: called CC=3 SCAR=11.0
Pod documented
267 3 my($self) = @_;
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 131 ▶{
Subroutine type_meta: called CC=3 SCAR=12.0
Pod documented
275 131 my($self, $name) = @_;
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 12 ▶{
Subroutine mangler: called CC=1 SCAR=0.0
Pod documented
283 12 my($self, $sub) = @_;
284 12 $self->{mangler} = $self->{mymangler} = $sub;
285 }
286  
287  
288 sub function
289 1306 ▶{
Subroutine function: called CC=22 SCAR=31.0
Pod documented
290 1306 my $wrapper;
291 1306 ▶ $wrapper = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 32 1274 1306
292  
293 1306 ▶ croak "usage \$ffi->function( \$name, \\\@arguments, [\\\@var_args], [\$return_type])" unless @_ >= 3 && @_ <= 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 and @_ <= 6 0 1306 1306
Condition33%1/3
@_ >= 3 and @_ <= 6
!ll&&!rl&&r
001306
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 1306 my $self = shift;
296 1306 my $name = shift;
297 1306 my $fixed_args = shift;
298 1306 my $var_args;
299 1306 ▶ $var_args = shift if defined $_[0] && ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] eq "ARRAY" 30 1276 1306
Condition100%3/3
defined $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
6127030
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 1306 my $ret = shift;
301 1306 ▶ $ret = 'void' unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 16 1290 1306
302  
303 # special case: treat a single void argument type as an empty list of
304 # arguments, a la olde timey C compilers.
305 1306 ▶ 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 1305 1306
Condition100%6/6
not defined $var_args and @$fixed_args == 1 and $fixed_args->[0] eq "void"
!ll&&!rl&&r
6506551
not defined $var_args and @$fixed_args == 1
!ll&&!rl&&r
30620656
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 1306 ▶ my $fixed_arg_count = defined $var_args ? scalar(@$fixed_args) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $var_args ? : 30 1276 1306
311  
312 1306 ▶ my @args = map { $self->{tp}->parse($_) || croak "unknown type: $_" } @$fixed_args;
Condition50%1/2
$self->{'tp'}->parse($_) || croak("unknown type: $_")
l!l
15800
MC/DC0%0/1
$self->{'tp'}->parse($_) || croak("unknown type: $_")
$self->{'tp'}->parse($_)
Truth table50%1/2
A
A= $self->{'tp'}->parse($_)
Aresult
11
00
313 1306 ▶ if($var_args)
Branch: 1 branch
BranchTrueFalseTotal
if ($var_args) 30 1276 1306
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 1306 ▶ $ret = $self->{tp}->parse($ret) || croak "unknown type: $ret";
Condition50%1/2
$self->{'tp'}->parse($ret) || croak("unknown type: $ret")
l!l
13060
MC/DC0%0/1
$self->{'tp'}->parse($ret) || croak("unknown type: $ret")
$self->{'tp'}->parse($ret)
Truth table50%1/2
A
A= $self->{'tp'}->parse($ret)
Aresult
11
00
323 1306 ▶ my $address = $name =~ /^-?[0-9]+$/ ? $name : $self->find_symbol($name);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^-?[0-9]+$/ ? : 151 1155 1306
324 1306 ▶ 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 1248 1306
Condition100%3/3
defined $address or $self->ignore_not_found
l!l&&r!l&&!r
1243558
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 1248 ▶ return unless defined $address;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 5 1243 1248
326 1243 ▶ $address = @args > 0 ? _cast1() : _cast0() if $address == 0;
Branch: 2 branches
BranchTrueFalseTotal
if $address == 0 150 1093 1243
@args > 0 ? : 149 1 150
327 1243 my $function = FFI::Platypus::Function::Function->new($self, $address, $self->{abi}, $fixed_arg_count, $ret, @args);
328 1242 ▶ $wrapper
Branch: 1 branch
BranchTrueFalseTotal
$wrapper ? : 32 1210 1242
329 ? FFI::Platypus::Function::Wrapper->new($function, $wrapper)
330 : $function;
331 }
332  
333 sub _function_meta
334 2 ▶{
Subroutine _function_meta: called CC=1 SCAR=0.0
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) = @_;
339 2 $args = ['opaque','int',@$args];
340 $self->function(
341 4 ▶ $name, $args, $ret, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
342 4 my $xsub = shift;
343 4 $xsub->($meta, scalar(@_), @_);
344 },
345 2 );
346 }
347  
348  
349 sub attach
350 912 ▶{
Subroutine attach: called CC=9 SCAR=22.0
Pod documented
351 912 my $wrapper;
352 912 ▶ $wrapper = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 18 894 912
353  
354 912 my $self = shift;
355 912 my $name = shift;
356 912 my $args = shift;
357 912 my $varargs;
358 912 ▶ $varargs = shift if defined $_[0] && ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] eq "ARRAY" 2 910 912
Condition100%3/3
defined $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
19092
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 912 my $ret = shift;
360 912 my $proto = shift;
361  
362 912 ▶ $ret = 'void' unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 1 911 912
363  
364 912 ▶ my($c_name, $perl_name) = ref($name) ? @$name : ($name, $name);
Branch: 1 branch
BranchTrueFalseTotal
ref $name ? : 690 222 912
365  
366 912 ▶ croak "you tried to provide a perl name that looks like an address"
Branch: 1 branch
BranchTrueFalseTotal
if $perl_name =~ /^-?[0-9]+$/ 0 912 912
367 if $perl_name =~ /^-?[0-9]+$/;
368  
369 912 ▶ my $function = $varargs
Branch: 1 branch
BranchTrueFalseTotal
$varargs ? : 2 910 912
370 ? $self->function($c_name, $args, $varargs, $ret, $wrapper)
371 : $self->function($c_name, $args, $ret, $wrapper);
372  
373 909 ▶ if(defined $function)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function) 907 2 909
374 {
375 907 $function->attach($perl_name, $proto);
376 }
377  
378 909 $self;
379 }
380  
381  
382 sub closure
383 86 ▶{
Subroutine closure: called CC=3 SCAR=11.0
Pod documented
384 86 my($self, $coderef) = @_;
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 137 ▶{
Subroutine cast: called CC=1 SCAR=0.0
Pod documented
394 137 $_[0]->function(0 => [$_[1]] => $_[2])->call($_[3]);
395 }
396  
397  
398 sub attach_cast
399 4 ▶{
Subroutine attach_cast: called CC=4 SCAR=14.0
Pod documented
400 4 my($self, $name, $type1, $type2, $wrapper) = @_;
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 209 ▶{
Subroutine sizeof: called CC=2 SCAR=6.9
Pod documented
417 209 my($self,$name) = @_;
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 74 ▶{
Subroutine alignof: called CC=2 SCAR=6.9
Pod documented
426 74 my($self, $name) = @_;
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 12 ▶{
Subroutine kindof: called CC=2 SCAR=6.9
Pod documented
435 12 my($self, $name) = @_;
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 12 ▶{
Subroutine countof: called CC=2 SCAR=6.9
Pod documented
444 12 my($self, $name) = @_;
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 10 ▶{
Subroutine def: called CC=3 SCAR=11.0
Pod documented
453 10 my $self = shift;
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 12 ▶{
Subroutine unitof: called CC=2 SCAR=6.9
Pod documented
467 12 my($self, $name) = @_;
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 7 ▶{
Subroutine find_lib: called CC=1 SCAR=0.0
Pod documented
476 7 my $self = shift;
477 7 require FFI::CheckLib;
478 7 $self->lib(FFI::CheckLib::find_lib(@_));
479 7 $self;
480 }
481  
482  
483 sub find_symbol
484 1196 ▶{
Subroutine find_symbol: called CC=14 SCAR=26.4
Pod documented
485 1196 my($self, $name) = @_;
486  
487 1196 ▶ $self->{mangler} ||= $self->{mymangler};
Condition66%2/3
$self->{"mangler"} ||= $self->{"mymangler"}
l!l&&r!l&&!r
10450151
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 1196 ▶ unless(defined $self->{mangler})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"mangler"}) 151 1045 1196
490 {
491 151 my $class = _lang_class($self->{lang});
492 151 ▶ if($class->can('mangler'))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('mangler')) { } 2 149 151
493 {
494 2 $self->{mangler} = $class->mangler($self->lib);
495 }
496 else
497 {
498 149 ▶ $self->{mangler} = sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
499 }
500 }
501  
502 1196 foreach my $path (@{ $self->{lib} })
503 {
504 57 ▶ 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
10621632
$path || 0
l!l
1067160
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 1227 ▶ unless($handle)
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 2 1225 1227
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 1225 my $address = FFI::Platypus::DL::dlsym($handle, $self->{mangler}->($name));
512 1225 ▶ if($address)
Branch: 1 branch
BranchTrueFalseTotal
if ($address) { } 1099 126 1225
513 {
514 1099 ▶ $self->{handles}->{$path||0} = $handle;
Condition100%2/2
$path || 0
l!l
972127
MC/DC100%1/1
$path || 0
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
515 1099 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 27 ▶{
Subroutine bundle: called CC=2 SCAR=7.1
Pod documented
528 27 ▶ croak "bundle method only available with api => 1 or better" if $_[0]->{api} < 1;
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 3 ▶{
Subroutine package: called CC=2 SCAR=6.9
Pod documented
536 3 ▶ croak "package method only available with api => 0" if $_[0]->{api} > 0;
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 15 ▶{
Subroutine abis: called CC=1 SCAR=0.0
Pod documented
544 15 require FFI::Platypus::ShareConfig;
545 15 FFI::Platypus::ShareConfig->get("abi");
546 }
547  
548  
549 sub abi
550 13 ▶{
Subroutine abi: called CC=4 SCAR=13.9
Pod documented
551 13 my($self, $newabi) = @_;
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 249 ▶{
Subroutine DESTROY: called CC=4 SCAR=13.9
574 249 my($self) = @_;
575 249 foreach my $fini (@{ $self->{fini} })
576 {
577 1 $fini->($self);
578 }
579 249 foreach my $handle (values %{ $self->{handles} })
580 {
581 45 ▶ next unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 45 45
582 45 FFI::Platypus::DL::dlclose($handle);
583 }
584 249 delete $self->{handles};
585 }
586  
587 57 1;
588  
589 __END__
« lib/FFI/Build.pm ↑ Summary blib/lib/FFI/Platypus/Legacy.pm »