| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus; |
| 3 |
use FFI::Platypus::Memory qw( malloc free ); |
| 4 |
use Data::Dumper; |
| 5 |
|
| 6 |
do { |
| 7 |
package |
| 8 |
Foo1; |
| 9 |
|
| 10 |
use FFI::Platypus::Record; |
| 11 |
|
| 12 |
record_layout( |
| 13 |
uint8 => 'first', |
| 14 |
uint32 => 'second', |
| 15 |
); |
| 16 |
|
| 17 |
}; |
| 18 |
|
| 19 |
sub xdump_meta ($) |
| 20 |
{ |
| 21 |
my($type) = @_; |
| 22 |
my $ffi = FFI::Platypus->new; |
| 23 |
my $object = $ffi->type_meta($type); |
| 24 |
note(Data::Dumper->new([$object])->Indent(0)->Terse(1)->Sortkeys(1)->Dump); |
| 25 |
} |
| 26 |
|
| 27 |
subtest 'integer accessor' => sub { |
| 28 |
my $foo = Foo1->new( first => 1, second => 2 ); |
| 29 |
isa_ok $foo, 'Foo1'; |
| 30 |
|
| 31 |
my $size = $foo->_ffi_record_size; |
| 32 |
like $size, qr{^[0-9]+$}, "foo._record_size = $size"; |
| 33 |
|
| 34 |
is $foo->first, 1, 'foo.first = 1'; |
| 35 |
is $foo->second, 2, 'foo.second = 2'; |
| 36 |
|
| 37 |
$foo->first(22); |
| 38 |
is $foo->first, 22, 'foo.first = 22'; |
| 39 |
|
| 40 |
$foo->second(42); |
| 41 |
is $foo->second, 42, 'foo.second = 42'; |
| 42 |
|
| 43 |
$foo = Foo1->new( { first => 3, second => 4 } ); |
| 44 |
|
| 45 |
is $foo->first, 3, 'foo.first = 3'; |
| 46 |
is $foo->second, 4, 'foo.second = 4'; |
| 47 |
|
| 48 |
xdump_meta('record(Foo1)'); |
| 49 |
xdump_meta('record(8)'); |
| 50 |
|
| 51 |
}; |
| 52 |
|
| 53 |
do { |
| 54 |
package |
| 55 |
Color; |
| 56 |
|
| 57 |
use FFI::Platypus; |
| 58 |
use FFI::Platypus::Record; |
| 59 |
|
| 60 |
my $ffi = FFI::Platypus->new; |
| 61 |
$ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 62 |
|
| 63 |
record_layout($ffi, qw( |
| 64 |
uint8 red |
| 65 |
uint8 green |
| 66 |
uint8 blue |
| 67 |
)); |
| 68 |
|
| 69 |
$ffi->type('record(Color)' => 'Color'); |
| 70 |
$ffi->attach( [ color_get_red => 'get_red' ] => [ 'Color' ] => 'int' ); |
| 71 |
$ffi->attach( [ color_get_green => 'get_green' ] => [ 'Color' ] => 'int' ); |
| 72 |
$ffi->attach( [ color_get_blue => 'get_blue' ] => [ 'Color' ] => 'int' ); |
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'values match in C' => sub { |
| 76 |
my $color = Color->new( |
| 77 |
red => 50, |
| 78 |
green => 100, |
| 79 |
blue => 150, |
| 80 |
); |
| 81 |
|
| 82 |
isa_ok $color, 'Color'; |
| 83 |
|
| 84 |
is $color->get_red, 50, "color.get_red = 50"; |
| 85 |
is $color->get_green, 100, "color.get_green = 100"; |
| 86 |
is $color->get_blue, 150, "color.get_blue = 150"; |
| 87 |
|
| 88 |
}; |
| 89 |
|
| 90 |
do { |
| 91 |
package |
| 92 |
Foo2; |
| 93 |
|
| 94 |
use FFI::Platypus::Record; |
| 95 |
|
| 96 |
record_layout(qw( |
| 97 |
char : |
| 98 |
uint64_t uint64 |
| 99 |
char : |
| 100 |
uint32_t uint32 |
| 101 |
char : |
| 102 |
uint16_t uint16 |
| 103 |
char : |
| 104 |
uint8_t uint8 |
| 105 |
|
| 106 |
char : |
| 107 |
int64_t sint64 |
| 108 |
char : |
| 109 |
int32_t sint32 |
| 110 |
char : |
| 111 |
int16_t sint16 |
| 112 |
char : |
| 113 |
int8_t sint8 |
| 114 |
|
| 115 |
char : |
| 116 |
float float |
| 117 |
char : |
| 118 |
double double |
| 119 |
|
| 120 |
char : |
| 121 |
opaque opaque |
| 122 |
)); |
| 123 |
|
| 124 |
my $ffi = FFI::Platypus->new; |
| 125 |
$ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 126 |
|
| 127 |
$ffi->attach(["align_get_$_" => "get_$_"] => [ 'record(Foo2)' ] => $_) |
| 128 |
for qw( uint8 sint8 uint16 sint16 uint32 sint32 uint64 sint64 float double opaque ); |
| 129 |
}; |
| 130 |
|
| 131 |
subtest 'complex alignment' => sub { |
| 132 |
my $foo = Foo2->new; |
| 133 |
isa_ok $foo, 'Foo2'; |
| 134 |
|
| 135 |
$foo->uint64(512); |
| 136 |
is $foo->get_uint64, 512, "uint64 = 512"; |
| 137 |
|
| 138 |
$foo->sint64(-512); |
| 139 |
is $foo->get_sint64, -512, "sint64 = -512"; |
| 140 |
|
| 141 |
$foo->uint32(1024); |
| 142 |
is $foo->get_uint32, 1024, "uint32 = 1024"; |
| 143 |
|
| 144 |
$foo->sint32(-1024); |
| 145 |
is $foo->get_sint32, -1024, "sint32 = -1024"; |
| 146 |
|
| 147 |
$foo->uint16(2048); |
| 148 |
is $foo->get_uint16, 2048, "uint16 = 2048"; |
| 149 |
|
| 150 |
$foo->sint16(-2048); |
| 151 |
is $foo->get_sint16, -2048, "sint16 = -2048"; |
| 152 |
|
| 153 |
$foo->uint8(48); |
| 154 |
is $foo->get_uint8, 48, "uint8 = 48"; |
| 155 |
|
| 156 |
$foo->sint8(-48); |
| 157 |
is $foo->get_sint8, -48, "sint8 = -48"; |
| 158 |
|
| 159 |
$foo->float(1.5); |
| 160 |
is $foo->get_float, 1.5, "float = 1.5"; |
| 161 |
|
| 162 |
$foo->double(-1.5); |
| 163 |
is $foo->get_double, -1.5, "double = -1.5"; |
| 164 |
|
| 165 |
my $ptr = malloc 32; |
| 166 |
|
| 167 |
$foo->opaque($ptr); |
| 168 |
is $foo->get_opaque, $ptr, "get_opaque = $ptr"; |
| 169 |
is $foo->opaque, $ptr, "opaque = $ptr"; |
| 170 |
|
| 171 |
$foo->opaque(undef); |
| 172 |
is $foo->get_opaque, undef, "get_opaque = undef"; |
| 173 |
is $foo->opaque, undef, "opaque = undef"; |
| 174 |
|
| 175 |
free $ptr; |
| 176 |
}; |
| 177 |
|
| 178 |
subtest 'same name' => sub { |
| 179 |
eval { |
| 180 |
package |
| 181 |
Foo3; |
| 182 |
|
| 183 |
require FFI::Platypus::Record; |
| 184 |
FFI::Platypus::Record->import; |
| 185 |
|
| 186 |
record_layout( |
| 187 |
int => 'foo', |
| 188 |
int => 'foo', |
| 189 |
); |
| 190 |
}; |
| 191 |
|
| 192 |
isnt $@, '', 'two members of the same name not allowed'; |
| 193 |
note $@ if $@; |
| 194 |
}; |
| 195 |
|
| 196 |
do { |
| 197 |
package |
| 198 |
Foo4; |
| 199 |
|
| 200 |
use FFI::Platypus::Record; |
| 201 |
|
| 202 |
record_layout(qw( |
| 203 |
char : |
| 204 |
uint64_t[3] uint64 |
| 205 |
char : |
| 206 |
uint32_t[3] uint32 |
| 207 |
char : |
| 208 |
uint16_t[3] uint16 |
| 209 |
char : |
| 210 |
uint8_t[3] uint8 |
| 211 |
|
| 212 |
char : |
| 213 |
int64_t[3] sint64 |
| 214 |
char : |
| 215 |
int32_t[3] sint32 |
| 216 |
char : |
| 217 |
int16_t[3] sint16 |
| 218 |
char : |
| 219 |
int8_t[3] sint8 |
| 220 |
|
| 221 |
char : |
| 222 |
float[3] float |
| 223 |
char : |
| 224 |
double[3] double |
| 225 |
|
| 226 |
char : |
| 227 |
opaque[3] opaque |
| 228 |
)); |
| 229 |
|
| 230 |
my $ffi = FFI::Platypus->new; |
| 231 |
$ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 232 |
|
| 233 |
$ffi->attach(["align_array_get_$_" => "get_$_"] => [ 'record(Foo4)' ] => "${_}[3]" ) |
| 234 |
for qw( uint8 sint8 uint16 sint16 uint32 sint32 uint64 sint64 float double opaque ); |
| 235 |
}; |
| 236 |
|
| 237 |
subtest 'array alignment' => sub { |
| 238 |
my $foo = Foo4->new; |
| 239 |
isa_ok $foo, 'Foo4'; |
| 240 |
|
| 241 |
foreach my $bits (qw( 8 16 32 64 )) |
| 242 |
{ |
| 243 |
subtest "unsigned $bits integer" => sub { |
| 244 |
my $acc1 = "uint$bits"; |
| 245 |
my $acc2 = "get_uint$bits"; |
| 246 |
$foo->$acc1([1,2,3]); |
| 247 |
is $foo->$acc1, [1,2,3], "$acc1 = 1,2,3"; |
| 248 |
is $foo->$acc2, [1,2,3], "$acc2 = 1,2,3"; |
| 249 |
is $foo->$acc1(1), 2, "$acc1(1) = 2"; |
| 250 |
$foo->$acc1(1,20); |
| 251 |
is $foo->$acc1, [1,20,3], "$acc1 = 1,20,3"; |
| 252 |
}; |
| 253 |
|
| 254 |
subtest "signed $bits integer" => sub { |
| 255 |
my $acc1 = "sint$bits"; |
| 256 |
my $acc2 = "get_sint$bits"; |
| 257 |
$foo->$acc1([-1,2,-3]); |
| 258 |
is $foo->$acc1, [-1,2,-3], "$acc1 = -1,2,-3"; |
| 259 |
is $foo->$acc2, [-1,2,-3], "$acc2 = -1,2,-3"; |
| 260 |
is $foo->$acc1(2), -3, "$acc1(2) = -3"; |
| 261 |
$foo->$acc1(1,-20); |
| 262 |
is $foo->$acc1, [-1,-20,-3], "$acc1 = -1,-20,-3"; |
| 263 |
}; |
| 264 |
} |
| 265 |
|
| 266 |
foreach my $type (qw( float double )) |
| 267 |
{ |
| 268 |
subtest $type => sub { |
| 269 |
$foo->$type([1.5,undef,-1.5]); |
| 270 |
is $foo->$type, [1.5,0.0,-1.5], "$type = 1.5,0,-1.5"; |
| 271 |
is $foo->$type(0), 1.5; |
| 272 |
is $foo->$type(1), 0.0; |
| 273 |
is $foo->$type(2), -1.5; |
| 274 |
$foo->$type(1,20.0); |
| 275 |
is $foo->$type, [1.5,20.0,-1.5], "$type = 1.5,20,-1.5"; |
| 276 |
}; |
| 277 |
} |
| 278 |
|
| 279 |
subtest 'opaque' => sub { |
| 280 |
my $ptr1 = malloc 32; |
| 281 |
my $ptr2 = malloc 64; |
| 282 |
|
| 283 |
$foo->opaque([$ptr1,undef,$ptr2]); |
| 284 |
is $foo->opaque, [$ptr1,undef,$ptr2], "opaque = $ptr1,undef,$ptr2"; |
| 285 |
|
| 286 |
$foo->opaque(1,$ptr1); |
| 287 |
is $foo->opaque, [$ptr1,$ptr1,$ptr2], "opaque = $ptr1,$ptr1,$ptr2"; |
| 288 |
|
| 289 |
$foo->opaque(0,undef); |
| 290 |
is $foo->opaque, [undef,$ptr1,$ptr2], "opaque = undef,$ptr1,$ptr2"; |
| 291 |
|
| 292 |
is $foo->opaque(0), undef; |
| 293 |
is $foo->opaque(1), $ptr1; |
| 294 |
is $foo->opaque(2), $ptr2; |
| 295 |
|
| 296 |
free $ptr1; |
| 297 |
free $ptr2; |
| 298 |
}; |
| 299 |
|
| 300 |
my $align = $foo->_ffi_record_align; |
| 301 |
like $align, qr{^[0-9]+$}, "align = $align"; |
| 302 |
ok $align > 0, "align is positive"; |
| 303 |
}; |
| 304 |
|
| 305 |
do { |
| 306 |
package |
| 307 |
Foo5; |
| 308 |
|
| 309 |
use FFI::Platypus::Record; |
| 310 |
|
| 311 |
record_layout(qw( |
| 312 |
char : |
| 313 |
string value |
| 314 |
)); |
| 315 |
|
| 316 |
my $ffi = FFI::Platypus->new; |
| 317 |
$ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 318 |
|
| 319 |
$ffi->attach( |
| 320 |
[align_string_get_value => 'get_value'] => ['record(Foo5)'] => 'string', |
| 321 |
); |
| 322 |
|
| 323 |
$ffi->attach( |
| 324 |
[align_string_set_value => 'set_value'] => ['record(Foo5)','string'] => 'void', |
| 325 |
); |
| 326 |
}; |
| 327 |
|
| 328 |
subtest 'string ro' => sub { |
| 329 |
my $foo = Foo5->new; |
| 330 |
isa_ok $foo, 'Foo5'; |
| 331 |
|
| 332 |
is $foo->value, undef, 'foo.value = undef'; |
| 333 |
is $foo->get_value, undef, 'foo.get_value = undef'; |
| 334 |
|
| 335 |
$foo->set_value("my value"); |
| 336 |
|
| 337 |
is $foo->value, 'my value', 'foo.value = my value'; |
| 338 |
is $foo->get_value, 'my value', 'foo.get_value = my value'; |
| 339 |
|
| 340 |
eval { $foo->value("stuff") }; |
| 341 |
isnt $@, '', 'value is ro'; |
| 342 |
note $@ if $@; |
| 343 |
|
| 344 |
$foo->set_value(undef); |
| 345 |
|
| 346 |
is $foo->value, undef, 'foo.value = undef'; |
| 347 |
is $foo->get_value, undef, 'foo.get_value = undef'; |
| 348 |
}; |
| 349 |
|
| 350 |
do { |
| 351 |
package |
| 352 |
Foo6; |
| 353 |
|
| 354 |
use FFI::Platypus::Record; |
| 355 |
|
| 356 |
record_layout(qw( |
| 357 |
char : |
| 358 |
string(10) value |
| 359 |
)); |
| 360 |
|
| 361 |
my $ffi = FFI::Platypus->new; |
| 362 |
$ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 363 |
|
| 364 |
$ffi->attach([align_fixed_get_value=>'get_value'] => ['record(Foo6)'] => 'string'); |
| 365 |
}; |
| 366 |
|
| 367 |
subtest 'fixed string' => sub { |
| 368 |
my $foo = Foo6->new; |
| 369 |
isa_ok $foo, 'Foo6'; |
| 370 |
|
| 371 |
is $foo->value, "\0\0\0\0\0\0\0\0\0\0", 'foo.value = "\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0"'; |
| 372 |
is $foo->get_value, "", 'foo.get_value = ""'; |
| 373 |
|
| 374 |
$foo->value("one two three four five six seven eight"); |
| 375 |
|
| 376 |
is $foo->value, "one two th", 'foo.value = "one two th"'; |
| 377 |
|
| 378 |
$foo->value("123456789\0"); |
| 379 |
|
| 380 |
is $foo->value, "123456789\0", "foo.value = 123456789\\0"; |
| 381 |
is $foo->get_value, "123456789", "foo.get_value = 123456789"; |
| 382 |
}; |
| 383 |
|
| 384 |
do { |
| 385 |
package |
| 386 |
Foo7; |
| 387 |
|
| 388 |
use FFI::Platypus::Record; |
| 389 |
|
| 390 |
record_layout(qw( |
| 391 |
char : |
| 392 |
string_rw value |
| 393 |
)); |
| 394 |
|
| 395 |
my $ffi = FFI::Platypus->new; |
| 396 |
$ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 397 |
|
| 398 |
$ffi->attach( |
| 399 |
[align_string_get_value => 'get_value'] => |
| 400 |
['record(Foo7)'] => 'string' |
| 401 |
); |
| 402 |
|
| 403 |
}; |
| 404 |
|
| 405 |
subtest 'string rw' => sub { |
| 406 |
my $foo = Foo7->new; |
| 407 |
isa_ok $foo, 'Foo7'; |
| 408 |
|
| 409 |
$foo->value('hi there'); |
| 410 |
is $foo->value, "hi there", "foo.value = hi there"; |
| 411 |
is $foo->get_value, 'hi there', 'foo.get_value = hi there'; |
| 412 |
|
| 413 |
$foo->value(undef); |
| 414 |
is $foo->value, undef, 'foo.value = undef'; |
| 415 |
is $foo->get_value, undef, 'foo.get_value = undef'; |
| 416 |
|
| 417 |
$foo->value('starscream!!!'); |
| 418 |
is $foo->value, "starscream!!!", "foo.value = starscream!!!"; |
| 419 |
is $foo->get_value, 'starscream!!!', 'foo.get_value = starscream!!!'; |
| 420 |
}; |
| 421 |
|
| 422 |
subtest 'record with custom ffi' => sub { |
| 423 |
|
| 424 |
{ |
| 425 |
package |
| 426 |
Foo8; |
| 427 |
|
| 428 |
use FFI::Platypus::Record; |
| 429 |
|
| 430 |
my $ffi = FFI::Platypus->new; |
| 431 |
$ffi->type('string rw' => 'foo_t'); |
| 432 |
|
| 433 |
record_layout($ffi, qw( |
| 434 |
foo_t foo |
| 435 |
)); |
| 436 |
} |
| 437 |
|
| 438 |
my $foo8 = Foo8->new; |
| 439 |
isa_ok $foo8, 'Foo8'; |
| 440 |
|
| 441 |
$foo8->foo("yo this is a string"); |
| 442 |
is( $foo8->foo, "yo this is a string" ); |
| 443 |
|
| 444 |
}; |
| 445 |
|
| 446 |
subtest 'record with ffi args' => sub { |
| 447 |
|
| 448 |
{ package |
| 449 |
FFI::Platypus::Lang::Foo9; |
| 450 |
sub native_type_map |
| 451 |
{ |
| 452 |
return { foo_t => 'sint32' }; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
{ package |
| 457 |
Foo9; |
| 458 |
|
| 459 |
use FFI::Platypus::Record; |
| 460 |
|
| 461 |
record_layout |
| 462 |
[ lang => 'Foo9', api => 1 ], |
| 463 |
foo_t => 'foo' |
| 464 |
; |
| 465 |
} |
| 466 |
|
| 467 |
my $foo8 = Foo8->new; |
| 468 |
isa_ok $foo8, 'Foo8'; |
| 469 |
|
| 470 |
$foo8->foo(-42); |
| 471 |
is( $foo8->foo, -42 ); |
| 472 |
}; |
| 473 |
|
| 474 |
subtest 'api_1' => sub { |
| 475 |
|
| 476 |
my $api; |
| 477 |
my $ffi = FFI::Platypus->new; |
| 478 |
|
| 479 |
no warnings 'once'; |
| 480 |
no warnings 'redefine'; |
| 481 |
local *FFI::Platypus::new = do { |
| 482 |
my $orig = FFI::Platypus->can('new'); |
| 483 |
sub { |
| 484 |
my $class = shift; |
| 485 |
my %args = @_; |
| 486 |
$api = $args{api}; |
| 487 |
$api = 0 unless defined $args{api}; |
| 488 |
$class->$orig(@_); |
| 489 |
}; |
| 490 |
}; |
| 491 |
|
| 492 |
subtest 'no $ffi or args' => sub { |
| 493 |
|
| 494 |
local $@; |
| 495 |
undef $api; |
| 496 |
|
| 497 |
eval q{ |
| 498 |
package Foo10; |
| 499 |
use FFI::Platypus::Record; |
| 500 |
record_layout_1( |
| 501 |
string => 'a', |
| 502 |
); |
| 503 |
}; |
| 504 |
|
| 505 |
is "$@", ''; |
| 506 |
is( $api, 2 ); |
| 507 |
}; |
| 508 |
|
| 509 |
subtest 'args' => sub { |
| 510 |
|
| 511 |
local $@; |
| 512 |
undef $api; |
| 513 |
|
| 514 |
eval q{ |
| 515 |
package Foo11; |
| 516 |
use FFI::Platypus::Record; |
| 517 |
record_layout_1( [], |
| 518 |
string => 'a', |
| 519 |
); |
| 520 |
}; |
| 521 |
|
| 522 |
is "$@", ''; |
| 523 |
is( $api, 2 ); |
| 524 |
}; |
| 525 |
|
| 526 |
subtest '$ffi' => sub { |
| 527 |
|
| 528 |
local $@; |
| 529 |
undef $api; |
| 530 |
|
| 531 |
eval q{ |
| 532 |
package Foo12; |
| 533 |
use FFI::Platypus::Record; |
| 534 |
record_layout_1( $ffi, |
| 535 |
string => 'a', |
| 536 |
); |
| 537 |
}; |
| 538 |
|
| 539 |
is "$@", ''; |
| 540 |
is( $api, undef ); |
| 541 |
}; |
| 542 |
|
| 543 |
}; |
| 544 |
|
| 545 |
done_testing; |