Skip to content

Commit

Permalink
GROOVY-9199
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 1, 2019
1 parent 9a72850 commit d0d6a97
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ public void writeForStatement(ForStatement loop) {
writeForInLoop(loop);
}
}

protected void writeIteratorHasNext(MethodVisitor mv) {
iteratorHasNextMethod.call(mv);
}

protected void writeIteratorNext(MethodVisitor mv) {
iteratorNextMethod.call(mv);
}

protected void writeForInLoop(ForStatement loop) {
controller.getAcg().onLineNumber(loop,"visitForLoop");
writeStatementLabel(loop);

CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
Expand Down Expand Up @@ -221,7 +221,7 @@ protected void writeForLoopWithClosureList(ForStatement loop) {
controller.getCompileStack().pop();
controller.getCompileStack().pop();
}

private void visitExpressionOrStatement(Object o) {
if (o == EmptyExpression.INSTANCE) return;
if (o instanceof Expression) {
Expand Down Expand Up @@ -258,7 +258,7 @@ public void writeWhileLoop(WhileStatement loop) {
}
}

if(!boolHandled) {
if (!boolHandled) {
bool.visit(controller.getAcg());
controller.getOperandStack().jump(IFEQ, breakLabel);
}
Expand All @@ -268,7 +268,7 @@ public void writeWhileLoop(WhileStatement loop) {
mv.visitJumpInsn(GOTO, continueLabel);
mv.visitLabel(breakLabel);

controller.getCompileStack().pop();
controller.getCompileStack().pop();
}

public void writeDoWhileLoop(DoWhileStatement loop) {
Expand Down Expand Up @@ -315,25 +315,25 @@ public void writeIfElse(IfStatement ifElse) {
Label l1 = new Label();
mv.visitJumpInsn(GOTO, l1);
mv.visitLabel(l0);

controller.getCompileStack().pushBooleanExpression();
ifElse.getElseBlock().visit(controller.getAcg());
controller.getCompileStack().pop();

mv.visitLabel(l1);
}
}
}

public void writeTryCatchFinally(TryCatchStatement statement) {
controller.getAcg().onLineNumber(statement, "visitTryCatchFinally");
writeStatementLabel(statement);

MethodVisitor mv = controller.getMethodVisitor();
CompileStack compileStack = controller.getCompileStack();
OperandStack operandStack = controller.getOperandStack();

Statement tryStatement = statement.getTryStatement();
final Statement finallyStatement = statement.getFinallyStatement();
Statement finallyStatement = statement.getFinallyStatement();

// start try block, label needed for exception table
Label tryStart = new Label();
Expand Down Expand Up @@ -384,11 +384,10 @@ public void writeTryCatchFinally(TryCatchStatement statement) {
compileStack.writeExceptionTable(tryBlock, catchStart, exceptionTypeInternalName);
}

// Label used to handle exceptions in catches and regularly
// visited finals.
// used to handle exceptions in catches and regularly visited finals
Label catchAny = new Label();

// add "catch any" block to exception table for try part we do this
// add "catch any" block to exception table for try part we do this
// after the exception blocks, because else this one would supersede
// any of those otherwise
compileStack.writeExceptionTable(tryBlock, catchAny, null);
Expand All @@ -401,18 +400,22 @@ public void writeTryCatchFinally(TryCatchStatement statement) {
// start finally
mv.visitLabel(finallyStart);
finallyStatement.visit(controller.getAcg());
mv.visitInsn(NOP); //**

// goto after all-catching block
Label skipCatchAll = new Label();
mv.visitJumpInsn(GOTO, skipCatchAll);

// start a block catching any Exception
mv.visitLabel(catchAny);
//store exception
//TODO: maybe define a Throwable and use it here instead of Object
// store exception
// TODO: maybe define a Throwable and use it here instead of Object
operandStack.push(ClassHelper.OBJECT_TYPE);
final int anyExceptionIndex = compileStack.defineTemporaryVariable("exception", true);
int anyExceptionIndex = compileStack.defineTemporaryVariable("exception", true);

// GROOVY-9199
controller.resetLineNumber();
int line = finallyStatement.getLineNumber();
if (line > 0) mv.visitLineNumber(line, catchAny);

finallyStatement.visit(controller.getAcg());

Expand All @@ -423,17 +426,17 @@ public void writeTryCatchFinally(TryCatchStatement statement) {
mv.visitLabel(skipCatchAll);
compileStack.removeVar(anyExceptionIndex);
}
private BlockRecorder makeBlockRecorder(final Statement finallyStatement) {

private BlockRecorder makeBlockRecorder(Statement finallyStatement) {
final BlockRecorder block = new BlockRecorder();
Runnable tryRunner = new Runnable() {
block.excludedStatement = new Runnable() {
@Override
public void run() {
controller.getCompileStack().pushBlockRecorderVisit(block);
finallyStatement.visit(controller.getAcg());
controller.getCompileStack().popBlockRecorderVisit(block);
}
};
block.excludedStatement = tryRunner;
controller.getCompileStack().pushBlockRecorder(block);
return block;
}
Expand Down Expand Up @@ -467,19 +470,19 @@ public void writeSwitch(SwitchStatement statement) {
controller.getMethodVisitor().visitLabel(breakLabel);

controller.getCompileStack().removeVar(switchVariableIndex);
controller.getCompileStack().pop();
controller.getCompileStack().pop();
}

protected void writeCaseStatement(
CaseStatement statement, int switchVariableIndex,
Label thisLabel, Label nextLabel)
Label thisLabel, Label nextLabel)
{
controller.getAcg().onLineNumber(statement, "visitCaseStatement");
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();

mv.visitVarInsn(ALOAD, switchVariableIndex);

statement.getExpression().visit(controller.getAcg());
operandStack.box();
controller.getBinaryExpressionHelper().getIsCaseMethod().call(mv);
Expand Down Expand Up @@ -544,6 +547,7 @@ public void writeSynchronized(SynchronizedStatement statement) {
mv.visitInsn(NOP);

Runnable finallyPart = new Runnable() {
@Override
public void run() {
mv.visitVarInsn(ALOAD, index);
mv.visitInsn(MONITOREXIT);
Expand Down Expand Up @@ -584,7 +588,7 @@ public void writeThrow(ThrowStatement statement) {
// we should infer the type of the exception from the expression
mv.visitTypeInsn(CHECKCAST, "java/lang/Throwable");
mv.visitInsn(ATHROW);

controller.getOperandStack().remove(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ public void writeForStatement(ForStatement loop) {
writeForInLoop(loop);
}
}

protected void writeIteratorHasNext(MethodVisitor mv) {
iteratorHasNextMethod.call(mv);
}

protected void writeIteratorNext(MethodVisitor mv) {
iteratorNextMethod.call(mv);
}

protected void writeForInLoop(ForStatement loop) {
controller.getAcg().onLineNumber(loop,"visitForLoop");
writeStatementLabel(loop);

CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
Expand Down Expand Up @@ -221,7 +221,7 @@ protected void writeForLoopWithClosureList(ForStatement loop) {
controller.getCompileStack().pop();
controller.getCompileStack().pop();
}

private void visitExpressionOrStatement(Object o) {
if (o == EmptyExpression.INSTANCE) return;
if (o instanceof Expression) {
Expand Down Expand Up @@ -258,7 +258,7 @@ public void writeWhileLoop(WhileStatement loop) {
}
}

if(!boolHandled) {
if (!boolHandled) {
bool.visit(controller.getAcg());
controller.getOperandStack().jump(IFEQ, breakLabel);
}
Expand All @@ -268,7 +268,7 @@ public void writeWhileLoop(WhileStatement loop) {
mv.visitJumpInsn(GOTO, continueLabel);
mv.visitLabel(breakLabel);

controller.getCompileStack().pop();
controller.getCompileStack().pop();
}

public void writeDoWhileLoop(DoWhileStatement loop) {
Expand Down Expand Up @@ -315,25 +315,25 @@ public void writeIfElse(IfStatement ifElse) {
Label l1 = new Label();
mv.visitJumpInsn(GOTO, l1);
mv.visitLabel(l0);

controller.getCompileStack().pushBooleanExpression();
ifElse.getElseBlock().visit(controller.getAcg());
controller.getCompileStack().pop();

mv.visitLabel(l1);
}
}
}

public void writeTryCatchFinally(TryCatchStatement statement) {
controller.getAcg().onLineNumber(statement, "visitTryCatchFinally");
writeStatementLabel(statement);

MethodVisitor mv = controller.getMethodVisitor();
CompileStack compileStack = controller.getCompileStack();
OperandStack operandStack = controller.getOperandStack();

Statement tryStatement = statement.getTryStatement();
final Statement finallyStatement = statement.getFinallyStatement();
Statement finallyStatement = statement.getFinallyStatement();

// start try block, label needed for exception table
Label tryStart = new Label();
Expand Down Expand Up @@ -384,11 +384,10 @@ public void writeTryCatchFinally(TryCatchStatement statement) {
compileStack.writeExceptionTable(tryBlock, catchStart, exceptionTypeInternalName);
}

// Label used to handle exceptions in catches and regularly
// visited finals.
// used to handle exceptions in catches and regularly visited finals
Label catchAny = new Label();

// add "catch any" block to exception table for try part we do this
// add "catch any" block to exception table for try part we do this
// after the exception blocks, because else this one would supersede
// any of those otherwise
compileStack.writeExceptionTable(tryBlock, catchAny, null);
Expand All @@ -401,18 +400,22 @@ public void writeTryCatchFinally(TryCatchStatement statement) {
// start finally
mv.visitLabel(finallyStart);
finallyStatement.visit(controller.getAcg());
mv.visitInsn(NOP); //**

// goto after all-catching block
Label skipCatchAll = new Label();
mv.visitJumpInsn(GOTO, skipCatchAll);

// start a block catching any Exception
mv.visitLabel(catchAny);
//store exception
//TODO: maybe define a Throwable and use it here instead of Object
// store exception
// TODO: maybe define a Throwable and use it here instead of Object
operandStack.push(ClassHelper.OBJECT_TYPE);
final int anyExceptionIndex = compileStack.defineTemporaryVariable("exception", true);
int anyExceptionIndex = compileStack.defineTemporaryVariable("exception", true);

// GROOVY-9199
controller.resetLineNumber();
int line = finallyStatement.getLineNumber();
if (line > 0) mv.visitLineNumber(line, catchAny);

finallyStatement.visit(controller.getAcg());

Expand All @@ -423,17 +426,17 @@ public void writeTryCatchFinally(TryCatchStatement statement) {
mv.visitLabel(skipCatchAll);
compileStack.removeVar(anyExceptionIndex);
}
private BlockRecorder makeBlockRecorder(final Statement finallyStatement) {

private BlockRecorder makeBlockRecorder(Statement finallyStatement) {
final BlockRecorder block = new BlockRecorder();
Runnable tryRunner = new Runnable() {
block.excludedStatement = new Runnable() {
@Override
public void run() {
controller.getCompileStack().pushBlockRecorderVisit(block);
finallyStatement.visit(controller.getAcg());
controller.getCompileStack().popBlockRecorderVisit(block);
}
};
block.excludedStatement = tryRunner;
controller.getCompileStack().pushBlockRecorder(block);
return block;
}
Expand Down Expand Up @@ -467,19 +470,19 @@ public void writeSwitch(SwitchStatement statement) {
controller.getMethodVisitor().visitLabel(breakLabel);

controller.getCompileStack().removeVar(switchVariableIndex);
controller.getCompileStack().pop();
controller.getCompileStack().pop();
}

protected void writeCaseStatement(
CaseStatement statement, int switchVariableIndex,
Label thisLabel, Label nextLabel)
Label thisLabel, Label nextLabel)
{
controller.getAcg().onLineNumber(statement, "visitCaseStatement");
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();

mv.visitVarInsn(ALOAD, switchVariableIndex);

statement.getExpression().visit(controller.getAcg());
operandStack.box();
controller.getBinaryExpressionHelper().getIsCaseMethod().call(mv);
Expand Down Expand Up @@ -544,6 +547,7 @@ public void writeSynchronized(SynchronizedStatement statement) {
mv.visitInsn(NOP);

Runnable finallyPart = new Runnable() {
@Override
public void run() {
mv.visitVarInsn(ALOAD, index);
mv.visitInsn(MONITOREXIT);
Expand Down Expand Up @@ -584,7 +588,7 @@ public void writeThrow(ThrowStatement statement) {
// we should infer the type of the exception from the expression
mv.visitTypeInsn(CHECKCAST, "java/lang/Throwable");
mv.visitInsn(ATHROW);

controller.getOperandStack().remove(1);
}

Expand Down
Loading

0 comments on commit d0d6a97

Please sign in to comment.