| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus; |
| 3 |
use FFI::CheckLib qw( find_lib ); |
| 4 |
use FFI::Platypus::Memory qw( malloc free ); |
| 5 |
|
| 6 |
my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'; |
| 7 |
my $record_size = My::FooRecord->ffi_record_size; |
| 8 |
note "record size = $record_size"; |
| 9 |
|
| 10 |
subtest 'not a reference' => sub { |
| 11 |
my $ffi = FFI::Platypus->new( lib => [@lib] ); |
| 12 |
|
| 13 |
$ffi->type("record($record_size)" => 'foo_record_t'); |
| 14 |
my $get_name = $ffi->function( foo_get_name => [ 'foo_record_t' ] => 'string' ); |
| 15 |
my $get_value = $ffi->function( foo_get_value => [ 'foo_record_t' ] => 'sint32' ); |
| 16 |
my $is_null = $ffi->function( pointer_is_null => [ 'foo_record_t' ] => 'int' ); |
| 17 |
my $create = $ffi->function( foo_create => [ 'string', 'sint32' ] => 'foo_record_t' ); |
| 18 |
my $null = $ffi->function( pointer_null => [] => 'foo_record_t' ); |
| 19 |
|
| 20 |
subtest in => sub { |
| 21 |
my $packed = pack('A16l', "hi there\0", 42); |
| 22 |
note "packed size = ", length $packed; |
| 23 |
|
| 24 |
is $get_value->($packed), 42, "get_value(\$packed) = 42"; |
| 25 |
is $get_name->($packed), "hi there", "get_name(\$packed) = hi there"; |
| 26 |
is $is_null->(undef), 1, "is_null(undef)"; |
| 27 |
}; |
| 28 |
|
| 29 |
subtest out => sub { |
| 30 |
my $packed = $create->("platypus", 47); |
| 31 |
note "packed size = ", length $packed; |
| 32 |
|
| 33 |
is $get_value->($packed), 47, "get_value(\$packed) = 47"; |
| 34 |
is $get_name->($packed), 'platypus', "get_value(\$packed) = platypus"; |
| 35 |
is $null->(), undef, 'null() = undef'; |
| 36 |
}; |
| 37 |
|
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
subtest 'return null' => sub { |
| 42 |
|
| 43 |
is( |
| 44 |
[FFI::Platypus->new( api => 1, lib => [@lib] )->function( pointer_null => [] => 'record(10)*' )->call], |
| 45 |
[], |
| 46 |
); |
| 47 |
|
| 48 |
is( |
| 49 |
[FFI::Platypus->new( api => 2, lib => [@lib] )->function( pointer_null => [] => 'record(10)*' )->call], |
| 50 |
[undef], |
| 51 |
); |
| 52 |
|
| 53 |
}; |
| 54 |
|
| 55 |
subtest 'is a reference' => sub { |
| 56 |
my $ffi = FFI::Platypus->new( lib => [@lib] ); |
| 57 |
|
| 58 |
$ffi->type("record(My::FooRecord)" => 'foo_record_t'); |
| 59 |
my $get_name = $ffi->function( foo_get_name => [ 'foo_record_t' ] => 'string' ); |
| 60 |
my $get_value = $ffi->function( foo_get_value => [ 'foo_record_t' ] => 'sint32' ); |
| 61 |
my $is_null = $ffi->function( pointer_is_null => [ 'foo_record_t' ] => 'int' ); |
| 62 |
my $create = $ffi->function( foo_create => [ 'string', 'sint32' ] => 'foo_record_t' ); |
| 63 |
my $null = $ffi->function( pointer_null => [] => 'foo_record_t' ); |
| 64 |
|
| 65 |
subtest in => sub { |
| 66 |
my $packed = pack('A16l', "hi there\0", 42); |
| 67 |
note "packed size = ", length $packed; |
| 68 |
|
| 69 |
is $get_value->(\$packed), 42, "get_value(\\\$packed) = 42"; |
| 70 |
is $get_name->(\$packed), "hi there", "get_name(\\\$packed) = hi there"; |
| 71 |
is $is_null->(\undef), 1, "is_null(\\undef)"; |
| 72 |
}; |
| 73 |
|
| 74 |
subtest out => sub { |
| 75 |
my $packed = $create->("platypus", 47); |
| 76 |
note "packed size = ", length $packed; |
| 77 |
|
| 78 |
isa_ok $packed, 'My::FooRecord'; |
| 79 |
is $packed->my_method, "starscream", "packed.my_method = starscream"; |
| 80 |
is $get_value->($packed), 47, "get_value(\$packed) = 47"; |
| 81 |
is $get_name->($packed), 'platypus', "get_value(\$packed) = platypus"; |
| 82 |
is $null->(), undef, 'null() = \undef'; |
| 83 |
}; |
| 84 |
|
| 85 |
}; |
| 86 |
|
| 87 |
subtest 'closure' => sub { |
| 88 |
|
| 89 |
{ package Closure::Record::RW; |
| 90 |
|
| 91 |
use FFI::Platypus::Record; |
| 92 |
|
| 93 |
record_layout( |
| 94 |
'string_rw' => 'one', |
| 95 |
'string_rw' => 'two', |
| 96 |
'int' => 'three', |
| 97 |
'string_rw' => 'four', |
| 98 |
'int[2]' => 'myarray1', |
| 99 |
'opaque' => 'opaque1', |
| 100 |
'opaque[2]' => 'myarray2', |
| 101 |
'string(5)' => 'fixedfive', |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
my $ffi = FFI::Platypus->new( lib => [@lib] ); |
| 106 |
|
| 107 |
$ffi->type('record(Closure::Record::RW)' => 'cx_struct_rw_t'); |
| 108 |
eval { $ffi->type('(cx_struct_rw_t,int)->void' => 'cx_closure_t') }; |
| 109 |
is $@, '', 'allow record type as arg'; |
| 110 |
|
| 111 |
my $cx_closure_set = $ffi->function(cx_closure_set => [ 'cx_closure_t' ] => 'void' ); |
| 112 |
my $cx_closure_call = $ffi->function(cx_closure_call => [ 'cx_struct_rw_t', 'int' ] => 'void' ); |
| 113 |
|
| 114 |
my $r = Closure::Record::RW->new; |
| 115 |
$r->one("one"); |
| 116 |
$r->two("two"); |
| 117 |
$r->three(3); |
| 118 |
$r->four("four"); |
| 119 |
$r->myarray1([1,2]); |
| 120 |
$r->opaque1(malloc(22)); |
| 121 |
$r->myarray2([malloc(33),malloc(44)]); |
| 122 |
$r->fixedfive("five\0"); |
| 123 |
is($r->_ffi_record_ro, 0); |
| 124 |
|
| 125 |
my $here = 0; |
| 126 |
|
| 127 |
my $f = $ffi->closure(sub { |
| 128 |
my($r2,$num) = @_; |
| 129 |
is($r2->_ffi_record_ro, 1); |
| 130 |
is($r2->one, "one"); |
| 131 |
is($r2->two, "two"); |
| 132 |
is($r2->three, 3); |
| 133 |
{ |
| 134 |
local $@ = ''; |
| 135 |
eval { $r2->three(64) }; |
| 136 |
isnt $@, ''; |
| 137 |
note "error = $@"; |
| 138 |
} |
| 139 |
is($r2->three, 3); |
| 140 |
is($r2->four, "four"); |
| 141 |
is($r2->myarray1, [1,2]); |
| 142 |
{ |
| 143 |
local $@ = ''; |
| 144 |
eval { $r2->myarray1([3,4]) }; |
| 145 |
isnt $@, ''; |
| 146 |
note "error = $@"; |
| 147 |
} |
| 148 |
is($r2->myarray1, [1,2]); |
| 149 |
{ |
| 150 |
local $@ = ''; |
| 151 |
eval { $r2->myarray1(3,4) }; |
| 152 |
isnt $@, ''; |
| 153 |
note "error = $@"; |
| 154 |
} |
| 155 |
is($r2->myarray1, [1,2]); |
| 156 |
|
| 157 |
is($r2->opaque1, $r->opaque1); |
| 158 |
{ |
| 159 |
local $@ = ''; |
| 160 |
eval { $r2->opaque1(undef) }; |
| 161 |
isnt $@, ''; |
| 162 |
note "error = $@"; |
| 163 |
} |
| 164 |
is($r2->opaque1, $r->opaque1); |
| 165 |
|
| 166 |
is($r2->myarray2, $r->myarray2); |
| 167 |
{ |
| 168 |
local $@ = ''; |
| 169 |
eval { $r2->myarray2([undef,undef]) }; |
| 170 |
isnt $@, ''; |
| 171 |
note "error = $@"; |
| 172 |
} |
| 173 |
is($r2->myarray2, $r->myarray2); |
| 174 |
{ |
| 175 |
local $@ = ''; |
| 176 |
eval { $r2->myarray2(undef,undef) }; |
| 177 |
isnt $@, ''; |
| 178 |
note "error = $@"; |
| 179 |
} |
| 180 |
is($r2->myarray2, $r->myarray2); |
| 181 |
|
| 182 |
{ |
| 183 |
local $@ = ''; |
| 184 |
eval { $r2->one("new string!") }; |
| 185 |
isnt $@, ''; |
| 186 |
note "error = $@"; |
| 187 |
} |
| 188 |
is($r2->one, "one"); |
| 189 |
|
| 190 |
is($r2->fixedfive, "five\0"); |
| 191 |
{ |
| 192 |
local $@ = ''; |
| 193 |
eval { $r2->fixedfive("xxxxx") }; |
| 194 |
isnt $@, ''; |
| 195 |
note "error = $@"; |
| 196 |
} |
| 197 |
is($r2->fixedfive, "five\0"); |
| 198 |
|
| 199 |
is($num, 42); |
| 200 |
$here = 1; |
| 201 |
}); |
| 202 |
|
| 203 |
$cx_closure_set->($f); |
| 204 |
$cx_closure_call->($r, 42); |
| 205 |
|
| 206 |
is($here, 1); |
| 207 |
|
| 208 |
$here = 0; |
| 209 |
my $f2 = $ffi->closure(sub { |
| 210 |
my($r2, $num) = @_; |
| 211 |
is($r2, undef); |
| 212 |
is($num, 0); |
| 213 |
$here = 1; |
| 214 |
}); |
| 215 |
|
| 216 |
$cx_closure_set->($f2); |
| 217 |
$cx_closure_call->(undef, undef); |
| 218 |
is($here, 1); |
| 219 |
|
| 220 |
}; |
| 221 |
|
| 222 |
subtest 'api = 1 fixed string' => sub { |
| 223 |
|
| 224 |
my $ffi = FFI::Platypus->new( api => 1, lib => [@lib] ); |
| 225 |
|
| 226 |
{ |
| 227 |
package My::FooRecord2; |
| 228 |
use FFI::Platypus::Record; |
| 229 |
eval { record_layout( $ffi, qw( string(5)* foo string(5) bar )) }; |
| 230 |
} |
| 231 |
|
| 232 |
is "$@", ""; |
| 233 |
|
| 234 |
my $r = My::FooRecord2->new( foo => '12345', bar => '67890' ); |
| 235 |
|
| 236 |
is $r->foo, '12345'; |
| 237 |
is $r->bar, '67890'; |
| 238 |
|
| 239 |
}; |
| 240 |
|
| 241 |
done_testing; |
| 242 |
|
| 243 |
package |
| 244 |
My::FooRecord; |
| 245 |
|
| 246 |
use constant ffi_record_size => do { |
| 247 |
my $ffi = FFI::Platypus->new; |
| 248 |
$ffi->sizeof('char[16]') + $ffi->sizeof('sint32'); |
| 249 |
}; |
| 250 |
|
| 251 |
sub my_method { "starscream" } |