Search in sources :

Example 1 with Casting

use of lucee.runtime.interpreter.ref.cast.Casting 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 Casting

use of lucee.runtime.interpreter.ref.cast.Casting in project Lucee by lucee.

the class MemberUtil method call.

public static Object call(PageContext pc, Object coll, Collection.Key methodName, Object[] args, short[] types, String[] strTypes) throws PageException {
    // look for members
    short type;
    String strType;
    Map<Key, FunctionLibFunction> members = null;
    for (int i = 0; i < types.length; i++) {
        type = types[i];
        strType = strTypes[i];
        members = getMembers(pc, type);
        FunctionLibFunction member = members.get(methodName);
        if (member != null) {
            List<FunctionLibFunctionArg> _args = member.getArg();
            if (args.length < _args.size()) {
                ArrayList<Ref> refs = new ArrayList<Ref>();
                int pos = member.getMemberPosition();
                FunctionLibFunctionArg flfa;
                Iterator<FunctionLibFunctionArg> it = _args.iterator();
                int glbIndex = 0, argIndex = -1;
                while (it.hasNext()) {
                    glbIndex++;
                    flfa = it.next();
                    if (glbIndex == pos) {
                        refs.add(new Casting(strType, type, coll));
                    } else if (args.length > ++argIndex) {
                        // careful, argIndex is only incremented when condition above is false
                        refs.add(new Casting(flfa.getTypeAsString(), flfa.getType(), args[argIndex]));
                    }
                }
                return new BIFCall(coll, member, refs.toArray(new Ref[refs.size()])).getValue(pc);
            }
        }
    }
    // do reflection
    if (pc.getConfig().getSecurityManager().getAccess(lucee.runtime.security.SecurityManager.TYPE_DIRECT_JAVA_ACCESS) == lucee.runtime.security.SecurityManager.VALUE_YES) {
        if (!(coll instanceof Undefined)) {
            Object res = callMethod(coll, methodName, args);
            if (res != DEFAULT)
                return res;
        }
    }
    // merge
    if (types.length > 1) {
        Map<Key, FunctionLibFunction> tmp;
        members = null;
        for (int i = 0; i < types.length; i++) {
            tmp = getMembers(pc, types[i]);
            if (members == null)
                members = tmp;
            else {
                Iterator<Entry<Key, FunctionLibFunction>> it = tmp.entrySet().iterator();
                Entry<Key, FunctionLibFunction> e;
                while (it.hasNext()) {
                    e = it.next();
                    members.put(e.getKey(), e.getValue());
                }
            }
        }
    }
    Set<Key> set = members.keySet();
    String msg = ExceptionUtil.similarKeyMessage(set.toArray(new Key[set.size()]), methodName.getString(), "function", "functions", "Object", true);
    throw new ExpressionException(msg);
// throw new ExpressionException("No matching function member ["+methodName+"] found, available function members are ["+
// lucee.runtime.type.util.ListUtil.sort(CollectionUtil.getKeyList(members.keySet().iterator(), ","),"textnocase","asc",",")+"]");
}
Also used : Undefined(lucee.runtime.type.scope.Undefined) ArrayList(java.util.ArrayList) 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) Entry(java.util.Map.Entry) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) BIFCall(lucee.runtime.interpreter.ref.func.BIFCall) Key(lucee.runtime.type.Collection.Key) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 3 with Casting

use of lucee.runtime.interpreter.ref.cast.Casting in project Lucee by lucee.

the class CFMLExpressionInterpreter method functionArg.

/**
 * 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.
 * @param flf FLD Function definition .
 * @return CFXD Element
 * @throws PageException
 */
