Search in sources :

Example 1 with FunctionMember

use of lucee.transformer.bytecode.expression.var.FunctionMember in project Lucee by lucee.

the class AbstrCFMLExprTransformer method newOp.

private Expression newOp(ExprData data, Expression expr) throws TemplateException {
    if (!(expr instanceof Variable))
        return expr;
    Variable var = (Variable) expr;
    Member m = var.getFirstMember();
    if (!(m instanceof DataMember))
        return expr;
    ExprString n = ((DataMember) m).getName();
    if (!(n instanceof LitString))
        return expr;
    LitString ls = (LitString) n;
    if (!"new".equalsIgnoreCase(ls.getString()))
        return expr;
    int start = data.srcCode.getPos();
    ExprString exprName = readComponentPath(data);
    if (exprName == null) {
        data.srcCode.setPos(start);
        return expr;
    }
    comments(data);
    if (data.srcCode.isCurrent('(')) {
        FunctionMember func = getFunctionMember(data, Identifier.toIdentifier(data.factory, "_createComponent", Identifier.CASE_ORIGNAL, null, null), true);
        func.addArgument(new Argument(exprName, "string"));
        Variable v = expr.getFactory().createVariable(expr.getStart(), expr.getEnd());
        v.addMember(func);
        comments(data);
        return v;
    }
    data.srcCode.setPos(start);
    return expr;
}
Also used : LitString(lucee.transformer.expression.literal.LitString) OpVariable(lucee.transformer.bytecode.op.OpVariable) Variable(lucee.transformer.expression.var.Variable) NamedArgument(lucee.transformer.bytecode.expression.var.NamedArgument) Argument(lucee.transformer.bytecode.expression.var.Argument) ExprString(lucee.transformer.expression.ExprString) DataMember(lucee.transformer.expression.var.DataMember) FunctionMember(lucee.transformer.bytecode.expression.var.FunctionMember) DataMember(lucee.transformer.expression.var.DataMember) Member(lucee.transformer.expression.var.Member) FunctionMember(lucee.transformer.bytecode.expression.var.FunctionMember)

Example 2 with FunctionMember

use of lucee.transformer.bytecode.expression.var.FunctionMember in project Lucee by lucee.

the class AbstrCFMLExprTransformer method startElement.

/**
 * Extrahiert den Start Element einer Variale,
 * dies ist entweder eine Funktion, eine Scope Definition oder eine undefinierte Variable.
 * <br />
 * EBNF:<br />
 * <code>identifier "(" functionArg ")" | scope | identifier;</code>
 * @param name Einstiegsname
 * @return CFXD Element
 * @throws TemplateException
 */
private Variable startElement(ExprData data, Identifier name, Position line) throws TemplateException {
    // check function
    if (data.srcCode.isCurrent('(')) {
        FunctionMember func = getFunctionMember(data, name, true);
        Variable var = name.getFactory().createVariable(line, data.srcCode.getPosition());
        var.addMember(func);
        comments(data);
        return var;
    }
    // check scope
    Variable var = scope(data, name, line);
    if (var != null)
        return var;
    // undefined variable
    var = name.getFactory().createVariable(line, data.srcCode.getPosition());
    var.addMember(data.factory.createDataMember(name));
    comments(data);
    return var;
}
Also used : OpVariable(lucee.transformer.bytecode.op.OpVariable) Variable(lucee.transformer.expression.var.Variable) FunctionMember(lucee.transformer.bytecode.expression.var.FunctionMember)

Example 3 with FunctionMember

use of lucee.transformer.bytecode.expression.var.FunctionMember in project Lucee by lucee.

the class AbstrCFMLExprTransformer method getFunctionMember.

/**
 * Liest die Argumente eines Funktonsaufruf ein und prueft ob die Funktion
 * innerhalb der FLD (Function Library Descriptor) definiert ist.
 * Falls sie existiert wird die Funktion gegen diese geprueft und ein build-in-function CFXD Element generiert,
 * ansonsten ein normales funcion-call Element.
 * <br />
 * EBNF:<br />
 * <code>[impOp{"," impOp}];</code>
 * @param name Identifier der Funktion als Zeichenkette
 * @param checkLibrary Soll geprueft werden ob die Funktion innerhalb der Library existiert.
 * @return CFXD Element
 * @throws TemplateException
 */
