Search in sources :

Example 1 with Function

use of org.mule.mvel2.ast.Function in project mvel by mikebrock.

the class FunctionsTest method testFunctionDefAndCall2.

public void testFunctionDefAndCall2() {
    ExpressionCompiler compiler = new ExpressionCompiler("function heyFoo() { return 'Foobar'; };\n" + "return heyFoo() + heyFoo();");
    Serializable s = compiler.compile();
    Map<String, Function> m = extractAllDeclaredFunctions((CompiledExpression) s);
    assertTrue(m.containsKey("heyFoo"));
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    OptimizerFactory.setDefaultOptimizer("dynamic");
}
Also used : Function(org.mvel2.ast.Function) Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 2 with Function

use of org.mule.mvel2.ast.Function in project mvel by mikebrock.

the class DynamicFunctionAccessor method getValue.

public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
    Object[] parms = null;
    Function function = (Function) ctx;
    if (parameters != null && parameters.length != 0) {
        parms = new Object[parameters.length];
        for (int i = 0; i < parms.length; i++) {
            parms[i] = parameters[i].getValue(ctx, elCtx, variableFactory);
        }
    }
    if (nextNode != null) {
        return nextNode.getValue(function.call(ctx, elCtx, variableFactory, parms), elCtx, variableFactory);
    } else {
        return function.call(ctx, elCtx, variableFactory, parms);
    }
}
Also used : Function(org.mvel2.ast.Function)

Example 3 with Function

use of org.mule.mvel2.ast.Function in project mvel by mvel.

the class CompilerTools method extractAllDeclaredFunctions.

/**
 * Returns an ordered Map of all functions declared within an compiled script.
 *
 * @param compile compile
 * @return - ordered Map
 */
public static Map<String, Function> extractAllDeclaredFunctions(CompiledExpression compile) {
    Map<String, Function> allFunctions = new LinkedHashMap<String, Function>();
    ASTIterator instructions = new ASTLinkedList(compile.getFirstNode());
    ASTNode n;
    while (instructions.hasMoreNodes()) {
        if ((n = instructions.nextNode()) instanceof Function) {
            allFunctions.put(n.getName(), (Function) n);
        }
    }
    return allFunctions;
}
Also used : Function(org.mvel2.ast.Function) ASTNode(org.mvel2.ast.ASTNode) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Function

use of org.mule.mvel2.ast.Function in project mvel by mvel.

the class FunctionParser method parse.

public Function parse() {
    int start = cursor;
    int startCond = 0;
    int endCond = 0;
    int blockStart;
    int blockEnd;
    int end = cursor + length;
    cursor = ParseTools.captureToNextTokenJunction(expr, cursor, end, pCtx);
    if (expr[cursor = ParseTools.nextNonBlank(expr, cursor)] == '(') {
        /**
         * If we discover an opening bracket after the function name, we check to see
         * if this function accepts parameters.
         */
        endCond = cursor = balancedCaptureWithLineAccounting(expr, startCond = cursor, end, '(', pCtx);
        startCond++;
        cursor++;
        cursor = ParseTools.skipWhitespace(expr, cursor);
        if (cursor >= end) {
            throw new CompileException("incomplete statement", expr, cursor);
        } else if (expr[cursor] == '{') {
            blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
        } else {
            blockStart = cursor - 1;
            cursor = ParseTools.captureToEOS(expr, cursor, end, pCtx);
            blockEnd = cursor;
        }
    } else {
        /**
         * This function has not parameters.
         */
        if (expr[cursor] == '{') {
            /**
             * This function is bracketed.  We capture the entire range in the brackets.
             */
            blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
        } else {
            /**
             * This is a single statement function declaration.  We only capture the statement.
             */
            blockStart = cursor - 1;
            cursor = ParseTools.captureToEOS(expr, cursor, end, pCtx);
            blockEnd = cursor;
        }
    }
    /**
     * Trim any whitespace from the captured block range.
     */
    blockStart = ParseTools.trimRight(expr, blockStart + 1);
    blockEnd = ParseTools.trimLeft(expr, start, blockEnd);
    cursor++;
    /**
     * Check if the function is manually terminated.
     */
    if (splitAccumulator != null && ParseTools.isStatementNotManuallyTerminated(expr, cursor)) {
        /**
         * Add an EndOfStatement to the split accumulator in the parser.
         */
        splitAccumulator.add(new EndOfStatement(pCtx));
    }
    /**
     * Produce the funciton node.
     */
    return new Function(name, expr, startCond, endCond - startCond, blockStart, blockEnd - blockStart, fields, pCtx);
}
Also used : Function(org.mvel2.ast.Function) EndOfStatement(org.mvel2.ast.EndOfStatement) CompileException(org.mvel2.CompileException)

Example 5 with Function

use of org.mule.mvel2.ast.Function in project mvel by mvel.

the class FunctionsTest method testFunctionDefAndCall2.

public void testFunctionDefAndCall2() {
    ExpressionCompiler compiler = new ExpressionCompiler("function heyFoo() { return 'Foobar'; };\n" + "return heyFoo() + heyFoo();");
    Serializable s = compiler.compile();
    Map<String, Function> m = extractAllDeclaredFunctions((CompiledExpression) s);
    assertTrue(m.containsKey("heyFoo"));
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    assertEquals("FoobarFoobar", executeExpression(s, new HashMap()));
    OptimizerFactory.setDefaultOptimizer("dynamic");
}
Also used : Function(org.mvel2.ast.Function) Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Aggregations

Function (org.mvel2.ast.Function)12 CompileException (org.mvel2.CompileException)4 StringAppender (org.mvel2.util.StringAppender)3 Serializable (java.io.Serializable)2 EndOfStatement (org.mvel2.ast.EndOfStatement)2 Proto (org.mvel2.ast.Proto)2 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)2 NullType (org.mvel2.util.NullType)2 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ErrorDetail (org.mvel2.ErrorDetail)1 ASTNode (org.mvel2.ast.ASTNode)1 FunctionInstance (org.mvel2.ast.FunctionInstance)1 IfNode (org.mvel2.ast.IfNode)1 Stacklang (org.mvel2.ast.Stacklang)1 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)1