Search in sources :

Example 36 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class TagInclude method _writeOut.

/**
 * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
 */
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    Type type = Types.PAGE_CONTEXT;
    Method func = DO_INCLUDE_RUN_ONCE2;
    // cachedwithin
    Expression cachedwithin = null;
    Attribute attr = getAttribute("cachedwithin");
    if (attr != null && attr.getValue() != null) {
        cachedwithin = attr.getValue();
        type = Types.PAGE_CONTEXT_IMPL;
        func = DO_INCLUDE_RUN_ONCE3;
    }
    GeneratorAdapter adapter = bc.getAdapter();
    adapter.loadArg(0);
    if (cachedwithin != null)
        adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    // template
    getAttribute("template").getValue().writeOut(bc, Expression.MODE_REF);
    // run Once
    attr = getAttribute("runonce");
    ExprBoolean expr = (attr == null) ? bc.getFactory().FALSE() : bc.getFactory().toExprBoolean(attr.getValue());
    expr.writeOut(bc, Expression.MODE_VALUE);
    // cachedwithin
    if (cachedwithin != null)
        cachedwithin.writeOut(bc, Expression.MODE_REF);
    adapter.invokeVirtual(type, func);
}
Also used : Type(org.objectweb.asm.Type) Expression(lucee.transformer.expression.Expression) ExprBoolean(lucee.transformer.expression.ExprBoolean) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 37 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class TagParam method _writeOut.

@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    GeneratorAdapter adapter = bc.getAdapter();
    Argument.checkDefaultValue(this);
    // attributes
    Expression name = getAttribute("name").getValue();
    Attribute attrDefault = getAttribute("default");
    Expression def = null;
    // .writeOut(bc, Expression.MODE_REF);
    if (attrDefault != null)
        def = attrDefault.getValue();
    // else adapter.visitInsn(Opcodes.ACONST_NULL);
    // check attributes name
    // if(StringUtil.isEmpty(name)) throw new ExpressionException("The attribute name is required");
    /*name.writeOut(bc, Expression.MODE_REF);
			adapter.invokeStatic(Types.STRING_UTIL, IS_EMPTY);
			Label end = new Label();
			adapter.visitJumpInsn(Opcodes.IFEQ, end);
			//adapter.visitLabel(new Label());
			adapter.newInstance(Types.EXPRESSION_EXCEPTION);
			adapter.dup();
			adapter.push("for param a name is required");
			adapter.invokeConstructor(Types.EXPRESSION_EXCEPTION, CONSTR_STRING);
			adapter.throwException();
			adapter.visitLabel(end);*/
    // value
    // Object value=VariableInterpreter.getVariableEL(this,name,NullSupportHelper.NULL(this));
    int value = adapter.newLocal(Types.OBJECT);
    // pc
    adapter.loadArg(0);
    // name
    name.writeOut(bc, Expression.MODE_REF);
    // pc
    adapter.loadArg(0);
    adapter.invokeStatic(NULL_SUPPORT_HELPER, NULL);
    adapter.invokeStatic(VARIABLE_INTERPRETER, GET_VARIABLE_EL);
    adapter.storeLocal(value);
    // check value 2=value; 3=defaultValue; isNew=4
    int isNew = adapter.newLocal(Types.BOOLEAN_VALUE);
    adapter.push(false);
    adapter.storeLocal(isNew, Types.BOOLEAN_VALUE);
    // Label l3 = new Label();
    // mv.visitLabel(l3);
    adapter.loadArg(0);
    adapter.invokeStatic(NULL_SUPPORT_HELPER, NULL);
    adapter.loadLocal(value);
    Label l4 = new Label();
    adapter.visitJumpInsn(Opcodes.IF_ACMPNE, l4);
    Label l5 = new Label();
    adapter.visitLabel(l5);
    if (def != null)
        def.writeOut(bc, Expression.MODE_REF);
    else
        ASMConstants.NULL(adapter);
    adapter.dup();
    int ldef = adapter.newLocal(Types.OBJECT);
    adapter.storeLocal(ldef);
    Label l6 = new Label();
    adapter.visitJumpInsn(Opcodes.IFNONNULL, l6);
    Label l7 = new Label();
    adapter.visitLabel(l7);
    adapter.newInstance(Types.EXPRESSION_EXCEPTION);
    adapter.dup();
    adapter.newInstance(Types.STRING_BUILDER);
    adapter.dup();
    adapter.push("The required parameter [");
    adapter.invokeConstructor(Types.STRING_BUILDER, CONSTR_STRING);
    name.writeOut(bc, Expression.MODE_REF);
    adapter.invokeVirtual(Types.STRING_BUILDER, APPEND_OBJECT);
    adapter.visitLdcInsn("] was not provided.");
    adapter.invokeVirtual(Types.STRING_BUILDER, APPEND_STRING);
    adapter.invokeVirtual(Types.STRING_BUILDER, TO_STRING);
    adapter.invokeConstructor(Types.EXPRESSION_EXCEPTION, CONSTR_STRING);
    adapter.throwException();
    adapter.visitLabel(l6);
    adapter.loadLocal(ldef);
    adapter.storeLocal(value);
    adapter.push(true);
    adapter.storeLocal(isNew);
    adapter.visitLabel(l4);
    // pc
    adapter.loadArg(0);
    adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    // type
    Attribute attrType = getAttribute("type");
    if (attrType != null)
        attrType.getValue().writeOut(bc, Expression.MODE_REF);
    else
        adapter.push("any");
    // adapter.push("any");
    // name
    name.writeOut(bc, Expression.MODE_REF);
    // adapter.push("url.test");
    // value
    adapter.loadLocal(value);
    Attribute attrMin = getAttribute("min");
    Attribute attrMax = getAttribute("max");
    Attribute attrPattern = getAttribute("pattern");
    Attribute maxLength = getAttribute("maxLength");
    // min/max
    if (attrMin != null || attrMax != null) {
        // min
        if (attrMin != null)
            attrMin.getValue().writeOut(bc, Expression.MODE_VALUE);
        else
            adapter.visitLdcInsn(new Double("NaN"));
        // max
        if (attrMax != null)
            attrMax.getValue().writeOut(bc, Expression.MODE_VALUE);
        else
            adapter.visitLdcInsn(new Double("NaN"));
    } else {
        adapter.visitLdcInsn(new Double("NaN"));
        adapter.visitLdcInsn(new Double("NaN"));
    }
    // pattern
    if (attrPattern != null)
        attrPattern.getValue().writeOut(bc, Expression.MODE_REF);
    else
        ASMConstants.NULL(adapter);
    // maxlength
    if (maxLength != null)
        bc.getFactory().toExprInt(maxLength.getValue()).writeOut(bc, Expression.MODE_VALUE);
    else
        adapter.push(-1);
    // adapter.push(-1);
    // isNew
    adapter.loadLocal(isNew, Types.BOOLEAN_VALUE);
    // adapter.push(true);
    // subparam
    adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, SUB_PARAM);
