Search in sources :

Example 1 with Body

use of lucee.transformer.bytecode.Body in project Lucee by lucee.

the class Function method evaluate.

@Override
public void evaluate(Tag tag, TagLibTag libTag, FunctionLib[] flibs) throws EvaluatorException {
    // Body p=(Body) tag.getParent();
    // Statement pp = p.getParent();
    boolean isCI = true;
    try {
        isCI = ASMUtil.getAncestorPage(tag).isComponent() || ASMUtil.getAncestorPage(tag).isInterface();
    } catch (TransformerException e) {
    }
    Attribute attrName = tag.getAttribute("name");
    if (attrName != null) {
        Expression expr = attrName.getValue();
        PageSource ps = null;
        if (expr instanceof LitString && !isCI) {
            Page p = ASMUtil.getAncestorPage(tag, null);
            if (p != null) {
                SourceCode sc = p.getSourceCode();
                if (sc instanceof PageSourceCode) {
                    PageSourceCode psc = (PageSourceCode) sc;
                    ps = psc.getPageSource();
                }
            }
            checkFunctionName(((LitString) expr).getString(), flibs, ps);
        }
    }
    // attribute modifier
    boolean isStatic = false;
    {
        Attribute attrModifier = tag.getAttribute("modifier");
        if (attrModifier != null) {
            ExprString expr = tag.getFactory().toExprString(attrModifier.getValue());
            if (!(expr instanceof Literal))
                throw new EvaluatorException("Attribute modifier of the Tag Function, must be one of the following literal string values: [abstract,final,static]");
            String modifier = StringUtil.emptyIfNull(((Literal) expr).getString()).trim();
            if (!StringUtil.isEmpty(modifier) && !"abstract".equalsIgnoreCase(modifier) && !"final".equalsIgnoreCase(modifier) && !"static".equalsIgnoreCase(modifier))
                throw new EvaluatorException("Attribute modifier of the Tag Function, must be one of the following literal string values: [abstract,final,static]");
            isStatic = "static".equalsIgnoreCase(modifier);
            boolean abstr = "abstract".equalsIgnoreCase(modifier);
            if (abstr)
                throwIfNotEmpty(tag);
        }
    }
    // cachedWithin
    {
        Attribute attrCachedWithin = tag.getAttribute("cachedwithin");
        if (attrCachedWithin != null) {
            Expression val = attrCachedWithin.getValue();
            tag.addAttribute(new Attribute(attrCachedWithin.isDynamicType(), attrCachedWithin.getName(), ASMUtil.cachedWithinValue(val), attrCachedWithin.getType()));
        }
    }
    // Attribute localMode
    {
        Attribute attrLocalMode = tag.getAttribute("localmode");
        if (attrLocalMode != null) {
            Expression expr = attrLocalMode.getValue();
            String str = ASMUtil.toString(expr, null);
            if (!StringUtil.isEmpty(str) && AppListenerUtil.toLocalMode(str, -1) == -1)
                throw new EvaluatorException("Attribute localMode of the Tag Function, must be a literal value (modern, classic, true or false)");
        // boolean output = ((LitBoolean)expr).getBooleanValue();
        // if(!output) ASMUtil.removeLiterlChildren(tag, true);
        }
    }
    // Attribute Output
    {
        Attribute attrOutput = tag.getAttribute("output");
        if (attrOutput != null) {
            Expression expr = tag.getFactory().toExprBoolean(attrOutput.getValue());
            if (!(expr instanceof LitBoolean))
                throw new EvaluatorException("Attribute output of the Tag Function, must be a literal boolean value (true or false, yes or no)");
        }
    }
    // Buffer output
    {
        Attribute attrBufferOutput = tag.getAttribute("bufferoutput");
        if (attrBufferOutput != null) {
            Expression expr = tag.getFactory().toExprBoolean(attrBufferOutput.getValue());
            if (!(expr instanceof LitBoolean))
                throw new EvaluatorException("Attribute bufferOutput of the Tag Function, must be a literal boolean value (true or false, yes or no)");
        }
    }
    // check attribute values
    Map<String, Attribute> attrs = tag.getAttributes();
    Iterator<Attribute> it = attrs.values().iterator();
    while (it.hasNext()) {
        checkAttributeValue(tag, it.next());
    }
    // add to static scope
    if (isStatic) {
        // remove that tag from parent
        ASMUtil.remove(tag);
        Body body = (Body) tag.getParent();
        StaticBody sb = Static.getStaticBody(body);
        sb.addStatement(tag);
    }
}
Also used : SourceCode(lucee.transformer.util.SourceCode) PageSourceCode(lucee.transformer.util.PageSourceCode) PageSourceCode(lucee.transformer.util.PageSourceCode) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) ExprString(lucee.transformer.expression.ExprString) StaticBody(lucee.transformer.bytecode.StaticBody) Page(lucee.transformer.bytecode.Page) LitBoolean(lucee.transformer.expression.literal.LitBoolean) LitString(lucee.transformer.expression.literal.LitString) ExprString(lucee.transformer.expression.ExprString) PageSource(lucee.runtime.PageSource) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) EvaluatorException(lucee.transformer.cfml.evaluator.EvaluatorException) Literal(lucee.transformer.expression.literal.Literal) StaticBody(lucee.transformer.bytecode.StaticBody) Body(lucee.transformer.bytecode.Body) TransformerException(lucee.transformer.TransformerException)

