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");
}
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);
}
}
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;
}
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);
}
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");
}
Aggregations