From e4bdd3560cffe2a441c5bb1cb30dad46f0b75a1d Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Wed, 19 Jul 2023 14:33:19 +0000 Subject: [PATCH] [K/JS] Change order of exported properties definition for non ES-classes compilation ^KT-60131 Fixed --- .../transformers/irToJs/JsClassGenerator.kt | 2 +- .../js/test/fir/FirJsBoxTestGenerated.java | 12 +++++++ .../js/test/ir/IrBoxJsES6TestGenerated.java | 12 +++++++ .../js/test/ir/IrBoxJsTestGenerated.java | 12 +++++++ .../export/exportInterfaceWithoutClases.kt | 36 +++++++++++++++++++ .../export/exportInterfaceWithoutClases.kt | 33 +++++++++++++++++ 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt create mode 100644 js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/JsClassGenerator.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/JsClassGenerator.kt index 0e961e5e117..51d64866a10 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/JsClassGenerator.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/JsClassGenerator.kt @@ -245,7 +245,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo } ) } else { - classBlock.statements += JsExpressionStatement( + classModel.postDeclarationBlock.statements += JsExpressionStatement( defineProperty(classPrototypeRef, propertyName.ident, getterForwarder, setterForwarder, context.staticContext) ) } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsBoxTestGenerated.java index ab693b76f16..b832e195e9f 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsBoxTestGenerated.java @@ -2289,6 +2289,12 @@ public class FirJsBoxTestGenerated extends AbstractFirJsBoxTest { runTest("js/js.translator/testData/box/esModules/export/exportInterface.kt"); } + @Test + @TestMetadata("exportInterfaceWithoutClases.kt") + public void testExportInterfaceWithoutClases() throws Exception { + runTest("js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt"); + } + @Test @TestMetadata("exportNestedClass.kt") public void testExportNestedClass() throws Exception { @@ -2854,6 +2860,12 @@ public class FirJsBoxTestGenerated extends AbstractFirJsBoxTest { runTest("js/js.translator/testData/box/export/exportInterface.kt"); } + @Test + @TestMetadata("exportInterfaceWithoutClases.kt") + public void testExportInterfaceWithoutClases() throws Exception { + runTest("js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt"); + } + @Test @TestMetadata("exportNestedClass.kt") public void testExportNestedClass() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsES6TestGenerated.java index 1dbf367057e..cfd90874cb9 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsES6TestGenerated.java @@ -2395,6 +2395,12 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test { runTest("js/js.translator/testData/box/esModules/export/exportInterface.kt"); } + @Test + @TestMetadata("exportInterfaceWithoutClases.kt") + public void testExportInterfaceWithoutClases() throws Exception { + runTest("js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt"); + } + @Test @TestMetadata("exportNestedClass.kt") public void testExportNestedClass() throws Exception { @@ -2960,6 +2966,12 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test { runTest("js/js.translator/testData/box/export/exportInterface.kt"); } + @Test + @TestMetadata("exportInterfaceWithoutClases.kt") + public void testExportInterfaceWithoutClases() throws Exception { + runTest("js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt"); + } + @Test @TestMetadata("exportNestedClass.kt") public void testExportNestedClass() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java index 72e929b8e0c..ed79409fce1 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java @@ -2289,6 +2289,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { runTest("js/js.translator/testData/box/esModules/export/exportInterface.kt"); } + @Test + @TestMetadata("exportInterfaceWithoutClases.kt") + public void testExportInterfaceWithoutClases() throws Exception { + runTest("js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt"); + } + @Test @TestMetadata("exportNestedClass.kt") public void testExportNestedClass() throws Exception { @@ -2854,6 +2860,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { runTest("js/js.translator/testData/box/export/exportInterface.kt"); } + @Test + @TestMetadata("exportInterfaceWithoutClases.kt") + public void testExportInterfaceWithoutClases() throws Exception { + runTest("js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt"); + } + @Test @TestMetadata("exportNestedClass.kt") public void testExportNestedClass() throws Exception { diff --git a/js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt b/js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt new file mode 100644 index 00000000000..19736104a72 --- /dev/null +++ b/js/js.translator/testData/box/esModules/export/exportInterfaceWithoutClases.kt @@ -0,0 +1,36 @@ +// KT-60131 +// IGNORE_BACKEND: JS +// RUN_PLAIN_BOX_FUNCTION +// DONT_TARGET_EXACT_BACKEND: JS +// ES_MODULES + +// MODULE: export_interface +// FILE: lib.kt + +@JsExport +interface BaseInterface { + val prop: String +} + +class Impl : BaseClass("foobar") + +open class BaseClass(private val _prop: String) : BaseInterface { + final override val prop: String get() = _prop +} + +@JsExport +fun getImpl(): BaseInterface = Impl() + +@JsExport +fun getBase(): BaseInterface = BaseClass("base") + +// FILE: main.mjs +// ENTRY_ES_MODULE +import { getBase, getImpl } from "./exportInterfaceWithoutClases-export_interface_v5.mjs" + +export function box() { + if (getBase().prop != "base") return "fail 1"; + if (getImpl().prop != "foobar") return "fail 2"; + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt b/js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt new file mode 100644 index 00000000000..a34932b4dfc --- /dev/null +++ b/js/js.translator/testData/box/export/exportInterfaceWithoutClases.kt @@ -0,0 +1,33 @@ +// KT-60131 +// IGNORE_BACKEND: JS +// RUN_PLAIN_BOX_FUNCTION +// INFER_MAIN_MODULE +// SKIP_DCE_DRIVEN + +// MODULE: export_interface +// FILE: lib.kt + +@JsExport +interface BaseInterface { + val prop: String +} + +class Impl : BaseClass("foobar") + +open class BaseClass(private val _prop: String) : BaseInterface { + final override val prop: String get() = _prop +} + +@JsExport +fun getImpl(): BaseInterface = Impl() + +@JsExport +fun getBase(): BaseInterface = BaseClass("base") + +// FILE: test.js +function box() { + if (this["export_interface"].getBase().prop != "base") return "fail 1"; + if (this["export_interface"].getImpl().prop != "foobar") return "fail 2"; + + return "OK" +} \ No newline at end of file