@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
14
14
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
15
15
import org.jetbrains.kotlin.ir.types.IrType
16
16
import org.jetbrains.kotlin.ir.types.classifierOrFail
17
+ import org.jetbrains.kotlin.ir.types.classifierOrNull
17
18
import org.jetbrains.kotlin.ir.types.isAny
18
19
import org.jetbrains.kotlin.ir.util.*
19
20
import org.jetbrains.kotlin.js.backend.ast.*
@@ -31,25 +32,55 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
31
32
private val classBlock = JsGlobalBlock ()
32
33
private val classModel = JsIrClassModel (irClass)
33
34
35
+ private val es6mode = context.staticContext.backendContext.es6mode
36
+
34
37
fun generate (): JsStatement {
35
38
assert (! irClass.descriptor.isExpect)
36
39
37
- maybeGeneratePrimaryConstructor()
40
+ if ( ! es6mode) maybeGeneratePrimaryConstructor()
38
41
val transformer = IrDeclarationToJsTransformer ()
39
42
40
43
// Properties might be lowered out of classes
41
44
// We'll use IrSimpleFunction::correspondingProperty to collect them into set
42
45
val properties = mutableSetOf<IrProperty >()
43
46
47
+ val jsClass = JsClass (name = className)
48
+
49
+ if (baseClass != null && ! baseClass.isAny()) {
50
+ jsClass.baseClass = baseClassName?.makeRef()
51
+ }
52
+
53
+ if (es6mode) classModel.preDeclarationBlock.statements + = jsClass.makeStmt()
54
+
44
55
for (declaration in irClass.declarations) {
45
56
when (declaration) {
46
57
is IrConstructor -> {
47
- classBlock.statements + = declaration.accept(transformer, context)
48
- classModel.preDeclarationBlock.statements + = generateInheritanceCode()
58
+ if (es6mode) {
59
+ declaration.accept(IrFunctionToJsTransformer (), context).let {
60
+ // HACK: add superCall to Error
61
+ if ((baseClass?.classifierOrNull?.owner as ? IrClass )?.symbol == = context.staticContext.backendContext.throwableClass) {
62
+ it.body.statements.add(0 , JsInvocation (JsNameRef (" super" )).makeStmt())
63
+ }
64
+
65
+ if (it.body.statements.any { it !is JsEmpty }) {
66
+ jsClass.constructor = it
67
+ }
68
+ }
69
+ } else {
70
+ classBlock.statements + = declaration.accept(transformer, context)
71
+ classModel.preDeclarationBlock.statements + = generateInheritanceCode()
72
+ }
49
73
}
50
74
is IrSimpleFunction -> {
51
75
properties.addIfNotNull(declaration.correspondingPropertySymbol?.owner)
52
- generateMemberFunction(declaration)?.let { classBlock.statements + = it }
76
+
77
+ if (es6mode) {
78
+ val (_, function) = generateMemberFunction(declaration)
79
+ function?.let { jsClass.members + = it }
80
+ } else {
81
+ val (memberRef, function) = generateMemberFunction(declaration)
82
+ function?.let { classBlock.statements + = jsAssignment(memberRef, it.apply { name = null }).makeStmt() }
83
+ }
53
84
}
54
85
is IrClass -> {
55
86
classBlock.statements + = JsClassGenerator (declaration, context).generate()
@@ -111,17 +142,15 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
111
142
return this .overriddenSymbols.any { it.owner.overridesExternal() }
112
143
}
113
144
114
- private fun generateMemberFunction (declaration : IrSimpleFunction ): JsStatement ? {
115
-
145
+ private fun generateMemberFunction (declaration : IrSimpleFunction ): Pair <JsNameRef , JsFunction ?> {
116
146
val memberName = context.getNameForMemberFunction(declaration.realOverrideTarget)
117
147
val memberRef = JsNameRef (memberName, classPrototypeRef)
118
148
119
149
if (declaration.isReal && declaration.body != null ) {
120
150
val translatedFunction = declaration.accept(IrFunctionToJsTransformer (), context)
121
-
122
151
assert (! declaration.isStaticMethodOfClass)
123
152
124
- return jsAssignment (memberRef, translatedFunction. apply { name = null }).makeStmt( )
153
+ return Pair (memberRef, translatedFunction)
125
154
}
126
155
127
156
// do not generate code like
@@ -144,7 +173,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
144
173
}
145
174
}
146
175
147
- return null
176
+ return Pair (memberRef, null )
148
177
}
149
178
150
179
private fun maybeGeneratePrimaryConstructor () {
0 commit comments