| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::CheckLib; |
| 3 |
use FFI::Platypus; |
| 4 |
use FFI::Platypus::Memory qw( malloc free ); |
| 5 |
|
| 6 |
my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'; |
| 7 |
|
| 8 |
foreach my $api (0, 1, 2) |
| 9 |
{ |
| 10 |
|
| 11 |
subtest "api = $api" => sub { |
| 12 |
|
| 13 |
local $SIG{__WARN__} = sub { |
| 14 |
my $message = shift; |
| 15 |
return if $message =~ /^Subroutine main::.* redefined/; |
| 16 |
warn $message; |
| 17 |
}; |
| 18 |
|
| 19 |
my $ffi = FFI::Platypus->new( api => $api, experimental => ($api > 2 ? $api : undef), lib => [@lib] ); |
| 20 |
|
| 21 |
$ffi->attach( [pointer_null => 'null'] => [] => 'opaque'); |
| 22 |
$ffi->attach( [pointer_is_null => 'is_null'] => ['opaque'] => 'int'); |
| 23 |
$ffi->attach( [pointer_set_my_pointer => 'setp'] => ['opaque'] => 'void'); |
| 24 |
$ffi->attach( [pointer_get_my_pointer => 'getp'] => [] => 'opaque'); |
| 25 |
$ffi->attach( [pointer_get_my_pointer_arg => 'geta'] => ['opaque*'] => 'void'); |
| 26 |
|
| 27 |
is [null()], [$api >= 2 ? (undef) : ()], 'null = ()/undef'; |
| 28 |
is is_null(undef), 1, 'is_null(undef) == 1'; |
| 29 |
is is_null(), 1, 'is_null() == 1'; |
| 30 |
|
| 31 |
my $ptr = malloc 32; |
| 32 |
is is_null($ptr), 0, 'is_null($ptr) = 0'; |
| 33 |
|
| 34 |
setp($ptr); |
| 35 |
is getp(), $ptr, "setp($ptr); getp() = $ptr"; |
| 36 |
|
| 37 |
do { |
| 38 |
my $tmp; |
| 39 |
geta(\$tmp); |
| 40 |
is $tmp, $ptr, "get(\$tmp); tmp = $ptr"; |
| 41 |
}; |
| 42 |
|
| 43 |
do { |
| 44 |
my $tmp = malloc 32; |
| 45 |
my $tmp2 = $tmp; |
| 46 |
setp(undef); |
| 47 |
geta(\$tmp); |
| 48 |
is $tmp, undef, "get(\\\$tmp); \\\$tmp = undef"; |
| 49 |
free $tmp2; |
| 50 |
}; |
| 51 |
|
| 52 |
free $ptr; |
| 53 |
|
| 54 |
$ffi->attach( [pointer_arg_array_in => 'aa_in'] => ['opaque[3]'] => 'int'); |
| 55 |
$ffi->attach( [pointer_arg_array_in => 'aa_in_ptr'] => ['opaque*'] => 'int') if $api >= 2; |
| 56 |
$ffi->attach( [pointer_arg_array_null_in => 'aa_null_in'] => ['opaque[3]'] => 'int'); |
| 57 |
$ffi->attach( [pointer_arg_array_out => 'aa_out'] => ['opaque[3]'] => 'void'); |
| 58 |
$ffi->attach( [pointer_arg_array_out => 'aa_out_ptr'] => ['opaque*'] => 'void') if $api >= 2; |
| 59 |
$ffi->attach( [pointer_arg_array_null_out => 'aa_null_out'] => ['opaque[3]'] => 'void'); |
| 60 |
|
| 61 |
do { |
| 62 |
my @stuff = map { perl_to_c_string_copy($_) } qw( one two three ); |
| 63 |
is aa_in([@stuff]), 1, "aa_in([one two three])"; |
| 64 |
free $_ for @stuff; |
| 65 |
}; |
| 66 |
|
| 67 |
if($api >= 2) |
| 68 |
{ |
| 69 |
my @stuff = map { perl_to_c_string_copy($_) } qw( one two three ); |
| 70 |
is aa_in_ptr([@stuff]), 1, "aa_in([one two three])"; |
| 71 |
free $_ for @stuff; |
| 72 |
}; |
| 73 |
|
| 74 |
is aa_null_in([undef,undef,undef]), 1, "aa_null_in([undef,undef,undef])"; |
| 75 |
|
| 76 |
do { |
| 77 |
my @list = (undef,undef,undef); |
| 78 |
aa_out(\@list); |
| 79 |
is [map { $ffi->cast('opaque' => 'string', $_) } @list], [qw( four five six )], 'aa_out()'; |
| 80 |
}; |
| 81 |
|
| 82 |
if($api >= 2) |
| 83 |
{ |
| 84 |
my @list = (undef,undef,undef); |
| 85 |
aa_out(\@list); |
| 86 |
is [map { $ffi->cast('opaque' => 'string', $_) } @list], [qw( four five six )], 'aa_out()'; |
| 87 |
}; |
| 88 |
|
| 89 |
do { |
| 90 |
my @list1 = (malloc 32, malloc 32, malloc 32); |
| 91 |
my @list2 = @list1; |
| 92 |
aa_null_out(\@list2); |
| 93 |
is [@list2], [undef,undef,undef], 'aa_null_out()'; |
| 94 |
free $_ for @list1; |
| 95 |
}; |
| 96 |
|
| 97 |
$ffi->attach( [pointer_ret_array_out => 'ra_out'] => [] => 'opaque[3]'); |
| 98 |
$ffi->attach( [pointer_ret_array_out_null_terminated => 'ra_out_nt'] => [] => 'opaque[]'); |
| 99 |
$ffi->attach( [pointer_ret_array_null_out => 'ra_null_out'] => [] => 'opaque[3]'); |
| 100 |
|
| 101 |
is [map { $ffi->cast('opaque' => 'string', $_) } @{ ra_out() } ], [qw( seven eight nine )], "ra_out()"; |
| 102 |
is [map { $ffi->cast('opaque' => 'string', $_) } @{ ra_out_nt() } ], [qw( seven eight nine )], "ra_out_nt()"; |
| 103 |
is ra_null_out(), [undef,undef,undef], 'ra_null_out'; |
| 104 |
|
| 105 |
|
| 106 |
$ffi->attach( [pointer_pointer_pointer_to_pointer => 'pp2p'] => ['opaque*'] => 'opaque'); |
| 107 |
$ffi->attach( [pointer_pointer_to_pointer_pointer => 'p2pp'] => ['opaque'] => 'opaque*'); |
| 108 |
|
| 109 |
is pp2p(\undef), undef, 'pp2p(\undef) = undef'; |
| 110 |
|
| 111 |
do { |
| 112 |
my $ptr = malloc 32; |
| 113 |
is pp2p(\$ptr), $ptr, "pp2p(\\$ptr) = $ptr"; |
| 114 |
free $ptr; |
| 115 |
}; |
| 116 |
|
| 117 |
is p2pp(undef), \undef, 'p2pp(undef) = \undef'; |
| 118 |
|
| 119 |
do { |
| 120 |
my $ptr = malloc 32; |
| 121 |
is ${p2pp($ptr)}, $ptr, "pp2p($ptr) = \\$ptr"; |
| 122 |
free $ptr; |
| 123 |
}; |
| 124 |
|
| 125 |
$ffi->attach( [pointer_set_closure => 'set_closure'] => ['(opaque)->opaque'] => 'void'); |
| 126 |
$ffi->attach( [pointer_call_closure => 'call_closure'] => ['opaque'] => 'opaque'); |
| 127 |
|
| 128 |
my $save = 1; |
| 129 |
my $closure = $ffi->closure(sub { $save = $_[0] }); |
| 130 |
|
| 131 |
set_closure($closure); |
| 132 |
|
| 133 |
is call_closure(undef), undef, "call_closure(undef) = undef"; |
| 134 |
is $save, undef, "save = undef"; |
| 135 |
|
| 136 |
do { |
| 137 |
my $ptr = malloc 32; |
| 138 |
|
| 139 |
is call_closure($ptr), $ptr, "call_closure(\\$ptr) = $ptr"; |
| 140 |
is $save, $ptr, "save = $ptr"; |
| 141 |
|
| 142 |
free $ptr; |
| 143 |
}; |
| 144 |
|
| 145 |
subtest 'custom type input' => sub { |
| 146 |
$ffi->custom_type(type1 => { perl_to_native => sub { |
| 147 |
is $ffi->cast('opaque'=>'string',$_[0]), "abc"; |
| 148 |
free $_[0]; |
| 149 |
perl_to_c_string_copy("def"); |
| 150 |
} }); |
| 151 |
$ffi->attach(['pointer_set_my_pointer' => 'custom1_setp'] => ['type1'] => 'void'); |
| 152 |
|
| 153 |
custom1_setp(perl_to_c_string_copy("abc")); |
| 154 |
|
| 155 |
my $ptr = getp(); |
| 156 |
is $ffi->cast('opaque'=>'string',$ptr), "def"; |
| 157 |
free $ptr; |
| 158 |
}; |
| 159 |
|
| 160 |
subtest 'custom type output' => sub { |
| 161 |
|
| 162 |
setp(perl_to_c_string_copy("ABC")); |
| 163 |
|
| 164 |
$ffi->custom_type(type2 => { native_to_perl => sub { |
| 165 |
is $ffi->cast('opaque'=>'string',$_[0]), "ABC"; |
| 166 |
free $_[0]; |
| 167 |
"DEF"; |
| 168 |
} }); |
| 169 |
|
| 170 |
$ffi->attach([pointer_get_my_pointer => 'custom2_getp'] => [] => 'type2'); |
| 171 |
|
| 172 |
is custom2_getp(), "DEF"; |
| 173 |
|
| 174 |
setp(undef); |
| 175 |
}; |
| 176 |
}; |
| 177 |
} |
| 178 |
|
| 179 |
foreach my $api (1,2) { |
| 180 |
|
| 181 |
subtest 'object' => sub { |
| 182 |
|
| 183 |
{ package Roger } |
| 184 |
|
| 185 |
my $ffi = FFI::Platypus->new( api => $api, experimental => ($api > 2 ? $api : undef), lib => [@lib] ); |
| 186 |
$ffi->type('object(Roger)', 'roger_t'); |
| 187 |
$ffi->type('object(Roger,opaque)', 'roger2_t'); |
| 188 |
|
| 189 |
my $ptr = malloc 200; |
| 190 |
|
| 191 |
subtest 'argument' => sub { |
| 192 |
|
| 193 |
is $ffi->cast('roger_t' => 'opaque', bless(\$ptr, 'Roger')), $ptr; |
| 194 |
is $ffi->cast('roger2_t' => 'opaque', bless(\$ptr, 'Roger')), $ptr; |
| 195 |
|
| 196 |
}; |
| 197 |
|
| 198 |
subtest 'return value' => sub { |
| 199 |
|
| 200 |
is $ffi->cast('opaque' => 'roger_t', undef), undef; |
| 201 |
|
| 202 |
my $obj1 = $ffi->cast('opaque' => 'roger_t', $ptr); |
| 203 |
isa_ok $obj1, 'Roger'; |
| 204 |
is $$obj1, $ptr; |
| 205 |
|
| 206 |
my $obj2 = $ffi->cast('opaque' => 'roger2_t', $ptr); |
| 207 |
isa_ok $obj2, 'Roger'; |
| 208 |
is $$obj2, $ptr; |
| 209 |
|
| 210 |
}; |
| 211 |
|
| 212 |
is |
| 213 |
[$ffi->function( pointer_null => [] => 'roger_t' )->call], |
| 214 |
[$api >= 2 ? (undef) : ()], |
| 215 |
; |
| 216 |
|
| 217 |
free $ptr; |
| 218 |
}; |
| 219 |
}; |
| 220 |
|
| 221 |
done_testing; |
| 222 |
|
| 223 |
package |
| 224 |
MyPerlStrDup; |
| 225 |
|
| 226 |
use FFI::Platypus::Memory qw( malloc memcpy ); |
| 227 |
|
| 228 |
sub main::perl_to_c_string_copy |
| 229 |
{ |
| 230 |
my($string) = @_; |
| 231 |
my $ptr = malloc(length($string)+1); |
| 232 |
memcpy($ptr, FFI::Platypus->new->cast('string' => 'opaque', $string), length($string)+1); |
| 233 |
$ptr; |
| 234 |
}; |
| 235 |
|