use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class MVELRuntime method execute.
/**
* Main interpreter.
*
* @param debugger Run in debug mode
* @param expression The compiled expression object
* @param ctx The root context object
* @param variableFactory The variable factory to be injected
* @return The resultant value
* @see org.mvel2.MVEL
*/
public static Object execute(boolean debugger, final CompiledExpression expression, final Object ctx, VariableResolverFactory variableFactory) {
Object v1, v2;
ExecutionStack stk = new ExecutionStack();
variableFactory.setTiltFlag(false);
ASTNode tk = expression.getFirstNode();
Integer operator;
if (tk == null)
return null;
try {
do {
if (tk.fields == -1) {
/**
* This may seem silly and redundant, however, when an MVEL script recurses into a block
* or substatement, a new runtime loop is entered. Since the debugger state is not
* passed through the AST, it is not possible to forward the state directly. So when we
* encounter a debugging symbol, we check the thread local to see if there is are registered
* breakpoints. If we find them, we assume that we are debugging.
*
* The consequence of this of course, is that it's not ideal to compileShared expressions with
* debugging symbols which you plan to use in a production enviroment.
*/
if (debugger || (debugger = hasDebuggerContext())) {
try {
debuggerContext.get().checkBreak((LineLabel) tk, variableFactory, expression);
} catch (NullPointerException e) {
// do nothing for now. this isn't as calus as it seems.
}
}
continue;
} else if (stk.isEmpty()) {
stk.push(tk.getReducedValueAccelerated(ctx, ctx, variableFactory));
}
if (variableFactory.tiltFlag()) {
return stk.pop();
}
switch(operator = tk.getOperator()) {
case RETURN:
variableFactory.setTiltFlag(true);
return stk.pop();
case NOOP:
continue;
case TERNARY:
if (!stk.popBoolean()) {
//noinspection StatementWithEmptyBody
while (tk.nextASTNode != null && !(tk = tk.nextASTNode).isOperator(TERNARY_ELSE)) ;
}
stk.clear();
continue;
case TERNARY_ELSE:
return stk.pop();
case END_OF_STMT:
/**
* If the program doesn't end here then we wipe anything off the stack that remains.
* Althought it may seem like intuitive stack optimizations could be leveraged by
* leaving hanging values on the stack, trust me it's not a good idea.
*/
if (tk.nextASTNode != null) {
stk.clear();
}
continue;
}
stk.push(tk.nextASTNode.getReducedValueAccelerated(ctx, ctx, variableFactory), operator);
try {
while (stk.isReduceable()) {
if ((Integer) stk.peek() == CHOR) {
stk.pop();
v1 = stk.pop();
v2 = stk.pop();
if (!isEmpty(v2) || !isEmpty(v1)) {
stk.clear();
stk.push(!isEmpty(v2) ? v2 : v1);
} else
stk.push(null);
} else {
stk.op();
}
}
} catch (ClassCastException e) {
throw new CompileException("syntax error or incomptable types", new char[0], 0, e);
} catch (CompileException e) {
throw e;
} catch (Exception e) {
throw new CompileException("failed to compileShared sub expression", new char[0], 0, e);
}
} while ((tk = tk.nextASTNode) != null);
return stk.peek();
} catch (NullPointerException e) {
if (tk != null && tk.isOperator() && tk.nextASTNode != null) {
throw new CompileException("incomplete statement: " + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", tk.getExpr(), tk.getStart());
} else {
throw e;
}
}
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class GenericsTypeInferenceTest method testTypeByMethod.
public final void testTypeByMethod() {
ParserContext context = new ParserContext();
context.setStrongTyping(true);
context.addInput("a", A.class);
CompiledExpression compiledExpression = new ExpressionCompiler("!a.show").compile(context);
assertEquals(Boolean.class, compiledExpression.getKnownEgressType());
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class AbstractTest method _test.
protected static Object _test(String ex) {
ExpressionCompiler compiler = new ExpressionCompiler(ex);
StringAppender failErrors = new StringAppender();
CompiledExpression compiled = compiler.compile();
Object first = null, second = null, third = null, fourth = null, fifth = null, sixth = null, seventh = null, eighth = null;
System.out.println(DebugTools.decompile((Serializable) compiled));
if (!Boolean.getBoolean("mvel2.disable.jit")) {
setDefaultOptimizer("ASM");
try {
first = executeExpression(compiled, new Base(), createTestMap());
} catch (Exception e) {
failErrors.append("\nFIRST TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
try {
second = executeExpression(compiled, new Base(), createTestMap());
} catch (Exception e) {
failErrors.append("\nSECOND TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
}
try {
third = MVEL.eval(ex, new Base(), createTestMap());
} catch (Exception e) {
failErrors.append("\nTHIRD TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (first != null && !first.getClass().isArray()) {
if (!first.equals(second)) {
System.out.println(failErrors.toString());
throw new AssertionError("Different result from test 1 and 2 (Compiled Re-Run / JIT) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
}
if (!first.equals(third)) {
if (failErrors != null)
System.out.println(failErrors.toString());
throw new AssertionError("Different result from test 1 and 3 (Compiled to Interpreted) [first: " + valueOf(first) + " (" + (first != null ? first.getClass().getName() : null) + "); third: " + valueOf(third) + " (" + (third != null ? third.getClass().getName() : "null") + ")]");
}
}
setDefaultOptimizer("reflective");
Serializable compiled2 = compileExpression(ex);
try {
fourth = executeExpression(compiled2, new Base(), createTestMap());
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nFOURTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
try {
fifth = executeExpression(compiled2, new Base(), createTestMap());
} catch (Exception e) {
e.printStackTrace();
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nFIFTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (fourth != null && !fourth.getClass().isArray()) {
if (!fourth.equals(fifth)) {
throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run X2) [fourth: " + valueOf(fourth) + "; fifth: " + valueOf(fifth) + "]");
}
}
ParserContext ctx = new ParserContext();
ctx.setSourceFile("unittest");
ctx.setDebugSymbols(true);
ExpressionCompiler debuggingCompiler = new ExpressionCompiler(ex);
// debuggingCompiler.setDebugSymbols(true);
CompiledExpression compiledD = debuggingCompiler.compile(ctx);
try {
sixth = executeExpression(compiledD, new Base(), createTestMap());
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nSIXTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (sixth != null && !sixth.getClass().isArray()) {
if (!fifth.equals(sixth)) {
System.out.println("Payload 1 -- No Symbols: ");
System.out.println(decompile(compiled));
System.out.println();
System.out.println("Payload 2 -- With Symbols: ");
System.out.println(decompile(compiledD));
System.out.println();
throw new AssertionError("Different result from test 5 and 6 (Compiled to Compiled+DebuggingSymbols) [first: " + valueOf(fifth) + "; second: " + valueOf(sixth) + "]");
}
}
try {
seventh = executeExpression(compiledD, new Base(), createTestMap());
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nSEVENTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (seventh != null && !seventh.getClass().isArray()) {
if (!seventh.equals(sixth)) {
throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
}
}
try {
Serializable xx = serializationTest(compiledD);
AbstractParser.resetParserContext();
eighth = executeExpression(xx, new Base(), new MapVariableResolverFactory(createTestMap()));
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nEIGHTH TEST (Serializability): { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (eighth != null && !eighth.getClass().isArray()) {
if (!eighth.equals(seventh)) {
throw new AssertionError("Different result from test 7 and 8 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
}
}
if (failErrors.length() > 0) {
System.out.println(decompile(compiledD));
throw new AssertionError("Detailed Failure Report:\n" + failErrors.toString());
}
return fourth;
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class CoreConfidenceTests method testStaticNestedWithMethodCall.
public void testStaticNestedWithMethodCall() {
String expr = "item = new Item( \"Some Item\"); $msg.addItem( item ); return $msg";
ExpressionCompiler compiler = new ExpressionCompiler(expr);
ParserContext context = new ParserContext();
context.setStrictTypeEnforcement(false);
context.addImport("Message", Message.class);
context.addImport("Item", Item.class);
// Serializable compiledExpression = compiler.compileShared(context);
Map vars = new HashMap();
vars.put("$msg", new Message());
Message msg = (Message) executeExpression(compiler.compile(context), vars);
Item item = (Item) msg.getItems().get(0);
assertEquals("Some Item", item.getName());
}
use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class CoreConfidenceTests method testArray2.
public void testArray2() {
String ex = " TestHelper.method(1, {\"a\", \"b\"});\n" + " TestHelper.method(2, {new String(\"a\"), new String(\"b\")});\n" + " TestHelper.method(3, {new Fooz(\"a\"), new Fooz(\"b\")});";
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addImport(TestHelper.class);
ctx.addImport(Fooz.class);
ExpressionCompiler compiler = new ExpressionCompiler(ex);
OptimizerFactory.setDefaultOptimizer("ASM");
CompiledExpression expr = compiler.compile(ctx);
executeExpression(expr);
OptimizerFactory.setDefaultOptimizer("reflective");
expr = compiler.compile(ctx);
executeExpression(expr);
}
Aggregations