| 1 |
package Test::FauxAttach; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test2::V0 (); |
| 6 |
|
| 7 |
my @funcs; |
| 8 |
|
| 9 |
# This plugin implements an alternative to attach/sub_ref |
| 10 |
# without the process level-leak which makes it easier to |
| 11 |
# find real leaks. It relies on no attached sub being called |
| 12 |
# in END, etc. blocks, which we cannot normally rely on. |
| 13 |
# it's also probably a lot slower than a real xsub. |
| 14 |
|
| 15 |
sub import |
| 16 |
{ |
| 17 |
die "load Test::FauxAttach before FFI::Platypus::Function" |
| 18 |
if $INC{'FFI/Platypus/Function.pm'}; |
| 19 |
require FFI::Platypus::Function; |
| 20 |
|
| 21 |
no warnings 'redefine'; |
| 22 |
*FFI::Platypus::Function::Function::_sub_ref = sub { |
| 23 |
my($self, $location) = @_; |
| 24 |
push @funcs, $self; |
| 25 |
my $i = $#funcs; |
| 26 |
sub { $funcs[$i]->call(@_) }; |
| 27 |
}; |
| 28 |
|
| 29 |
*FFI::Platypus::Function::Function::_attach = sub { |
| 30 |
my($self, $perl_name, $location, $proto) = @_; |
| 31 |
Test2::V0::note(" attaching: $perl_name"); |
| 32 |
my $xsub = $self->_sub_ref($location); |
| 33 |
FFI::Platypus::Function::Wrapper::_set_prototype($proto, $xsub) if defined $proto; |
| 34 |
no strict 'refs'; |
| 35 |
*{"$perl_name"} = $xsub; |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
END { |
| 40 |
Test2::V0::note("deleting @{[ scalar @funcs ]} attached functions"); |
| 41 |
@funcs = (); |
| 42 |
} |
| 43 |
|
| 44 |
1; |