use of lucee.transformer.bytecode.visitor.OnFinally in project Lucee by lucee.
the class ForEach method _writeOut.
@Override
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);
// Value
// ForEachUtil.toIterator(value)
value.writeOut(bc, Expression.MODE_REF);
adapter.invokeStatic(FOR_EACH_UTIL, FOR_EACH);
// adapter.invokeStatic(COLLECTION_UTIL, TO_ITERATOR);
// Iterator it=...
adapter.storeLocal(it);
TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
GeneratorAdapter a = bc.getAdapter();
// if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(a,fcf.getFinalEntryLabel());
a.loadLocal(it);
a.invokeStatic(FOR_EACH_UTIL, RESET);
/*if(fcf!=null){
Label l=fcf.getAfterFinalGOTOLabel();
if(l!=null)a.visitJumpInsn(Opcodes.GOTO, l);
}*/
}
}, getFlowControlFinal());
tfv.visitTryBegin(bc);
// Key
// new VariableReference(...)
key.writeOut(bc, Expression.MODE_REF);
// VariableReference item=...
adapter.storeLocal(item);
// while
ExpressionUtil.visitLine(bc, getStart());
adapter.visitLabel(begin);
// hasNext
adapter.loadLocal(it);
adapter.invokeInterface(Types.ITERATOR, HAS_NEXT);
adapter.ifZCmp(Opcodes.IFEQ, end);
// item.set(pc,it.next());
adapter.loadLocal(item);
adapter.loadArg(0);
adapter.loadLocal(it);
adapter.invokeInterface(Types.ITERATOR, NEXT);
adapter.invokeInterface(Types.REFERENCE, SET);
adapter.pop();
// Body
body.writeOut(bc);
adapter.visitJumpInsn(Opcodes.GOTO, begin);
adapter.visitLabel(end);
tfv.visitTryEnd(bc);
}
use of lucee.transformer.bytecode.visitor.OnFinally in project Lucee by lucee.
the class Return method _writeOut.
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
GeneratorAdapter adapter = bc.getAdapter();
if (expr == null)
ASMConstants.NULL(adapter);
else
expr.writeOut(bc, Expression.MODE_REF);
Stack<OnFinally> finallies = bc.getOnFinallyStack();
int len = finallies.size();
OnFinally onFinally;
if (len > 0) {
int rtn = adapter.newLocal(Types.OBJECT);
adapter.storeLocal(rtn, Types.OBJECT);
for (int i = len - 1; i >= 0; i--) {
onFinally = finallies.get(i);
if (!bc.insideFinally(onFinally))
onFinally.writeOut(bc);
}
adapter.loadLocal(rtn, Types.OBJECT);
}
if (bc.getMethod().getReturnType().equals(Types.VOID)) {
adapter.pop();
adapter.visitInsn(Opcodes.RETURN);
} else
adapter.visitInsn(Opcodes.ARETURN);
}
use of lucee.transformer.bytecode.visitor.OnFinally in project Lucee by lucee.
the class TagLoop method writeOutTypeFile.
/**
* write out file loop
* @param adapter
* @throws TemplateException
*/
private void writeOutTypeFile(BytecodeContext bc) throws TransformerException {
WhileVisitor whileVisitor = new WhileVisitor();
loopVisitor = whileVisitor;
GeneratorAdapter adapter = bc.getAdapter();
// charset=@charset
int charset = adapter.newLocal(Types.STRING);
Attribute attrCharset = getAttribute("charset");
if (attrCharset == null)
adapter.visitInsn(Opcodes.ACONST_NULL);
else
attrCharset.getValue().writeOut(bc, Expression.MODE_REF);
adapter.storeLocal(charset);
// startline=@startline
int startline = adapter.newLocal(Types.INT_VALUE);
Attribute attrStartLine = getAttribute("startline");
// CF8
if (attrStartLine == null)
attrStartLine = getAttribute("from");
if (attrStartLine == null)
adapter.push(1);
else {
attrStartLine.getValue().writeOut(bc, Expression.MODE_VALUE);
adapter.visitInsn(Opcodes.D2I);
}
adapter.storeLocal(startline);
// endline=@endline
int endline = adapter.newLocal(Types.INT_VALUE);
Attribute attrEndLine = getAttribute("endline");
if (attrEndLine == null)
attrEndLine = getAttribute("to");
if (attrEndLine == null)
adapter.push(-1);
else {
attrEndLine.getValue().writeOut(bc, Expression.MODE_VALUE);
adapter.visitInsn(Opcodes.D2I);
}
adapter.storeLocal(endline);
// VariableReference index=VariableInterpreter.getVariableReference(pc,@index);
int index = -1, item = -1;
// item
Attribute attrItem = getAttribute("item");
if (attrItem != null) {
item = adapter.newLocal(Types.VARIABLE_REFERENCE);
adapter.loadArg(0);
attrItem.getValue().writeOut(bc, Expression.MODE_REF);
adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
adapter.storeLocal(item);
}
// index
Attribute attrIndex = getAttribute("index");
if (attrIndex != null) {
index = adapter.newLocal(Types.VARIABLE_REFERENCE);
adapter.loadArg(0);
attrIndex.getValue().writeOut(bc, Expression.MODE_REF);
adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
adapter.storeLocal(index);
}
// java.io.File file=FileUtil.toResourceExisting(pc,@file);
int resource = adapter.newLocal(Types.RESOURCE);
adapter.loadArg(0);
getAttribute("file").getValue().writeOut(bc, Expression.MODE_REF);
adapter.invokeStatic(RESOURCE_UTIL, TO_RESOURCE_EXISTING);
adapter.storeLocal(resource);
// pc.getConfig().getSecurityManager().checkFileLocation(resource);
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CONFIG);
adapter.invokeInterface(Types.CONFIG_WEB, GET_SECURITY_MANAGER);
adapter.loadLocal(resource);
adapter.invokeInterface(Types.SECURITY_MANAGER, CHECK_FILE_LOCATION);
// char[] carr=new char[characters];
Attribute attr = getAttribute("characters");
int carr = -1;
if (attr != null) {
carr = adapter.newLocal(Types.CHAR_ARRAY);
attr.getValue().writeOut(bc, Expression.MODE_VALUE);
adapter.cast(Types.DOUBLE_VALUE, Types.INT_VALUE);
adapter.newArray(Types.CHAR);
adapter.storeLocal(carr);
}
// BufferedReader reader = IOUtil.getBufferedReader(resource,charset);
final int br = adapter.newLocal(Types.BUFFERED_READER);
adapter.loadLocal(resource);
adapter.loadLocal(charset);
adapter.invokeStatic(IO_UTIL, GET_BUFFERED_READER);
adapter.storeLocal(br);
// String line;
int line = adapter.newLocal(Types.STRING);
// int count=0;
int count = adapter.newLocal(Types.INT_VALUE);
adapter.push(0);
adapter.storeLocal(count);
TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) {
bc.getAdapter().loadLocal(br);
bc.getAdapter().invokeStatic(IO_UTIL, CLOSE_EL);
}
}, null);
// TryFinallyVisitor tcfv=new TryFinallyVisitor();
// try
tfv.visitTryBegin(bc);
// tcfv.visitTryBegin(bc);
// while((line=br.readLine())!=null) {
// WhileVisitor wv=new WhileVisitor();
whileVisitor.visitBeforeExpression(bc);
DecisionObjectVisitor dv = new DecisionObjectVisitor();
dv.visitBegin();
if (attr != null) {
// IOUtil.read(bufferedreader,12)
adapter.loadLocal(br);
adapter.loadLocal(carr);
adapter.arrayLength();
adapter.invokeStatic(Types.IOUTIL, READ);
} else {
// br.readLine()
adapter.loadLocal(br);
adapter.invokeVirtual(Types.BUFFERED_READER, READ_LINE);
}
adapter.dup();
adapter.storeLocal(line);
dv.visitNEQ();
adapter.visitInsn(Opcodes.ACONST_NULL);
dv.visitEnd(bc);
whileVisitor.visitAfterExpressionBeforeBody(bc);
// if(++count < startLine) continue;
DecisionIntVisitor dv2 = new DecisionIntVisitor();
dv2.visitBegin();
adapter.iinc(count, 1);
adapter.loadLocal(count);
dv2.visitLT();
adapter.loadLocal(startline);
dv2.visitEnd(bc);
Label end = new Label();
adapter.ifZCmp(Opcodes.IFEQ, end);
whileVisitor.visitContinue(bc);
adapter.visitLabel(end);
// if(endLine!=-1 && count > endLine) break;
DecisionIntVisitor div = new DecisionIntVisitor();
div.visitBegin();
adapter.loadLocal(endline);
div.visitNEQ();
adapter.push(-1);
div.visitEnd(bc);
Label end2 = new Label();
adapter.ifZCmp(Opcodes.IFEQ, end2);
DecisionIntVisitor div2 = new DecisionIntVisitor();
div2.visitBegin();
adapter.loadLocal(count);
div2.visitGT();
adapter.loadLocal(endline);
div2.visitEnd(bc);
Label end3 = new Label();
adapter.ifZCmp(Opcodes.IFEQ, end3);
whileVisitor.visitBreak(bc);
adapter.visitLabel(end3);
adapter.visitLabel(end2);
// index and item
if (index != -1 && item != -1) {
// index.set(pc,line);
adapter.loadLocal(index);
adapter.loadArg(0);
adapter.loadLocal(count);
adapter.cast(Types.INT_VALUE, Types.DOUBLE_VALUE);
adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);
adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
adapter.pop();
// item.set(pc,line);
adapter.loadLocal(item);
adapter.loadArg(0);
adapter.loadLocal(line);
adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
adapter.pop();
} else // only index
if (index != -1) {
// index.set(pc,line);
adapter.loadLocal(index);
adapter.loadArg(0);
adapter.loadLocal(line);
adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
adapter.pop();
} else // only item
{
// item.set(pc,line);
adapter.loadLocal(item);
adapter.loadArg(0);
adapter.loadLocal(line);
adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
adapter.pop();
}
getBody().writeOut(bc);
whileVisitor.visitAfterBody(bc, getEnd());
tfv.visitTryEnd(bc);
}
use of lucee.transformer.bytecode.visitor.OnFinally in project Lucee by lucee.
the class TagSilent method _writeOut.
/**
* @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
*/
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
final GeneratorAdapter adapter = bc.getAdapter();
final int silentMode = adapter.newLocal(Types.BOOLEAN_VALUE);
// boolean silentMode= pc.setSilent();
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_SILENT);
adapter.storeLocal(silentMode);
// call must be
TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) {
// if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(adapter,fcf.getFinalEntryLabel());
// if(!silentMode)pc.unsetSilent();
Label _if = new Label();
adapter.loadLocal(silentMode);
NotVisitor.visitNot(bc);
adapter.ifZCmp(Opcodes.IFEQ, _if);
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, UNSET_SILENT);
adapter.pop();
adapter.visitLabel(_if);
/*if(fcf!=null) {
Label l = fcf.getAfterFinalGOTOLabel();
if(l!=null)adapter.visitJumpInsn(Opcodes.GOTO, l);
}*/
}
}, getFlowControlFinal());
tfv.visitTryBegin(bc);
getBody().writeOut(bc);
tfv.visitTryEnd(bc);
}
use of lucee.transformer.bytecode.visitor.OnFinally in project Lucee by lucee.
the class TagTry method _writeOut.
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
final GeneratorAdapter adapter = bc.getAdapter();
adapter.visitLabel(begin);
Body tryBody = new BodyBase(getFactory());
List<Tag> catches = new ArrayList<Tag>();
Tag tmpFinal = null;
tryBody.setParent(getBody().getParent());
List<Statement> statements = getBody().getStatements();
Statement stat;
Tag tag;
{
Iterator<Statement> it = statements.iterator();
while (it.hasNext()) {
stat = it.next();
if (stat instanceof Tag) {
tag = (Tag) stat;
if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Catch")) {
catches.add(tag);
continue;
} else if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Finally")) {
tmpFinal = tag;
continue;
}
}
tryBody.addStatement(stat);
}
;
}
final Tag _finally = tmpFinal;
// has no try body, if there is no try body, no catches are executed, only finally
if (!tryBody.hasStatements()) {
if (_finally != null && _finally.getBody() != null) {
BodyBase.writeOut(bc, _finally.getBody());
// ExpressionUtil.writeOut(_finally.getBody(), bc);
}
return;
}
final int old = adapter.newLocal(Types.PAGE_EXCEPTION);
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CATCH);
adapter.storeLocal(old);
TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
adapter.loadArg(0);
adapter.loadLocal(old);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH_PE);
if (_finally != null) {
ExpressionUtil.visitLine(bc, _finally.getStart());
BodyBase.writeOut(bc, _finally.getBody());
// ExpressionUtil.writeOut(_finally.getBody(), bc);
}
}
}, getFlowControlFinal());
// Try
tcfv.visitTryBegin(bc);
BodyBase.writeOut(bc, tryBody);
// ExpressionUtil.writeOut(tryBody, bc);
int e = tcfv.visitTryEndCatchBeging(bc);
// if(e instanceof lucee.runtime.exp.Abort) throw e;
Label abortEnd = new Label();
adapter.loadLocal(e);
// Abort.isAbort(t);
adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
// adapter.instanceOf(Types.ABORT);
adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
adapter.loadLocal(e);
adapter.throwException();
adapter.visitLabel(abortEnd);
// PageExceptionImpl old=pc.getCatch();
// PageException pe=Caster.toPageEception(e);
int pe = adapter.newLocal(Types.PAGE_EXCEPTION);
adapter.loadLocal(e);
adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
adapter.storeLocal(pe);
Iterator<Tag> it = catches.iterator();
Attribute attrType;
Expression type;
Label endAllIfs = new Label();
Tag tagElse = null;
while (it.hasNext()) {
tag = it.next();
Label endIf = new Label();
attrType = tag.getAttribute("type");
type = bc.getFactory().createLitString("any");
if (attrType != null)
type = attrType.getValue();
if (type instanceof LitString && ((LitString) type).getString().equalsIgnoreCase("any")) {
tagElse = tag;
continue;
}
ExpressionUtil.visitLine(bc, tag.getStart());
// if(pe.typeEqual(@type)
adapter.loadLocal(pe);
type.writeOut(bc, Expression.MODE_REF);
adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
adapter.ifZCmp(Opcodes.IFEQ, endIf);
catchBody(bc, adapter, tag, pe, true, true);
adapter.visitJumpInsn(Opcodes.GOTO, endAllIfs);
adapter.visitLabel(endIf);
}
// else
if (tagElse != null) {
catchBody(bc, adapter, tagElse, pe, true, true);
} else {
// pc.setCatch(pe,true);
adapter.loadArg(0);
adapter.loadLocal(pe);
adapter.push(false);
adapter.push(true);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH3);
// throw pe;
adapter.loadLocal(pe);
adapter.throwException();
}
adapter.visitLabel(endAllIfs);
// PageExceptionImpl old=pc.getCatch();
tcfv.visitCatchEnd(bc);
}
Aggregations