Search in sources :

Example 1 with FunctionValueImpl

use of lucee.runtime.type.FunctionValueImpl 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)

Example 2 with FunctionValueImpl

use of lucee.runtime.type.FunctionValueImpl in project Lucee by lucee.

the class Struct_ method _call.

protected static Struct _call(Object[] objArr, String expMessage, int type) throws PageException {
    StructImpl sct = type < 0 ? new StructImpl() : new StructImpl(type);
    FunctionValueImpl fv;
    for (int i = 0; i < objArr.length; i++) {
        if (objArr[i] instanceof FunctionValue) {
            fv = ((FunctionValueImpl) objArr[i]);
            if (fv.getNames() == null) {
                sct.set(fv.getNameAsKey(), fv.getValue());
            } else {
                String[] arr = fv.getNames();
                Struct s = sct;
                for (int y = 0; y < arr.length - 1; y++) {
                    s = touch(s, arr[y]);
                }
                s.set(KeyImpl.init(arr[arr.length - 1]), fv.getValue());
            }
        } else {
            throw new ExpressionException(expMessage);
        }
    }
    return sct;
}
Also used : StructImpl(lucee.runtime.type.StructImpl) FunctionValue(lucee.runtime.type.FunctionValue) FunctionValueImpl(lucee.runtime.type.FunctionValueImpl) ExpressionException(lucee.runtime.exp.ExpressionException) Struct(lucee.runtime.type.Struct)

Example 3 with FunctionValueImpl

use of lucee.runtime.type.FunctionValueImpl in project Lucee by lucee.

the class Caster method toFunctionValues.

public static Struct toFunctionValues(Object[] args, int offset, int len) throws ExpressionException {
    // TODO nicht sehr optimal
    Struct sct = new StructImpl(StructImpl.TYPE_LINKED);
    for (int i = offset; i < offset + len; i++) {
        if (args[i] instanceof FunctionValueImpl) {
            FunctionValueImpl value = (FunctionValueImpl) args[i];
            sct.setEL(value.getNameAsKey(), value.getValue());
        } else
            throw new ExpressionException("Missing argument name, when using named parameters to a function, every parameter must have a name [" + i + ":" + args[i].getClass().getName() + "].");
    }
    return sct;
}
Also used : StructImpl(lucee.runtime.type.StructImpl) FunctionValueImpl(lucee.runtime.type.FunctionValueImpl) ExpressionException(lucee.runtime.exp.ExpressionException) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) ObjectStruct(lucee.runtime.type.scope.ObjectStruct) MapAsStruct(lucee.runtime.type.wrap.MapAsStruct) Struct(lucee.runtime.type.Struct) CollectionStruct(lucee.runtime.type.CollectionStruct)

Example 4 with FunctionValueImpl

use of lucee.runtime.type.FunctionValueImpl in project Lucee by lucee.

the class Caster method toFunctionValues.

public static Object[] toFunctionValues(Struct args) {
    // TODO nicht sehr optimal
    Iterator<Entry<Key, Object>> it = args.entryIterator();
    Entry<Key, Object> e;
    List<FunctionValue> fvalues = new ArrayList<FunctionValue>();
    while (it.hasNext()) {
        e = it.next();
        fvalues.add(new FunctionValueImpl(e.getKey().getString(), e.getValue()));
    }
    return fvalues.toArray(new FunctionValue[fvalues.size()]);
}
Also used : Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) FunctionValue(lucee.runtime.type.FunctionValue) JavaObject(lucee.runtime.java.JavaObject) Key(lucee.runtime.type.Collection.Key) FunctionValueImpl(lucee.runtime.type.FunctionValueImpl)

Aggregations

FunctionValueImpl (lucee.runtime.type.FunctionValueImpl)4 ExpressionException (lucee.runtime.exp.ExpressionException)3 FunctionValue (lucee.runtime.type.FunctionValue)3 ArrayList (java.util.ArrayList)2 Struct (lucee.runtime.type.Struct)2 StructImpl (lucee.runtime.type.StructImpl)2 Entry (java.util.Map.Entry)1 FunctionException (lucee.runtime.exp.FunctionException)1 BIF (lucee.runtime.ext.function.BIF)1 InterpreterException (lucee.runtime.interpreter.InterpreterException)1 Casting (lucee.runtime.interpreter.ref.cast.Casting)1 LFunctionValue (lucee.runtime.interpreter.ref.literal.LFunctionValue)1 JavaObject (lucee.runtime.java.JavaObject)1 XMLMultiElementStruct (lucee.runtime.text.xml.struct.XMLMultiElementStruct)1 XMLStruct (lucee.runtime.text.xml.struct.XMLStruct)1 Key (lucee.runtime.type.Collection.Key)1 CollectionStruct (lucee.runtime.type.CollectionStruct)1 ObjectStruct (lucee.runtime.type.scope.ObjectStruct)1 MapAsStruct (lucee.runtime.type.wrap.MapAsStruct)1 FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)1