diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt index 207c5838d05..e3fe25548c1 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt @@ -102,15 +102,16 @@ private fun JsIrBackendContext.performInlining(moduleFragment: IrModuleFragment) } private fun JsIrBackendContext.lower(moduleFragment: IrModuleFragment, dependencies: List) { +<<<<<<< HEAD moduleFragment.files.forEach(UnitMaterializationLowering(this)::lower) + moduleFragment.files.forEach(EnumClassLowering(this)::runOnFilePostfix) + moduleFragment.files.forEach(EnumUsageLowering(this)::lower) moduleFragment.files.forEach(VarargLowering(this)::lower) moduleFragment.files.forEach(LateinitLowering(this, true)::lower) moduleFragment.files.forEach(DefaultArgumentStubGenerator(this)::runOnFilePostfix) moduleFragment.files.forEach(DefaultParameterInjector(this)::runOnFilePostfix) moduleFragment.files.forEach(DefaultParameterCleaner(this)::runOnFilePostfix) moduleFragment.files.forEach(SharedVariablesLowering(this)::runOnFilePostfix) - moduleFragment.files.forEach(EnumClassLowering(this)::runOnFilePostfix) - moduleFragment.files.forEach(EnumUsageLowering(this)::lower) moduleFragment.files.forEach(ReturnableBlockLowering(this)::lower) moduleFragment.files.forEach(LocalDelegatedPropertiesLowering()::lower) moduleFragment.files.forEach(LocalDeclarationsLowering(this)::runOnFilePostfix) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt index 7118b96981a..9da2fa7d1c6 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt @@ -12,12 +12,15 @@ import org.jetbrains.kotlin.backend.common.ir.copyParameterDeclarationsFrom import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.lower.irBlockBody import org.jetbrains.kotlin.backend.common.lower.irIfThen +import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl +import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl @@ -144,6 +147,34 @@ class EnumClassTransformer(val context: JsIrBackendContext, private val irClass: } } + private fun List.toArrayLiteral(arrayType: IrType, elementType: IrType): IrExpression { + val startOffset = firstOrNull()?.startOffset ?: UNDEFINED_OFFSET + val endOffset = lastOrNull()?.endOffset ?: UNDEFINED_OFFSET + + val irVararg = IrVarargImpl(startOffset, endOffset, arrayType, elementType, this) + + return IrCallImpl(startOffset, endOffset, arrayType, context.intrinsics.arrayLiteral).apply { + putValueArgument(0, irVararg) + } + } + + private fun createEnumValuesBody(): IrBody { + val valuesFun = findFunctionDescriptorForMemberWithSyntheticBodyKind(IrSyntheticBodyKind.ENUM_VALUES) + val entryInstanceToFunction = context.enumEntryToGetInstanceFunction + + return context.createIrBuilder(valuesFun.symbol).run { + irBlockBody { + +irReturn( + enumEntries.map { + val function = entryInstanceToFunction[it.symbol]!! + irCall(function) + }.toArrayLiteral(valuesFun.returnType, irClass.defaultType) + ) + } + } + } + + private fun lowerEnumConstructorsSignature() { irClass.declarations.transform { declaration -> if (declaration is IrConstructor) { @@ -199,7 +230,7 @@ class EnumClassTransformer(val context: JsIrBackendContext, private val irClass: irClass.transformChildrenVoid(object : IrElementTransformerVoid() { override fun visitSyntheticBody(body: IrSyntheticBody): IrBody { return when (body.kind) { - IrSyntheticBodyKind.ENUM_VALUES -> builder.irBlockBody { } // TODO: Implement + IrSyntheticBodyKind.ENUM_VALUES -> createEnumValuesBody() IrSyntheticBodyKind.ENUM_VALUEOF -> createEnumValueOfBody() } } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt index efa4893562f..9723a654e6c 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt @@ -176,6 +176,11 @@ class SimpleNameGenerator : NameGenerator { } is IrSimpleFunction -> { + if (declaration.dispatchReceiverParameter == null && declaration.parent is IrClass) { + nameBuilder.append(getNameForDeclaration(declaration.parent as IrDeclaration, context)) + nameBuilder.append('.') + } + nameBuilder.append(declaration.name.asString()) declaration.extensionReceiverParameter?.let { nameBuilder.append("_\$${it.type.render()}") } declaration.typeParameters.forEach { nameBuilder.append("_${it.name.asString()}") } diff --git a/compiler/testData/codegen/box/checkcastOptimization/kt19246.kt b/compiler/testData/codegen/box/checkcastOptimization/kt19246.kt index 3c25210a8bc..7fce122da6a 100644 --- a/compiler/testData/codegen/box/checkcastOptimization/kt19246.kt +++ b/compiler/testData/codegen/box/checkcastOptimization/kt19246.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR // WITH_RUNTIME enum class ResultType constructor(val reason: String) { diff --git a/compiler/testData/codegen/box/enum/classForEnumEntry.kt b/compiler/testData/codegen/box/enum/classForEnumEntry.kt index c4bcf6ef586..0cc2e1ffd00 100644 --- a/compiler/testData/codegen/box/enum/classForEnumEntry.kt +++ b/compiler/testData/codegen/box/enum/classForEnumEntry.kt @@ -1,5 +1,4 @@ // IGNORE_BACKEND: JS_IR -// TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/enum/companionObjectInEnum.kt b/compiler/testData/codegen/box/enum/companionObjectInEnum.kt index 9169980e7ca..3e6d423f64e 100644 --- a/compiler/testData/codegen/box/enum/companionObjectInEnum.kt +++ b/compiler/testData/codegen/box/enum/companionObjectInEnum.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR enum class Game { ROCK, PAPER, diff --git a/compiler/testData/codegen/box/enum/defaultCtor/constructorWithVararg.kt b/compiler/testData/codegen/box/enum/defaultCtor/constructorWithVararg.kt index 4f9d2940ec9..f293632dffa 100644 --- a/compiler/testData/codegen/box/enum/defaultCtor/constructorWithVararg.kt +++ b/compiler/testData/codegen/box/enum/defaultCtor/constructorWithVararg.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR enum class Test(vararg xs: Int) { OK; val values = xs diff --git a/compiler/testData/codegen/box/enum/defaultCtor/secondaryConstructorWithVararg.kt b/compiler/testData/codegen/box/enum/defaultCtor/secondaryConstructorWithVararg.kt index 4d89df2513e..15c5ad34e9c 100644 --- a/compiler/testData/codegen/box/enum/defaultCtor/secondaryConstructorWithVararg.kt +++ b/compiler/testData/codegen/box/enum/defaultCtor/secondaryConstructorWithVararg.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR enum class Test(val x: Int, val str: String) { OK; constructor(vararg xs: Int) : this(xs.size + 42, "OK") diff --git a/compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt b/compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt index 3abb9af1076..6cad0c98a04 100644 --- a/compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt +++ b/compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR enum class Empty fun box(): String { diff --git a/compiler/testData/codegen/box/enum/modifierFlags.kt b/compiler/testData/codegen/box/enum/modifierFlags.kt index 51ffdb90bd8..7b756dd06ac 100644 --- a/compiler/testData/codegen/box/enum/modifierFlags.kt +++ b/compiler/testData/codegen/box/enum/modifierFlags.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND: JVM_IR // IGNORE_BACKEND: JS_IR -// TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/enum/noClassForSimpleEnum.kt b/compiler/testData/codegen/box/enum/noClassForSimpleEnum.kt index 3021d83e5e6..301cfbedc7d 100644 --- a/compiler/testData/codegen/box/enum/noClassForSimpleEnum.kt +++ b/compiler/testData/codegen/box/enum/noClassForSimpleEnum.kt @@ -1,5 +1,4 @@ // IGNORE_BACKEND: JS_IR -// TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/enum/sortEnumEntries.kt b/compiler/testData/codegen/box/enum/sortEnumEntries.kt index 1d4f0c579c2..8b894a1ffe2 100644 --- a/compiler/testData/codegen/box/enum/sortEnumEntries.kt +++ b/compiler/testData/codegen/box/enum/sortEnumEntries.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR // WITH_RUNTIME import Game.* diff --git a/compiler/testData/codegen/box/enum/valueof.kt b/compiler/testData/codegen/box/enum/valueof.kt index d658e0b4feb..89aeaa9cce6 100644 --- a/compiler/testData/codegen/box/enum/valueof.kt +++ b/compiler/testData/codegen/box/enum/valueof.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR enum class Color { RED, BLUE diff --git a/compiler/testData/codegen/box/objects/interfaceCompanionObjectReference.kt b/compiler/testData/codegen/box/objects/interfaceCompanionObjectReference.kt index 7aa9b0d6e88..09feb75175d 100644 --- a/compiler/testData/codegen/box/objects/interfaceCompanionObjectReference.kt +++ b/compiler/testData/codegen/box/objects/interfaceCompanionObjectReference.kt @@ -1,6 +1,5 @@ // !LANGUAGE: +NestedClassesInAnnotations // IGNORE_BACKEND: JVM_IR -// IGNORE_BACKEND: JS_IR // WITH_RUNTIME import kotlin.test.* diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/functionEqualsHashCode.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/functionEqualsHashCode.kt index 4ce5e6124c1..72b169fe9fb 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/functionEqualsHashCode.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/functionEqualsHashCode.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/propertyEqualsHashCode.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/propertyEqualsHashCode.kt index 3020ea983a2..82dc465e532 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/propertyEqualsHashCode.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/propertyEqualsHashCode.kt @@ -1,5 +1,4 @@ // IGNORE_BACKEND: JVM_IR -// IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/specialBuiltins/valuesInsideEnum.kt b/compiler/testData/codegen/box/specialBuiltins/valuesInsideEnum.kt index 9370d065993..ee008bfcf82 100644 --- a/compiler/testData/codegen/box/specialBuiltins/valuesInsideEnum.kt +++ b/compiler/testData/codegen/box/specialBuiltins/valuesInsideEnum.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR enum class Variants { O, K; companion object { diff --git a/js/js.translator/testData/box/enum/enumIsComparable.kt b/js/js.translator/testData/box/enum/enumIsComparable.kt index d2d19f8ec86..34150e62fdc 100644 --- a/js/js.translator/testData/box/enum/enumIsComparable.kt +++ b/js/js.translator/testData/box/enum/enumIsComparable.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR // EXPECTED_REACHABLE_NODES: 1291 package foo diff --git a/js/js.translator/testData/box/enum/implementsComparable.kt b/js/js.translator/testData/box/enum/implementsComparable.kt index 88902766163..2956ff3d4e3 100644 --- a/js/js.translator/testData/box/enum/implementsComparable.kt +++ b/js/js.translator/testData/box/enum/implementsComparable.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR // EXPECTED_REACHABLE_NODES: 1295 package foo diff --git a/js/js.translator/testData/box/enum/standardMethods.kt b/js/js.translator/testData/box/enum/standardMethods.kt index 440ebb55281..29d5fb4ec54 100644 --- a/js/js.translator/testData/box/enum/standardMethods.kt +++ b/js/js.translator/testData/box/enum/standardMethods.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JS_IR // EXPECTED_REACHABLE_NODES: 1555 package foo diff --git a/libraries/stdlib/js/irRuntime/Enum.kt b/libraries/stdlib/js/irRuntime/Enum.kt index f58405e9a29..1160f711da3 100644 --- a/libraries/stdlib/js/irRuntime/Enum.kt +++ b/libraries/stdlib/js/irRuntime/Enum.kt @@ -5,9 +5,9 @@ package kotlin -public class Enum>(val name: String, val ordinal: Int) : Comparable> { +public class Enum>(val name: String, val ordinal: Int) : Comparable { - override fun compareTo(other: Enum) = ordinal.compareTo(other.ordinal) + override fun compareTo(other: E) = ordinal.compareTo(other.ordinal) override fun equals(other: Any?) = this === other diff --git a/libraries/stdlib/js/irRuntime/core.kt b/libraries/stdlib/js/irRuntime/core.kt index 83f200c0d00..dca15ae1154 100644 --- a/libraries/stdlib/js/irRuntime/core.kt +++ b/libraries/stdlib/js/irRuntime/core.kt @@ -85,5 +85,4 @@ fun getNumberHashCode(obj: dynamic) = js(""" } """).unsafeCast() -// TODO: Use getObjectHashCode instead -fun identityHashCode(obj: dynamic): Int = hashCode(obj) \ No newline at end of file +fun identityHashCode(obj: dynamic): Int = getObjectHashCode(obj) \ No newline at end of file