Search in sources :

Example 11 with FunctionLibFunction

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

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

the class ClassUtilImpl method loadBIF.

@Override
public BIF loadBIF(PageContext pc, String name) throws InstantiationException, IllegalAccessException {
    // first of all we chek if itis a class
    Class<?> res = lucee.commons.lang.ClassUtil.loadClass(name, null);
    if (res != null) {
        if (Reflector.isInstaneOf(res, BIF.class)) {
            return (BIF) res.newInstance();
        }
        return new BIFProxy(res);
    }
    FunctionLib[] flds = ((ConfigWebImpl) pc.getConfig()).getFLDs(pc.getCurrentTemplateDialect());
    FunctionLibFunction flf;
    for (int i = 0; i < flds.length; i++) {
        flf = flds[i].getFunction(name);
        if (flf != null)
            return flf.getBIF();
    }
    return null;
}
Also used : ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLib(lucee.transformer.library.function.FunctionLib) BIFProxy(lucee.runtime.functions.BIFProxy) BIF(lucee.runtime.ext.function.BIF)

Example 13 with FunctionLibFunction

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

the class FunctionException method getFunctionInfo.

private static String getFunctionInfo(PageContext pc, String functionName) {
    FunctionLib[] flds;
    int dialect = pc.getCurrentTemplateDialect();
    flds = ((ConfigImpl) pc.getConfig()).getFLDs(dialect);
    FunctionLibFunction function = null;
    for (int i = 0; i < flds.length; i++) {
        function = flds[i].getFunction(functionName.toLowerCase());
        if (function != null)
            break;
    }
    if (function == null)
        return "";
    StringBuilder rtn = new StringBuilder();
    rtn.append(function.getName() + "(");
    int optionals = 0;
    ArrayList<FunctionLibFunctionArg> args = function.getArg();
    for (int i = 0; i < args.size(); i++) {
        FunctionLibFunctionArg arg = args.get(i);
        if (i != 0)
            rtn.append(", ");
        if (!arg.getRequired()) {
            rtn.append("[");
            optionals++;
        }
        rtn.append(arg.getName());
        rtn.append(":");
        rtn.append(arg.getTypeAsString());
    }
    for (int i = 0; i < optionals; i++) rtn.append("]");
    rtn.append("):" + function.getReturnTypeAsString());
    return rtn.toString();
}
Also used : FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLib(lucee.transformer.library.function.FunctionLib) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 14 with FunctionLibFunction

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

the class AbstrCFMLScriptTransformer method funcStatement.

/**
 * Liest ein function Statement ein.
 * <br />
 * EBNF:<br />
 * <code>identifier spaces "(" spaces identifier spaces {"," spaces identifier spaces} ")" spaces block;</code>
 * @return function Statement
 * @throws TemplateException
 */
