Skip to content

Commit

Permalink
RakuAST: Have try fatalize a returned Failure
Browse files Browse the repository at this point in the history
The try statement prefix is supposed to fatalize returned Failures, i.e.
have them be thrown as exceptions, so they will be caught by the
generated CATCH block and stored in $!.
  • Loading branch information
niner committed Nov 3, 2022
1 parent 7542441 commit 4519b9d
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Raku/ast/statementprefixes.rakumod
Expand Up @@ -115,7 +115,8 @@ class RakuAST::StatementPrefix::Try is RakuAST::StatementPrefix is RakuAST::Sink
method PRODUCE-IMPLICIT-LOOKUPS() {
self.IMPL-WRAP-LIST([
RakuAST::Type::Setting.new(RakuAST::Name.from-identifier('Nil')),
RakuAST::Var::Lexical.new('$!')
RakuAST::Var::Lexical.new('$!'),
RakuAST::Type::Setting.new(RakuAST::Name.from-identifier('Failure')),
])
}

Expand All @@ -131,20 +132,41 @@ class RakuAST::StatementPrefix::Try is RakuAST::StatementPrefix is RakuAST::Sink
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups);
my $nil := @lookups[0].IMPL-TO-QAST($context);
my $bang := @lookups[1].IMPL-TO-QAST($context);
my $Failure := @lookups[2].IMPL-TO-QAST($context);
my $tmp := QAST::Node.unique('fatalizee');
my $qast := self.IMPL-CALLISH-QAST($context);
QAST::Op.new(
:op('handle'),

# Success path puts Nil into $! and evaluates to the block.
QAST::Stmt.new(
:resultchild(0),
self.IMPL-CALLISH-QAST($context),
QAST::Op.new( :op('p6assign'), $bang, $nil )
QAST::Stmts.new(
:resultchild(0),
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name($tmp), :scope('local'), :decl('var') ),
$qast
),
QAST::Op.new(
:op('if'),
QAST::Op.new(
:op('istype'),
QAST::Var.new( :name($tmp), :scope('local') ),
$Failure,
),
QAST::Op.new(
:op('callmethod'), :name('sink'),
QAST::Var.new( :name($tmp), :scope('local') )
)
)),
QAST::Op.new( :op('p6store'), $bang, $nil )
),

# On failure, capture the exception object into $!.
'CATCH', QAST::Stmts.new(
QAST::Op.new(
:op('p6assign'),
:op('p6store'),
$bang,
QAST::Op.new(
:name<&EXCEPTION>, :op<call>,
Expand Down

0 comments on commit 4519b9d

Please sign in to comment.