don't forget to put receiver on stack when calling extension functions
This commit is contained in:
@@ -722,13 +722,17 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
methodDescriptor.getDescriptor());
|
methodDescriptor.getDescriptor());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
methodDescriptor = typeMapper.mapSignature((JetFunction) declarationPsiElement);
|
final JetFunction jetFunction = (JetFunction) declarationPsiElement;
|
||||||
if (functionParent instanceof NamespaceDescriptorImpl && declarationPsiElement instanceof JetFunction) {
|
methodDescriptor = typeMapper.mapSignature(jetFunction);
|
||||||
|
if (functionParent instanceof NamespaceDescriptorImpl) {
|
||||||
|
if (jetFunction.getReceiverTypeRef() != null) {
|
||||||
|
ensureReceiverOnStack(expression);
|
||||||
|
}
|
||||||
pushMethodArguments(expression, methodDescriptor);
|
pushMethodArguments(expression, methodDescriptor);
|
||||||
final String owner = NamespaceCodegen.getJVMClassName(DescriptorRenderer.getFQName(functionParent));
|
final String owner = NamespaceCodegen.getJVMClassName(DescriptorRenderer.getFQName(functionParent));
|
||||||
v.invokestatic(owner, methodDescriptor.getName(), methodDescriptor.getDescriptor());
|
v.invokestatic(owner, methodDescriptor.getName(), methodDescriptor.getDescriptor());
|
||||||
}
|
}
|
||||||
else if (functionParent instanceof ClassDescriptor && declarationPsiElement instanceof JetFunction) {
|
else if (functionParent instanceof ClassDescriptor) {
|
||||||
ensureReceiverOnStack(expression);
|
ensureReceiverOnStack(expression);
|
||||||
pushMethodArguments(expression, methodDescriptor);
|
pushMethodArguments(expression, methodDescriptor);
|
||||||
final String owner = JetTypeMapper.jvmNameForInterface((ClassDescriptor) functionParent);
|
final String owner = JetTypeMapper.jvmNameForInterface((ClassDescriptor) functionParent);
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
fun StringBuilder.takeFirst(): Char {
|
||||||
|
if (this.length() == 0) return '\0'
|
||||||
|
val c = this.charAt(0)
|
||||||
|
this.deleteCharAt(0)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(expr: StringBuilder): Int {
|
||||||
|
val c = expr.takeFirst()
|
||||||
|
when(c) {
|
||||||
|
'\0' => throw new Exception("zero")
|
||||||
|
else => throw new Exception("nonzero" + c)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,9 +12,8 @@ import org.jetbrains.jet.lang.psi.JetNamespace;
|
|||||||
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
||||||
import org.jetbrains.jet.parsing.JetParsingTest;
|
import org.jetbrains.jet.parsing.JetParsingTest;
|
||||||
|
|
||||||
import javax.swing.JComponent;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.channels.NonWritableChannelException;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -25,6 +24,17 @@ import java.util.Map;
|
|||||||
public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
|
public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
|
||||||
private MyClassLoader myClassLoader;
|
private MyClassLoader myClassLoader;
|
||||||
|
|
||||||
|
protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException {
|
||||||
|
boolean caught = false;
|
||||||
|
try {
|
||||||
|
foo.invoke(instance, args);
|
||||||
|
}
|
||||||
|
catch(InvocationTargetException ex) {
|
||||||
|
caught = exceptionClass.isInstance(ex.getTargetException());
|
||||||
|
}
|
||||||
|
assertTrue(caught);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
@@ -128,14 +138,19 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Codegens generateClassesInFile() {
|
protected Codegens generateClassesInFile() {
|
||||||
Codegens state = new Codegens(getProject(), false);
|
try {
|
||||||
JetFile jetFile = (JetFile) myFixture.getFile();
|
Codegens state = new Codegens(getProject(), false);
|
||||||
AnalyzingUtils.checkForSyntacticErrors(jetFile);
|
JetFile jetFile = (JetFile) myFixture.getFile();
|
||||||
final JetNamespace namespace = jetFile.getRootNamespace();
|
AnalyzingUtils.checkForSyntacticErrors(jetFile);
|
||||||
|
final JetNamespace namespace = jetFile.getRootNamespace();
|
||||||
|
|
||||||
NamespaceCodegen codegen = state.forNamespace(namespace);
|
NamespaceCodegen codegen = state.forNamespace(namespace);
|
||||||
codegen.generate(namespace);
|
codegen.generate(namespace);
|
||||||
return state;
|
return state;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
System.out.println(generateToText());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Method generateFunction() {
|
protected Method generateFunction() {
|
||||||
|
|||||||
@@ -17,4 +17,10 @@ public class ExtensionFunctionsTest extends CodegenTestCase {
|
|||||||
final Character c = (Character) foo.invoke(null);
|
final Character c = (Character) foo.invoke(null);
|
||||||
assertEquals('f', c.charValue());
|
assertEquals('f', c.charValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testWhenFail() throws Exception {
|
||||||
|
loadFile();
|
||||||
|
Method foo = generateFunction("foo");
|
||||||
|
assertThrows(foo, Exception.class, null, new StringBuilder());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.jetbrains.jet.codegen;
|
|||||||
|
|
||||||
import jet.NoPatternMatchedException;
|
import jet.NoPatternMatchedException;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,14 +24,7 @@ public class PatternMatchingTest extends CodegenTestCase {
|
|||||||
loadFile();
|
loadFile();
|
||||||
Method foo = generateFunction();
|
Method foo = generateFunction();
|
||||||
assertTrue((Boolean) foo.invoke(null, 0));
|
assertTrue((Boolean) foo.invoke(null, 0));
|
||||||
boolean caught = false;
|
assertThrows(foo, NoPatternMatchedException.class, null, 1);
|
||||||
try {
|
|
||||||
foo.invoke(null, 1);
|
|
||||||
}
|
|
||||||
catch(InvocationTargetException ex) {
|
|
||||||
caught = ex.getTargetException() instanceof NoPatternMatchedException;
|
|
||||||
}
|
|
||||||
assertTrue(caught);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPattern() throws Exception {
|
public void testPattern() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user