Created
February 11, 2012 15:28
-
-
Save moritz/1800832 to your computer and use it in GitHub Desktop.
Backtrace 2.0 in Rakudo
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
sub f() { | |
for 1..5 { | |
die 'foo'; | |
} | |
} | |
try f; | |
use MONKEY_TYPING; | |
augment class Backtrace { | |
method next-interesting-index(Int $idx is copy = 0) { | |
++$idx; | |
loop (; $idx < $.end; ++$idx) { | |
my $cand = $.at_pos($idx); | |
return $idx unless $cand.is-hidden || $cand.is-setting; | |
} | |
Int; | |
} | |
method outer-caller-idx(Int $startidx) { | |
my %outers; | |
my $start = $.at_pos($startidx).code; | |
my $current = $start.outer; | |
while $current { | |
%outers{$current.static_id} = $start; | |
$current = $current.outer; | |
} | |
($startidx + 1 .. $.end).grep({$.at_pos($_).code && %outers{$.at_pos($_).code.static_id}}); | |
} | |
method nice() { | |
my Int $i = self.next-interesting-index(-1); | |
my @frames; | |
while $i.defined { | |
my $prev = self.at_pos($i); | |
my @outer_callers := self.outer-caller-idx($i); | |
my ($target_idx) = @outer_callers.keys.grep({self.at_pos($i).code.^isa(Routine)}); | |
$target_idx //= @outer_callers[0]; | |
my $current = self.at_pos($target_idx); | |
@frames.push: $current.clone(line => $prev.line); | |
$i = self.next-interesting-index($target_idx); | |
} | |
@frames.join; | |
} | |
} | |
say $!.message; | |
print $!.backtrace.nice; |
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
foo | |
in sub f at nice-bt.pl:3 | |
in block <anon> at nice-bt.pl:7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment