Debian Codegraph @

t/ffi_platypus.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use FFI::CheckLib;
4 use Data::Dumper;
5 use File::Spec;
6 use FFI::Platypus::TypeParser;
7
8 my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
9
10 sub xdump ($)
11 {
12 my($object) = @_;
13 note(Data::Dumper->new([$object])->Indent(0)->Terse(1)->Sortkeys(1)->Dump);
14 }
15
16 subtest 'constructor' => sub {
17
18 subtest 'basic' => sub {
19 my $ffi = eval { FFI::Platypus->new };
20 diag $@ if $@;
21 isa_ok $ffi, 'FFI::Platypus';
22 };
23
24 subtest 'no arguments' => sub {
25 my $ffi = FFI::Platypus->new;
26 isa_ok $ffi, 'FFI::Platypus';
27 is [$ffi->lib], [], 'ffi.lib';
28 };
29
30 subtest 'with single lib' => sub {
31 my $ffi = FFI::Platypus->new( lib => "libfoo.so" );
32 isa_ok $ffi, 'FFI::Platypus';
33 is [$ffi->lib], ['libfoo.so'], 'ffi.lib';
34 };
35
36 subtest 'with multiple lib' => sub {
37 my $ffi = FFI::Platypus->new( lib => ["libfoo.so", "libbar.so", "libbaz.so" ] );
38 isa_ok $ffi, 'FFI::Platypus';
39 is [$ffi->lib], ['libfoo.so', 'libbar.so', 'libbaz.so'], 'ffi.lib';
40 };
41
42 };
43
44 subtest 'abi' => sub {
45
46 my $ffi = FFI::Platypus->new;
47
48 my %abis = %{ $ffi->abis };
49
50 ok defined $abis{default_abi}, 'has a default ABI';
51
52 foreach my $abi (keys %abis)
53 {
54 subtest $abi => sub {
55 eval { $ffi->abi($abi) };
56 is $@, '', 'string';
57 is $ffi->{tp}->abi, $abis{$abi}, 'type parser';
58 eval { $ffi->abi($abis{$abi}) };
59 is $@, '', 'integer';
60 is $ffi->{tp}->abi, $abis{$abi}, 'type parser';
61 };
62 }
63
64 subtest 'bogus' => sub {
65 eval { $ffi->abi('bogus') };
66 like $@, qr{no such ABI: bogus}, 'string';
67 eval { $ffi->abi(999999) };
68 like $@, qr{no such ABI: 999999}, 'integer';
69 };
70
71 };
72
73 subtest 'alignof' => sub {
74 my $ffi = FFI::Platypus->new;
75
76 my $pointer_align = $ffi->alignof('opaque');
77
78 subtest 'ffi types' => sub {
79
80 foreach my $type (qw( sint8 uint8 sint16 uint16 sint32 uint32 sint64 uint64 float double opaque string ))
81 {
82 my $align = $ffi->alignof($type);
83 like $align, qr{^[0-9]$}, "alignof $type = $align";
84 is(FFI::Platypus->alignof($type), $align);
85
86 next if $type eq 'string';
87
88 my $align2 = $ffi->alignof("$type [2]");
89 is $align2, $align, "alignof $type [2] = $align";
90
91 my $align3 = $ffi->alignof("$type *");
92 is $align3, $pointer_align, "alignof $type * = $pointer_align";
93
94 $ffi->custom_type("custom_$type" => {
95 native_type => $type,
96 native_to_perl => sub {},
97 });
98
99 my $align4 = $ffi->alignof("custom_$type");
100 is $align4, $align, "alignof custom_$type = $align";
101 }
102 };
103
104
105 subtest 'aliases' => sub {
106 $ffi->type('ushort' => 'foo');
107
108 my $align = $ffi->alignof('ushort');
109 like $align, qr{^[0-9]$}, "alignof ushort = $align";
110
111 my $align2 = $ffi->alignof('foo');
112 is $align2, $align, "alignof foo = $align";
113
114 };
115
116 subtest 'closure' => sub {
117 $ffi->type('(int)->int' => 'closure_t');
118
119 my $align = $ffi->alignof('closure_t');
120 is $align, $pointer_align, "sizeof closure_t = $pointer_align";
121
122 };
123
124 subtest 'record' => sub {
125 my $align = $ffi->alignof('record(22)');
126 is $align, 1;
127 xdump($ffi->type_meta('record(22)'));
128 };
129 };
130
131 subtest 'custom type' => sub {
132
133 my $ffi = FFI::Platypus->new;
134
135 my @basic_types = (qw( float double opaque ), map { ("uint$_", "sint$_") } (8,16,32,64));
136
137 foreach my $basic (@basic_types)
138 {
139 subtest $basic => sub {
140 eval { $ffi->custom_type("foo_${basic}_1", { native_type => $basic, perl_to_native => sub {} }) };
141 is $@, '', 'ffi.custom_type 1';
142 xdump({ "${basic}_1" => $ffi->type_meta("foo_${basic}_1") });
143
144 eval { $ffi->custom_type("bar_${basic}_1", { native_type => $basic, native_to_perl => sub {} }) };
145 is $@, '', 'ffi.custom_type 1';
146 xdump({ "${basic}_1" => $ffi->type_meta("bar_${basic}_1") });
147
148 eval { $ffi->custom_type("baz_${basic}_1", { native_type => $basic, perl_to_native => sub {}, native_to_perl => sub {} }) };
149 is $@, '', 'ffi.custom_type 1';
150 xdump({ "${basic}_1" => $ffi->type_meta("baz_${basic}_1") });
151
152 eval { $ffi->custom_type("foo_${basic}_2", { native_type => $basic, perl_to_native => sub {}, perl_to_native_post => sub { } }) };
153 is $@, '', 'ffi.custom_type 1';
154 xdump({ "${basic}_1" => $ffi->type_meta("foo_${basic}_2") });
155
156 eval { $ffi->custom_type("bar_${basic}_2", { native_type => $basic, native_to_perl => sub {}, perl_to_native_post => sub { } }) };
157 is $@, '', 'ffi.custom_type 1';
158 xdump({ "${basic}_1" => $ffi->type_meta("bar_${basic}_2") });
159
160 eval { $ffi->custom_type("baz_${basic}_2", { native_type => $basic, perl_to_native => sub {}, native_to_perl => sub {}, perl_to_native_post => sub { } }) };
161 is $@, '', 'ffi.custom_type 1';
162 xdump({ "${basic}_1" => $ffi->type_meta("baz_${basic}_2") });
163 };
164 }
165 };
166
167 subtest 'find lib' => sub {
168
169 subtest 'find_lib' =>sub {
170 my $ffi = FFI::Platypus->new;
171 $ffi->find_lib(lib => 'test', symbol => 'f0', libpath => 't/ffi');
172 my $address = $ffi->find_symbol('f0');
173 ok $address, "found f0 = $address";
174 };
175
176 subtest external => sub {
177 my $ffi = FFI::Platypus->new( lib => [@lib] );
178
179 my $good = $ffi->find_symbol('f0');
180 ok $good, "ffi.find_symbol(f0) = $good";
181
182 my $bad = $ffi->find_symbol('bogus');
183 is $bad, undef, 'ffi.find_symbol(bogus) = undef';
184 };
185
186 subtest internal => sub {
187 my $ffi = FFI::Platypus->new( lib => [undef] );
188
189 my $good = $ffi->find_symbol('printf');
190 ok $good, "ffi.find_symbol(printf) = $good";
191
192 my $bad = $ffi->find_symbol('bogus');
193 is $bad, undef, 'ffi.find_symbol(bogus) = undef';
194 };
195 };
196
197 subtest 'find symbol' => sub {
198 subtest external => sub {
199 my $ffi = FFI::Platypus->new( lib => [@lib] );
200
201 my $good = $ffi->find_symbol('f0');
202 ok $good, "ffi.find_symbol(f0) = $good";
203
204 my $bad = $ffi->find_symbol('bogus');
205 is $bad, undef, 'ffi.find_symbol(bogus) = undef';
206 };
207
208 subtest internal => sub {
209 my $ffi = FFI::Platypus->new( lib => [undef] );
210
211 my $good = $ffi->find_symbol('printf');
212 ok $good, "ffi.find_symbol(printf) = $good";
213
214 my $bad = $ffi->find_symbol('bogus');
215 is $bad, undef, 'ffi.find_symbol(bogus) = undef';
216 };
217 };
218
219 subtest 'lib' => sub {
220 subtest 'basic' => sub {
221
222 my $ffi = FFI::Platypus->new;
223
224 my($lib) = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
225 ok -e $lib, "exists $lib";
226
227 eval { $ffi->lib($lib) };
228 is $@, '', 'ffi.lib (set)';
229
230 is [eval { $ffi->lib }], [$lib], 'ffi.lib (get)';
231
232 };
233
234 subtest 'undef' => sub {
235
236 subtest 'baseline' => sub {
237 my $ffi = FFI::Platypus->new;
238 is([$ffi->lib], []);
239 };
240
241 subtest 'lib => [undef]' => sub {
242 my $ffi = FFI::Platypus->new(lib => [undef]);
243 is([$ffi->lib], [undef]);
244 };
245
246 subtest 'lib => undef' => sub {
247 my $ffi = FFI::Platypus->new(lib => undef);
248 is([$ffi->lib], [undef]);
249 };
250
251 };
252
253 subtest 'coderef' => sub {
254
255 my $ffi = FFI::Platypus->new;
256
257 my($lib) = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
258 ok -e $lib, "exists $lib";
259
260 eval { $ffi->lib(sub{ $lib }) };
261 is $@, '', 'ffi.lib (set)';
262
263 is [eval { $ffi->lib }], [$lib], 'ffi.lib (get)';
264
265 };
266 };
267
268 subtest 'sizeof' => sub {
269 my $ffi = FFI::Platypus->new;
270
271 subtest integers => sub {
272 is $ffi->sizeof('uint8'), 1, 'sizeof uint8 = 1';
273 is $ffi->sizeof('uint16'), 2, 'sizeof uint16 = 2';
274 is $ffi->sizeof('uint32'), 4, 'sizeof uint32 = 4';
275 is $ffi->sizeof('uint64'), 8, 'sizeof uint64 = 8';
276
277 is $ffi->sizeof('sint8'), 1, 'sizeof sint8 = 1';
278 is $ffi->sizeof('sint16'), 2, 'sizeof sint16 = 2';
279 is $ffi->sizeof('sint32'), 4, 'sizeof sint32 = 4';
280 is $ffi->sizeof('sint64'), 8, 'sizeof sint64 = 8';
281 };
282
283 subtest 'class methods' => sub {
284 my $class = 'FFI::Platypus';
285 is $class->sizeof('uint8'), 1, 'sizeof uint8 = 1';
286 is $class->sizeof('uint16'), 2, 'sizeof uint16 = 2';
287 is $class->sizeof('uint32'), 4, 'sizeof uint32 = 4';
288 is $class->sizeof('uint64'), 8, 'sizeof uint64 = 8';
289
290 is $class->sizeof('sint8'), 1, 'sizeof sint8 = 1';
291 is $class->sizeof('sint16'), 2, 'sizeof sint16 = 2';
292 is $class->sizeof('sint32'), 4, 'sizeof sint32 = 4';
293 is $class->sizeof('sint64'), 8, 'sizeof sint64 = 8';
294 };
295
296 subtest floats => sub {
297 is $ffi->sizeof('float'), 4, 'sizeof float = 4';
298 is $ffi->sizeof('double'), 8, 'sizeof double = 8';
299 };
300
301 subtest pointers => sub {
302 my $pointer_size = $ffi->sizeof('opaque');
303 ok $pointer_size == 4 || $pointer_size == 8, "sizeof opaque = $pointer_size";
304
305 is $ffi->sizeof('uint8*'), $pointer_size, "sizeof uint8* = $pointer_size";
306 is $ffi->sizeof('uint16*'), $pointer_size, "sizeof uint16* = $pointer_size";
307 is $ffi->sizeof('uint32*'), $pointer_size, "sizeof uint32* = $pointer_size";
308 is $ffi->sizeof('uint64*'), $pointer_size, "sizeof uint64* = $pointer_size";
309
310 is $ffi->sizeof('sint8*'), $pointer_size, "sizeof sint8* = $pointer_size";
311 is $ffi->sizeof('sint16*'), $pointer_size, "sizeof sint16* = $pointer_size";
312 is $ffi->sizeof('sint32*'), $pointer_size, "sizeof sint32* = $pointer_size";
313 is $ffi->sizeof('sint64*'), $pointer_size, "sizeof sint64* = $pointer_size";
314
315 is $ffi->sizeof('float*'), $pointer_size, "sizeof float* = $pointer_size";
316 is $ffi->sizeof('double*'), $pointer_size, "sizeof double* = $pointer_size";
317 is $ffi->sizeof('opaque*'), $pointer_size, "sizeof opaque* = $pointer_size";
318
319 is $ffi->sizeof('string'), $pointer_size, "sizeof string = $pointer_size";
320 is $ffi->sizeof('(int)->int'), $pointer_size, "sizeof (int)->int = $pointer_size";
321 };
322
323 subtest arrays => sub {
324 foreach my $type (qw( uint8 uint16 uint32 uint64 sint8 sint16 sint32 sint64 float double opaque ))
325 {
326 my $unit_size = $ffi->sizeof($type);
327 foreach my $size (1..10)
328 {
329 is $ffi->sizeof("$type [$size]"), $unit_size*$size, "sizeof $type [32] = @{[$unit_size*$size]}";
330 }
331 }
332
333 };
334
335 subtest custom_type => sub {
336
337 foreach my $type (qw( uint8 uint16 uint32 uint64 sint8 sint16 sint32 sint64 float double opaque ))
338 {
339 my $expected = $ffi->sizeof($type);
340 $ffi->custom_type( "my_$type" => { native_type => $type, native_to_perl => sub {} } );
341 is $ffi->sizeof("my_$type"), $expected, "sizeof my_$type = $expected";
342 }
343 };
344 };
345
346 subtest 'type' => sub {
347 subtest 'simple type' => sub {
348 my $ffi = FFI::Platypus->new;
349 eval { $ffi->type('sint8') };
350 is $@, '', 'ffi.type(sint8)';
351 };
352
353 subtest 'aliased type' => sub {
354 my $ffi = FFI::Platypus->new;
355 eval { $ffi->type('sint8', 'my_integer_8') };
356 is $@, '', 'ffi.type(sint8 => my_integer_8)';
357
358 isa_ok $ffi->{tp}->types->{my_integer_8}, 'FFI::Platypus::Type';
359
360 ok scalar(grep { $_ eq 'my_integer_8' } $ffi->types), 'ffi.types returns my_integer_8';
361 };
362
363 my @list = grep { FFI::Platypus::TypeParser->new->have_type($_) } qw( sint8 uint8 sint16 uint16 sint32 uint32 sint64 uint64 float double opaque string longdouble complex_float complex_double );
364
365 subtest 'ffi basic types' => sub {
366 foreach my $name (@list)
367 {
368 subtest $name => sub {
369 my $ffi = FFI::Platypus->new;
370 eval { $ffi->type($name) };
371 is $@, '', "ffi.type($name)";
372 my $meta = $ffi->type_meta($name);
373 note xdump( $meta);
374 cmp_ok $meta->{size}, '>', 0, "size = " . $meta->{size};
375 };
376 }
377
378 };
379
380 subtest 'ffi pointer types' => sub {
381 foreach my $name (map { "$_ *" } @list)
382 {
383 subtest $name => sub {
384 skip_all 'ME GRIMLOCK SAY STRING CAN NO BE POINTER' if $name eq 'string *';
385 my $ffi = FFI::Platypus->new;
386 eval { $ffi->type($name) };
387 is $@, '', "ffi.type($name)";
388 my $meta = $ffi->type_meta($name);
389 note xdump( $meta);
390 cmp_ok $meta->{size}, '>', 0, "size = " . $meta->{size};
391 }
392 }
393
394 };
395
396 subtest 'ffi array types' => sub {
397 my $size = 5;
398
399 foreach my $basic (@list)
400 {
401 my $name = "$basic [$size]";
402
403 subtest $name => sub {
404 skip_all 'ME GRIMLOCK SAY STRING CAN NO BE ARRAY' if $name =~ /^string \[[0-9]+\]$/; # TODO: actually this should be doable
405 my $ffi = FFI::Platypus->new;
406 eval { $ffi->type($name) };
407 is $@, '', "ffi.type($name)";
408 my $meta = $ffi->type_meta($name);
409 note xdump( $meta);
410 cmp_ok $meta->{size}, '>', 0, "size = " . $meta->{size};
411 is $meta->{element_count}, $size, "size = $size";
412 };
413
414 $size += 2;
415
416 }
417
418 };
419
420 subtest 'closure types' => sub {
421 my $ffi = FFI::Platypus->new;
422
423 $ffi->type('int[22]' => 'my_int_array');
424 $ffi->type('int' => 'myint');
425
426 $ffi->type('(int)->int' => 'foo');
427
428 is $ffi->type_meta('foo')->{type}, 'closure', '(int)->int is a legal closure type';
429 note xdump($ffi->type_meta('foo'));
430
431 SKIP: {
432 skip "arrays not currently supported as closure argument types", 1;
433 $ffi->type('(my_int_array)->myint' => 'bar');
434 is $ffi->type_meta('bar')->{type}, 'closure', '(int)->int is a legal closure type';
435 note xdump($ffi->type_meta('bar'));
436 }
437
438 eval { $ffi->type('((int)->int)->int') };
439 isnt $@, '', 'inline closure illegal';
440
441 eval { $ffi->type('(foo)->int') };
442 isnt $@, '', 'argument type closure illegal';
443
444 eval { $ffi->type('(int)->foo') };
445 isnt $@, '', 'return type closure illegal';
446
447 $ffi->type('(int,int,int,char,string,opaque)->void' => 'baz');
448 is $ffi->type_meta('baz')->{type}, 'closure', 'a more complicated closure';
449 note xdump($ffi->type_meta('baz'));
450 };
451
452 subtest 'record' => sub {
453 { package My::Record22; use constant ffi_record_size => 22 }
454 { package My::Record44; use constant _ffi_record_size => 44 }
455
456 my $ffi = FFI::Platypus->new;
457
458 $ffi->type('record(1)' => 'my_record_1');
459 note xdump($ffi->type_meta('my_record_1'));
460 $ffi->type('record (32)' => 'my_record_32');
461 note xdump($ffi->type_meta('my_record_32'));
462
463 is $ffi->type_meta('my_record_1')->{size}, 1, "sizeof my_record_1 = 1";
464 is $ffi->type_meta('my_record_32')->{size}, 32, "sizeof my_record_32 = 32";
465
466 $ffi->type('record(My::Record22)' => 'my_record_22');
467 note xdump($ffi->type_meta('my_record_22'));
468 $ffi->type('record (My::Record44)' => 'my_record_44');
469 note xdump($ffi->type_meta('my_record_44'));
470
471 is $ffi->type_meta('my_record_22')->{size}, 22, "sizeof my_record_22 = 22";
472 is $ffi->type_meta('my_record_44')->{size}, 44, "sizeof my_record_44 = 44";
473 };
474
475 subtest 'string' => sub {
476 my $ffi = FFI::Platypus->new;
477 my $ptr_size = $ffi->sizeof('opaque');
478
479 foreach my $type ('string', 'string_rw', 'string_ro', 'string rw', 'string ro')
480 {
481 subtest $type => sub {
482 my $meta = $ffi->type_meta($type);
483
484 is $meta->{size}, $ptr_size, "sizeof $type = $ptr_size";
485
486 my $access = $type =~ /rw$/ ? 'rw' : 'ro';
487
488 is $meta->{access}, $access, "access = $access";
489
490 note xdump($meta);
491 }
492 }
493
494 foreach my $type ('string (10)', 'string(10)')
495 {
496 subtest $type => sub {
497
498 my $meta = $ffi->type_meta($type);
499
500 is $meta->{type}, 'record', 'is actually a record type';
501 is $meta->{size}, 10, "sizeof $type = 10";
502
503 note xdump($meta);
504
505 };
506 }
507 };
508
509 subtest 'private' => sub {
510 # this tests the private OO type API used only internally
511 # to FFI::Platypus. DO NOT USE FFI::Platypus::Type
512 # its interface can and WILL change.
513
514 my @names = qw(
515 void
516 uint8
517 sint8
518 uint16
519 sint16
520 uint32
521 sint32
522 uint64
523 sint64
524 float
525 double
526 longdouble
527 opaque
528 pointer
529 );
530
531 foreach my $name (@names)
532 {
533 subtest $name => sub {
534 skip_all 'test requires longdouble support'
535 unless FFI::Platypus::TypeParser->new->have_type($name);
536 my $type = eval { FFI::Platypus::TypeParser::Version0->new->parse($name) };
537 is $@, '', "type = FFI::Platypus::TypeParser::Version0->new->parse($name)";
538 isa_ok $type, 'FFI::Platypus::Type';
539 my $expected = $name eq 'opaque' ? 'pointer' : $name;
540 is eval { $type->meta->{ffi_type} }, $expected, "type.meta.ffi_type = $expected";
541 }
542 }
543
544 subtest string => sub {
545 my $type = eval { FFI::Platypus::TypeParser::Version0->new->parse('string') };
546 is $@, '', "type = FFI::Platypus::TypeParser::Version0->new->parse(string)";
547 isa_ok $type, 'FFI::Platypus::Type';
548 is eval { $type->meta->{ffi_type} }, 'pointer', 'type.meta.ffi_type = pointer';
549 };
550 };
551
552 };
553
554 subtest 'class or instance method' => sub {
555 my @class = FFI::Platypus->types;
556 my @instance = FFI::Platypus->new->types;
557 is \@class, \@instance, 'class and instance methods are identical';
558 note "type: $_" foreach sort @class;
559 };
560
561 subtest 'cast' => sub {
562 my $ffi = FFI::Platypus->new( lib => [@lib] );
563
564 subtest 'cast from string to pointer' => sub {
565 my $string = "foobarbaz";
566 my $pointer = $ffi->cast(string => opaque => $string);
567
568 is $ffi->function(string_matches_foobarbaz => ['opaque'] => 'int')->call($pointer), 1, 'dynamic';
569
570 $ffi->attach_cast(cast1 => string => 'opaque');
571 my $pointer2 = cast1($string);
572
573 is $ffi->function(string_matches_foobarbaz => ['opaque'] => 'int')->call($pointer2), 1, 'static';
574
575 };
576
577 subtest 'cast from pointer to string' => sub {
578 my $pointer = $ffi->function(string_return_foobarbaz => [] => 'opaque')->call();
579 my $string = $ffi->cast(opaque => string => $pointer);
580
581 is $string, "foobarbaz", "dynamic";
582
583 $ffi->attach_cast(cast2 => pointer => 'string');
584 my $string2 = cast2($pointer);
585
586 is $string2, "foobarbaz", "static";
587
588 };
589
590 subtest 'cast closure to opaque' => sub {
591 my $testname = 'dynamic';
592
593 my $closure = $ffi->closure(sub { is $_[0], "testvalue", $testname });
594 my $pointer = $ffi->cast('(string)->void' => opaque => $closure);
595
596 $ffi->function(string_set_closure => ['opaque'] => 'void')->call($pointer);
597 $ffi->function(string_call_closure => ['string'] => 'void')->call("testvalue");
598
599 $ffi->function(string_set_closure => ['(string)->void'] => 'void')->call($pointer);
600 $ffi->function(string_call_closure => ['string'] => 'void')->call("testvalue");
601
602 $ffi->attach_cast('cast3', '(string)->void' => 'opaque');
603 my $pointer2 = cast3($closure);
604
605 $testname = 'static';
606 $ffi->function(string_set_closure => ['opaque'])->call($pointer2);
607 $ffi->function(string_call_closure => ['string'])->call("testvalue");
608
609 $ffi->function(string_set_closure => ['(string)->void'])->call($pointer2);
610 $ffi->function(string_call_closure => ['string'])->call("testvalue");
611 };
612
613 subtest 'attach cast with wrapper' => sub {
614 $ffi->attach_cast('cast4', 'int', 'int', sub {
615 my($xsub, $in) = @_;
616 my $out = $xsub->($in);
617 return $out + 4;
618 });
619 is(cast4(4), 8);
620 is(prototype \&cast4, '$');
621 };
622 };
623
624 subtest 'ignore_not_found' => sub {
625
626 subtest 'ignore_not_found=undef' => sub {
627 my $ffi = FFI::Platypus->new( lib => [@lib] );
628
629 my $f1 = eval { $ffi->function(f1 => [] => 'void') };
630 is $@, '', 'no exception';
631 ok ref($f1), 'returned a function';
632 note "f1 isa ", ref($f1);
633
634 my $f2 = eval { $ffi->function(bogus => [] => 'void') };
635 isnt $@, '', 'function exception';
636 note "exception=$@";
637
638 eval { $ffi->attach(bogus => [] => 'void') };
639 isnt $@, '', 'attach exception';
640 note "exception=$@";
641
642 };
643
644 subtest 'ignore_not_found=0' => sub {
645 my $ffi = FFI::Platypus->new( lib => [@lib] );
646 $ffi->ignore_not_found(0);
647
648 my $f1 = eval { $ffi->function(f1 => [] => 'void') };
649 is $@, '', 'no exception';
650 ok ref($f1), 'returned a function';
651 note "f1 isa ", ref($f1);
652
653 my $f2 = eval { $ffi->function(bogus => [] => 'void') };
654 isnt $@, '', 'function exception';
655 note "exception=$@";
656
657 eval { $ffi->attach(bogus => [] => 'void') };
658 isnt $@, '', 'attach exception';
659 note "exception=$@";
660 };
661
662 subtest 'ignore_not_found=0 (constructor)' => sub {
663 my $ffi = FFI::Platypus->new( ignore_not_found => 0, lib => [@lib] );
664
665 my $f1 = eval { $ffi->function(f1 => [] => 'void') };
666 is $@, '', 'no exception';
667 ok ref($f1), 'returned a function';
668 note "f1 isa ", ref($f1);
669
670 my $f2 = eval { $ffi->function(bogus => [] => 'void') };
671 isnt $@, '', 'function exception';
672 note "exception=$@";
673
674 eval { $ffi->attach(bogus => [] => 'void') };
675 isnt $@, '', 'attach exception';
676 note "exception=$@";
677 };
678
679 subtest 'ignore_not_found=1' => sub {
680 my $ffi = FFI::Platypus->new( lib => [@lib] );
681 $ffi->ignore_not_found(1);
682
683 my $f1 = eval { $ffi->function(f1 => [] => 'void') };
684 is $@, '', 'no exception';
685 ok ref($f1), 'returned a function';
686 note "f1 isa ", ref($f1);
687
688 my $f2 = eval { $ffi->function(bogus => [] => 'void') };
689 is $@, '', 'function no exception';
690 is $f2, undef, 'f2 is undefined';
691
692 eval { $ffi->attach(bogus => [] => 'void') };
693 is $@, '', 'attach no exception';
694
695 };
696
697 subtest 'ignore_not_found=1 (constructor)' => sub {
698 my $ffi = FFI::Platypus->new( ignore_not_found => 1, lib => [@lib] );
699
700 my $f1 = eval { $ffi->function(f1 => [] => 'void') };
701 is $@, '', 'no exception';
702 ok ref($f1), 'returned a function';
703 note "f1 isa ", ref($f1);
704
705 my $f2 = eval { $ffi->function(bogus => [] => 'void') };
706 is $@, '', 'function no exception';
707
708 is $f2, undef, 'f2 is undefined';
709 eval { $ffi->attach(bogus => [] => 'void') };
710 is $@, '', 'attach no exception';
711 };
712
713 subtest 'ignore_not_found bool context' => sub {
714 my $ffi = FFI::Platypus->new( ignore_not_found => 1, lib => [@lib] );
715
716 my $f1 = eval { $ffi->function(f1 => [] => 'void') };
717 ok $f1, 'f1 exists and resolved to boolean true';
718
719 my $f2 = eval { $ffi->function(bogus => [] => 'void') };
720 ok !$f2, 'f2 does not exist and resolved to boolean false';
721 };
722 };
723
724 subtest 'attach basic' => sub {
725
726 package
727 attach_basic;
728
729 use FFI::Platypus;
730 use Test2::V0 -no_srand => 1;
731
732 my $ffi = FFI::Platypus->new( lib => [@lib] );
733
734 $ffi->attach('f0' => ['uint8'] => 'uint8');
735 $ffi->attach([f0=>'f1'] => ['uint8'] => 'uint8');
736 $ffi->attach([f0=>'Roger::f1'] => ['uint8'] => 'uint8');
737
738 is f0(22), 22, 'f0(22) = 22';
739 is f1(22), 22, 'f1(22) = 22';
740 is Roger::f1(22), 22, 'Roger::f1(22) = 22';
741
742 $ffi->attach([f0 => 'f0_wrap'] => ['uint8'] => uint8 => sub {
743 my($inner, $value) = @_;
744
745 return $inner->($value+1)+2;
746 });
747
748 $ffi->attach([f0 => 'f0_wrap2'] => ['uint8'] => uint8 => '$' => sub {
749 my($inner, $value) = @_;
750
751 return $inner->($value+1)+2;
752 });
753
754 is f0_wrap(22), 25, 'f0_wrap(22) = 25';
755 is f0_wrap2(22), 25, 'f0_wrap(22) = 25';
756 };
757
758 subtest 'attach void' => sub {
759
760 package
761 attach_void;
762
763 use FFI::Platypus;
764 use Test2::V0 -no_srand => 1;
765
766 my $ffi = FFI::Platypus->new( lib => [@lib] );
767
768 $ffi->attach('f2' => ['int*'] => 'void');
769 $ffi->attach([f2=>'f2_implicit'] => ['int*']);
770
771 my $i_ptr = 42;
772
773 f2(\$i_ptr);
774 is $i_ptr, 43, '$i_ptr = 43 after f2(\$i_ptr)';
775
776 f2_implicit(\$i_ptr);
777 is $i_ptr, 44, '$i_ptr = 44 after f2_implicit(\$i_ptr)';
778
779 };
780
781 subtest 'customer mangler' => sub {
782
783 my $ffi = FFI::Platypus->new( lib => [@lib] );
784 $ffi->mangler( sub { "mystrangeprefix_$_[0]" });
785 is($ffi->function(bar => [] => 'int')->call, 42 );
786 };
787
788 subtest '->package is only allowed for api = 0' => sub {
789
790 my @warnings;
791 local $SIG{__WARN__} = sub {
792 note "[warning]\n", $_[0];
793 push @warnings, $_[0];
794 };
795
796 subtest 'api = 0' => sub {
797 my $ffi = FFI::Platypus->new( api => 0 );
798 local $@ = '';
799 eval { $ffi->package };
800 is "$@", "";
801 };
802
803 subtest 'api = 1' => sub {
804 my $ffi = FFI::Platypus->new( api => 1 );
805 local $@ = '';
806 eval { $ffi->package };
807 like "$@", qr/^package method only available with api => 0/;
808 };
809
810 };
811
812 subtest 'warning defaults' => sub {
813
814 my @warnings;
815 local $SIG{__WARN__} = sub {
816 note "[warning]\n", $_[0];
817 push @warnings, $_[0] if $_[0] =~ /^warning: error loading/;
818 };
819
820 subtest 'api = 0' => sub {
821 @warnings = ();
822 my $ffi = FFI::Platypus->new( api => 0, lib => ['corpus/bogus.so'] );
823 is $ffi->find_symbol('foo'), undef;
824 is \@warnings, [];
825 };
826
827 subtest 'api = 1' => sub {
828 @warnings = ();
829 my $ffi = FFI::Platypus->new( api => 1, lib => ['corpus/bogus.so'] );
830 local $@ = '';
831 is $ffi->find_symbol('foo'), undef;
832 like $warnings[0], qr/^warning: error loading corpus\/bogus\.so/;
833 };
834
835 };
836
837 subtest 'language plugin api version' => sub {
838
839 my %args;
840
841 my $native_type_map = sub
842 {
843 my $class = shift;
844 %args = @_;
845 {};
846 };
847
848 {
849 package FFI::Platypus::Lang::Frooble;
850 no warnings 'once';
851 *native_type_map = $native_type_map;
852 }
853
854 subtest 'api = 0' => sub {
855 my $ffi = FFI::Platypus->new( lang => 'Frooble' );
856 is $args{api}, undef;
857 };
858
859 subtest 'api = 1' => sub {
860 my $ffi = FFI::Platypus->new( lang => 'Frooble', api => 1 );
861 is $args{api}, 1;
862 };
863
864 };
865
866 subtest 'language load_custom_types hook' => sub {
867
868 my @args;
869
870 {
871 package FFI::Platypus::Lang::Frooble2;
872 sub native_type_map {}
873 no warnings 'once';
874 *load_custom_types = sub {
875 @args = @_;
876 };
877 }
878
879 FFI::Platypus->new( lang => 'Frooble2', api => 1 );
880
881 is($args[0], 'FFI::Platypus::Lang::Frooble2');
882 isa_ok $args[1], 'FFI::Platypus';
883
884 };
885
886 subtest 'api attribute' => sub {
887
888 is(
889 FFI::Platypus->new->api,
890 0,
891 'default is zero',
892 );
893
894 is(
895 FFI::Platypus->new( api => 0 )->api,
896 0,
897 'explicit zero',
898 );
899
900 is(
901 FFI::Platypus->new( api => 1 )->api,
902 1,
903 'explicit one',
904 );
905
906 };
907
908 subtest 'kindof' => sub {
909 is(
910 FFI::Platypus->kindof('void'),
911 'void',
912 'void',
913 );
914 is(
915 FFI::Platypus->kindof('sint8'),
916 'scalar',
917 'scalar',
918 );
919 is(
920 FFI::Platypus->kindof('sint8*'),
921 'pointer',
922 'pointer',
923 );
924 is(
925 FFI::Platypus->kindof('sint8[10]'),
926 'array',
927 'array (fixed)',
928 );
929 is(
930 FFI::Platypus->kindof('sint8[]'),
931 'array',
932 'array (var)',
933 );
934 is(
935 FFI::Platypus->kindof('string'),
936 'string',
937 'string',
938 );
939 };
940
941 subtest 'countof' => sub {
942 is(
943 FFI::Platypus->countof('void'),
944 0,
945 'void',
946 );
947 is(
948 FFI::Platypus->countof('sint8'),
949 1,
950 'scalar',
951 );
952 is(
953 FFI::Platypus->countof('sint8*'),
954 1,
955 'pointer',
956 );
957 is(
958 FFI::Platypus->countof('sint8[10]'),
959 10,
960 'array (fixed)',
961 );
962 is(
963 FFI::Platypus->countof('sint8[]'),
964 0,
965 'array (var)',
966 );
967 is(
968 FFI::Platypus->countof('string'),
969 1,
970 'string',
971 );
972 };
973
974 subtest 'def' => sub {
975 my $ffi = FFI::Platypus->new( api => 1 );
976
977 subtest 'needs to be a real type' => sub {
978 local $@;
979 eval { $ffi->def(undef, '[] illegal (', 'value') };
980 like $@, qr/bad type name/;
981 };
982
983 is
984 $ffi->def(undef, '[] illegal (',),
985 undef,
986 ;
987
988 is
989 $ffi->def(undef, 'int', 'roger'),
990 'roger',
991 ;
992
993 is
994 $ffi->def('main', 'int'),
995 'roger',
996 ;
997
998 is
999 $ffi->def(undef, 'int'),
1000 'roger',
1001 ;
1002
1003 is
1004 $ffi->def('foo', 'int'),
1005 undef,
1006 ;
1007
1008 is
1009 $ffi->def('foo', 'int', 'prime'),
1010 'prime',
1011 ;
1012
1013 is
1014 $ffi->def('foo', 'int'),
1015 'prime',
1016 ;
1017
1018 is
1019 $ffi->def('foo', 'int', undef),
1020 undef,
1021 ;
1022
1023 is
1024 $ffi->def('foo', 'int'),
1025 undef,
1026 ;
1027 };
1028
1029 subtest 'unitof' => sub {
1030 is(
1031 FFI::Platypus->unitof('void'),
1032 undef,
1033 'void',
1034 );
1035 is(
1036 FFI::Platypus->unitof('sint8'),
1037 undef,
1038 'scalar',
1039 );
1040 is(
1041 FFI::Platypus->unitof('sint8*'),
1042 'sint8',
1043 'pointer',
1044 );
1045 is(
1046 FFI::Platypus->unitof('sint8[10]'),
1047 'sint8',
1048 'array (fixed)',
1049 );
1050 is(
1051 FFI::Platypus->unitof('sint8[]'),
1052 'sint8',
1053 'array (var)',
1054 );
1055 is(
1056 FFI::Platypus->unitof('string'),
1057 undef,
1058 'string',
1059 );
1060 };
1061
1062 subtest 'pass undef into closure method should just return undef' => sub {
1063
1064 my $ret = eval { FFI::Platypus->closure(undef) };
1065 is "$@", '', 'no error';
1066 is $ret, U(), 'returns undef';
1067
1068 };
1069
1070 done_testing;