generate .size property for arrays

This commit is contained in:
Dmitry Jemerov
2011-05-20 13:54:15 +02:00
parent c943525e08
commit 4c7768b47d
2 changed files with 23 additions and 6 deletions
@@ -445,8 +445,8 @@ public class ExpressionCodegen extends JetVisitor {
@Override
public void visitSimpleNameExpression(JetSimpleNameExpression expression) {
final DeclarationDescriptor descriptor = bindingContext.resolveReferenceExpression(expression);
final DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (descriptor instanceof VariableDescriptor) {
final DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (isClass(container, "Number")) {
Type castType = getCastType(expression.getReferencedName());
if (castType != null) {
@@ -493,13 +493,20 @@ public class ExpressionCodegen extends JetVisitor {
}
}
boolean isStatic = descriptor.getContainingDeclaration() instanceof NamespaceDescriptorImpl;
final boolean directToField = expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER;
final StackValue iValue = intermediateValueForProperty(propertyDescriptor, directToField);
if (!isStatic) {
if (isClass(container, "Array") && propertyDescriptor.getName().equals("size")) {
ensureReceiverOnStack(expression);
v.arraylength();
myStack.push(StackValue.onStack(Type.INT_TYPE));
}
else {
boolean isStatic = container instanceof NamespaceDescriptorImpl;
final boolean directToField = expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER;
final StackValue iValue = intermediateValueForProperty(propertyDescriptor, directToField);
if (!isStatic) {
ensureReceiverOnStack(expression);
}
myStack.push(iValue);
}
myStack.push(iValue);
}
else {
throw new UnsupportedOperationException("don't know how to generate reference " + descriptor);
@@ -367,6 +367,16 @@ public class NamespaceGenTest extends CodegenTestCase {
assertEquals(4, result.length);
}
public void testArraySize() throws Exception {
loadText("fun foo(a: Array<Int>) = a.size");
System.out.println(generateToText());
final Method main = generateFunction();
Object[] args = new Object[] { new int[4] };
int result = (Integer) main.invoke(null, args);
assertEquals(4, result);
}
public void testIntRange() throws Exception {
loadText("fun foo() = 1..10");
final Method main = generateFunction();