[K/JS] Optimize logical operators generating
This commit is contained in:
+14
@@ -239,6 +239,20 @@ class IrElementToJsExpressionTransformer : BaseIrElementToJsNodeTransformer<JsEx
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitWhen(expression: IrWhen, context: JsGenerationContext): JsExpression {
|
override fun visitWhen(expression: IrWhen, context: JsGenerationContext): JsExpression {
|
||||||
|
if (expression.origin == IrStatementOrigin.ANDAND) {
|
||||||
|
return JsBinaryOperation(
|
||||||
|
JsBinaryOperator.AND,
|
||||||
|
expression.branches[0].condition.accept(this, context),
|
||||||
|
expression.branches[0].result.accept(this, context)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (expression.origin == IrStatementOrigin.OROR) {
|
||||||
|
return JsBinaryOperation(
|
||||||
|
JsBinaryOperator.OR,
|
||||||
|
expression.branches[0].condition.accept(this, context),
|
||||||
|
expression.branches[1].result.accept(this, context)
|
||||||
|
)
|
||||||
|
}
|
||||||
val lastBranch = expression.branches.lastOrNull()
|
val lastBranch = expression.branches.lastOrNull()
|
||||||
val implicitElse =
|
val implicitElse =
|
||||||
if (lastBranch == null || !isElseBranch(lastBranch))
|
if (lastBranch == null || !isElseBranch(lastBranch))
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ fun test(): Long {
|
|||||||
|
|
||||||
// EXPECTATIONS JS_IR
|
// EXPECTATIONS JS_IR
|
||||||
// test.kt:6 box
|
// test.kt:6 box
|
||||||
|
// test.kt:10 test
|
||||||
// test.kt:12 test
|
// test.kt:12 test
|
||||||
// test.kt:7 box
|
// test.kt:7 box
|
||||||
|
|
||||||
|
|||||||
+6
@@ -7270,6 +7270,12 @@ public class FirJsES6BoxTestGenerated extends AbstractFirJsES6BoxTest {
|
|||||||
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("logicalOperators.kt")
|
||||||
|
public void testLogicalOperators() {
|
||||||
|
runTest("js/js.translator/testData/box/jsAstOptimizations/logicalOperators.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("tempVarDeclOnAssignment.kt")
|
@TestMetadata("tempVarDeclOnAssignment.kt")
|
||||||
public void testTempVarDeclOnAssignment() {
|
public void testTempVarDeclOnAssignment() {
|
||||||
|
|||||||
Generated
+6
@@ -7164,6 +7164,12 @@ public class FirLightTreeJsBoxTestGenerated extends AbstractFirLightTreeJsBoxTes
|
|||||||
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("logicalOperators.kt")
|
||||||
|
public void testLogicalOperators() {
|
||||||
|
runTest("js/js.translator/testData/box/jsAstOptimizations/logicalOperators.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("tempVarDeclOnAssignment.kt")
|
@TestMetadata("tempVarDeclOnAssignment.kt")
|
||||||
public void testTempVarDeclOnAssignment() {
|
public void testTempVarDeclOnAssignment() {
|
||||||
|
|||||||
+6
@@ -7164,6 +7164,12 @@ public class FirPsiJsBoxTestGenerated extends AbstractFirPsiJsBoxTest {
|
|||||||
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("logicalOperators.kt")
|
||||||
|
public void testLogicalOperators() {
|
||||||
|
runTest("js/js.translator/testData/box/jsAstOptimizations/logicalOperators.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("tempVarDeclOnAssignment.kt")
|
@TestMetadata("tempVarDeclOnAssignment.kt")
|
||||||
public void testTempVarDeclOnAssignment() {
|
public void testTempVarDeclOnAssignment() {
|
||||||
|
|||||||
+6
@@ -7270,6 +7270,12 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
|
|||||||
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("logicalOperators.kt")
|
||||||
|
public void testLogicalOperators() {
|
||||||
|
runTest("js/js.translator/testData/box/jsAstOptimizations/logicalOperators.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("tempVarDeclOnAssignment.kt")
|
@TestMetadata("tempVarDeclOnAssignment.kt")
|
||||||
public void testTempVarDeclOnAssignment() {
|
public void testTempVarDeclOnAssignment() {
|
||||||
|
|||||||
+6
@@ -7164,6 +7164,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
|
|||||||
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
runTest("js/js.translator/testData/box/jsAstOptimizations/inlineEmptyFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("logicalOperators.kt")
|
||||||
|
public void testLogicalOperators() {
|
||||||
|
runTest("js/js.translator/testData/box/jsAstOptimizations/logicalOperators.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("tempVarDeclOnAssignment.kt")
|
@TestMetadata("tempVarDeclOnAssignment.kt")
|
||||||
public void testTempVarDeclOnAssignment() {
|
public void testTempVarDeclOnAssignment() {
|
||||||
|
|||||||
+2
-2
@@ -7,7 +7,7 @@ function box() {
|
|||||||
if (!(flag1 === 0)) {
|
if (!(flag1 === 0)) {
|
||||||
var tmp;
|
var tmp;
|
||||||
var tmp_0;
|
var tmp_0;
|
||||||
if (equals(OK, 'OK') ? flag1 === 1 : false) {
|
if (equals(OK, 'OK') && flag1 === 1) {
|
||||||
var tmp_1 = flag2;
|
var tmp_1 = flag2;
|
||||||
tmp_0 = typeof tmp_1 === 'number';
|
tmp_0 = typeof tmp_1 === 'number';
|
||||||
} else {
|
} else {
|
||||||
@@ -26,7 +26,7 @@ function box() {
|
|||||||
if (!(flag1 === 0)) {
|
if (!(flag1 === 0)) {
|
||||||
var tmp_2;
|
var tmp_2;
|
||||||
var tmp_3;
|
var tmp_3;
|
||||||
if (equals(OK, 'OK') ? flag1 === 1 : false) {
|
if (equals(OK, 'OK') && flag1 === 1) {
|
||||||
var tmp_4 = flag2;
|
var tmp_4 = flag2;
|
||||||
tmp_3 = typeof tmp_4 === 'number';
|
tmp_3 = typeof tmp_4 === 'number';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
function test() {
|
||||||
|
return (!a || (b && c)) && foo() || bar();
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
val a = true
|
||||||
|
val b = true
|
||||||
|
val c = true
|
||||||
|
|
||||||
|
fun foo() = true
|
||||||
|
fun bar() = true
|
||||||
|
|
||||||
|
// EXPECT_GENERATED_JS: function=test expect=logicalOperators.js
|
||||||
|
fun test(): Boolean = (!a || (b && c)) && foo() || bar()
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
return if (test()) "OK" else "FAILED"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user