Skip to content

Commit

Permalink
First working gather/take for Rakudo/JVM
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jun 16, 2013
1 parent d3ae978 commit ec48e79
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/core/GatherIter.pm
Expand Up @@ -3,10 +3,31 @@ class GatherIter is Iterator {
has $!reified; # Parcel of this iterator's results
has $!infinite; # true if iterator is known infinite

#?if jvm
my $GATHER_PROMPT = [];
my $SENTINEL := [];
#?endif
method new($block, Mu :$infinite) {
#?if parrot
my Mu $coro :=
nqp::clone(nqp::getattr(&coro, Code, '$!do'));
nqp::ifnull($coro($block), Nil);
#?endif
#?if jvm
my Mu $takings;
my Mu $state;
my sub yield() {
nqp::continuationcontrol(0, $GATHER_PROMPT, -> Mu \c {
$state := sub () is rw { nqp::continuationinvoke(c, -> | { Nil }); };
});
}
$state := sub () is rw {
nqp::p6handletake( $block().eager(),
($takings := nqp::getpayload(nqp::exception()); yield(); nqp::resume(nqp::exception())));
$takings := $SENTINEL; yield();
};
my $coro := sub () is rw { nqp::continuationreset($GATHER_PROMPT, $state); $takings };
#?endif
my Mu $new := nqp::create(self);
nqp::bindattr($new, GatherIter, '$!coro', $coro);
nqp::bindattr($new, GatherIter, '$!infinite', $infinite);
Expand All @@ -33,7 +54,12 @@ class GatherIter is Iterator {
my $count = nqp::istype($n, Whatever) ?? 1 !! $n;
while !$end && $count > 0 {
$parcel := $!coro();
#?if parrot
$end = nqp::p6bool(nqp::isnull($parcel));
#?endif
#?if jvm
$end = nqp::p6bool(nqp::eqaddr($parcel, $SENTINEL));
#?endif
nqp::push($rpa, $parcel) unless $end;
$count = $count - 1;
}
Expand All @@ -50,8 +76,8 @@ class GatherIter is Iterator {

method infinite() { $!infinite }

my sub coro(\block) {
#?if parrot
my sub coro(\block) {
Q:PIR {
.local pmc block, handler, taken
block = find_lex 'block'
Expand All @@ -77,11 +103,8 @@ class GatherIter is Iterator {
goto gather_done # should never get here
};
True
#?endif
#?if !parrot
die "GatherIter NYI on JVM backend"
#?endif
}
#?endif
}


Expand Down
4 changes: 4 additions & 0 deletions src/vm/jvm/Perl6/Ops.nqp
Expand Up @@ -125,6 +125,10 @@ my $p6bool := -> $qastcomp, $op {
};
$ops.add_hll_op('perl6', 'p6bool', $p6bool);

$ops.add_hll_op('perl6', 'p6handletake', -> $qastcomp, $op {
$qastcomp.as_jast(QAST::Op.new( :op('handle'), $op[0], 'TAKE', $op[1]));
});

# MapIter core.
$ops.add_hll_op('perl6', 'p6mapiter', -> $qastcomp, $op {
# Create labels.
Expand Down

0 comments on commit ec48e79

Please sign in to comment.