Skip to content

Commit

Permalink
feat: Support desc format in 'new' instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Aug 5, 2024
1 parent 45837eb commit 48ef26b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,21 @@ public void visitJumpInsn(ASTIdentifier label) {

@Override
public void visitTypeInsn(ASTIdentifier type) {
String literal = type.literal();
if (opcode == NEW) {
add(new AllocateInstruction(Types.instanceTypeFromInternalName(type.literal())));
char first = literal.charAt(0);
if (first == 'L' && literal.charAt(literal.length()-1) == ';')
literal = literal.substring(1, literal.length()-1); // Adapt if user put in desc format accidentally
else if (first == '[')
throw new IllegalStateException("Cannot use 'new' to allocate an array type");
add(new AllocateInstruction(Types.instanceTypeFromInternalName(literal)));
} else if (opcode == ANEWARRAY) {
ClassType arrayType = new TypeReader(type.literal()).requireClassType();
ClassType arrayType = new TypeReader(literal).requireClassType();
if (arrayType instanceof PrimitiveType)
throw new IllegalStateException("Cannot create primitive array: " + arrayType.descriptor());
add(new AllocateInstruction(Types.arrayType(arrayType)));
} else {
TypeReader reader = new TypeReader(type.literal());
TypeReader reader = new TypeReader(literal);
ObjectType objectType = Objects.requireNonNullElse((ObjectType) reader.read(), Types.OBJECT);
Instruction instruction = switch (opcode) {
case CHECKCAST -> new CheckCastInstruction(objectType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SKIP-ROUND-TRIP-EQUALITY
.super java/lang/Object
.class public super Example {
.method public exampleMethod ()V {
parameters: { this },
code: {
A:
new java/lang/String
pop
// Desc format will be mapped to proper internal name
new Ljava/lang/String;
pop
return
B:
}
}
}

0 comments on commit 48ef26b

Please sign in to comment.