private Ref[] functionArg(String name, boolean checkLibrary, FunctionLibFunction flf, char end) throws PageException {
    // get Function Library
    checkLibrary = checkLibrary && flf != null;
    // Function Attributes
    List<Ref> arr = new ArrayList<Ref>();
    List<FunctionLibFunctionArg> arrFuncLibAtt = null;
    int libLen = 0;
    if (checkLibrary) {
        arrFuncLibAtt = flf.getArg();
        libLen = arrFuncLibAtt.size();
    }
    int count = 0;
    Ref ref;
    do {
        cfml.next();
        cfml.removeSpace();
        // finish
        if (cfml.isCurrent(end))
            break;
        // too many Attributes
        boolean isDynamic = false;
        int max = -1;
        if (checkLibrary) {
            isDynamic = isDynamic(flf);
            max = flf.getArgMax();
            // Dynamic
            if (isDynamic) {
                if (max != -1 && max <= count)
                    throw new InterpreterException("too many Attributes in function [" + name + "]");
            } else // Fix
            {
                if (libLen <= count)
                    throw new InterpreterException("too many Attributes in function [" + name + "]");
            }
        }
        if (checkLibrary && !isDynamic) {
            // current attribues from library
            FunctionLibFunctionArg funcLibAtt = (FunctionLibFunctionArg) arrFuncLibAtt.get(count);
            short type = CFTypes.toShort(funcLibAtt.getTypeAsString(), false, CFTypes.TYPE_UNKNOW);
            if (type == CFTypes.TYPE_VARIABLE_STRING) {
                arr.add(functionArgDeclarationVarString());
            } else {
                ref = functionArgDeclaration();
                arr.add(new Casting(funcLibAtt.getTypeAsString(), type, ref));
            }
        } else {
            arr.add(functionArgDeclaration());
        }
        cfml.removeSpace();
        count++;
    } while (cfml.isCurrent(','));
    // end with ) ??
    if (!cfml.forwardIfCurrent(end)) {
        if (name.startsWith("_json"))
            throw new InterpreterException("Invalid Syntax Closing [" + end + "] not found");
        throw new InterpreterException("Invalid Syntax Closing [" + end + "] for function [" + name + "] not found");
    }
    // check min attributes
    if (checkLibrary && flf.getArgMin() > count)
        throw new InterpreterException("to less Attributes in function [" + name + "]");
    cfml.removeSpace();
    return (Ref[]) arr.toArray(new Ref[arr.size()]);
}
Also used : Casting(lucee.runtime.interpreter.ref.cast.Casting) Ref(lucee.runtime.interpreter.ref.Ref) ArrayList(java.util.ArrayList) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 4 with Casting

use of lucee.runtime.interpreter.ref.cast.Casting 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 5 with Casting

use of lucee.runtime.interpreter.ref.cast.Casting in project Lucee by lucee.

the class BIF method call.

@Override
public Object call(PageContext pageContext, Object[] args, boolean doIncludePath) throws PageException {
    ArrayList<FunctionLibFunctionArg> flfas = flf.getArg();
    FunctionLibFunctionArg flfa;
    List<Ref> refs = new ArrayList<Ref>();
    for (int i = 0; i < args.length; i++) {
        if (i >= flfas.size())
            throw new ApplicationException("too many Attributes in function call [" + flf.getName() + "]");
        flfa = flfas.get(i);
        refs.add(new Casting(flfa.getTypeAsString(), flfa.getType(), args[i]));
    }
    BIFCall call = new BIFCall(flf, refs.toArray(new Ref[refs.size()]));
    return call.getValue(pageContext);
}
Also used : Casting(lucee.runtime.interpreter.ref.cast.Casting) Ref(lucee.runtime.interpreter.ref.Ref) ApplicationException(lucee.runtime.exp.ApplicationException) ArrayList(java.util.ArrayList) BIFCall(lucee.runtime.interpreter.ref.func.BIFCall) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Aggregations

Casting (lucee.runtime.interpreter.ref.cast.Casting)8 ArrayList (java.util.ArrayList)6 Ref (lucee.runtime.interpreter.ref.Ref)6 FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)6 ExpressionException (lucee.runtime.exp.ExpressionException)5 BIFCall (lucee.runtime.interpreter.ref.func.BIFCall)4 LFunctionValue (lucee.runtime.interpreter.ref.literal.LFunctionValue)4 LString (lucee.runtime.interpreter.ref.literal.LString)3 InterpreterException (lucee.runtime.interpreter.InterpreterException)2 Key (lucee.runtime.type.Collection.Key)2 FunctionValue (lucee.runtime.type.FunctionValue)2 FunctionLibFunction (lucee.transformer.library.function.FunctionLibFunction)2 Entry (java.util.Map.Entry)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 FunctionException (lucee.runtime.exp.FunctionException)1 BIF (lucee.runtime.ext.function.BIF)1 LNumber (lucee.runtime.interpreter.ref.literal.LNumber)1 BigMinus (lucee.runtime.interpreter.ref.op.BigMinus)1 BigPlus (lucee.runtime.interpreter.ref.op.BigPlus)1 Minus (lucee.runtime.interpreter.ref.op.Minus)1