Skip to content

Commit

Permalink
Throw if keep/break is invoked on a vow more than once
Browse files Browse the repository at this point in the history
This should resolve /issues/3137
  • Loading branch information
dumarchie committed Sep 4, 2019
1 parent 9d1505d commit c7ec96a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core.c/Promise.pm6
Expand Up @@ -15,6 +15,12 @@ my class X::Promise::Vowed is Exception {
has $.promise;
method message() { "Access denied to keep/break this Promise; already vowed" }
}
my class X::Promise::IllegalTransition is Exception {
has $.promise;
method message() {
"Illegal attempt to keep/break this Promise (status: $!promise.status())";
}
}
my role X::Promise::Broken {
has $.result-backtrace;
multi method gist(::?CLASS:D:) {
Expand Down Expand Up @@ -96,6 +102,9 @@ my class Promise does Awaitable {

method !keep(Mu \result --> Nil) {
$!lock.protect({
X::Promise::IllegalTransition.new(promise => self).throw
if $!status != Planned;

$!result := result;
$!status := Kept;
self!schedule_thens();
Expand Down Expand Up @@ -125,6 +134,9 @@ my class Promise does Awaitable {

method !break(\result --> Nil) {
$!lock.protect({
X::Promise::IllegalTransition.new(promise => self).throw
if $!status != Planned;

$!result := nqp::istype(result, Exception)
?? result
!! X::AdHoc.new(payload => result);
Expand Down

0 comments on commit c7ec96a

Please sign in to comment.