tuple literals
This commit is contained in:
@@ -1788,6 +1788,29 @@ public class ExpressionCodegen extends JetVisitor {
|
||||
myMap.leaveTemp(subjectType.getSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTupleExpression(JetTupleExpression expression) {
|
||||
final List<JetExpression> entries = expression.getEntries();
|
||||
if (entries.size() > 22) {
|
||||
throw new UnsupportedOperationException("tuple too large");
|
||||
}
|
||||
final String className = "jet/Tuple" + entries.size();
|
||||
Type tupleType = Type.getObjectType(className);
|
||||
StringBuilder signature = new StringBuilder("(");
|
||||
for (JetExpression entry : entries) {
|
||||
signature.append("Ljava/lang/Object;");
|
||||
}
|
||||
signature.append(")V");
|
||||
|
||||
v.anew(tupleType);
|
||||
v.dup();
|
||||
for (JetExpression entry : entries) {
|
||||
gen(entry, OBJECT_TYPE);
|
||||
}
|
||||
v.invokespecial(className, "<init>", signature.toString());
|
||||
myStack.push(StackValue.onStack(tupleType));
|
||||
}
|
||||
|
||||
private void throwNewException(final String className) {
|
||||
v.anew(Type.getObjectType(className));
|
||||
v.dup();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import jet.IntRange;
|
||||
import jet.Tuple2;
|
||||
import org.jetbrains.jet.parsing.JetParsingTest;
|
||||
|
||||
import java.awt.*;
|
||||
@@ -435,4 +436,12 @@ public class NamespaceGenTest extends CodegenTestCase {
|
||||
main.invoke(null, list);
|
||||
assertEquals("JetLanguage", list.get(0));
|
||||
}
|
||||
|
||||
public void testTupleLiteral() throws Exception {
|
||||
loadText("fun foo() = (1, \"foo\")");
|
||||
final Method main = generateFunction();
|
||||
Tuple2 tuple2 = (Tuple2) main.invoke(null);
|
||||
assertEquals(1, tuple2._1);
|
||||
assertEquals("foo", tuple2._2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user