Search in sources :

Example 21 with TransformerException

use of lucee.transformer.TransformerException 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);
}
Also used : Statement(lucee.transformer.bytecode.Statement) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) Iterator(java.util.Iterator) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryCatchFinallyVisitor(lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor) Body(lucee.transformer.bytecode.Body) BodyBase(lucee.transformer.bytecode.BodyBase) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 22 with TransformerException

use of lucee.transformer.TransformerException in project Lucee by lucee.

the class TagTry2 method _writeOut.

@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    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;
    }
    TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) throws TransformerException {
            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();
    int oldPE = adapter.newLocal(Types.PAGE_EXCEPTION);
    int oldName = adapter.newLocal(Types.STRING);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CATCH);
    adapter.storeLocal(oldPE);
    adapter.loadArg(0);
    adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, GET_CATCH_NAME);
    adapter.storeLocal(oldName);
    // 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();
        // type
        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, extractName(tag));
        adapter.visitJumpInsn(Opcodes.GOTO, endAllIfs);
        adapter.visitLabel(endIf);
    }
    // else
    if (tagElse != null) {
        catchBody(bc, adapter, tagElse, pe, true, true, extractName(tagElse));
    } 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);
    adapter.loadLocal(oldName);
    Label notNull = new Label();
    adapter.visitJumpInsn(Opcodes.IFNONNULL, notNull);
    // NULL
    adapter.loadArg(0);
    adapter.loadLocal(oldPE);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH_PE);
    Label end = new Label();
    adapter.visitJumpInsn(Opcodes.GOTO, end);
    adapter.visitLabel(notNull);
    // NOT NULL
    adapter.loadArg(0);
    adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    adapter.loadLocal(oldPE);
    adapter.loadLocal(oldName);
    adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, SET_CATCH_PE2);
    adapter.visitLabel(end);
    tcfv.visitCatchEnd(bc);
}
Also used : Statement(lucee.transformer.bytecode.Statement) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) Iterator(java.util.Iterator) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryCatchFinallyVisitor(lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor) Body(lucee.transformer.bytecode.Body) BodyBase(lucee.transformer.bytecode.BodyBase) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 23 with TransformerException

use of lucee.transformer.TransformerException in project Lucee by lucee.

the class VariableString method variableToStringArray.

public static String[] variableToStringArray(Variable var, boolean rawIfPossible) throws TransformerException {
    List<Member> members = var.getMembers();
    List<String> arr = new ArrayList<String>();
    if (var.getScope() != Scope.SCOPE_UNDEFINED)
        arr.add(ScopeFactory.toStringScope(var.getScope(), "undefined"));
    Iterator<Member> it = members.iterator();
    DataMember dm;
    Expression n;
    while (it.hasNext()) {
        Object o = it.next();
        if (!(o instanceof DataMember))
            throw new TransformerException("can't translate Variable to a String", var.getStart());
        dm = (DataMember) o;
        n = dm.getName();
        if (n instanceof Literal) {
            if (rawIfPossible && n instanceof Identifier) {
                arr.add(((Identifier) n).getRaw());
            } else {
                arr.add(((Literal) n).getString());
            }
        } else
            throw new TransformerException("argument name must be a constant value", var.getStart());
    }
    return arr.toArray(new String[arr.size()]);
}
Also used : Identifier(lucee.transformer.bytecode.literal.Identifier) Expression(lucee.transformer.expression.Expression) Literal(lucee.transformer.expression.literal.Literal) ArrayList(java.util.ArrayList) DataMember(lucee.transformer.expression.var.DataMember) ExprString(lucee.transformer.expression.ExprString) DataMember(lucee.transformer.expression.var.DataMember) Member(lucee.transformer.expression.var.Member) TransformerException(lucee.transformer.TransformerException)

Example 24 with TransformerException

use of lucee.transformer.TransformerException in project Lucee by lucee.

the class QueryExecute method evaluate.

@Override
public void evaluate(BIF bif, FunctionLibFunction flf) throws EvaluatorException {
    Variable var = bif.getParent();
    if (var != null) {
        Assign ass = var.assign();
        if (ass != null) {
            try {
                String str = VariableString.variableToString(ass.getVariable(), false);
                addArgument(bif, str);
            } catch (TransformerException e) {
                SystemOut.printDate(e);
            }
        }
    }
}
Also used : Variable(lucee.transformer.expression.var.Variable) Assign(lucee.transformer.bytecode.expression.var.Assign) VariableString(lucee.transformer.bytecode.expression.var.VariableString) TransformerException(lucee.transformer.TransformerException)

Aggregations

TransformerException (lucee.transformer.TransformerException)24 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)11 LitString (lucee.transformer.expression.literal.LitString)9 ExprString (lucee.transformer.expression.ExprString)7 Expression (lucee.transformer.expression.Expression)7 BytecodeContext (lucee.transformer.bytecode.BytecodeContext)5 OnFinally (lucee.transformer.bytecode.visitor.OnFinally)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 TemplateException (lucee.runtime.exp.TemplateException)4 Body (lucee.transformer.bytecode.Body)4 Page (lucee.transformer.bytecode.Page)4 TryCatchFinallyVisitor (lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor)4 Literal (lucee.transformer.expression.literal.Literal)4 Label (org.objectweb.asm.Label)4 BodyBase (lucee.transformer.bytecode.BodyBase)3 Statement (lucee.transformer.bytecode.Statement)3 EvaluatorException (lucee.transformer.cfml.evaluator.EvaluatorException)3 DataMember (lucee.transformer.expression.var.DataMember)3 Member (lucee.transformer.expression.var.Member)3