Skip to content

Commit b864add

Browse files
committed
[js] When compiling a block for use while it's outer block does not yet exist stub the context with a null.
1 parent b07b852 commit b864add

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/vm/js/Compiler.nqp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
188188
@tmps;
189189
}
190190

191+
method cuid() {
192+
$!qast ?? $!qast.cuid !! NQPMu;
193+
}
194+
191195
method var_type($var) { %!var_types{$var.name} }
192196
method register_var_type($var, $type) { %!var_types{$var.name} := $type }
193197

@@ -860,15 +864,28 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
860864
my $cuid := self.mangled_cuid($_.key);
861865
my $reg := $_.value;
862866

867+
my $expected_outer := %*BLOCKS_INFO{$_.key}.outer;
868+
869+
if $expected_outer.cuid ne $*BLOCK.cuid {
870+
@capture_inners.push("/*static block*/\n");
871+
@capture_inners.push("(function() \{\n");
872+
@capture_inners.push("var {$expected_outer.ctx} = null;\n");
873+
}
874+
863875
if %*BLOCKS_AS_METHOD{$_.key} {
864876
@capture_inners.push("$reg = $cuid.method");
865877
}
866878
else {
867879
@capture_inners.push("$reg = $cuid.capture");
868880
}
869881

882+
870883
@capture_inners.push(%*BLOCKS_DONE{$_.key});
871884
@capture_inners.push(";\n");
885+
886+
if $expected_outer.cuid ne $*BLOCK.cuid {
887+
@capture_inners.push("\})();\n");
888+
}
872889
}
873890
Chunk.void(|@capture_inners);
874891
}
@@ -909,6 +926,8 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
909926
my $*CTX := self.unique_var('ctx');
910927
$*BLOCK.ctx($*CTX);
911928

929+
%*BLOCKS_INFO{$node.cuid} := $*BLOCK;
930+
912931
my $body_want := $node.blocktype eq 'immediate' ?? $want !! $T_OBJ;
913932

914933
my $as_method := self.looks_like_a_method($node);
@@ -1238,6 +1257,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
12381257

12391258
# Blocks we've seen while compiling.
12401259
my %*BLOCKS_DONE;
1260+
my %*BLOCKS_INFO;
12411261
my %*BLOCKS_AS_METHOD;
12421262
my %*BLOCKS_DONE_CPS;
12431263

0 commit comments

Comments
 (0)