Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Typed exceptions from PRE/POST failures
jnthn++ for the help
  • Loading branch information
moritz committed Mar 18, 2012
1 parent 5264177 commit d1505ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Perl6/World.pm
Expand Up @@ -1254,14 +1254,26 @@ class Perl6::World is HLL::World {
PAST::Var.new( :name($sym), :scope('lexical_6model') ));
}
elsif $phaser eq 'PRE' || $phaser eq 'POST' {
my $what := self.add_string_constant(
$phaser eq 'PRE' ?? 'Precondition' !! 'Postcondition'
);
$what.named('what');
my $condition := self.add_string_constant(~$/<blorst>);
$condition.named('condition');

$phaser_past[1] := PAST::Op.new(
:pasttype('unless'),
$phaser_past[1],
PAST::Op.new(
:pasttype('call'), :name('&die'),
self.add_string_constant($phaser eq 'PRE' ??
'Precondition failed' !!
'Postcondition failed')));
:pasttype('callmethod'), :name('throw'),
PAST::Op.new(
:pasttype('callmethod'), :name('new'),
self.get_ref(self.find_symbol(['X', 'Phaser', 'PrePost'])),
$what,
$condition,
)
),
);
@!CODES[+@!CODES - 1].add_phaser($phaser, $block);
return PAST::Var.new(:name('Nil'), :scope('lexical_6model'));
}
Expand Down
10 changes: 10 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -497,4 +497,14 @@ my class X::Package::Stubbed does X::Comp {
}
}

my class X::Phaser::PrePost is Exception {
has $.what = 'Precondition';
has $.condition;
method message {
$.condition.defined
?? "$.what '$.condition' failed"
!! "$.what failed";
}
}

# vim: ft=perl6

0 comments on commit d1505ae

Please sign in to comment.