[IR] [JS_IR] Supported fun interfaces in JS

This commit is contained in:
Igor Chevdar
2020-02-06 12:07:12 +03:00
parent 4a7b4d655c
commit 9bc7a43991
8 changed files with 45 additions and 6 deletions
@@ -415,7 +415,7 @@ fun IrClass.createImplicitParameterDeclarationWithWrappedDescriptor() {
startOffset, endOffset,
IrDeclarationOrigin.INSTANCE_RECEIVER,
IrValueParameterSymbolImpl(thisReceiverDescriptor),
Name.identifier("<this>"),
Name.special("<this>"),
index = -1,
type = symbol.typeWithParameters(typeParameters),
varargElementType = null,
@@ -65,6 +65,8 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
abstract fun getSuperTypeForWrapper(typeOperand: IrType): IrType
override fun lower(irFile: IrFile) {
cachedImplementations.clear()
inlineCachedImplementations.clear()
enclosingContainer = irFile.declarations.filterIsInstance<IrClass>().find { it.isFileClass }
?: irFile
irFile.transformChildrenVoid()
@@ -475,11 +475,21 @@ private val bridgesConstructionPhase = makeDeclarationTransformerPhase(
prerequisite = setOf(suspendFunctionsLoweringPhase)
)
private val singleAbstractMethodPhase = makeIrModulePhase(
::JsSingleAbstractMethodLowering,
name = "SingleAbstractMethod",
description = "Replace SAM conversions with instances of interface-implementing classes"
).toModuleLowering()
private val typeOperatorLoweringPhase = makeBodyLoweringPhase(
::TypeOperatorLowering,
name = "TypeOperatorLowering",
description = "Lower IrTypeOperator with corresponding logic",
prerequisite = setOf(bridgesConstructionPhase, removeInlineFunctionsWithReifiedTypeParametersLoweringPhase)
prerequisite = setOf(
bridgesConstructionPhase,
removeInlineFunctionsWithReifiedTypeParametersLoweringPhase,
singleAbstractMethodPhase
)
)
private val secondaryConstructorLoweringPhase = makeDeclarationTransformerPhase(
@@ -580,6 +590,7 @@ val loweringList = listOf<Lowering>(
copyInlineFunctionBodyLoweringPhase,
createScriptFunctionsPhase,
provisionalFunctionExpressionPhase,
singleAbstractMethodPhase,
lateinitNullableFieldsPhase,
lateinitDeclarationLoweringPhase,
lateinitUsageLoweringPhase,
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.lower.SingleAbstractMethodLowering
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.util.render
class JsSingleAbstractMethodLowering(context: JsIrBackendContext) : SingleAbstractMethodLowering(context) {
override fun getSuperTypeForWrapper(typeOperand: IrType): IrType {
// FE doesn't allow type parameters for now.
// And since there is a to-do in common SingleAbstractMethodLowering (at function visitTypeOperator),
// we don't have to be more saint than a pope here.
return typeOperand.classOrNull?.defaultType ?: error("Unsupported SAM conversion: ${typeOperand.render()}")
}
}
@@ -1,6 +1,7 @@
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS, JS_IR
// IGNORE_BACKEND: JS
// SKIP_DCE_DRIVEN
fun interface Foo {
fun invoke(): String
+2 -1
View File
@@ -1,7 +1,8 @@
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS, JS_IR
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// SKIP_DCE_DRIVEN
fun interface KRunnable {
fun invoke()
+2 -1
View File
@@ -1,7 +1,8 @@
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS, JS_IR
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// SKIP_DCE_DRIVEN
fun interface Fn<T, R> {
fun run(s: String, i: Int, t: T): R
@@ -1,7 +1,8 @@
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS, JS_IR
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// SKIP_DCE_DRIVEN
fun interface KRunnable {
fun invoke()