| 1 | package FFI::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use constant; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use FFI::Platypus; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use FFI::Platypus::Buffer (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Scalar::Util qw( refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | use Exporter::Tidy | ||||||||||||||||||||||||||||
| 12 | 5 | ▶ | deref => do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | our @types = qw( ptr str int uint short ushort char uchar float double int64 uint64 long ulong ); | |||||||||||||||||||||||||||
| 14 | 5 | [map { ("deref_$_\_get","deref_$_\_set") } (@types, qw( size_t time_t dev_t gid_t uid_t ))]; | |||||||||||||||||||||||||||
| 15 | }, | ||||||||||||||||||||||||||||
| 16 | buffer => [qw( scalar_to_buffer buffer_to_scalar )], | ||||||||||||||||||||||||||||
| 17 | types => [qw( _size_t _time_t _dev_t _gid_t _uid_t )], | ||||||||||||||||||||||||||||
| 18 | locate_module_share_lib => [qw( locate_module_share_lib )], | ||||||||||||||||||||||||||||
| 19 | 5 | ; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # ABSTRACT: Some useful pointer utilities when writing FFI modules (Deprecated) | ||||||||||||||||||||||||||||
| 22 | 5 | our $VERSION = '0.17'; # VERSION | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub locate_module_share_lib (;$$) | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
locate_module_share_lib: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 0 | require FFI::Platypus::ConfigData; | |||||||||||||||||||||||||||
| 29 | 0 | my($module, $modlibname) = @_; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | ($module, $modlibname) = caller() unless defined $modlibname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | my @modparts = split(/::/,$module); | |||||||||||||||||||||||||||
| 32 | 0 | my $modfname = $modparts[-1]; | |||||||||||||||||||||||||||
| 33 | 0 | my $modpname = join('/',@modparts); | |||||||||||||||||||||||||||
| 34 | 0 | my $c = @modparts; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | foreach my $dlext (@{ FFI::Platypus::ConfigData->config('config_dlext') }) | |||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 0 | my $file = "$modlibname/auto/$modpname/$modfname.$dlext"; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | unless(-e $file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | 0 | $modlibname =~ s,[\\/][^\\/]+$,,; | |||||||||||||||||||||||||||
| 42 | 0 | $file = "$modlibname/arch/auto/$modpname/$modfname.$dlext"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | return $file if -e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | (); | |||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | our $ffi = FFI::Platypus->new; | |||||||||||||||||||||||||||
| 50 | 5 | $ffi->package; | |||||||||||||||||||||||||||
| 51 | 5 | $ffi->attach( [lookup_type => '_lookup_type'] => ['string'] => 'string'); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # nicked this from FFI/Raw.pm | ||||||||||||||||||||||||||||
| 54 | sub _ffi_void () { ord 'v' } | ||||||||||||||||||||||||||||
| 55 | sub _ffi_int () { ord 'i' } | ||||||||||||||||||||||||||||
| 56 | sub _ffi_uint () { ord 'I' } | ||||||||||||||||||||||||||||
| 57 | sub _ffi_short () { ord 'z' } | ||||||||||||||||||||||||||||
| 58 | sub _ffi_ushort () { ord 'Z' } | ||||||||||||||||||||||||||||
| 59 | sub _ffi_long () { ord 'l' } | ||||||||||||||||||||||||||||
| 60 | sub _ffi_ulong () { ord 'L' } | ||||||||||||||||||||||||||||
| 61 | sub _ffi_int64 () { ord 'x' } | ||||||||||||||||||||||||||||
| 62 | sub _ffi_uint64 () { ord 'X' } | ||||||||||||||||||||||||||||
| 63 | sub _ffi_char () { ord 'c' } | ||||||||||||||||||||||||||||
| 64 | sub _ffi_uchar () { ord 'C' } | ||||||||||||||||||||||||||||
| 65 | sub _ffi_float () { ord 'f' } | ||||||||||||||||||||||||||||
| 66 | sub _ffi_double () { ord 'd' } | ||||||||||||||||||||||||||||
| 67 | sub _ffi_str () { ord 's' } | ||||||||||||||||||||||||||||
| 68 | sub _ffi_ptr () { ord 'p' } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | foreach my $type (qw( size_t time_t dev_t gid_t uid_t )) | |||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 25 | my $real_type = _lookup_type($type); | |||||||||||||||||||||||||||
| 73 | 25 | ▶ | if($real_type) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 25 | constant->import("_$type" => eval "_ffi_$real_type\()"); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | $ffi->type( void => 'raw_void' ); | |||||||||||||||||||||||||||
| 80 | 5 | $ffi->type( string => 'raw_str' ); | |||||||||||||||||||||||||||
| 81 | 5 | $ffi->type( int => 'raw_int' ); | |||||||||||||||||||||||||||
| 82 | 5 | $ffi->type( 'unsigned int' => 'raw_uint' ); | |||||||||||||||||||||||||||
| 83 | 5 | $ffi->type( short => 'raw_short' ); | |||||||||||||||||||||||||||
| 84 | 5 | $ffi->type( 'unsigned short' => 'raw_ushort' ); | |||||||||||||||||||||||||||
| 85 | 5 | $ffi->type( long => 'raw_long' ); | |||||||||||||||||||||||||||
| 86 | 5 | $ffi->type( 'unsigned long' => 'raw_ulong' ); | |||||||||||||||||||||||||||
| 87 | 5 | $ffi->type( 'uint64' => 'raw_uint64' ); | |||||||||||||||||||||||||||
| 88 | 5 | $ffi->type( 'sint64' => 'raw_int64' ); | |||||||||||||||||||||||||||
| 89 | 5 | $ffi->type( 'signed char' => 'raw_char' ); | |||||||||||||||||||||||||||
| 90 | 5 | $ffi->type( 'unsigned char' => 'raw_uchar' ); | |||||||||||||||||||||||||||
| 91 | 5 | $ffi->type( 'float' => 'raw_float' ); | |||||||||||||||||||||||||||
| 92 | 5 | $ffi->type( 'double' => 'raw_double' ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | $ffi->custom_type(raw_ptr => { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | perl_to_native => sub { ref($_[0]) ? ${$_[0]} : $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | native_to_perl => sub { $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 5 | }); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | for (qw( ptr str int uint short ushort long ulong char uchar float double int64 uint64 )) | |||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 70 | $ffi->attach( "deref_${_}_get" => ['raw_ptr'] => "raw_$_" => '$' ); | |||||||||||||||||||||||||||
| 102 | 70 | $ffi->attach( "deref_${_}_set" => ['raw_ptr',"raw_$_"] => 'void' => '$$' ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | foreach my $type (our @types) | |||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 70 | my $code_type = eval qq{ _ffi_$type }; | |||||||||||||||||||||||||||
| 108 | 70 | foreach my $otype (qw( size_t time_t dev_t gid_t uid_t )) | |||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 350 | ▶ | if((_lookup_type($otype)||'') eq $type) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
_lookup_type($otype) || ''
MC/DC0%0/1
_lookup_type($otype) || ''
_lookup_type($otype)
Truth table50%1/2
A
A= _lookup_type($otype)
| |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 25 | *{"deref_$otype\_get"} = \&{"deref_$type\_get"}; | |||||||||||||||||||||||||||
| 114 | 25 | *{"deref_$otype\_set"} = \&{"deref_$type\_set"}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | ▶ | use constant _incantation => | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 121 | 5 | ▶ | $^O eq 'MSWin32' && $Config{archname} =~ /MSWin32-x64/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O eq "MSWin32" and $FFI::Util::Config{"archname"} =~ /MSWin32-x64/
MC/DC0%0/2
$^O eq "MSWin32" and $FFI::Util::Config{"archname"} =~ /MSWin32-x64/
$^O eq "MSWin32"
$FFI::Util::Config{"archname"} =~ /MSWin32-x64/
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $FFI::Util::Config{"archname"} =~ /MSWin32-x64/
| |||||||||||||||||||||||||||||
| 122 | ? 'Q' | ||||||||||||||||||||||||||||
| 123 | 5 | : 'L!'; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub scalar_to_buffer ($) | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
scalar_to_buffer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 5 | ▶ | (unpack(_incantation, pack 'P', $_[0]), do { use bytes; length $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub buffer_to_scalar ($$) | ||||||||||||||||||||||||||||
| 133 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
buffer_to_scalar: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | unpack 'P'.$_[1], pack _incantation, defined $_[0] ? $_[0] : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||