private final Statement funcStatement(ExprData data, Body parent) throws TemplateException {
    int pos = data.srcCode.getPos();
    // read 5 tokens (returntype,access modifier,"abstract|final|static","function", function name)
    String str = variableDec(data, false);
    // if there is no token at all we have no function
    if (str == null) {
        data.srcCode.setPos(pos);
        return null;
    }
    comments(data);
    String[] tokens = new String[] { str, null, null, null, null };
    tokens[1] = variableDec(data, false);
    comments(data);
    if (tokens[1] != null) {
        tokens[2] = variableDec(data, false);
        comments(data);
        if (tokens[2] != null) {
            tokens[3] = variableDec(data, false);
            comments(data);
            if (tokens[3] != null) {
                tokens[4] = identifier(data, false);
                comments(data);
            }
        }
    }
    // function name
    String functionName = null;
    for (int i = tokens.length - 1; i >= 0; i--) {
        // first from right is the function name
        if (tokens[i] != null) {
            functionName = tokens[i];
            tokens[i] = null;
            break;
        }
    }
    if (functionName == null || functionName.indexOf(',') != -1 || functionName.indexOf('[') != -1) {
        data.srcCode.setPos(pos);
        return null;
    }
    // throw new TemplateException(data.srcCode, "invalid syntax");
    String returnType = null;
    // search for "function"
    boolean hasOthers = false, first = true;
    for (int i = tokens.length - 1; i >= 0; i--) {
        if ("function".equalsIgnoreCase(tokens[i])) {
            // if it is the first "function" (from right) and we had already something else, the syntax is broken!
            if (hasOthers && first)
                throw new TemplateException(data.srcCode, "invalid syntax");
            else // we already have a return type,so this is the 3th "function"!
            if (returnType != null)
                throw new TemplateException(data.srcCode, "invalid syntax");
            else if (!first)
                returnType = tokens[i];
            first = false;
            tokens[i] = null;
        } else if (tokens[i] != null) {
            hasOthers = true;
        }
    }
    // no "function" found
    if (first) {
        data.srcCode.setPos(pos);
        return null;
    }
    // access modifier
    int _access, access = -1;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null && (_access = ComponentUtil.toIntAccess(tokens[i], -1)) != -1) {
            // we already have an access modifier
            if (access != -1) {
                // we already have a return type
                if (returnType != null)
                    throw new TemplateException(data.srcCode, "invalid syntax");
                returnType = tokens[i];
            } else
                access = _access;
            tokens[i] = null;
        }
    }
    // no access defined
    if (access == -1)
        access = Component.ACCESS_PUBLIC;
    // Non access modifier
    int _modifier, modifier = Component.MODIFIER_NONE;
    boolean isStatic = false;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null) {
            _modifier = ComponentUtil.toModifier(tokens[i], Component.MODIFIER_NONE, Component.MODIFIER_NONE);
            // abstract|final
            if (_modifier != Component.MODIFIER_NONE) {
                // we already have an Non access modifier
                if (modifier != Component.MODIFIER_NONE || isStatic) {
                    // we already have a return type
                    if (returnType != null)
                        throw new TemplateException(data.srcCode, "invalid syntax");
                    returnType = tokens[i];
                } else
                    modifier = _modifier;
                tokens[i] = null;
            } else // static
            if (tokens[i].equalsIgnoreCase("static")) {
                // we already have an Non access modifier
                if (modifier != Component.MODIFIER_NONE || isStatic) {
                    // we already have a return type
                    if (returnType != null)
                        throw new TemplateException(data.srcCode, "invalid syntax");
                    returnType = tokens[i];
                } else
                    isStatic = true;
                tokens[i] = null;
            }
        }
    }
    // return type
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null) {
            if (returnType != null)
                throw new TemplateException(data.srcCode, "invalid syntax");
            returnType = tokens[i];
        }
    }
    Position line = data.srcCode.getPosition();
    // Name
    if (!data.isCFC && !data.isInterface) {
        FunctionLibFunction flf = getFLF(data, functionName);
        try {
            if (flf != null && flf.getFunctionClassDefinition().getClazz() != CFFunction.class) {
                PageSource ps = null;
                if (data.srcCode instanceof PageSourceCode) {
                    ps = ((PageSourceCode) data.srcCode).getPageSource();
                }
                String path = null;
                if (ps != null) {
                    path = ps.getDisplayPath();
                    path = path.replace('\\', '/');
                }
                if (// TODO make better
                path == null || path.indexOf("/library/function/") == -1)
                    throw new TemplateException(data.srcCode, "The name [" + functionName + "] is already used by a built in Function");
            }
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            throw new PageRuntimeException(Caster.toPageException(t));
        }
    }
    Function res = closurePart(data, functionName, access, modifier, returnType, line, false);
    if (isStatic) {
        if (data.context == CTX_INTERFACE)
            throw new TemplateException(data.srcCode, "static functions are not allowed within the interface body");
        TagOther tag = createStaticTag(data, res.getStart());
        tag.getBody().addStatement(res);
        return tag;
    }
    return res;
}
Also used : CFFunction(lucee.runtime.functions.system.CFFunction) PageSourceCode(lucee.transformer.util.PageSourceCode) TemplateException(lucee.runtime.exp.TemplateException) Position(lucee.transformer.Position) TagOther(lucee.transformer.bytecode.statement.tag.TagOther) PageSource(lucee.runtime.PageSource) Function(lucee.transformer.bytecode.statement.udf.Function) CFFunction(lucee.runtime.functions.system.CFFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) PageRuntimeException(lucee.runtime.exp.PageRuntimeException)

Example 15 with FunctionLibFunction

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

the class GetFunctionKeywords method call.

public static Array call(PageContext pc) throws PageException {
    synchronized (keywords) {
        if (keywords == null) {
            Set<String> set = new HashSet<String>();
            FunctionLib[] flds;
            flds = ((ConfigImpl) pc.getConfig()).getFLDs(pc.getCurrentTemplateDialect());
            Map<String, FunctionLibFunction> functions;
            Iterator<FunctionLibFunction> it;
            FunctionLibFunction flf;
            String[] arr;
            for (int i = 0; i < flds.length; i++) {
                functions = flds[i].getFunctions();
                it = functions.values().iterator();
                while (it.hasNext()) {
                    flf = it.next();
                    if (flf.getStatus() != TagLib.STATUS_HIDDEN && flf.getStatus() != TagLib.STATUS_UNIMPLEMENTED && !ArrayUtil.isEmpty(flf.getKeywords())) {
                        arr = flf.getKeywords();
                        if (arr != null)
                            for (int y = 0; y < arr.length; y++) {
                                set.add(arr[y].toLowerCase());
                            }
                    }
                }
            }
            keywords = Caster.toArray(set);
            ArraySort.call(pc, keywords, "textnocase");
        // }
        }
    }
    return keywords;
}
Also used : FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLib(lucee.transformer.library.function.FunctionLib) HashSet(java.util.HashSet)

Aggregations

FunctionLibFunction (lucee.transformer.library.function.FunctionLibFunction)15 FunctionLib (lucee.transformer.library.function.FunctionLib)6 FunctionLibFunctionArg (lucee.transformer.library.function.FunctionLibFunctionArg)5 Ref (lucee.runtime.interpreter.ref.Ref)4 BIFCall (lucee.runtime.interpreter.ref.func.BIFCall)4 LString (lucee.runtime.interpreter.ref.literal.LString)4 ExpressionException (lucee.runtime.exp.ExpressionException)3 PageRuntimeException (lucee.runtime.exp.PageRuntimeException)3 TemplateException (lucee.runtime.exp.TemplateException)3 Key (lucee.runtime.type.Collection.Key)3 ArrayList (java.util.ArrayList)2 Entry (java.util.Map.Entry)2 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)2 CFFunction (lucee.runtime.functions.system.CFFunction)2 Casting (lucee.runtime.interpreter.ref.cast.Casting)2 Position (lucee.transformer.Position)2 BIF (lucee.transformer.bytecode.expression.var.BIF)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1