From df8684051524247ef8848e698dc62cdd96b7d82d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 12 Jul 2016 16:34:09 +0300 Subject: [PATCH] KT-13025: when receiver of call of 'invoke' method is a Function, check whether receiver is an extension function and generate additional '.call' invocation. Fix #KT-13025 --- .../js/test/semantics/InvokeConventionTest.java | 4 ++++ .../callTranslator/FunctionCallCases.kt | 10 +++++++++- .../expression/invoke/cases/extensionInvoke.kt | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 js/js.translator/testData/expression/invoke/cases/extensionInvoke.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InvokeConventionTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InvokeConventionTest.java index e0d591598ff..85a85751b9d 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InvokeConventionTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InvokeConventionTest.java @@ -61,4 +61,8 @@ public final class InvokeConventionTest extends AbstractExpressionTest { public void testInvokeWithImplicitDispatchReceiverAndExtensionReceiver() throws Exception { checkFooBoxIsOk(); } + + public void testExtensionInvoke() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt index 0e87b61249a..c1ce92404d2 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.js.translate.callTranslator import com.google.dart.compiler.backend.js.ast.* import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor +import org.jetbrains.kotlin.builtins.isExtensionFunctionType import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.Visibilities @@ -179,7 +180,14 @@ object InvokeIntrinsic : FunctionCallCase() { } override fun FunctionCallInfo.dispatchReceiver(): JsExpression { - return JsInvocation(dispatchReceiver!!, argumentsInfo.translateArguments) + val receiver = resolvedCall.dispatchReceiver!! + val jsReceiver = if (receiver.type.isExtensionFunctionType) { + pureFqn(Namer.CALL_FUNCTION, dispatchReceiver) + } + else { + dispatchReceiver!! + } + return JsInvocation(jsReceiver, argumentsInfo.translateArguments) } /** diff --git a/js/js.translator/testData/expression/invoke/cases/extensionInvoke.kt b/js/js.translator/testData/expression/invoke/cases/extensionInvoke.kt new file mode 100644 index 00000000000..91e36f7e5a8 --- /dev/null +++ b/js/js.translator/testData/expression/invoke/cases/extensionInvoke.kt @@ -0,0 +1,16 @@ +package foo + +class A(val f: (B.() -> Int)?) + +class B(val x: Int) + +fun test(g: (B.() -> Int)?): Int? { + val a = A(g) + val b = B(2) + return a.f?.invoke(b) +} + +fun box(): String { + assertEquals(5, test { x + 3 }) + return "OK" +} \ No newline at end of file