| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus::Closure; |
| 3 |
use FFI::CheckLib; |
| 4 |
|
| 5 |
my $libtest = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'; |
| 6 |
|
| 7 |
subtest 'basic' => sub { |
| 8 |
my $ffi = FFI::Platypus->new; |
| 9 |
|
| 10 |
my $closure = $ffi->closure(sub { $_[0] + 1}); |
| 11 |
isa_ok $closure, 'FFI::Platypus::Closure'; |
| 12 |
is $closure->(1), 2, 'closure.(1) = 2'; |
| 13 |
|
| 14 |
my $c = sub { $_[0] + 2 }; |
| 15 |
$closure = $ffi->closure($c); |
| 16 |
isa_ok $closure, 'FFI::Platypus::Closure'; |
| 17 |
is $closure->(1), 3, 'closure.(1) = 3'; |
| 18 |
is $closure->call(1), 3, 'closure.call(1) = 3'; |
| 19 |
|
| 20 |
$closure = $ffi->closure($c); |
| 21 |
isa_ok $closure, 'FFI::Platypus::Closure'; |
| 22 |
is $closure->(1), 3, 'closure.(1) = 3'; |
| 23 |
is $closure->call(1), 3, 'closure.call(1) = 3'; |
| 24 |
}; |
| 25 |
|
| 26 |
subtest 'sticky' => sub { |
| 27 |
my $closure = FFI::Platypus::Closure->new(sub { 'foo' }); |
| 28 |
isa_ok $closure, 'FFI::Platypus::Closure'; |
| 29 |
|
| 30 |
my $refcnt = $closure->_svrefcnt; |
| 31 |
note "_svrefcnt = $refcnt"; |
| 32 |
|
| 33 |
eval { $closure->sticky }; |
| 34 |
is $@, '', 'called $closure->sticky'; |
| 35 |
|
| 36 |
is($closure->_svrefcnt, $refcnt+2); |
| 37 |
|
| 38 |
eval { $closure->sticky }; |
| 39 |
is $@, '', 'called $closure->sticky'; |
| 40 |
|
| 41 |
is($closure->_svrefcnt, $refcnt+2); |
| 42 |
|
| 43 |
eval { $closure->unstick }; |
| 44 |
is $@, '', 'called $closure->unstick'; |
| 45 |
|
| 46 |
is($closure->_svrefcnt, $refcnt); |
| 47 |
}; |
| 48 |
|
| 49 |
subtest 'sticky and unstick return $self' => sub { |
| 50 |
my $closure = FFI::Platypus::Closure->new(sub { 'foo' }); |
| 51 |
isa_ok $closure, 'FFI::Platypus::Closure'; |
| 52 |
|
| 53 |
is($closure->sticky, $closure, '$closure->sticky returns $closure'); |
| 54 |
is($closure->unstick, $closure, '$closure->unstick returns $closure'); |
| 55 |
|
| 56 |
is( |
| 57 |
$closure->sticky->unstick, $closure, |
| 58 |
'$closure->sticky->unstick can be chained', |
| 59 |
); |
| 60 |
}; |
| 61 |
|
| 62 |
subtest 'private' => sub { |
| 63 |
my $closure = FFI::Platypus::Closure->new(sub { $_[0] + 1}); |
| 64 |
isa_ok $closure, 'FFI::Platypus::Closure'; |
| 65 |
is $closure->(1), 2, 'closure.(1) = 2'; |
| 66 |
}; |
| 67 |
|
| 68 |
subtest 'space' => sub { |
| 69 |
my $ffi = FFI::Platypus->new; |
| 70 |
|
| 71 |
eval { $ffi->type('(int,int)->void') }; |
| 72 |
is $@, '', 'good without space'; |
| 73 |
|
| 74 |
eval { $ffi->type('(int, int) -> void') }; |
| 75 |
is $@, '', 'good with space'; |
| 76 |
}; |
| 77 |
|
| 78 |
subtest 'die' => sub { |
| 79 |
my $ffi = FFI::Platypus->new; |
| 80 |
$ffi->lib($libtest); |
| 81 |
|
| 82 |
my $closure = $ffi->closure(sub { |
| 83 |
die "omg i don't want to die!"; |
| 84 |
}); |
| 85 |
|
| 86 |
my $set_closure = $ffi->function(pointer_set_closure => ['(opaque)->opaque'] => 'void'); |
| 87 |
my $call_closure = $ffi->function(pointer_call_closure => ['opaque'] => 'opaque'); |
| 88 |
|
| 89 |
$set_closure->($closure); |
| 90 |
|
| 91 |
my $warning; |
| 92 |
do { |
| 93 |
local $SIG{__WARN__} = sub { $warning = $_[0] }; |
| 94 |
$call_closure->(undef); |
| 95 |
}; |
| 96 |
|
| 97 |
like $warning, qr{omg i don't want to die}; |
| 98 |
pass 'does not exit'; |
| 99 |
note "warning = '$warning'"; |
| 100 |
}; |
| 101 |
|
| 102 |
subtest 'reuse' => sub { |
| 103 |
my $ffi = FFI::Platypus->new; |
| 104 |
$ffi->lib($libtest); |
| 105 |
|
| 106 |
my $closure = $ffi->closure(sub { |
| 107 |
if (@_) { |
| 108 |
return $_[0] * 7; |
| 109 |
} |
| 110 |
return 21; |
| 111 |
}); |
| 112 |
|
| 113 |
my $set_closure1 = $ffi->function( closure_set_closure1 => ['()->int'] => 'void'); |
| 114 |
my $set_closure2 = $ffi->function( closure_set_closure2 => ['(int)->int'] => 'void'); |
| 115 |
my $call_closure1 = $ffi->function( closure_call_closure1 => [] => 'int'); |
| 116 |
my $call_closure2 = $ffi->function( closure_call_closure2 => ['int'] => 'int'); |
| 117 |
|
| 118 |
$set_closure1->($closure); |
| 119 |
$set_closure2->($closure); |
| 120 |
|
| 121 |
is $call_closure1->(), 21; |
| 122 |
is $call_closure2->(42), 294; |
| 123 |
}; |
| 124 |
|
| 125 |
subtest 'immediate' => sub { |
| 126 |
my $ffi = FFI::Platypus->new; |
| 127 |
$ffi->lib($libtest); |
| 128 |
|
| 129 |
my $ret = $ffi->function( closure_call_closure_immediate => ['()->int'] => 'int')->call( |
| 130 |
$ffi->closure(sub { return 42; }) |
| 131 |
); |
| 132 |
|
| 133 |
is $ret, 42; |
| 134 |
}; |
| 135 |
|
| 136 |
subtest 'closure passing into a closure' => sub { |
| 137 |
|
| 138 |
my $ffi = FFI::Platypus->new; |
| 139 |
eval { $ffi->type('((int)->int)->int') }; |
| 140 |
isnt "$@", ""; |
| 141 |
note "error = $@"; |
| 142 |
|
| 143 |
$ffi->type('(int)->int' => 'foo_t'); |
| 144 |
eval { $ffi->type('()->foo_t') }; |
| 145 |
isnt "$@", ""; |
| 146 |
note "error = $@"; |
| 147 |
|
| 148 |
}; |
| 149 |
|
| 150 |
done_testing; |