Skip to content

Commit

Permalink
Code in start blocks see dynamics of starter.
Browse files Browse the repository at this point in the history
So `my $*E = 42; say await start $*E;` now gives 42, not an exception.
  • Loading branch information
jnthn committed Nov 23, 2015
1 parent eb9421b commit bedf2a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/core/Promise.pm
Expand Up @@ -23,6 +23,7 @@ my class Promise {
has $!lock;
has $!cond;
has @!thens;
has Mu $!dynamic_context;

submethod BUILD(:$!scheduler = $*SCHEDULER) {
$!lock := nqp::create(Lock);
Expand Down Expand Up @@ -157,10 +158,11 @@ my class Promise {
}

method start(Promise:U: &code, :&catch, :$scheduler = $*SCHEDULER, |c) {
my $p = Promise.new(:$scheduler);
my $p := Promise.new(:$scheduler);
nqp::bindattr($p, Promise, '$!dynamic_context', nqp::ctx());
my $vow = $p.vow;
$scheduler.cue(
{ $vow.keep(code(|c)) },
{ my $*PROMISE := $p; $vow.keep(code(|c)) },
:catch(-> $ex { catch($ex) if &catch; $vow.break($ex); }) );
$p
}
Expand Down
27 changes: 18 additions & 9 deletions src/core/stubs.pm
Expand Up @@ -7,6 +7,7 @@ my class Exception { ... }
my class X::AdHoc { ... }
my class FatRat { ... }
my class Pair { ... }
my class Promise { ... }
my class X::OutOfRange { ... }
my class X::Dynamic::NotFound { ... }

Expand All @@ -26,19 +27,27 @@ my class MixHash { ... }
sub DYNAMIC(\name) is raw {
my Mu \x := nqp::getlexdyn(nqp::unbox_s(name));
if nqp::isnull(x) {
my str $pkgname = nqp::replace(nqp::unbox_s(name), 1, 1, '');
if nqp::existskey((my \globalwho := GLOBAL.WHO), $pkgname) {
x := nqp::atkey(globalwho, $pkgname);
my \prom := nqp::getlexdyn('$*PROMISE');
unless nqp::isnull(prom) {
x := nqp::getlexreldyn(
nqp::getattr(prom, Promise, '$!dynamic_context'),
nqp::unbox_s(name));
}
elsif nqp::existskey((my \processwho := PROCESS.WHO), $pkgname) {
x := nqp::atkey(processwho, $pkgname);
}
else {
if nqp::isnull(x) {
my str $pkgname = nqp::replace(nqp::unbox_s(name), 1, 1, '');
if nqp::existskey((my \globalwho := GLOBAL.WHO), $pkgname) {
x := nqp::atkey(globalwho, $pkgname);
}
elsif nqp::existskey((my \processwho := PROCESS.WHO), $pkgname) {
x := nqp::atkey(processwho, $pkgname);
}
else {
#my $last = now;
#say "initializing {name}";
x := INITIALIZE_DYNAMIC(name);
x := INITIALIZE_DYNAMIC(name);
#say " done at {now - $last}";
fail x if nqp::istype(x, Exception);
fail x if nqp::istype(x, Exception);
}
}
}
x
Expand Down

0 comments on commit bedf2a7

Please sign in to comment.