Skip to content

Commit

Permalink
Merge pull request #182 from killme2008/feature/v4.2.8-dev
Browse files Browse the repository at this point in the history
(fix) issue #181
  • Loading branch information
killme2008 authored Dec 19, 2019
2 parents 0f5a865 + df646a5 commit 8d21d13
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
*/
public class ASMCodeGenerator implements CodeGenerator {

private static final String RUNTIME_UTILS = "com/googlecode/aviator/runtime/RuntimeUtils";
private static final String OBJECT_DESC = "Lcom/googlecode/aviator/runtime/type/AviatorObject;";
private static final String JAVA_TYPE_OWNER =
"com/googlecode/aviator/runtime/type/AviatorJavaType";
Expand Down Expand Up @@ -398,7 +399,7 @@ public void onMult(final Token<?> lookhead) {
public void onAssignment(final Token<?> lookhead) {
loadEnv();

this.mv.visitMethodInsn(INVOKEVIRTUAL, JAVA_TYPE_OWNER, "setValue",
this.mv.visitMethodInsn(INVOKEVIRTUAL, OBJECT_OWNER, "setValue",
"(Lcom/googlecode/aviator/runtime/type/AviatorObject;Ljava/util/Map;)Lcom/googlecode/aviator/runtime/type/AviatorObject;");
this.popOperand(3);
this.pushOperand();
Expand Down Expand Up @@ -773,7 +774,9 @@ public Expression getResult() {
exp.setLambdaBootstraps(this.lambdaBootstraps);
exp.setFuncsArgs(this.funcsArgs);
return exp;
} catch (Exception e) {
} catch (ExpressionRuntimeException e) {
throw e;
} catch (Throwable e) {
if (e.getCause() instanceof ExpressionRuntimeException) {
throw (ExpressionRuntimeException) e.getCause();
}
Expand Down Expand Up @@ -1072,6 +1075,8 @@ public void onMethodInvoke(final Token<?> lookhead, final List<FunctionArgument>
}
this.mv.visitMethodInsn(INVOKEINTERFACE, "com/googlecode/aviator/runtime/type/AviatorFunction",
"call", getInvokeMethodDesc(parameterCount));
this.mv.visitMethodInsn(INVOKESTATIC, RUNTIME_UTILS, "assertNotNull",
"(Lcom/googlecode/aviator/runtime/type/AviatorObject;)Lcom/googlecode/aviator/runtime/type/AviatorObject;");

this.popOperand(); // method object
this.popOperand(); // env map
Expand Down Expand Up @@ -1239,8 +1244,7 @@ public void onMethodName(final Token<?> lookhead) {
}
} else {
loadEnv();
this.mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/aviator/runtime/RuntimeUtils",
"getFunction",
this.mv.visitMethodInsn(INVOKESTATIC, RUNTIME_UTILS, "getFunction",
"(Ljava/lang/Object;Ljava/util/Map;)Lcom/googlecode/aviator/runtime/type/AviatorFunction;");
this.popOperand();
}
Expand Down Expand Up @@ -1294,8 +1298,7 @@ private void createAviatorFunctionObject(final String methodName) {
loadEnv();
this.pushOperand();
this.mv.visitLdcInsn(methodName);
this.mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/aviator/runtime/RuntimeUtils",
"getFunction",
this.mv.visitMethodInsn(INVOKESTATIC, RUNTIME_UTILS, "getFunction",
"(Ljava/util/Map;Ljava/lang/String;)Lcom/googlecode/aviator/runtime/type/AviatorFunction;");
this.popOperand(2);
this.pushOperand();
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/com/googlecode/aviator/runtime/RuntimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Options;
import com.googlecode.aviator.runtime.type.AviatorFunction;
import com.googlecode.aviator.runtime.type.AviatorNil;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.utils.Env;

Expand All @@ -26,30 +27,43 @@ private RuntimeUtils() {
*
* @return
*/
public static final AviatorEvaluatorInstance getInstance(Map<String, Object> env) {
public static final AviatorEvaluatorInstance getInstance(final Map<String, Object> env) {
return ((Env) env).getInstance();

}

public static final MathContext getMathContext(Map<String, Object> env) {
/**
* Ensure the object is not null, cast null into AviatorNil.
*
* @param object
* @return
*/
public static final AviatorObject assertNotNull(final AviatorObject object) {
if (object != null) {
return object;
}
return AviatorNil.NIL;
}

public static final MathContext getMathContext(final Map<String, Object> env) {
return getInstance(env).getOptionValue(Options.MATH_CONTEXT).mathContext;
}


public static final void printTrace(Map<String, Object> env, String msg) {
public static final void printTrace(final Map<String, Object> env, final String msg) {
try {
getInstance(env).getTraceOutputStream().write(("[Aviator TRACE] " + msg + "\n").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

public static final boolean isTracedEval(Map<String, Object> env) {
public static final boolean isTracedEval(final Map<String, Object> env) {
return getInstance(env).getOptionValue(Options.TRACE_EVAL).bool;
}


public static AviatorFunction getFunction(Object object, Map<String, Object> env) {
public static AviatorFunction getFunction(final Object object, final Map<String, Object> env) {
if (object instanceof AviatorFunction) {
return (AviatorFunction) object;
} else if (object instanceof AviatorObject) {
Expand All @@ -61,7 +75,7 @@ public static AviatorFunction getFunction(Object object, Map<String, Object> env
throw new ClassCastException("Could not cast object " + object + " into a aviator function.");
}

public static AviatorFunction getFunction(Map<String, Object> env, String name) {
public static AviatorFunction getFunction(final Map<String, Object> env, final String name) {
return getInstance(env).getFunction(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public Object getValue(final Map<String, Object> env) {
return null;
}

@Override
public AviatorObject setValue(final AviatorObject value, final Map<String, Object> env) {
if (this.name.contains(".")) {
throw new ExpressionRuntimeException("Can't assignment value to `" + this.name + "`");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ public AviatorObject neg(final Map<String, Object> env) {
throw new ExpressionRuntimeException(desc(env) + " doesn't support negative operation '-'");
}

public AviatorObject setValue(final AviatorObject value, final Map<String, Object> env) {
throw new ExpressionRuntimeException(
"Can't assign value " + value.desc(env) + " to " + desc(env));
}

public AviatorObject not(final Map<String, Object> env) {
throw new ExpressionRuntimeException(desc(env) + " doesn't support not operation '!'");
}


public String desc(final Map<String, Object> env) {
return "<" + getAviatorType() + ", " + getValue(env) + ">";
}


public abstract Object getValue(Map<String, Object> env);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.math.BigDecimal;
Expand All @@ -29,11 +30,14 @@
import java.util.Map;
import org.junit.Test;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Options;
import com.googlecode.aviator.exception.CompileExpressionErrorException;
import com.googlecode.aviator.exception.ExpressionRuntimeException;
import com.googlecode.aviator.exception.ExpressionSyntaxErrorException;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorLong;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import junit.framework.Assert;
Expand All @@ -52,6 +56,37 @@ public void testIssue77() {
AviatorEvaluator.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, false);
}

@Test(expected = ExpressionRuntimeException.class)
public void testIssue181() {
Map<String, Object> env = new HashMap<>();
env.put("a1", 3);
env.put("a2", 4);
AviatorEvaluator.execute("(a1%2=0)&&(a2%2==0)", env);
}

@Test
public void testReturnNullCustomFunction() {
AviatorEvaluatorInstance evaluator = AviatorEvaluator.newInstance();
evaluator.addFunction(new AbstractFunction() {


@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
try {
return AviatorLong.valueOf(FunctionUtils.getJavaObject(arg1, env));
} catch (Exception e) {
return null;
}
}

@Override
public String getName() {
return "SafeCastLong";
}
});

assertNull(evaluator.execute("SafeCastLong('a')"));
}

@Test
public void testIssue162() {
Expand Down

0 comments on commit 8d21d13

Please sign in to comment.