private FunctionMember getFunctionMember(ExprData data, final ExprString name, boolean checkLibrary) throws TemplateException {
    // get Function Library
    checkLibrary = checkLibrary && data.flibs != null;
    FunctionLibFunction flf = null;
    if (checkLibrary) {
        if (!(name instanceof Literal))
            // should never happen!
            throw new TemplateException(data.srcCode, "syntax error");
        for (int i = 0; i < data.flibs.length; i++) {
            flf = data.flibs[i].getFunction(((Literal) name).getString());
            if (flf != null)
                break;
        }
        if (flf == null) {
            checkLibrary = false;
        }
    }
    FunctionMember fm = null;
    while (true) {
        int pos = data.srcCode.getPos();
        // Element Function
        if (checkLibrary) {
            BIF bif = new BIF(data.factory, data.settings, flf);
            // TODO data.ep.add(flf, bif, data.srcCode);
            bif.setArgType(flf.getArgType());
            try {
                bif.setClassDefinition(flf.getFunctionClassDefinition());
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
                throw new PageRuntimeException(t);
            }
            bif.setReturnType(flf.getReturnTypeAsString());
            fm = bif;
            if (flf.getArgType() == FunctionLibFunction.ARG_DYNAMIC && flf.hasDefaultValues()) {
                ArrayList<FunctionLibFunctionArg> args = flf.getArg();
                Iterator<FunctionLibFunctionArg> it = args.iterator();
                FunctionLibFunctionArg arg;
                while (it.hasNext()) {
                    arg = it.next();
                    if (arg.getDefaultValue() != null)
                        bif.addArgument(new NamedArgument(data.factory.createLitString(arg.getName()), data.factory.createLitString(arg.getDefaultValue()), arg.getTypeAsString(), false));
                }
            }
        } else {
            fm = new UDF(name);
        }
        int count = getFunctionMemberAttrs(data, name, checkLibrary, fm, flf);
        if (checkLibrary) {
            // pre
            if (flf.hasTteClass()) {
                FunctionLibFunction tmp = flf.getEvaluator().pre((BIF) fm, flf);
                if (tmp != null && tmp != flf) {
                    flf = tmp;
                    data.srcCode.setPos(pos);
                    continue;
                }
            }
            // check max attributes
            {
                boolean isDynamic = flf.getArgType() == FunctionLibFunction.ARG_DYNAMIC;
                int max = flf.getArgMax();
                // Dynamic
                if (isDynamic) {
                    if (max != -1 && max < fm.getArguments().length)
                        throw new TemplateException(data.srcCode, "too many Attributes (" + max + ":" + fm.getArguments().length + ") in function [ " + ASMUtil.display(name) + " ]");
                } else // Fix
                {
                    if (flf.getArg().size() < fm.getArguments().length) {
                        TemplateException te = new TemplateException(data.srcCode, "too many Attributes (" + flf.getArg().size() + ":" + fm.getArguments().length + ") in function call [" + ASMUtil.display(name) + "]");
                        UDFUtil.addFunctionDoc(te, flf);
                        throw te;
                    }
                }
            }
            // check min attributes
            if (flf.getArgMin() > count) {
                TemplateException te = new TemplateException(data.srcCode, "too few attributes in function [" + ASMUtil.display(name) + "]");
                if (flf.getArgType() == FunctionLibFunction.ARG_FIX)
                    UDFUtil.addFunctionDoc(te, flf);
                throw te;
            }
            // evaluator
            if (flf.hasTteClass()) {
                flf.getEvaluator().execute((BIF) fm, flf);
            }
        }
        comments(data);
        if (checkLibrary)
            data.ep.add(flf, (BIF) fm, data.srcCode);
        break;
    }
    return fm;
}
Also used : TemplateException(lucee.runtime.exp.TemplateException) FunctionMember(lucee.transformer.bytecode.expression.var.FunctionMember) UDF(lucee.transformer.bytecode.expression.var.UDF) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) Literal(lucee.transformer.expression.literal.Literal) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) NamedArgument(lucee.transformer.bytecode.expression.var.NamedArgument) BIF(lucee.transformer.bytecode.expression.var.BIF) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Aggregations

FunctionMember (lucee.transformer.bytecode.expression.var.FunctionMember)3 NamedArgument (lucee.transformer.bytecode.expression.var.NamedArgument)2 OpVariable (lucee.transformer.bytecode.op.OpVariable)2 Variable (lucee.transformer.expression.var.Variable)2 PageRuntimeException (lucee.runtime.exp.PageRuntimeException)1 TemplateException (lucee.runtime.exp.TemplateException)1 Argument (lucee.transformer.bytecode.expression.var.Argument)1 BIF (lucee.transformer.bytecode.expression.var.BIF)1 UDF (lucee.transformer.bytecode.expression.var.UDF)1 ExprString (lucee.transformer.expression.ExprString)1 LitString (lucee.transformer.expression.literal.LitString)1 Literal (lucee.transformer.expression.literal.Literal)1 DataMember (lucee.transformer.expression.var.DataMember)1 Member (lucee.transformer.expression.var.Member)1 FunctionLibFunction (lucee.transformer.library.function.FunctionLibFunction)1 FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)1