Skip to content

Commit

Permalink
GROOVY-10031
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 12, 2021
1 parent 0500084 commit 9d6d7ee
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ public void testArrayMulti6() {
runConformTest(sources, "[[Ljava.lang.String;");
}

@Test
public void testArrayMulti7() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[][] array = new int[1][]\n" +
"assert array.length == 1\n" +
"assert array[0] == null\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[I");
}

@Test
public void testClosuresBasic() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,10 @@ public void loadWrapper(Expression argument) {

public void visitArrayExpression(ArrayExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
/* GRECLIPSE edit
ClassNode elementType = expression.getElementType();
String arrayTypeName = BytecodeHelper.getClassInternalName(elementType);
*/
List sizeExpression = expression.getSizeExpression();

int size = 0;
Expand All @@ -1736,6 +1738,7 @@ public void visitArrayExpression(ArrayExpression expression) {
BytecodeHelper.pushConstant(mv, size);
}

/* GRECLIPSE edit -- GROOVY-10031: use NEWARRAY/ANEWARRAY for flat array
int storeIns = AASTORE;
if (sizeExpression != null) {
arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType());
Expand Down Expand Up @@ -1771,6 +1774,47 @@ public void visitArrayExpression(ArrayExpression expression) {
} else {
mv.visitTypeInsn(ANEWARRAY, arrayTypeName);
}
*/
ClassNode arrayType = expression.getType();
ClassNode elementType = arrayType.getComponentType();

int storeIns = AASTORE;
if (!elementType.isArray() || sizeExpression == null) {
if (ClassHelper.isPrimitiveType(elementType)) {
int primType = 0;
if (elementType == ClassHelper.boolean_TYPE) {
primType = T_BOOLEAN;
storeIns = BASTORE;
} else if (elementType == ClassHelper.byte_TYPE) {
primType = T_BYTE;
storeIns = BASTORE;
} else if (elementType == ClassHelper.char_TYPE) {
primType = T_CHAR;
storeIns = CASTORE;
} else if (elementType == ClassHelper.double_TYPE) {
primType = T_DOUBLE;
storeIns = DASTORE;
} else if (elementType == ClassHelper.float_TYPE) {
primType = T_FLOAT;
storeIns = FASTORE;
} else if (elementType == ClassHelper.int_TYPE) {
primType = T_INT;
storeIns = IASTORE;
} else if (elementType == ClassHelper.long_TYPE) {
primType = T_LONG;
storeIns = LASTORE;
} else if (elementType == ClassHelper.short_TYPE) {
primType = T_SHORT;
storeIns = SASTORE;
}
mv.visitIntInsn(NEWARRAY, primType);
} else {
mv.visitTypeInsn(ANEWARRAY, BytecodeHelper.getClassInternalName(elementType));
}
} else {
mv.visitMultiANewArrayInsn(BytecodeHelper.getTypeDescription(arrayType), dimensions);
}
// GRECLIPSE end

for (int i = 0; i < size; i++) {
mv.visitInsn(DUP);
Expand All @@ -1786,7 +1830,7 @@ public void visitArrayExpression(ArrayExpression expression) {
controller.getOperandStack().remove(1);
}

controller.getOperandStack().push(expression.getType());
controller.getOperandStack().push(arrayType);
}

public void visitClosureListExpression(ClosureListExpression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1602,9 +1602,10 @@ public void loadWrapper(final Expression argument) {
@Override
public void visitArrayExpression(final ArrayExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
/* GRECLIPSE edit
ClassNode elementType = expression.getElementType();
String arrayTypeName = BytecodeHelper.getClassInternalName(elementType);

*/
int size = 0;
int dimensions = 0;
if (expression.hasInitializer()) {
Expand All @@ -1621,6 +1622,7 @@ public void visitArrayExpression(final ArrayExpression expression) {
controller.getOperandStack().remove(dimensions);
}

/* GRECLIPSE edit -- GROOVY-10031: use NEWARRAY/ANEWARRAY for flat array
int storeIns = AASTORE;
if (expression.hasInitializer()) {
if (ClassHelper.isPrimitiveType(elementType)) {
Expand Down Expand Up @@ -1658,6 +1660,47 @@ public void visitArrayExpression(final ArrayExpression expression) {
arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType());
mv.visitMultiANewArrayInsn(arrayTypeName, dimensions);
}
*/
ClassNode arrayType = expression.getType();
ClassNode elementType = arrayType.getComponentType();

int storeIns = AASTORE;
if (!elementType.isArray() || expression.hasInitializer()) {
if (ClassHelper.isPrimitiveType(elementType)) {
int primType = 0;
if (elementType == ClassHelper.boolean_TYPE) {
primType = T_BOOLEAN;
storeIns = BASTORE;
} else if (elementType == ClassHelper.byte_TYPE) {
primType = T_BYTE;
storeIns = BASTORE;
} else if (elementType == ClassHelper.char_TYPE) {
primType = T_CHAR;
storeIns = CASTORE;
} else if (elementType == ClassHelper.double_TYPE) {
primType = T_DOUBLE;
storeIns = DASTORE;
} else if (elementType == ClassHelper.float_TYPE) {
primType = T_FLOAT;
storeIns = FASTORE;
} else if (elementType == ClassHelper.int_TYPE) {
primType = T_INT;
storeIns = IASTORE;
} else if (elementType == ClassHelper.long_TYPE) {
primType = T_LONG;
storeIns = LASTORE;
} else if (elementType == ClassHelper.short_TYPE) {
primType = T_SHORT;
storeIns = SASTORE;
}
mv.visitIntInsn(NEWARRAY, primType);
} else {
mv.visitTypeInsn(ANEWARRAY, BytecodeHelper.getClassInternalName(elementType));
}
} else {
mv.visitMultiANewArrayInsn(BytecodeHelper.getTypeDescription(arrayType), dimensions);
}
// GRECLIPSE end

for (int i = 0; i < size; i += 1) {
mv.visitInsn(DUP);
Expand All @@ -1673,7 +1716,7 @@ public void visitArrayExpression(final ArrayExpression expression) {
controller.getOperandStack().remove(1);
}

controller.getOperandStack().push(expression.getType());
controller.getOperandStack().push(arrayType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1669,9 +1669,10 @@ public void loadWrapper(final Expression argument) {
@Override
public void visitArrayExpression(final ArrayExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
/* GRECLIPSE edit
ClassNode elementType = expression.getElementType();
String arrayTypeName = BytecodeHelper.getClassInternalName(elementType);

*/
int size = 0;
int dimensions = 0;
if (expression.hasInitializer()) {
Expand All @@ -1688,6 +1689,7 @@ public void visitArrayExpression(final ArrayExpression expression) {
controller.getOperandStack().remove(dimensions);
}

/* GRECLIPSE edit -- GROOVY-10031: use NEWARRAY/ANEWARRAY for flat array
int storeIns = AASTORE;
if (expression.hasInitializer()) {
if (ClassHelper.isPrimitiveType(elementType)) {
Expand Down Expand Up @@ -1725,6 +1727,47 @@ public void visitArrayExpression(final ArrayExpression expression) {
arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType());
mv.visitMultiANewArrayInsn(arrayTypeName, dimensions);
}
*/
ClassNode arrayType = expression.getType();
ClassNode elementType = arrayType.getComponentType();

int storeIns = AASTORE;
if (!elementType.isArray() || expression.hasInitializer()) {
if (ClassHelper.isPrimitiveType(elementType)) {
int primType = 0;
if (elementType == ClassHelper.boolean_TYPE) {
primType = T_BOOLEAN;
storeIns = BASTORE;
} else if (elementType == ClassHelper.byte_TYPE) {
primType = T_BYTE;
storeIns = BASTORE;
} else if (elementType == ClassHelper.char_TYPE) {
primType = T_CHAR;
storeIns = CASTORE;
} else if (elementType == ClassHelper.double_TYPE) {
primType = T_DOUBLE;
storeIns = DASTORE;
} else if (elementType == ClassHelper.float_TYPE) {
primType = T_FLOAT;
storeIns = FASTORE;
} else if (elementType == ClassHelper.int_TYPE) {
primType = T_INT;
storeIns = IASTORE;
} else if (elementType == ClassHelper.long_TYPE) {
primType = T_LONG;
storeIns = LASTORE;
} else if (elementType == ClassHelper.short_TYPE) {
primType = T_SHORT;
storeIns = SASTORE;
}
mv.visitIntInsn(NEWARRAY, primType);
} else {
mv.visitTypeInsn(ANEWARRAY, BytecodeHelper.getClassInternalName(elementType));
}
} else {
mv.visitMultiANewArrayInsn(BytecodeHelper.getTypeDescription(arrayType), dimensions);
}
// GRECLIPSE end

for (int i = 0; i < size; i += 1) {
mv.visitInsn(DUP);
Expand All @@ -1740,7 +1783,7 @@ public void visitArrayExpression(final ArrayExpression expression) {
controller.getOperandStack().remove(1);
}

controller.getOperandStack().push(expression.getType());
controller.getOperandStack().push(arrayType);
}

@Override
Expand Down

0 comments on commit 9d6d7ee

Please sign in to comment.