JS: support explicit SAM constructor invocation

This commit is contained in:
Anton Bannykh
2020-04-09 15:34:11 +03:00
parent 0ee32ea9c5
commit 81798282b9
10 changed files with 99 additions and 4 deletions
@@ -9444,6 +9444,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt");
}
@TestMetadata("samConstructorExplicitInvocation.kt")
public void testSamConstructorExplicitInvocation() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/samConstructorExplicitInvocation.kt");
}
@TestMetadata("suspendFunInterfaceConversionCodegen.kt")
public void testSuspendFunInterfaceConversionCodegen() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/suspendFunInterfaceConversionCodegen.kt");
@@ -9444,6 +9444,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt");
}
@TestMetadata("samConstructorExplicitInvocation.kt")
public void testSamConstructorExplicitInvocation() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/samConstructorExplicitInvocation.kt");
}
@TestMetadata("suspendFunInterfaceConversionCodegen.kt")
public void testSuspendFunInterfaceConversionCodegen() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/suspendFunInterfaceConversionCodegen.kt");
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.sam.SamConstructorDescriptor
import org.jetbrains.kotlin.util.OperatorNameConventions
import java.util.*
@@ -184,7 +185,7 @@ object InvokeIntrinsic : FunctionCallCase() {
object ConstructorCallCase : FunctionCallCase() {
fun canApply(callInfo: FunctionCallInfo): Boolean {
return callInfo.callableDescriptor is ConstructorDescriptor
return callInfo.callableDescriptor is ConstructorDescriptor || callInfo.callableDescriptor is SamConstructorDescriptor
}
override fun FunctionCallInfo.noReceivers() = doTranslate { translateArguments }
@@ -197,6 +198,11 @@ object ConstructorCallCase : FunctionCallCase() {
getArguments: CallArgumentTranslator.ArgumentsInfo.() -> List<JsExpression>
): JsExpression {
val functionRef = ReferenceTranslator.translateAsValueReference(callableDescriptor, context)
if (callableDescriptor is SamConstructorDescriptor) {
return JsNew(functionRef, argumentsInfo.getArguments())
}
val invocationArguments = mutableListOf<JsExpression>()
val constructorDescriptor = callableDescriptor as ClassConstructorDescriptor
@@ -592,7 +592,7 @@ class ClassTranslator private constructor(
listOfNotNull(innerContext.continuationParameterDescriptor)
val arguments = parameters.map {
TranslationUtils.coerce(innerContext, innerContext.getAliasForDescriptor(it)!!, context.currentModule.builtIns.anyType)
TranslationUtils.coerce(innerContext, ReferenceTranslator.translateAsValueReference(it, innerContext), context.currentModule.builtIns.anyType)
}
function.body = JsBlock(JsReturn(JsInvocation(pureFqn(Namer.SAM_FIELD_NAME, JsThisRef()), arguments)))