| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package FFI; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 165080 | use strict; | 
|  | 4 |  |  |  |  | 16 |  | 
|  | 4 |  |  |  |  | 114 |  | 
| 4 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 12 |  | 
|  | 4 |  |  |  |  | 98 |  | 
| 5 | 4 |  |  | 4 |  | 20 | use Carp (); | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 58 |  | 
| 6 | 4 |  |  | 4 |  | 2048 | use FFI::Platypus; | 
|  | 4 |  |  |  |  | 22161 |  | 
|  | 4 |  |  |  |  | 337 |  | 
| 7 | 4 |  | 33 | 4 |  | 33 | use constant _is_win32 => $^O =~ /^(MSWin32|cygwin|msys2?)$/ && FFI::Platypus->abis->{stdcall}; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 2234 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | # ABSTRACT: Perl Foreign Function Interface based on libffi | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.08'; # VERSION | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $ffi = FFI::Platypus->new; | 
| 13 |  |  |  |  |  |  | $ffi->lib(undef); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | my $stdcall_ffi = _is_win32 | 
| 16 |  |  |  |  |  |  | ? do { | 
| 17 |  |  |  |  |  |  | my $ffi = FFI::Platypus->new; | 
| 18 |  |  |  |  |  |  | $ffi->lib(undef); | 
| 19 |  |  |  |  |  |  | $ffi->abi('stdcall'); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  | : $ffi; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | { | 
| 41 | 10 | 50 |  | 10 |  | 58 | if($_[0] =~ s/^([sc])//) | 
| 42 |  |  |  |  |  |  | { | 
| 43 | 10 | 100 |  |  |  | 39 | return $stdcall_ffi if $1 eq 's'; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | else | 
| 46 |  |  |  |  |  |  | { | 
| 47 | 0 |  |  |  |  | 0 | Carp::croak("first character of signature must be s or c"); | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 8 |  |  |  |  | 16 | $ffi; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub call | 
| 54 |  |  |  |  |  |  | { | 
| 55 | 9 |  |  | 9 | 1 | 27940 | my $addr = shift; | 
| 56 | 9 |  |  |  |  | 17 | my $signature = shift; | 
| 57 | 9 |  |  |  |  | 18 | my $ffi = _ffi($signature); | 
| 58 | 9 |  |  |  |  | 31 | my($ret_type, @args_types) = map { $typemap{$_} } split //, $signature; | 
|  | 23 |  |  |  |  | 72 |  | 
| 59 | 9 |  |  |  |  | 36 | $ffi->function($addr => \@args_types => $ret_type)->call(@_); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub callback | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 1 |  |  | 1 | 1 | 14 | my($signature, $sub) = @_; | 
| 65 | 1 |  |  |  |  | 4 | my $ffi = _ffi($signature); | 
| 66 | 1 |  |  |  |  | 4 | my($ret_type, @args_types) = map { $typemap{$_} } split //, $signature; | 
|  | 3 |  |  |  |  | 9 |  | 
| 67 | 1 |  |  |  |  | 7 | my $type = '(' . join(',', @args_types) . ')->' . $ret_type; | 
| 68 | 1 |  |  |  |  | 6 | my $closure = $ffi->closure($sub); | 
| 69 | 1 |  |  |  |  | 1487 | 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 |  |  | 1 |  | 13 | sub addr { shift->{addr} } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | 1; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | __END__ |