From b1884456b8982a95bac232371254f5a3b15b60b9 Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Wed, 19 Jul 2023 10:41:25 +0000 Subject: [PATCH] [K/JS] Fix initialization issue for objects which annotated with @JsExport and contain nested classes Merge-request: KT-MR-11146 Merged-by: Artem Kobzar --- .../js/export/ExportModelToJsStatements.kt | 10 +++------ .../box/classObject/objectLazyInitialized.kt | 21 ++++++++++++++++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToJsStatements.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToJsStatements.kt index 48155007c8a..0890d9e21d4 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToJsStatements.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToJsStatements.kt @@ -119,11 +119,7 @@ class ExportModelToJsStatements( is ExportedObject -> { require(namespace != null || esModules) { "Only namespaced properties are allowed" } val (name, objectClassInitialization) = declaration.getNameAndInitialization() - val newNameSpace = when { - namespace != null -> jsElementAccess(declaration.name, namespace) - else -> - jsElementAccess(Namer.PROTOTYPE_NAME, name.makeRef()) - } + val newNameSpace = jsElementAccess(Namer.PROTOTYPE_NAME, name.makeRef()) val staticsExport = declaration.nestedClasses.flatMap { generateDeclarationExport(it, newNameSpace, esModules, declaration.ir) } @@ -256,11 +252,11 @@ class ExportModelToJsStatements( private fun ExportedClass.getNameAndInitialization(): Pair { return when (val classRef = ir.getClassRef(staticContext)) { - !is JsNameRef -> { + is JsNameRef -> classRef.name!! to null + else -> { val stableName = JsName(name, true) stableName to JsVars(JsVars.JsVar(stableName, classRef)) } - else -> classRef.name!! to null } } diff --git a/js/js.translator/testData/box/classObject/objectLazyInitialized.kt b/js/js.translator/testData/box/classObject/objectLazyInitialized.kt index c4139f2bc18..3938f97781d 100644 --- a/js/js.translator/testData/box/classObject/objectLazyInitialized.kt +++ b/js/js.translator/testData/box/classObject/objectLazyInitialized.kt @@ -2,8 +2,6 @@ // See KT-6201 package foo -import kotlin.reflect.KProperty - var log = "" open class Mixin { @@ -39,8 +37,20 @@ object O5 { val someValue = O3.also { log = "O5 also" } } +@JsExport +object O6 { + init { + log = "O6 init" + } + object O7 { + init { + log = "O7 init" + } + } +} + fun box(): String { - if (log != "") return "Fail: something was initialized before any object was used" + if (log != "") return "Fail: something was initialized before any object was used: $log" O1 if (log != "O1 init") return "Fail: O1 didn't initialized lazy" O2 @@ -51,5 +61,10 @@ fun box(): String { if (log != "initCall") return "Fail: O4 didn't initialized lazy" O5 if (log != "O5 also") return "Fail: O5 didn't initialized lazy" + O6 + if (log != "O6 init") return "Fail: O6 didn't initialized lazy" + O6.O7 + if (log != "O7 init") return "Fail: O7 didn't initialized lazy" + return "OK" } \ No newline at end of file