// param(type, name, defaultValue,Double.NaN,Double.NaN,regex,-1);
// subparam(type, name, value, min, max, strPattern, maxLength, isNew); // SSO
}
Also used : Expression(lucee.transformer.expression.Expression) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter)

Example 38 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class TagReturn method _writeOut.

/**
 * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
 */
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    Attribute attr = getAttribute("expr");
    Expression expr = null;
    if (attr != null)
        expr = attr.getValue();
    new Return(expr, expr.getStart(), expr.getEnd()).writeOut(bc);
}
Also used : Return(lucee.transformer.bytecode.statement.Return) Expression(lucee.transformer.expression.Expression)

Example 39 with Expression

use of lucee.transformer.expression.Expression 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 40 with Expression

use of lucee.transformer.expression.Expression 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)

Aggregations

Expression (lucee.transformer.expression.Expression)74 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)34 LitString (lucee.transformer.expression.literal.LitString)25 TemplateException (lucee.runtime.exp.TemplateException)19 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)16 Variable (lucee.transformer.expression.var.Variable)16 Body (lucee.transformer.bytecode.Body)11 ExprString (lucee.transformer.expression.ExprString)11 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)11 Position (lucee.transformer.Position)10 OpVariable (lucee.transformer.bytecode.op.OpVariable)10 LitBoolean (lucee.transformer.expression.literal.LitBoolean)8 Literal (lucee.transformer.expression.literal.Literal)8 ArrayList (java.util.ArrayList)7 TransformerException (lucee.transformer.TransformerException)7 BodyBase (lucee.transformer.bytecode.BodyBase)7 Statement (lucee.transformer.bytecode.Statement)7 Argument (lucee.transformer.bytecode.expression.var.Argument)7 PrintOut (lucee.transformer.bytecode.statement.PrintOut)6 FunctionBody (lucee.transformer.bytecode.FunctionBody)5