[K/JS] Eliminate duplication of init block inside exported ES-classes ^KT-58246 Fixed

This commit is contained in:
Artem Kobzar
2023-04-25 16:21:08 +00:00
committed by Space Team
parent 9afddb1a7a
commit 27b103e2ca
7 changed files with 111 additions and 5 deletions
@@ -67,12 +67,14 @@ class ES6ConstructorLowering(val context: JsIrBackendContext) : DeclarationTrans
return runIf(isExported(context) && isPrimary) {
apply {
valueParameters = valueParameters.memoryOptimizedFilterNot { it.isBoxParameter }
(body as? IrBlockBody)?.let {
val selfReplacedConstructorCall = JsIrBuilder.buildCall(factoryFunction.symbol).apply {
valueParameters.forEachIndexed { i, it -> putValueArgument(i, JsIrBuilder.buildGetValue(it.symbol)) }
dispatchReceiver = JsIrBuilder.buildCall(context.intrinsics.jsNewTarget)
body = (body as? IrBlockBody)?.let {
context.irFactory.createBlockBody(it.startOffset, it.endOffset) {
val selfReplacedConstructorCall = JsIrBuilder.buildCall(factoryFunction.symbol).apply {
valueParameters.forEachIndexed { i, it -> putValueArgument(i, JsIrBuilder.buildGetValue(it.symbol)) }
dispatchReceiver = JsIrBuilder.buildCall(context.intrinsics.jsNewTarget)
}
statements.add(JsIrBuilder.buildReturn(symbol, selfReplacedConstructorCall, returnType))
}
it.statements.add(JsIrBuilder.buildReturn(symbol, selfReplacedConstructorCall, returnType))
}
}
}
@@ -1848,6 +1848,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
public void testAllFilesPresentInExport() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/esModules/export"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS, true);
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/esModules/export/exportClassWithInitBlock.kt");
}
}
@Nested
@@ -2096,6 +2102,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/export/exportClassPropertiesInDifferentCombinations.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportClassWithInternal.kt")
public void testExportClassWithInternal() throws Exception {
@@ -2189,6 +2189,12 @@ public class FirJsBoxTestGenerated extends AbstractFirJsBoxTest {
runTest("js/js.translator/testData/box/esModules/export/exportAllFile.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/esModules/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportEnumClass.kt")
public void testExportEnumClass() throws Exception {
@@ -2724,6 +2730,12 @@ public class FirJsBoxTestGenerated extends AbstractFirJsBoxTest {
runTest("js/js.translator/testData/box/export/exportClassPropertiesInDifferentCombinations.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportClassWithInternal.kt")
public void testExportClassWithInternal() throws Exception {
@@ -2295,6 +2295,12 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
runTest("js/js.translator/testData/box/esModules/export/exportAllFile.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/esModules/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportEnumClass.kt")
public void testExportEnumClass() throws Exception {
@@ -2830,6 +2836,12 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
runTest("js/js.translator/testData/box/export/exportClassPropertiesInDifferentCombinations.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportClassWithInternal.kt")
public void testExportClassWithInternal() throws Exception {
@@ -2189,6 +2189,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/esModules/export/exportAllFile.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/esModules/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportEnumClass.kt")
public void testExportEnumClass() throws Exception {
@@ -2724,6 +2730,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/export/exportClassPropertiesInDifferentCombinations.kt");
}
@Test
@TestMetadata("exportClassWithInitBlock.kt")
public void testExportClassWithInitBlock() throws Exception {
runTest("js/js.translator/testData/box/export/exportClassWithInitBlock.kt");
}
@Test
@TestMetadata("exportClassWithInternal.kt")
public void testExportClassWithInternal() throws Exception {
@@ -0,0 +1,30 @@
// EXPECTED_REACHABLE_NODES: 1252
// IGNORE_BACKEND: JS
// RUN_PLAIN_BOX_FUNCTION
// INFER_MAIN_MODULE
// ES_MODULES
// KT-58246
// MODULE: lib
// FILE: lib.kt
@JsExport
var result = 0
@JsExport
class Test {
init {
result += 1
}
}
// FILE: entry.mjs
// ENTRY_ES_MODULE
import { result, Test } from "./exportClassWithInitBlock-lib_v5.mjs";
export function box() {
new Test()
if (result.get() != 1) return "fail: init block didn't call or called more than 1 time"
return "OK"
}
@@ -0,0 +1,26 @@
// EXPECTED_REACHABLE_NODES: 1252
// IGNORE_BACKEND: JS
// RUN_PLAIN_BOX_FUNCTION
// INFER_MAIN_MODULE
// KT-58246
// MODULE: lib
// FILE: lib.kt
@JsExport
var result = 0
@JsExport
class Test {
init {
result += 1
}
}
// FILE: test.js
function box() {
new this.lib.Test()
if (this.lib.result != 1) return "fail (called " + result + " times): init block didn't call or called more than 1 time"
return "OK"
}