| 1 | package FFI; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use FFI::Platypus; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use constant _is_win32 => $^O =~ /^(MSWin32|cygwin|msys2?)$/ && FFI::Platypus->abis->{stdcall}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$^O =~ /^(MSWin32|cygwin|msys2?)$/ && 'FFI::Platypus'->abis->{'stdcall'}
MC/DC0%0/2
$^O =~ /^(MSWin32|cygwin|msys2?)$/ && 'FFI::Platypus'->abis->{'stdcall'}
$^O =~ /^(MSWin32|cygwin|msys2?)$/
'FFI::Platypus'->abis->{'stdcall'}
Truth table33%1/3
A && B
A= $^O =~ /^(MSWin32|cygwin|msys2?)$/B= 'FFI::Platypus'->abis->{'stdcall'}
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # ABSTRACT: Perl Foreign Function Interface based on libffi | ||||||||||||||||||||
| 10 | 3 | our $VERSION = '1.15'; # VERSION | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 3 | our $ffi = FFI::Platypus->new; | |||||||||||||||||||
| 13 | 3 | $ffi->lib(undef); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | my $stdcall_ffi = _is_win32 | ||||||||||||||||||||
| 16 | 3 | ? do { | |||||||||||||||||||
| 17 | my $ffi = FFI::Platypus->new; | ||||||||||||||||||||
| 18 | $ffi->lib(undef); | ||||||||||||||||||||
| 19 | $ffi->abi('stdcall'); | ||||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | : $ffi; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 3 | our %typemap = qw( | |||||||||||||||||||
| 24 | c char | ||||||||||||||||||||
| 25 | C uchar | ||||||||||||||||||||
| 26 | s short | ||||||||||||||||||||
| 27 | S ushort | ||||||||||||||||||||
| 28 | i int | ||||||||||||||||||||
| 29 | I uint | ||||||||||||||||||||
| 30 | l long | ||||||||||||||||||||
| 31 | L ulong | ||||||||||||||||||||
| 32 | f float | ||||||||||||||||||||
| 33 | d double | ||||||||||||||||||||
| 34 | p string | ||||||||||||||||||||
| 35 | v void | ||||||||||||||||||||
| 36 | o opaque | ||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub _ffi | ||||||||||||||||||||
| 40 | 10 | ▶ | { | ||||||||||||||||||
|
Subroutine
_ffi: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 41 | 10 | ▶ | if($_[0] =~ s/^([sc])//) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | { | ||||||||||||||||||||
| 43 | 10 | ▶ | return $stdcall_ffi if $1 eq 's'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | else | ||||||||||||||||||||
| 46 | { | ||||||||||||||||||||
| 47 | 0 | Carp::croak("first character of signature must be s or c"); | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 8 | $ffi; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | sub call | ||||||||||||||||||||
| 54 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 55 | 9 | my $addr = shift; | |||||||||||||||||||
| 56 | 9 | my $signature = shift; | |||||||||||||||||||
| 57 | 9 | my $ffi = _ffi($signature); | |||||||||||||||||||
| 58 | 9 | my($ret_type, @args_types) = map { $typemap{$_} } split //, $signature; | |||||||||||||||||||
| 59 | 9 | $ffi->function($addr => \@args_types => $ret_type)->call(@_); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub callback | ||||||||||||||||||||
| 63 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 64 | 1 | my($signature, $sub) = @_; | |||||||||||||||||||
| 65 | 1 | my $ffi = _ffi($signature); | |||||||||||||||||||
| 66 | 1 | my($ret_type, @args_types) = map { $typemap{$_} } split //, $signature; | |||||||||||||||||||
| 67 | 1 | my $type = '(' . join(',', @args_types) . ')->' . $ret_type; | |||||||||||||||||||
| 68 | 1 | my $closure = $ffi->closure($sub); | |||||||||||||||||||
| 69 | 1 | bless { | |||||||||||||||||||
| 70 | addr => $ffi->cast($type => 'opaque', $closure), | ||||||||||||||||||||
| 71 | sub => $sub, | ||||||||||||||||||||
| 72 | closure => $closure, | ||||||||||||||||||||
| 73 | }, 'FFI::Callback'; | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | package FFI::Callback; | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | ▶ | sub addr { shift->{addr} } | ||||||||||||||||||
|
Subroutine
addr: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 3 | 1; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||