Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation. Leak reporting output isn't very helpful just…
… yet.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package CatalystX::LeakChecker; | ||
|
||
use Moose::Role; | ||
use PadWalker 'closed_over'; | ||
use Scalar::Util 'weaken', 'isweak';; | ||
use aliased 'Data::Visitor::Callback', 'Visitor'; | ||
|
||
use namespace::autoclean -also => [qw/Visitor visit_code/]; | ||
|
||
sub visit_code { | ||
my ($self, $code, $weak_ctx, $leaks) = @_; | ||
my $vars = closed_over $code; | ||
|
||
while (my ($name, $val) = each %{ $vars }) { | ||
next unless $weak_ctx == ${ $val }; | ||
next if isweak ${ $val }; | ||
push @{ $leaks }, $name; | ||
} | ||
|
||
return $code; | ||
} | ||
|
||
sub format_table { | ||
my @leaks = @_; | ||
|
||
my $t = Text::SimpleTable->new([ 70, 'Variable' ]); | ||
$t->row($_) for @leaks; | ||
return $t->draw; | ||
} | ||
|
||
after finalize => sub { | ||
my ($ctx) = @_; | ||
my @leaks; | ||
|
||
my $weak_ctx = $ctx; | ||
weaken $weak_ctx; | ||
|
||
my $visitor = Visitor->new( | ||
code => sub { visit_code(@_, $weak_ctx, \@leaks) }, | ||
); | ||
$visitor->visit($ctx->stash); | ||
return unless @leaks; | ||
|
||
my $msg = "Leaked context:\n" . format_table(@leaks); | ||
$ctx->log->debug($msg) if $ctx->debug; | ||
}; | ||
|
||
1; |