JS: fix crash when js function contains for statement w/o initializer

Fix KT-20898
This commit is contained in:
Alexey Andreev
2017-10-23 13:35:58 +03:00
parent d9d565d8b0
commit cb0482f53e
5 changed files with 29 additions and 4 deletions
@@ -144,7 +144,7 @@ public class JsFor extends SourceInfoAwareJsNode implements JsStatement {
if (initVars != null) {
result = new JsFor(initVars.deepCopy(), conditionCopy, incrementalExprCopy, bodyCopy);
} else {
result = new JsFor(initExpression.deepCopy(), conditionCopy, incrementalExprCopy, bodyCopy);
result = new JsFor(initExpression != null ? initExpression.deepCopy() : null, conditionCopy, incrementalExprCopy, bodyCopy);
}
return result.withMetadataFrom(this);
@@ -536,12 +536,11 @@ public class JsAstMapper {
JsNode init = map(fromInit);
JsExpression condition = mapOptionalExpression(fromTest);
JsExpression increment = mapOptionalExpression(fromIncr);
assert (init != null);
if (init instanceof JsVars) {
toFor = new JsFor((JsVars) init, condition, increment);
}
else {
assert (init instanceof JsExpression);
assert (init == null || init instanceof JsExpression);
toFor = new JsFor((JsExpression) init, condition, increment);
}
@@ -223,7 +223,7 @@ class JsAstDeserializer(program: JsProgram, private val sourceRoots: Iterable<Fi
JsFor(initVars, condition, increment, body)
}
else {
JsFor(initExpr!!, condition, increment, body)
JsFor(initExpr, condition, increment, body)
}
}
@@ -5480,6 +5480,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("forWithoutInit.kt")
public void testForWithoutInit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsCode/forWithoutInit.kt");
doTest(fileName);
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsCode/function.kt");
+20
View File
@@ -0,0 +1,20 @@
// EXPECTED_REACHABLE_NODES: 1125
// FILE: a.kt
fun foo(n: Int): String = js("""
var result = "";
var i = 0;
for (; i < n; i++) {
result += i + ";"
}
return result;
""")
// FILE: b.kt
// RECOMPILE
fun box(): String {
val r = foo(3)
if (r != "0;1;2;") return "fail: $r"
return "OK"
}