/
Failure.pm
56 lines (47 loc) · 1.53 KB
/
Failure.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
my class Failure {
has $!exception;
has $!handled;
method new($ex) {
my $new = self.CREATE;
$new.BUILD($ex);
}
method BUILD($ex) {
$!exception = $ex;
self;
}
# TODO: should be Failure:D: multi just like method Bool,
# but obscure problems prevent us from making Mu.defined
# a multi. See http://irclog.perlgeek.de/perl6/2011-06-28#i_4016747
method defined() {
$!handled =1 if pir::repr_defined__IP(self);
Bool::False;
}
multi method Bool(Failure:D:) { $!handled = 1; Bool::False; }
method Int(Failure:D:) { $!handled ?? 0 !! $!exception.rethrow; }
method Num(Failure:D:) { $!handled ?? 0e0 !! $!exception.rethrow; }
multi method Str(Failure:D:) { $!handled ?? '' !! $!exception.rethrow; }
Failure.^add_fallback(
-> $, $ { True },
method ($name) {
die $!exception;
}
);
}
my &fail := -> *@msg {
my $value = @msg.join('');
my Mu $ex := Q:PIR {
# throw and immediately catch an exception, to capture
# the location at the point of the fail()
push_eh catch
$P0 = find_lex '$value'
$S0 = $P0
die $S0
catch:
.get_results (%r)
pop_eh
};
my $fail := Failure.new(EXCEPTION($ex));
my Mu $return := pir::find_caller_lex__Ps('RETURN');
$return($fail) unless nqp::isnull($return);
$fail
}