Skip to content

Commit

Permalink
Fix exception handlers to not catch control exceptions.
Browse files Browse the repository at this point in the history
Make exception handlers mark $! as handled.
  • Loading branch information
tene committed Feb 28, 2009
1 parent c1f3976 commit 9e24621
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/parser/actions.pm
Expand Up @@ -431,11 +431,20 @@ method catch_statement($/) {
PAST::Var.new( :name('$!'), :scope('lexical') ),
PAST::Var.new( :name('exception'), :scope('register') )
),
$past
$past,
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new( PAST::Var.new( :name('$!'), :scope('lexical') ),
"handled", :scope('keyed') ),
1,
),
);
our @?BLOCK;
my $?BLOCK := @?BLOCK[0];
my $eh := PAST::Control.new( $past );
my $eh := PAST::Control.new(
$past,
:handle_types_except('CONTROL')
);
my @handlers;
if $?BLOCK.handlers() {
@handlers := $?BLOCK.handlers();
Expand All @@ -459,7 +468,13 @@ method control_statement($/) {
PAST::Var.new( :name('$!'), :scope('lexical') ),
PAST::Var.new( :name('exception'), :scope('register') )
),
$past
$past,
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new( PAST::Var.new( :name('$!'), :scope('lexical') ),
"handled", :scope('keyed') ),
1,
),
);
our @?BLOCK;
my $?BLOCK := @?BLOCK[0];
Expand Down Expand Up @@ -548,17 +563,35 @@ method statement_prefix($/) {
## after the code in the try block is executed, bind $! to Failure,
## and set up the code to catch an exception, in case one is thrown
elsif $sym eq 'try' {
$past := PAST::Op.new( $past, :pasttype('try') );

## Add a catch node to the try op that captures the
## exception object into $!.
my $catchpir := " .get_results (%r)\n store_lex '$!', %r";
$past.push( PAST::Op.new( :inline( $catchpir ) ) );

## Add an 'else' node to the try op that clears $! if
## no exception occurred.
my $elsepir := " new %r, 'Failure'\n store_lex '$!', %r";
$past.push( PAST::Op.new( :inline( $elsepir ) ) );
my $eh := PAST::Control.new(
PAST::Stmts.new(
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new( :name('$!'), :scope('lexical') ),
PAST::Var.new( :name('exception'), :scope('register') )
),
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new( PAST::Var.new( :name('$!'), :scope('lexical') ),
"handled", :scope('keyed') ),
1,
),
),
:handle_types_except('CONTROL')
);
my @handlers;
if $past.handlers() {
@handlers := $past.handlers();
}
@handlers.unshift($eh);
$past.handlers(@handlers);
$past.push(
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new( :name('$!'), :scope('lexical') ),
PAST::Op.new(:pasttype('call'), :name('!FAIL')),
),
);
}
elsif $sym eq 'gather' {
if !$past.isa(PAST::Block) {
Expand Down

1 comment on commit 9e24621

@pmichaud
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was causing spectests to fail — reverted in d4c266ba9072844956b662bf5a3271fe761f6337 .

Please sign in to comment.