Search in sources :

Example 1 with FunctionLibFunctionArg

use of lucee.transformer.library.function.FunctionLibFunctionArg in project Lucee by lucee.

the class BIF method getFunctionArguments.

@Override
public FunctionArgument[] getFunctionArguments() {
    if (args == null) {
        ArrayList<FunctionLibFunctionArg> src = flf.getArg();
        args = new FunctionArgument[src.size()];
        String def;
        int index = -1;
        FunctionLibFunctionArg arg;
        Iterator<FunctionLibFunctionArg> it = src.iterator();
        while (it.hasNext()) {
            arg = it.next();
            def = arg.getDefaultValue();
            args[++index] = new FunctionArgumentImpl(KeyImpl.init(arg.getName()), arg.getTypeAsString(), arg.getType(), arg.getRequired(), def == null ? FunctionArgument.DEFAULT_TYPE_NULL : FunctionArgument.DEFAULT_TYPE_LITERAL, true, arg.getName(), arg.getDescription(), null);
        }
    }
    return args;
}
Also used : LString(lucee.runtime.interpreter.ref.literal.LString) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 2 with FunctionLibFunctionArg

use of lucee.transformer.library.function.FunctionLibFunctionArg 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 3 with FunctionLibFunctionArg

use of lucee.transformer.library.function.FunctionLibFunctionArg in project Lucee by lucee.

the class ConfigImpl method createFunction.

public void createFunction(FunctionLib fl, String filename) {
    // filename.substring(0,filename.length()-(getCFMLExtensions().length()+1));
    String name = toName(filename);
    FunctionLibFunction flf = new FunctionLibFunction(fl, true);
    flf.setArgType(FunctionLibFunction.ARG_DYNAMIC);
    flf.setFunctionClass("lucee.runtime.functions.system.CFFunction", null, null);
    flf.setName(name);
    flf.setReturn("object");
    FunctionLibFunctionArg arg = new FunctionLibFunctionArg(flf);
    arg.setName("__filename");
    arg.setRequired(true);
    arg.setType("string");
    arg.setHidden(true);
    arg.setDefaultValue(filename);
    flf.setArg(arg);
    arg = new FunctionLibFunctionArg(flf);
    arg.setName("__name");
    arg.setRequired(true);
    arg.setHidden(true);
    arg.setType("string");
    arg.setDefaultValue(name);
    flf.setArg(arg);
    arg = new FunctionLibFunctionArg(flf);
    arg.setName("__isweb");
    arg.setRequired(true);
    arg.setHidden(true);
    arg.setType("boolean");
    arg.setDefaultValue(this instanceof ConfigWeb ? "true" : "false");
    flf.setArg(arg);
    fl.setFunction(flf);
}
Also used : FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 4 with FunctionLibFunctionArg

use of lucee.transformer.library.function.FunctionLibFunctionArg 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 5 with FunctionLibFunctionArg

use of lucee.transformer.library.function.FunctionLibFunctionArg in project Lucee by lucee.

the class UDFUtil method addFunctionDoc.

/**
 * add detailed function documentation to the exception
 * @param pe
 * @param flf
 */
public static void addFunctionDoc(PageExceptionImpl pe, FunctionLibFunction flf) {
    ArrayList<FunctionLibFunctionArg> args = flf.getArg();
    Iterator<FunctionLibFunctionArg> it = args.iterator();
    // Pattern
    StringBuilder pattern = new StringBuilder(flf.getName());
    StringBuilder end = new StringBuilder();
    pattern.append("(");
    FunctionLibFunctionArg arg;
    int c = 0;
    while (it.hasNext()) {
        arg = it.next();
        if (!arg.isRequired()) {
            pattern.append(" [");
            end.append("]");
        }
        if (c++ > 0)
            pattern.append(", ");
        pattern.append(arg.getName());
        pattern.append(":");
        pattern.append(arg.getTypeAsString());
    }
    pattern.append(end);
    pattern.append("):");
    pattern.append(flf.getReturnTypeAsString());
    pe.setAdditional(KeyConstants._Pattern, pattern);
    // Documentation
    StringBuilder doc = new StringBuilder(flf.getDescription());
    StringBuilder req = new StringBuilder();
    StringBuilder opt = new StringBuilder();
    StringBuilder tmp;
    doc.append("\n");
    it = args.iterator();
    while (it.hasNext()) {
        arg = it.next();
        tmp = arg.isRequired() ? req : opt;
        tmp.append("- ");
        tmp.append(arg.getName());
        tmp.append(" (");
        tmp.append(arg.getTypeAsString());
        tmp.append("): ");
        tmp.append(arg.getDescription());
        tmp.append("\n");
    }
    if (req.length() > 0)
        doc.append("\nRequired:\n").append(req);
    if (opt.length() > 0)
        doc.append("\nOptional:\n").append(opt);
    pe.setAdditional(KeyImpl.init("Documentation"), doc);
}
Also used : FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Aggregations

FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)15 ArrayList (java.util.ArrayList)6 Casting (lucee.runtime.interpreter.ref.cast.Casting)6 Ref (lucee.runtime.interpreter.ref.Ref)5 FunctionLibFunction (lucee.transformer.library.function.FunctionLibFunction)5 ExpressionException (lucee.runtime.exp.ExpressionException)4 BIFCall (lucee.runtime.interpreter.ref.func.BIFCall)4 LString (lucee.runtime.interpreter.ref.literal.LString)4 TemplateException (lucee.runtime.exp.TemplateException)3 LFunctionValue (lucee.runtime.interpreter.ref.literal.LFunctionValue)3 Key (lucee.runtime.type.Collection.Key)3 Array (lucee.runtime.type.Array)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 Struct (lucee.runtime.type.Struct)2 StructImpl (lucee.runtime.type.StructImpl)2 ExprString (lucee.transformer.expression.ExprString)2 LitString (lucee.transformer.expression.literal.LitString)2 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 ClassDefinition (lucee.runtime.db.ClassDefinition)1