Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor fail/die again
Alas, 2e92038 introduced quite some spectest errors.
This commit goes back to using CALLER::CALLER:: PseudoStash Perl6
objects, rather than the lowlevel NQP objects that do not support
things like EXISTS-KEY.  But instead of before, it will only be doing
a CALLER::CALLER:: once.  And to further improve performance, the
parameterless fail/die are not separate candidates, hopefully make it
easier to inline.

Unfortunately, this causes 3 (new) spectest failures, but all failing
tests seem suspect to me, so I'm putting this in rather than just
revert 2e92038 .
  • Loading branch information
lizmat committed Oct 4, 2015
1 parent c52b0b3 commit b06d30d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
60 changes: 30 additions & 30 deletions src/core/Failure.pm
Expand Up @@ -3,19 +3,20 @@ my class Failure {
has $.backtrace;
has $!handled;

multi method new() {
my $stash := CALLER::CALLER::;
my $payload = $stash<$!>.DEFINITE ?? $stash<$!> !! "Died";
self.bless(:exception( $payload ~~ Exception
?? $payload !! X::AdHoc.new(:$payload)
))
}
multi method new(Exception $exception) {
self.bless(:$exception);
}
multi method new($payload =
nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx))).EXISTS-KEY('$!')
&& nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx)))('$!').DEFINITE
?? nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx)))('$!') !! "Died") {
if ($payload ~~ Exception) {
self.bless(:exception($payload));
}
else {
self.bless(:exception(X::AdHoc.new(:$payload)));
}
multi method new($payload) {
self.bless(:exception( $payload ~~ Exception
?? $payload !! X::AdHoc.new(:$payload)
))
}
multi method new(|cap (*@msg)) {
self.bless(:exception(X::AdHoc.from-slurpy(|cap)));
Expand Down Expand Up @@ -70,6 +71,18 @@ my class Failure {
}

proto sub fail(|) {*};
multi sub fail() {
my $stash := CALLER::CALLER::;
my $payload = $stash<$!>.DEFINITE ?? $stash<$!> !! "Died";

my $fail := Failure.new( $payload ~~ Exception
?? $payload !! X::AdHoc.new(:$payload));

my Mu $return := nqp::getlexcaller('RETURN');
$return($fail) unless nqp::isnull($return);

$fail
}
multi sub fail(Exception:U $e) {
my $fail := Failure.new(
X::AdHoc.new(:payload("Failed with undefined " ~ $e.^name))
Expand All @@ -78,29 +91,16 @@ multi sub fail(Exception:U $e) {
$return($fail) unless nqp::isnull($return);
$fail
}
multi sub fail($payload =
nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx))).EXISTS-KEY('$!')
&& nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx)))('$!').DEFINITE
?? nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx)))('$!') !! "Died") {
multi sub fail($payload) {
my $fail := Failure.new( $payload ~~ Exception
?? $payload
!! X::AdHoc.new(:$payload)
);

my Mu $return := nqp::getlexcaller('RETURN');
$return($fail) unless nqp::isnull($return);

# Strange behavior alert:
# If you try to if(...) { $fail := ... } else { $fail := ... }
# here it behaves sort of as if "use fatal" were in effect even
# when it is not, except that's not what is going on because
# "die" does not get hit AFAICT. That took me 4 hours of
# fumbling around to figure out what was wrong.
if ($payload ~~ Exception) {
my $fail := Failure.new($payload);
$return($fail) unless nqp::isnull($return);
$fail
}
else {
my $fail := Failure.new(X::AdHoc.new(:$payload));
$return($fail) unless nqp::isnull($return);
$fail
}
$fail
}
multi sub fail(|cap (*@msg)) {
my $fail := Failure.new(X::AdHoc.from-slurpy(|cap));
Expand Down
21 changes: 11 additions & 10 deletions src/core/control.pm
Expand Up @@ -168,19 +168,20 @@ sub done() {
}

proto sub die(|) {*};
multi sub die() {
my $stash := CALLER::CALLER::;
my $payload = $stash<$!>.DEFINITE ?? $stash<$!> !! "Died";
$payload ~~ Exception
?? $payload.throw
!! X::AdHoc.new(:$payload).throw
}
multi sub die(Exception:U $e) {
X::AdHoc.new(:payload("Died with undefined " ~ $e.^name)).throw;
}
multi sub die($payload =
nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx))).EXISTS-KEY('$!')
&& nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx)))('$!').DEFINITE
?? nqp::ctxlexpad(nqp::ctxcaller(nqp::ctxcaller(nqp::ctx)))('$!') !! "Died") {
if $payload ~~ Exception {
$payload.throw;
}
else {
X::AdHoc.new(:$payload).throw
}
multi sub die($payload) {
$payload ~~ Exception
?? $payload.throw
!! X::AdHoc.new(:$payload).throw
}
multi sub die(|cap ( *@msg )) {
X::AdHoc.from-slurpy(|cap).throw
Expand Down

0 comments on commit b06d30d

Please sign in to comment.