Search in sources :

Example 1 with InterpreterException

use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.

the class VT method getMatchingValueAndType.

private VT getMatchingValueAndType(FunctionLibFunctionArg flfa, FunctionValue[] fvalues, String[] names) throws ExpressionException {
    String flfan = flfa.getName();
    // first search if a argument match
    for (int i = 0; i < names.length; i++) {
        if (names[i] != null && names[i].equalsIgnoreCase(flfan)) {
            return new VT(fvalues[i].getValue(), flfa.getTypeAsString(), i);
        }
    }
    // then check if a alias match
    String alias = flfa.getAlias();
    if (!StringUtil.isEmpty(alias)) {
        for (int i = 0; i < names.length; i++) {
            if (names[i] != null && lucee.runtime.type.util.ListUtil.listFindNoCase(alias, names[i], ",") != -1) {
                return new VT(fvalues[i].getValue(), flfa.getTypeAsString(), i);
            }
        }
    }
    // if not required return the default value
    if (!flfa.getRequired()) {
        String defaultValue = flfa.getDefaultValue();
        String type = flfa.getTypeAsString().toLowerCase();
        if (defaultValue == null) {
            if (type.equals("boolean") || type.equals("bool"))
                return new VT(Boolean.FALSE, type, -1);
            if (type.equals("number") || type.equals("numeric") || type.equals("double"))
                return new VT(Constants.DOUBLE_ZERO, type, -1);
            return new VT(null, type, -1);
        }
        return new VT(defaultValue, type, -1);
    }
    ExpressionException ee = new InterpreterException("missing required argument [" + flfan + "] for function [" + flfa.getFunction().getName() + "]");
    UDFUtil.addFunctionDoc(ee, flfa.getFunction());
    throw ee;
}
Also used : InterpreterException(lucee.runtime.interpreter.InterpreterException) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 2 with InterpreterException

use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.

the class Elvis method getValue.

@Override
public Object getValue(PageContext pc) throws PageException {
    if (limited)
        throw new InterpreterException("invalid syntax, this operation is not supported in a json string.");
    if (left instanceof Variable) {
        Variable var = (Variable) left;
        String[] arr = LFunctionValue.toStringArray(pc, var);
        return lucee.runtime.op.Elvis.operate(pc, arr) ? left.getValue(pc) : right.getValue(pc);
    }
    Object val = left.getValue(pc);
    if (val != null)
        return val;
    return right.getValue(pc);
}
Also used : Variable(lucee.runtime.interpreter.ref.var.Variable) InterpreterException(lucee.runtime.interpreter.InterpreterException)

Example 3 with InterpreterException

use of lucee.runtime.interpreter.InterpreterException 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)

Example 4 with InterpreterException

use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.

the class VT method getValue.

@Override
public Object getValue(PageContext pc) throws PageException {
    Object[] arguments = null;
    if (isDynamic()) {
        arguments = RefUtil.getValue(pc, refArgs);
        if (flf.hasDefaultValues()) {
            List<Object> tmp = new ArrayList<Object>();
            ArrayList<FunctionLibFunctionArg> args = flf.getArg();
            Iterator<FunctionLibFunctionArg> it = args.iterator();
            FunctionLibFunctionArg arg;
            while (it.hasNext()) {
                arg = it.next();
                if (arg.getDefaultValue() != null)
                    tmp.add(new FunctionValueImpl(arg.getName(), arg.getDefaultValue()));
            }
            for (int i = 0; i < arguments.length; i++) {
                tmp.add(arguments[i]);
            }
            arguments = tmp.toArray();
        }
        arguments = new Object[] { arguments };
    } else {
        if (isNamed(pc, refArgs)) {
            FunctionValue[] fvalues = getFunctionValues(pc, refArgs);
            String[] names = getNames(fvalues);
            ArrayList<FunctionLibFunctionArg> list = flf.getArg();
            Iterator<FunctionLibFunctionArg> it = list.iterator();
            arguments = new Object[list.size()];
            FunctionLibFunctionArg flfa;
            int index = 0;
            VT vt;
            while (it.hasNext()) {
                flfa = it.next();
                vt = getMatchingValueAndType(flfa, fvalues, names);
                if (vt.index != -1)
                    names[vt.index] = null;
                arguments[index++] = new Casting(vt.type, CFTypes.toShort(vt.type, false, CFTypes.TYPE_UNKNOW), vt.value).getValue(pc);
            }
            for (int y = 0; y < names.length; y++) {
                if (names[y] != null) {
                    ExpressionException ee = new InterpreterException("argument [" + names[y] + "] is not allowed for function [" + flf.getName() + "]");
                    UDFUtil.addFunctionDoc(ee, flf);
                    throw ee;
                }
            }
        } else {
            arguments = RefUtil.getValue(pc, refArgs);
        }
    }
    BIF bif = flf.getBIF();
    if (flf.getMemberChaining() && obj != null) {
        bif.invoke(pc, arguments);
        return obj;
    }
    if (!isDynamic() && flf.getArgMin() > arguments.length) {
        throw new FunctionException(pc, flf.getName(), flf.getArgMin(), flf.getArgMax(), arguments.length);
    }
    return Caster.castTo(pc, flf.getReturnTypeAsString(), bif.invoke(pc, arguments), false);
}
Also used : InterpreterException(lucee.runtime.interpreter.InterpreterException) ArrayList(java.util.ArrayList) FunctionException(lucee.runtime.exp.FunctionException) ExpressionException(lucee.runtime.exp.ExpressionException) Casting(lucee.runtime.interpreter.ref.cast.Casting) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) FunctionValue(lucee.runtime.type.FunctionValue) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg) FunctionValueImpl(lucee.runtime.type.FunctionValueImpl) BIF(lucee.runtime.ext.function.BIF)

Aggregations

InterpreterException (lucee.runtime.interpreter.InterpreterException)4 ExpressionException (lucee.runtime.exp.ExpressionException)3 Casting (lucee.runtime.interpreter.ref.cast.Casting)2 LFunctionValue (lucee.runtime.interpreter.ref.literal.LFunctionValue)2 FunctionValue (lucee.runtime.type.FunctionValue)2 ArrayList (java.util.ArrayList)1 FunctionException (lucee.runtime.exp.FunctionException)1 BIF (lucee.runtime.ext.function.BIF)1 Variable (lucee.runtime.interpreter.ref.var.Variable)1 FunctionValueImpl (lucee.runtime.type.FunctionValueImpl)1 FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)1