Example 2 with Body

use of lucee.transformer.bytecode.Body in project Lucee by lucee.

the class Interface method evaluate.

@Override
public void evaluate(Tag tag, TagLibTag libTag) throws EvaluatorException {
    super.evaluate(tag, libTag);
    Body body = tag.getBody();
    List<Statement> statments = body.getStatements();
    Statement stat;
    Iterator<Statement> it = statments.iterator();
    Tag t;
    while (it.hasNext()) {
        stat = it.next();
        if (stat instanceof PrintOut) {
        // body.remove(stat);
        } else if (stat instanceof Tag) {
            t = (Tag) stat;
            if (stat instanceof TagImport) {
            // ignore
            } else if (stat instanceof TagFunction) {
                Function.throwIfNotEmpty(t);
                Attribute attr = t.getAttribute("access");
                if (attr != null) {
                    ExprString expr = t.getFactory().toExprString(attr.getValue());
                    if (!(expr instanceof LitString))
                        throw new EvaluatorException("the attribute access of the Tag function inside an interface must contain a constant value");
                    String access = ((LitString) expr).getString().trim();
                    if (!"public".equalsIgnoreCase(access))
                        throw new EvaluatorException("the attribute access of the tag function inside an interface definition can only have the value [public] not [" + access + "]");
                } else
                    t.addAttribute(new Attribute(false, "access", stat.getFactory().createLitString("public"), "string"));
            } else
                throw new EvaluatorException("tag " + libTag.getFullName() + " can only contain function definitions.");
        }
    }
}
Also used : TagFunction(lucee.transformer.bytecode.statement.tag.TagFunction) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) ExprString(lucee.transformer.expression.ExprString) Statement(lucee.transformer.bytecode.Statement) LitString(lucee.transformer.expression.literal.LitString) ExprString(lucee.transformer.expression.ExprString) TagImport(lucee.transformer.bytecode.statement.tag.TagImport) LitString(lucee.transformer.expression.literal.LitString) PrintOut(lucee.transformer.bytecode.statement.PrintOut) EvaluatorException(lucee.transformer.cfml.evaluator.EvaluatorException) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) Body(lucee.transformer.bytecode.Body)

Example 3 with Body

use of lucee.transformer.bytecode.Body in project Lucee by lucee.

the class Output method addEncodeToChildren.

private void addEncodeToChildren(Iterator it, Expression encodeForValue, FunctionLibFunction encodeForBIF) {
    Statement stat;
    while (it.hasNext()) {
        stat = (Statement) it.next();
        if (stat instanceof PrintOut) {
            PrintOut printOut = ((PrintOut) stat);
            Expression e = removeCastString(printOut.getExpr());
            if (!(e instanceof Literal)) {
                if (e instanceof Variable) {
                    Member member = ((Variable) e).getFirstMember();
                    if (member instanceof BIF) {
                        BIF bif = (BIF) member;
                        String cn = bif.getClassDefinition().getClassName();
                        if (cn.startsWith(ENCODE_FOR) || cn.equals(ESAPIEncode.class.getName())) {
                            continue;
                        }
                    }
                }
                printOut.setEncodeFor(encodeForValue);
            }
        } else if (stat instanceof Tag) {
            Body b = ((Tag) stat).getBody();
            if (b != null)
                addEncodeToChildren(b.getStatements().iterator(), encodeForValue, encodeForBIF);
        } else if (stat instanceof Body) {
            addEncodeToChildren(((Body) stat).getStatements().iterator(), encodeForValue, encodeForBIF);
        }
    }
}
Also used : Variable(lucee.transformer.expression.var.Variable) PrintOut(lucee.transformer.bytecode.statement.PrintOut) Expression(lucee.transformer.expression.Expression) Statement(lucee.transformer.bytecode.Statement) Literal(lucee.transformer.expression.literal.Literal) CastString(lucee.transformer.bytecode.cast.CastString) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) Member(lucee.transformer.expression.var.Member) Body(lucee.transformer.bytecode.Body) BIF(lucee.transformer.bytecode.expression.var.BIF)

Example 4 with Body

use of lucee.transformer.bytecode.Body in project Lucee by lucee.

the class TagCIObject method getStaticBodies.

