Skip to content

Commit

Permalink
fix: abstract duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
jumanji144 committed Jun 26, 2024
1 parent 130a689 commit 982710b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,12 @@ public void execute(SimpleInstruction instruction) {
case ACONST_NULL -> frame.pushNull();
case RETURN -> { /* no-op */ }
case AASTORE -> {
ClassType valueType = frame.pop();
ClassType indexType = frame.pop();
ClassType arrayType = frame.pop();
if (!(indexType instanceof PrimitiveType))
warn(instruction, "Array index on stack is not a primitive");
if (!(arrayType instanceof ArrayType))
warn(instruction, "Array reference on stack is not an array");
ClassType valueType = doArrayStore(instruction);
if (!(valueType instanceof ObjectType))
warn(instruction, "Value to store in array is not a reference");
}
case IASTORE, FASTORE, BASTORE, CASTORE, SASTORE -> {
ClassType valueType = frame.pop();
ClassType indexType = frame.pop();
ClassType arrayType = frame.pop();
if (!(indexType instanceof PrimitiveType))
warn(instruction, "Array index on stack is not a primitive");
if (!(arrayType instanceof ArrayType))
warn(instruction, "Array reference on stack is not an array");
ClassType valueType = doArrayStore(instruction);
if (!(valueType instanceof PrimitiveType))
warn(instruction, "Value to store in array is not a primitive");
}
Expand Down Expand Up @@ -276,6 +264,17 @@ else if (stackType instanceof InstanceType)
}
}

private ClassType doArrayStore(SimpleInstruction instruction) {
ClassType valueType = frame.pop();
ClassType indexType = frame.pop();
ClassType arrayType = frame.pop();
if (!(indexType instanceof PrimitiveType))
warn(instruction, "Array index on stack is not a primitive");
if (!(arrayType instanceof ArrayType))
warn(instruction, "Array reference on stack is not an array");
return valueType;
}

@Override
public void execute(ConstantInstruction<?> instruction) {
Constant constant = instruction.constant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,12 @@ public void execute(SimpleInstruction instruction) {
case RETURN -> {
/* no-op */ }
case AASTORE -> {
ClassType valueType = frame.pop().type();
ClassType indexType = frame.pop().type();
ClassType arrayType = frame.pop().type();
if (!(indexType instanceof PrimitiveType))
warn(instruction, "Array index on stack is not a primitive");
if (!(arrayType instanceof ArrayType))
warn(instruction, "Array reference on stack is not an array");
ClassType valueType = doArrayStore(instruction);
if (!(valueType instanceof ObjectType))
warn(instruction, "Value to store in array is not a reference");
}
case IASTORE, FASTORE , BASTORE, CASTORE, SASTORE -> {
ClassType valueType = frame.pop().type();
ClassType indexType = frame.pop().type();
ClassType arrayType = frame.pop().type();
if (!(indexType instanceof PrimitiveType))
warn(instruction, "Array index on stack is not a primitive");
if (!(arrayType instanceof ArrayType))
warn(instruction, "Array reference on stack is not an array");
ClassType valueType = doArrayStore(instruction);
if (!(valueType instanceof PrimitiveType))
warn(instruction, "Value to store in array is not a primitive");
}
Expand Down Expand Up @@ -428,6 +416,17 @@ else if (stackType instanceof InstanceType)
}
}

private ClassType doArrayStore(SimpleInstruction instruction) {
ClassType valueType = frame.pop().type();
ClassType indexType = frame.pop().type();
ClassType arrayType = frame.pop().type();
if (!(indexType instanceof PrimitiveType))
warn(instruction, "Array index on stack is not a primitive");
if (!(arrayType instanceof ArrayType))
warn(instruction, "Array reference on stack is not an array");
return valueType;
}

@Override
public void execute(ConstantInstruction<?> instruction) {
Constant constant = instruction.constant();
Expand Down

0 comments on commit 982710b

Please sign in to comment.