extension literal support and tests.
This commit is contained in:
@@ -362,7 +362,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
||||
@NotNull
|
||||
public JsNode visitFunctionLiteralExpression(@NotNull JetFunctionLiteralExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
return Translation.functionTranslator(expression.getFunctionLiteral(), context).translateAsLiteral();
|
||||
return Translation.functionTranslator(expression, context).translateAsLiteral();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -89,7 +89,7 @@ public final class FunctionTranslator extends AbstractTranslator {
|
||||
}
|
||||
|
||||
private boolean isLiteral() {
|
||||
return functionDeclaration instanceof JetFunctionLiteral;
|
||||
return functionDeclaration instanceof JetFunctionLiteralExpression;
|
||||
}
|
||||
|
||||
private boolean isDeclaration() {
|
||||
@@ -174,9 +174,9 @@ public final class FunctionTranslator extends AbstractTranslator {
|
||||
}
|
||||
|
||||
private boolean isExtensionFunction() {
|
||||
if (!(functionDeclaration instanceof JetNamedFunction)) {
|
||||
return false;
|
||||
}
|
||||
// if (!(functionDeclaration instanceof JetNamedFunction)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
FunctionDescriptor functionDescriptor = getFunctionDescriptor(context().bindingContext(), functionDeclaration);
|
||||
return DescriptorUtils.isExtensionFunction(functionDescriptor);
|
||||
|
||||
@@ -161,14 +161,21 @@ public final class CallTranslator extends AbstractTranslator {
|
||||
private JsExpression qualifiedMethodReference(@NotNull DeclarationDescriptor descriptor) {
|
||||
JsExpression methodReference = ReferenceTranslator.translateReference(descriptor, context());
|
||||
if (isExtensionFunction()) {
|
||||
AstUtil.setQualifier(methodReference,
|
||||
TranslationUtils.getExtensionFunctionImplicitReceiver(context(), functionDescriptor));
|
||||
return extensionFunctionReference(methodReference);
|
||||
} else if (receiver != null) {
|
||||
AstUtil.setQualifier(methodReference, receiver);
|
||||
}
|
||||
return methodReference;
|
||||
}
|
||||
|
||||
private JsExpression extensionFunctionReference(@NotNull JsExpression methodReference) {
|
||||
JsExpression qualifier = TranslationUtils.getExtensionFunctionImplicitReceiver(context(), functionDescriptor);
|
||||
if (qualifier != null) {
|
||||
AstUtil.setQualifier(methodReference, qualifier);
|
||||
}
|
||||
return methodReference;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression constructorCall() {
|
||||
JsNew constructorCall = new JsNew(qualifiedMethodReference(functionDescriptor));
|
||||
|
||||
@@ -208,12 +208,10 @@ public final class TranslationUtils {
|
||||
return getImplicitReceiverBasedOnOwner(context, referencedDescriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Nullable
|
||||
public static JsExpression getExtensionFunctionImplicitReceiver(@NotNull TranslationContext context,
|
||||
@NotNull FunctionDescriptor descriptor) {
|
||||
JsExpression receiverBasedOnOwner = getImplicitReceiverBasedOnOwner(context, descriptor);
|
||||
assert receiverBasedOnOwner != null : "Extension function must have receiver based on owner.";
|
||||
return receiverBasedOnOwner;
|
||||
return getImplicitReceiverBasedOnOwner(context, descriptor);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -42,4 +42,9 @@ public final class ExtensionFunctionTest extends TranslationTest {
|
||||
public void virtualExtensionOverride() throws Exception {
|
||||
testFooBoxIsTrue("virtualExtensionOverride.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionLiteralPassedToFunction() throws Exception {
|
||||
testFooBoxIsTrue("extensionLiteralPassedToFunction.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace foo
|
||||
|
||||
fun apply(i : Int, f : fun Int.(Int) : Int) = i.f(1);
|
||||
|
||||
fun box() : Boolean {
|
||||
return (apply(1, {i => i + this})) == 2
|
||||
}
|
||||
Reference in New Issue
Block a user