public List<StaticBody> getStaticBodies() {
    Body b = getBody();
    List<StaticBody> list = null;
    if (!ASMUtil.isEmpty(b)) {
        Statement stat;
        Iterator<Statement> it = b.getStatements().iterator();
        while (it.hasNext()) {
            stat = it.next();
            // StaticBody
            if (stat instanceof StaticBody) {
                it.remove();
                if (list == null)
                    list = new ArrayList<StaticBody>();
                list.add((StaticBody) stat);
            // return (StaticBody) stat;
            }
        }
    }
    return list;
}
Also used : StaticBody(lucee.transformer.bytecode.StaticBody) Statement(lucee.transformer.bytecode.Statement) ArrayList(java.util.ArrayList) Body(lucee.transformer.bytecode.Body) StaticBody(lucee.transformer.bytecode.StaticBody)

Example 5 with Body

use of lucee.transformer.bytecode.Body in project Lucee by lucee.

the class TagFunction method _writeOut.

public void _writeOut(BytecodeContext bc, int type) throws TransformerException {
    // private static final Expression EMPTY = LitString.toExprString("");
    Body functionBody = new BodyBase(bc.getFactory());
    RefBoolean isStatic = new RefBooleanImpl();
    Function func = createFunction(bc.getPage(), functionBody, isStatic, bc.getOutput());
    // ScriptBody sb=new ScriptBody(bc.getFactory());
    func.setParent(getParent());
    List<Statement> statements = getBody().getStatements();
    Statement stat;
    Tag tag;
    // suppress WS between cffunction and the last cfargument
    Tag last = null;
    if (bc.getSupressWSbeforeArg()) {
        // check if there is a cfargument at all
        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.Argument")) {
                    last = tag;
                }
            }
        }
        // check if there are only literal WS printouts
        if (last != null) {
            it = statements.iterator();
            while (it.hasNext()) {
                stat = it.next();
                if (stat == last)
                    break;
                if (stat instanceof PrintOut) {
                    PrintOut po = (PrintOut) stat;
                    Expression expr = po.getExpr();
                    if (!(expr instanceof LitString) || !StringUtil.isWhiteSpace(((LitString) expr).getString())) {
                        last = null;
                        break;
                    }
                }
            }
        }
    }
    Iterator<Statement> it = statements.iterator();
    boolean beforeLastArgument = last != null;
    while (it.hasNext()) {
        stat = it.next();
        if (beforeLastArgument) {
            if (stat == last) {
                beforeLastArgument = false;
            } else if (stat instanceof PrintOut) {
                PrintOut po = (PrintOut) stat;
                Expression expr = po.getExpr();
                if (expr instanceof LitString) {
                    LitString ls = (LitString) expr;
                    if (StringUtil.isWhiteSpace(ls.getString()))
                        continue;
                }
            }
        }
        if (stat instanceof Tag) {
            tag = (Tag) stat;
            if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Argument")) {
                addArgument(func, tag);
                continue;
            }
        }
        functionBody.addStatement(stat);
    }
    func._writeOut(bc, type);
}
Also used : RefBoolean(lucee.commons.lang.types.RefBoolean) Statement(lucee.transformer.bytecode.Statement) Function(lucee.transformer.bytecode.statement.udf.Function) IFunction(lucee.transformer.bytecode.statement.IFunction) LitString(lucee.transformer.expression.literal.LitString) PrintOut(lucee.transformer.bytecode.statement.PrintOut) Expression(lucee.transformer.expression.Expression) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl) Body(lucee.transformer.bytecode.Body) BodyBase(lucee.transformer.bytecode.BodyBase)

Aggregations

Body (lucee.transformer.bytecode.Body)33 ScriptBody (lucee.transformer.bytecode.ScriptBody)21 BodyBase (lucee.transformer.bytecode.BodyBase)17 FunctionBody (lucee.transformer.bytecode.FunctionBody)15 Statement (lucee.transformer.bytecode.Statement)15 Tag (lucee.transformer.bytecode.statement.tag.Tag)12 Expression (lucee.transformer.expression.Expression)11 Position (lucee.transformer.Position)9 TemplateException (lucee.runtime.exp.TemplateException)8 TagLibTag (lucee.transformer.library.tag.TagLibTag)8 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)7 HasBody (lucee.transformer.bytecode.statement.HasBody)6 LitString (lucee.transformer.expression.literal.LitString)6 ArrayList (java.util.ArrayList)5 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)5 PrintOut (lucee.transformer.bytecode.statement.PrintOut)5 EvaluatorException (lucee.transformer.cfml.evaluator.EvaluatorException)5 TransformerException (lucee.transformer.TransformerException)4 StaticBody (lucee.transformer.bytecode.StaticBody)4 Literal (lucee.transformer.expression.literal.Literal)4