Search in sources :

Example 1 with LFunctionValue

use of lucee.runtime.interpreter.ref.literal.LFunctionValue in project Lucee by lucee.

the class BIF method callWithNamedValues.

@Override
public Object callWithNamedValues(PageContext pageContext, Struct values, boolean doIncludePath) throws PageException {
    ArrayList<FunctionLibFunctionArg> flfas = flf.getArg();
    Iterator<FunctionLibFunctionArg> it = flfas.iterator();
    FunctionLibFunctionArg arg;
    Object val;
    List<Ref> refs = new ArrayList<Ref>();
    while (it.hasNext()) {
        arg = it.next();
        // match by name
        val = values.get(arg.getName(), null);
        // match by alias
        if (val == null) {
            String alias = arg.getAlias();
            if (!StringUtil.isEmpty(alias, true)) {
                String[] aliases = lucee.runtime.type.util.ListUtil.trimItems(lucee.runtime.type.util.ListUtil.listToStringArray(alias, ','));
                for (int x = 0; x < aliases.length; x++) {
                    val = values.get(aliases[x], null);
                    if (val != null)
                        break;
                }
            }
        }
        if (val == null) {
            if (arg.getRequired()) {
                String[] names = flf.getMemberNames();
                String n = ArrayUtil.isEmpty(names) ? "" : names[0];
                throw new ExpressionException("missing required argument [" + arg.getName() + "] for build in function call [" + n + "]");
            }
        } else {
            refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), val)));
        }
    }
    BIFCall call = new BIFCall(flf, refs.toArray(new Ref[refs.size()]));
    return call.getValue(pageContext);
}
Also used : ArrayList(java.util.ArrayList) LString(lucee.runtime.interpreter.ref.literal.LString) ExpressionException(lucee.runtime.exp.ExpressionException) LString(lucee.runtime.interpreter.ref.literal.LString) Casting(lucee.runtime.interpreter.ref.cast.Casting) Ref(lucee.runtime.interpreter.ref.Ref) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) BIFCall(lucee.runtime.interpreter.ref.func.BIFCall) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 2 with LFunctionValue

use of lucee.runtime.interpreter.ref.literal.LFunctionValue in project Lucee by lucee.

the class CFMLExpressionInterpreter method functionArgDeclaration.

/**
 * Liest einen gelableten  Funktionsparamter ein
 * <br />
 * EBNF:<br />
 * <code>assignOp [":" spaces assignOp];</code>
 * @return CFXD Element
 * @throws PageException
 */
private Ref functionArgDeclaration() throws PageException {
    Ref ref = impOp();
    if (cfml.forwardIfCurrent(':') || cfml.forwardIfCurrent('=')) {
        cfml.removeSpace();
        ref = new LFunctionValue(ref, assignOp());
    }
    return ref;
}
Also used : Ref(lucee.runtime.interpreter.ref.Ref) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue)

Example 3 with LFunctionValue

use of lucee.runtime.interpreter.ref.literal.LFunctionValue in project Lucee by lucee.

the class MemberUtil method callWithNamedValues.

public static Object callWithNamedValues(PageContext pc, Object coll, Collection.Key methodName, Struct args, short type, String strType) throws PageException {
    Map<Key, FunctionLibFunction> members = getMembers(pc, type);
    FunctionLibFunction member = members.get(methodName);
    if (member != null) {
        List<FunctionLibFunctionArg> _args = member.getArg();
        FunctionLibFunctionArg arg;
        if (args.size() < _args.size()) {
            Object val;
            ArrayList<Ref> refs = new ArrayList<Ref>();
            arg = _args.get(0);
            refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), coll)));
            for (int y = 1; y < _args.size(); y++) {
                arg = _args.get(y);
                // match by name
                val = args.get(arg.getName(), null);
                // match by alias
                if (val == null) {
                    String alias = arg.getAlias();
                    if (!StringUtil.isEmpty(alias, true)) {
                        String[] aliases = lucee.runtime.type.util.ListUtil.trimItems(lucee.runtime.type.util.ListUtil.listToStringArray(alias, ','));
                        for (int x = 0; x < aliases.length; x++) {
                            val = args.get(aliases[x], null);
                            if (val != null)
                                break;
                        }
                    }
                }
                if (val == null) {
                    if (arg.getRequired()) {
                        String[] names = member.getMemberNames();
                        String n = ArrayUtil.isEmpty(names) ? "" : names[0];
                        throw new ExpressionException("missing required argument [" + arg.getName() + "] for member function call [" + n + "]");
                    }
                } else {
                    refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), val)));
                // refs.add(new LFunctionValue(new LString(arg.getName()),new Casting(pc,arg.getTypeAsString(),arg.getType(),val)));
                }
            }
            return new BIFCall(coll, member, refs.toArray(new Ref[refs.size()])).getValue(pc);
        }
    }
    throw new ExpressionException("No matching function member [" + methodName + "] for call with named arguments found, available function members are [" + lucee.runtime.type.util.ListUtil.sort(CollectionUtil.getKeyList(members.keySet().iterator(), ","), "textnocase", "asc", ",") + "]");
}
Also used : ArrayList(java.util.ArrayList) LString(lucee.runtime.interpreter.ref.literal.LString) LString(lucee.runtime.interpreter.ref.literal.LString) ExpressionException(lucee.runtime.exp.ExpressionException) Casting(lucee.runtime.interpreter.ref.cast.Casting) Ref(lucee.runtime.interpreter.ref.Ref) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) BIFCall(lucee.runtime.interpreter.ref.func.BIFCall) Key(lucee.runtime.type.Collection.Key) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 4 with LFunctionValue

use of lucee.runtime.interpreter.ref.literal.LFunctionValue in project Lucee by lucee.

the class VT method isNamed.

private boolean isNamed(PageContext pc, Ref[] refArgs) throws PageException {
    if (ArrayUtil.isEmpty(refArgs))
        return false;
    Casting cast;
    int count = 0;
    for (int i = 0; i < refArgs.length; i++) {
        if (refArgs[i] instanceof Casting) {
            cast = (Casting) refArgs[i];
            if (cast.getRef() instanceof LFunctionValue && ((LFunctionValue) cast.getRef()).getValue(pc) instanceof FunctionValue) {
                count++;
            }
        }
    }
    if (count != 0 && count != refArgs.length) {
        ExpressionException ee = new InterpreterException("invalid argument for function " + flf.getName() + ", you can not mix named and unnamed arguments");
        UDFUtil.addFunctionDoc(ee, flf);
        throw ee;
    }
    return count != 0;
}
Also used : Casting(lucee.runtime.interpreter.ref.cast.Casting) InterpreterException(lucee.runtime.interpreter.InterpreterException) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) FunctionValue(lucee.runtime.type.FunctionValue) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) ExpressionException(lucee.runtime.exp.ExpressionException)

Aggregations

LFunctionValue (lucee.runtime.interpreter.ref.literal.LFunctionValue)4 ExpressionException (lucee.runtime.exp.ExpressionException)3 Ref (lucee.runtime.interpreter.ref.Ref)3 Casting (lucee.runtime.interpreter.ref.cast.Casting)3 ArrayList (java.util.ArrayList)2 BIFCall (lucee.runtime.interpreter.ref.func.BIFCall)2 LString (lucee.runtime.interpreter.ref.literal.LString)2 FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)2 InterpreterException (lucee.runtime.interpreter.InterpreterException)1 Key (lucee.runtime.type.Collection.Key)1 FunctionValue (lucee.runtime.type.FunctionValue)1 FunctionLibFunction (lucee.transformer.library.function.FunctionLibFunction)1