Skip to content

Commit

Permalink
Merge pull request #1467 from jstuder-gh/state_deep_cloning
Browse files Browse the repository at this point in the history
Issue 1341: Elevate statevars to enclosing block when wrapped
  • Loading branch information
zoffixznet committed Feb 2, 2018
2 parents 4507a56 + 299e852 commit 646266a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Perl6/Actions.nqp
Expand Up @@ -130,6 +130,14 @@ sub wanted($ast,$by) {
QAST::WVal.new( :value($*W.find_symbol(['Seq']))),
block_closure($block) );

# Elevate statevars to enclosing thunk
if $body.has_ann('has_statevar') && $block.has_ann('past_block') {
Perl6::Actions::migrate_blocks(
$body, $block.ann('past_block'),
-> $n { nqp::istype($n, QAST::Var) && $n.decl eq 'statevar' }
)
}

# conditional (if not always true (or if repeat))
if $repeat || !$cond.has_compile_time_value || !$cond.compile_time_value == $while {
$cond := QAST::Op.new( :op<callmethod>, :name<not>, $cond ) unless $while;
Expand Down Expand Up @@ -3201,6 +3209,10 @@ class Perl6::Actions is HLL::Actions does STDActions {
$<initializer><sym> eq '::=');
}
if $*SCOPE eq 'state' {
if nqp::istype( (my $outer_block := $*W.cur_lexpad), QAST::Block) {
$outer_block.annotate('has_statevar', $outer_block[0]);
}

$past := QAST::Op.new( :op('if'),
QAST::Op.new( :op('p6stateinit') ),
$past,
Expand Down Expand Up @@ -6537,7 +6549,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
# hash). Others get thunked and so need to have certain blocks in an
# expression moved into the thunk. This performs the migration. Takes an
# optional predicate to decide whether to move a block.
sub migrate_blocks($from, $to, $predicate?) {
our sub migrate_blocks($from, $to, $predicate?) {
my @decls := @($from[0]);
my int $n := nqp::elems(@decls);
my int $i := 0;
Expand Down

0 comments on commit 646266a

Please sign in to comment.