Skip to content

Commit

Permalink
feat: use the helper in loop transformers
Browse files Browse the repository at this point in the history
feat: use in statement loop
  • Loading branch information
xdecock committed Oct 30, 2024
1 parent ba299eb commit 534543f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lucee.transformer.TransformerException;
import lucee.transformer.bytecode.Body;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;
import lucee.transformer.expression.ExprBoolean;
import lucee.transformer.expression.Expression;

Expand Down Expand Up @@ -57,14 +58,18 @@ public DoWhile(Expression expr, Body body, Position start, Position end, String
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
GeneratorAdapter adapter = bc.getAdapter();
final int loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);

adapter.visitLabel(begin);
body.writeOut(bc);

InterruptHandlerInjector.writeLoopBodyEnd(adapter, loopCounter, beforeEnd, "during do while");
adapter.visitLabel(beforeEnd);

expr.writeOut(bc, Expression.MODE_VALUE);
adapter.ifZCmp(Opcodes.IFNE, begin);

InterruptHandlerInjector.writePreempt(adapter, end, "after do while");
adapter.visitLabel(end);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import lucee.transformer.bytecode.Body;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.ASMUtil;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;
import lucee.transformer.expression.Expression;

public final class For extends StatementBaseNoFinal implements FlowControlBreak, FlowControlContinue, HasBody {
Expand Down Expand Up @@ -69,6 +70,7 @@ public void _writeOut(BytecodeContext bc) throws TransformerException {
Label beforeInit = new Label();
Label afterInit = new Label();
Label afterUpdate = new Label();
final int loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);

bc.visitLine(getStart());
adapter.visitLabel(beforeInit);
Expand All @@ -87,13 +89,16 @@ public void _writeOut(BytecodeContext bc) throws TransformerException {
update.writeOut(bc, Expression.MODE_VALUE);
ASMUtil.pop(adapter, update, Expression.MODE_VALUE);
}

InterruptHandlerInjector.writeLoopBodyEnd(adapter, loopCounter, afterUpdate, "during for loop");
// ExpressionUtil.visitLine(bc, getStartLine());
adapter.visitLabel(afterUpdate);

if (condition != null) condition.writeOut(bc, Expression.MODE_VALUE);
else bc.getFactory().TRUE().writeOut(bc, Expression.MODE_VALUE);
adapter.visitJumpInsn(Opcodes.IFNE, afterInit);
// ExpressionUtil.visitLine(bc, getEndLine());
InterruptHandlerInjector.writePreempt(adapter, end, "after for loop");
adapter.visitLabel(end);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.expression.var.VariableRef;
import lucee.transformer.bytecode.util.Types;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;
import lucee.transformer.bytecode.visitor.OnFinally;
import lucee.transformer.bytecode.visitor.TryFinallyVisitor;
import lucee.transformer.expression.Expression;
Expand Down Expand Up @@ -81,6 +82,7 @@ public void _writeOut(BytecodeContext bc) throws TransformerException {
GeneratorAdapter adapter = bc.getAdapter();
final int it = adapter.newLocal(Types.ITERATOR);
final int item = adapter.newLocal(Types.REFERENCE);
final int loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);

// Value
// ForEachUtil.toIterator(value)
Expand Down Expand Up @@ -130,7 +132,7 @@ public void _writeOut(BytecodeContext bc) throws TransformerException {

// Body
body.writeOut(bc);
adapter.visitJumpInsn(Opcodes.GOTO, begin);
InterruptHandlerInjector.writeLoopBodyEnd(adapter, loopCounter, begin, "during foreach loop");
adapter.visitLabel(end);
tfv.visitTryEnd(bc);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lucee.transformer.TransformerException;
import lucee.transformer.bytecode.Body;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;
import lucee.transformer.expression.ExprBoolean;
import lucee.transformer.expression.Expression;

Expand Down Expand Up @@ -67,13 +68,14 @@ public While(boolean b, Body body, Position start, Position end, String label) {
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
GeneratorAdapter adapter = bc.getAdapter();
final int loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);
adapter.visitLabel(begin);

expr.writeOut(bc, Expression.MODE_VALUE);
adapter.ifZCmp(Opcodes.IFEQ, end);

body.writeOut(bc);
adapter.visitJumpInsn(Opcodes.GOTO, begin);
InterruptHandlerInjector.writeLoopBodyEnd(adapter, loopCounter, begin, "during for loop");

adapter.visitLabel(end);
}
Expand Down

0 comments on commit 534543f

Please sign in to comment.