Skip to content

Commit a3ed332

Browse files
anton-bannykhSpace
authored and
Space
committed
rrr/1.6.20/ilgonmic/kt-51973
[JS IR] Add non overridden property and method insode exported class [JS IR] Add method into exported interface in test [JS IR] Add interface properties cases to all file export test [JS IR] Fix usage of isExported inside IrJsUtils Co-authored-by: Anton Bannykh <[email protected]> Merge-request: KT-MR-6090 Merged-by: Ilya Goncharov <[email protected]> ^KT-51973 fixed
1 parent 368465e commit a3ed332

File tree

5 files changed

+152
-15
lines changed

5 files changed

+152
-15
lines changed

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ private fun shouldDeclarationBeExported(declaration: IrDeclarationWithName, cont
634634
}
635635

636636
fun IrOverridableDeclaration<*>.isAllowedFakeOverriddenDeclaration(context: JsIrBackendContext): Boolean {
637-
if (this.resolveFakeOverride(allowAbstract = true)?.parentClassOrNull.isExportedInterface()) {
637+
if (this.resolveFakeOverride(allowAbstract = true)?.parentClassOrNull.isExportedInterface(context)) {
638638
return true
639639
}
640640

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/JsClassGenerator.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
134134

135135
val overriddenSymbols = property.getter?.overriddenSymbols.orEmpty()
136136

137+
val backendContext = context.staticContext.backendContext
138+
137139
// Don't generate `defineProperty` if the property overrides a property from an exported class,
138140
// because we've already generated `defineProperty` for the base class property.
139141
// In other words, we only want to generate `defineProperty` once for each property.
@@ -142,7 +144,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
142144
// P.S. If the overridden property is owned by an interface - we should generate defineProperty
143145
// for overridden property in the first class which override those properties
144146
val hasOverriddenExportedInterfaceProperties = overriddenSymbols.any { it.owner.isDefinedInsideExportedInterface() }
145-
&& !overriddenSymbols.any { it.owner.parentClassOrNull.isExportedClass() }
147+
&& !overriddenSymbols.any { it.owner.parentClassOrNull.isExportedClass(backendContext) }
146148

147149
val getterOverridesExternal = property.getter?.overridesExternal() == true
148150
val overriddenExportedGetter = !property.getter?.overriddenSymbols.isNullOrEmpty() &&
@@ -213,7 +215,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
213215
}
214216

215217
private fun IrSimpleFunction.isDefinedInsideExportedInterface(): Boolean {
216-
return (!isFakeOverride && parentClassOrNull.isExportedInterface()) ||
218+
return (!isFakeOverride && parentClassOrNull.isExportedInterface(context.staticContext.backendContext)) ||
217219
overriddenSymbols.any { it.owner.isDefinedInsideExportedInterface() }
218220
}
219221

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/jsAstUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fun translateCall(
124124
val property = function.correspondingPropertySymbol?.owner
125125
if (
126126
property != null &&
127-
(property.isEffectivelyExternal() || property.isExportedMember())
127+
(property.isEffectivelyExternal() || property.isExportedMember(context.staticContext.backendContext))
128128
) {
129129
val propertyName = context.getNameForProperty(property)
130130
val nameRef = when (jsDispatchReceiver) {

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/IrJsUtils.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ package org.jetbrains.kotlin.ir.backend.js.utils
77

88
import org.jetbrains.kotlin.descriptors.isClass
99
import org.jetbrains.kotlin.descriptors.isInterface
10+
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
11+
import org.jetbrains.kotlin.ir.backend.js.export.isExported
1012
import org.jetbrains.kotlin.ir.declarations.IrClass
1113
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
1214
import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithVisibility
1315
import org.jetbrains.kotlin.ir.util.parentClassOrNull
1416

15-
fun IrDeclaration.isExportedMember() =
17+
fun IrDeclaration.isExportedMember(context: JsIrBackendContext) =
1618
(this is IrDeclarationWithVisibility && visibility.isPublicAPI) &&
17-
parentClassOrNull.let { it is IrClass && it.isJsExport() }
19+
parentClassOrNull?.isExported(context) == true
1820

19-
fun IrDeclaration?.isExportedClass() =
20-
this is IrClass && kind.isClass && isJsExport()
21+
fun IrDeclaration?.isExportedClass(context: JsIrBackendContext) =
22+
this is IrClass && kind.isClass && isExported(context)
2123

22-
fun IrDeclaration?.isExportedInterface() =
23-
this is IrClass && kind.isInterface && isJsExport()
24-
25-
fun IrDeclaration.isExportedInterfaceMember() =
26-
parentClassOrNull.isExportedInterface()
24+
fun IrDeclaration?.isExportedInterface(context: JsIrBackendContext) =
25+
this is IrClass && kind.isInterface && isExported(context)

js/js.translator/testData/box/export/exportAllFile.kt

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,151 @@
99

1010
abstract class A {
1111
abstract fun foo(k: String): String
12+
13+
abstract val bar: String
14+
15+
abstract val baz: String
16+
17+
abstract var bay: String
18+
19+
abstract var bat: String
1220
}
1321

14-
class B : A() {
22+
open class B : A() {
1523
override fun foo(k: String): String {
1624
return "O" + k
1725
}
26+
27+
override val bar: String = "bar"
28+
29+
override val baz: String
30+
get() = "baz"
31+
32+
override var bay: String = "bay"
33+
34+
private var _bat: String = "bat"
35+
36+
override var bat: String
37+
get() = _bat
38+
set(value) {
39+
_bat = value
40+
}
41+
}
42+
43+
interface I {
44+
val gap: String
45+
46+
val hap: String
47+
48+
var baz: String
49+
50+
var bay: String
51+
52+
fun foo(): String
53+
}
54+
55+
open class C : I {
56+
override val gap: String = "gap"
57+
58+
override val hap: String
59+
get() = "hap"
60+
61+
override var bay: String = "bay"
62+
63+
private var _baz = "baz"
64+
65+
override var baz: String
66+
get() = _baz
67+
set(value) {
68+
_baz = value
69+
}
70+
71+
val koo: String = "koo"
72+
73+
override fun foo(): String {
74+
return "foo"
75+
}
76+
77+
fun foi() = "foi"
78+
}
79+
80+
fun topFoo(a: A): String {
81+
if (a.bar != "bar3") return "fail"
82+
if (a.baz != "baz3") return "fail"
83+
return "OK"
84+
}
85+
86+
fun topBar(c: C): String {
87+
if (c.bay != "bay3") return "fail"
88+
if (c.baz != "baz3") return "fail"
89+
return "OK"
1890
}
1991

2092
// FILE: test.js
93+
2194
function box() {
22-
return new this["export_all_file"].B().foo("K");
95+
var exportObject = this["export_all_file"]
96+
97+
function H() {
98+
}
99+
100+
H.prototype = Object.create(exportObject.B.prototype);
101+
H.prototype.constructor = H;
102+
Object.defineProperty(H.prototype, "bar", {
103+
get: function() {
104+
return "bar3"
105+
}
106+
})
107+
108+
Object.defineProperty(H.prototype, "baz", {
109+
get: function() {
110+
return "baz3"
111+
}
112+
})
113+
114+
function J() {
115+
}
116+
117+
J.prototype = Object.create(exportObject.C.prototype);
118+
J.prototype.constructor = J;
119+
Object.defineProperty(J.prototype, "bay", {
120+
get: function() {
121+
return "bay3"
122+
}
123+
})
124+
125+
Object.defineProperty(J.prototype, "baz", {
126+
get: function() {
127+
return "baz3"
128+
}
129+
})
130+
131+
var b = new exportObject.B()
132+
if (b.foo("K") != "OK") return "fail 1";
133+
if (b.bar != "bar") return "fail 2"
134+
if (b.baz != "baz") return "fail 3"
135+
if (b.bay != "bay") return "fail 4"
136+
b.bay = "bay2"
137+
if (b.bay != "bay2") return "fail 5"
138+
if (b.bat != "bat") return "fail6"
139+
b.bat = "bat2"
140+
if (b.bat != "bat2") return "fail7"
141+
142+
var c = new exportObject.C()
143+
if (c.gap != "gap") return "fail 8"
144+
if (c.hap != "hap") return "fail 9"
145+
if (c.bay != "bay") return "fail 10"
146+
c.bay = c.bay + "2"
147+
if (c.bay != "bay2") return "fail 11"
148+
if (c.baz != "baz") return "fail 12"
149+
c.baz = c.baz + "2"
150+
if (c.baz != "baz2") return "fail 13"
151+
if (c.foo() != "foo") return "fail 14"
152+
if (c.koo != "koo") return "fail 15"
153+
if (c.foi() != "foi") return "fail 16"
154+
155+
if (exportObject.topFoo(new H()) != "OK") return "fail 17"
156+
if (exportObject.topBar(new J()) != "OK") return "fail 18"
157+
158+
return "OK"
23159
}

0 commit comments

Comments
 (0)