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