Search in sources :

Example 1 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class LoadIR method evalWithoutDispatch.

public void evalWithoutDispatch(final InterpretContext context) {
    if (this.token == null) {
        return;
    }
    if (this.inConstantPool) {
        final AviatorObject constant = context.loadConstant(this.token);
        assert (constant != null);
        context.push(constant);
        return;
    }
    // load token to stack
    switch(this.token.getType()) {
        case Number:
            // load numbers
            NumberToken numberToken = (NumberToken) this.token;
            Number number = numberToken.getNumber();
            if (TypeUtils.isBigInt(number)) {
                context.push(AviatorBigInt.valueOf(this.token.getLexeme()));
            } else if (TypeUtils.isDecimal(number)) {
                context.push(AviatorDecimal.valueOf(context.getEnv(), this.token.getLexeme()));
            } else if (TypeUtils.isDouble(number)) {
                context.push(AviatorDouble.valueOf(number.doubleValue()));
            } else {
                context.push(AviatorLong.valueOf(number.longValue()));
            }
            break;
        case String:
            context.push(new AviatorString((String) this.token.getValue(null), true, this.token.getMeta(Constants.INTER_META, true), this.token.getLineNo()));
            break;
        case Pattern:
            context.push(new AviatorPattern((String) this.token.getValue(null)));
            break;
        case Variable:
            if (this.token == Variable.TRUE) {
                context.push(AviatorBoolean.TRUE);
            } else if (this.token == Variable.FALSE) {
                context.push(AviatorBoolean.FALSE);
            } else if (this.token == Variable.NIL) {
                context.push(AviatorNil.NIL);
            } else {
                AviatorJavaType var;
                if (this.meta != null) {
                    var = context.loadVar(this.meta);
                    assert (var != null);
                } else {
                    var = new AviatorJavaType(this.token.getLexeme());
                }
                context.push(var);
            }
            break;
        default:
            throw new ExpressionRuntimeException("Can't load " + this.token);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorPattern(com.googlecode.aviator.runtime.type.AviatorPattern) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 2 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorEvaluatorInstanceUnitTest method testAssignableClazzWhiteList.

@Test
public void testAssignableClazzWhiteList() {
    final HashSet<Object> classes = new HashSet<>();
    classes.add(List.class);
    this.instance.setOption(Options.ASSIGNABLE_ALLOWED_CLASS_SET, classes);
    try {
        this.instance.execute("new java.util.Date()");
        fail();
    } catch (ExpressionRuntimeException e) {
        assertEquals("`class java.util.Date` is not in allowed class set, check Options.ALLOWED_CLASS_SET", e.getMessage());
    }
    List res = (List) this.instance.execute("l = new java.util.ArrayList();seq.add(l,1);l");
    assertEquals(1, res.size());
    assertEquals(Integer.valueOf(1), res.get(0));
    List res2 = (List) this.instance.execute("l = new java.util.LinkedList();seq.add(l,1);l");
    assertEquals(1, res2.size());
    assertEquals(Integer.valueOf(1), res2.get(0));
    // recover default value of option ALLOWED_CLASS_SET after test
    this.instance.getOptionValue(Options.ASSIGNABLE_ALLOWED_CLASS_SET).classes = null;
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorString method tryCompareDate.

private int tryCompareDate(final Map<String, Object> env, final Date otherDate) {
    try {
        final SimpleDateFormat simpleDateFormat = DATE_FORMATTER.get();
        final Date thisDate = simpleDateFormat.parse(getLexeme(env));
        return thisDate.compareTo(otherDate);
    } catch (final Throwable t) {
        throw new ExpressionRuntimeException("Compare date error", t);
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorPattern method match.

@Override
public AviatorObject match(final AviatorObject other, final Map<String, Object> env) {
    switch(other.getAviatorType()) {
        case Nil:
            return AviatorBoolean.FALSE;
        case String:
            AviatorString aviatorString = (AviatorString) other;
            Matcher m = this.pattern.matcher(aviatorString.getLexeme(env));
            if (m.matches()) {
                boolean captureGroups = RuntimeUtils.getInstance(env).getOptionValue(Options.PUT_CAPTURING_GROUPS_INTO_ENV).bool;
                if (captureGroups && env != Collections.EMPTY_MAP) {
                    int groupCount = m.groupCount();
                    for (int i = 0; i <= groupCount; i++) {
                        ((Env) env).override("$" + i, m.group(i));
                    }
                }
                return AviatorBoolean.TRUE;
            } else {
                return AviatorBoolean.FALSE;
            }
        case JavaType:
            AviatorJavaType javaType = (AviatorJavaType) other;
            final Object javaValue = javaType.getValue(env);
            if (javaValue == null) {
                return AviatorBoolean.FALSE;
            }
            if (TypeUtils.isString(javaValue)) {
                return match(new AviatorString(String.valueOf(javaValue)), env);
            } else {
                throw new ExpressionRuntimeException(desc(env) + " could not match " + other.desc(env));
            }
        default:
            throw new ExpressionRuntimeException(desc(env) + " could not match " + other.desc(env));
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) Matcher(java.util.regex.Matcher) Env(com.googlecode.aviator.utils.Env)

Example 5 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class ReducerFunction method call.

@Override
public final AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
    Object coll = arg1.getValue(env);
    AviatorFunction iteratorFn = (AviatorFunction) arg2;
    int maxLoopCount = RuntimeUtils.getInstance(env).getOptionValue(Options.MAX_LOOP_COUNT).number;
    AviatorObject result = AviatorNil.NIL;
    long c = 0;
    if (coll != Range.LOOP) {
        long arities = (long) arg2.meta(Constants.ARITIES_META);
        long index = 0;
        boolean unboxEntry = arities == 2 && coll != null && Map.class.isAssignableFrom(coll.getClass());
        // for..in loop
        for (Object obj : RuntimeUtils.seq(coll, env)) {
            if (arities == 1) {
                result = iteratorFn.call(env, AviatorRuntimeJavaType.valueOf(obj));
            } else {
                if (unboxEntry) {
                    Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
                    result = iteratorFn.call(env, AviatorRuntimeJavaType.valueOf(entry.getKey()), AviatorRuntimeJavaType.valueOf(entry.getValue()));
                } else {
                    result = iteratorFn.call(env, AviatorLong.valueOf(index++), AviatorRuntimeJavaType.valueOf(obj));
                }
            }
            if (!(result instanceof ReducerResult)) {
                continue;
            }
            boolean breakOut = false;
            ReducerResult midResult = (ReducerResult) result;
            result = midResult.obj;
            if (midResult.state == ReducerState.Empty) {
                continue;
            }
            switch(midResult.state) {
                case Break:
                    breakOut = true;
                    break;
                case Return:
                    return midResult;
                default:
                    break;
            }
            if (breakOut) {
                break;
            }
        }
    } else {
        // while statement
        while (true) {
            if (maxLoopCount > 0 && ++c > maxLoopCount) {
                throw new ExpressionRuntimeException("Overflow max loop count: " + maxLoopCount);
            }
            result = iteratorFn.call(env);
            if (!(result instanceof ReducerResult)) {
                continue;
            }
            boolean breakOut = false;
            ReducerResult midResult = (ReducerResult) result;
            result = midResult.obj;
            if (midResult.state == ReducerState.Empty) {
                continue;
            }
            switch(midResult.state) {
                case Break:
                    breakOut = true;
                    break;
                case Return:
                    return midResult;
                default:
                    break;
            }
            if (breakOut) {
                break;
            }
        }
    }
    Object contObj = arg3.getValue(env);
    if (contObj == Constants.REDUCER_EMPTY) {
        return result;
    }
    AviatorObject contResult = ((AviatorFunction) contObj).call(env);
    if (contResult == Constants.REDUCER_EMPTY) {
        // empty continuation, return current result.
        return result;
    } else {
        return contResult;
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Aggregations

ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)19 Date (java.util.Date)7 Test (org.junit.Test)7 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 HashMap (java.util.HashMap)6 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)4 SimpleDateFormat (java.text.SimpleDateFormat)4 ID (cn.devezhao.persist4j.engine.ID)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 NoSuchPropertyException (com.googlecode.aviator.exception.NoSuchPropertyException)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)1 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)1 Env (com.googlecode.aviator.utils.Env)1 RebuildException (com.rebuild.core.RebuildException)1 RepeatedRecordsException (com.rebuild.core.service.general.RepeatedRecordsException)1