Search in sources :

Example 21 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqIntoFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object fromSeq = arg2.getValue(env);
    AviatorObject result = arg1;
    for (Object e : RuntimeUtils.seq(fromSeq, env)) {
        result = SEQ_ADD.call(env, result, AviatorRuntimeJavaType.valueOf(e));
    }
    return result;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 22 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqNewArrayFunction method getElementClass.

private Class<?> getElementClass(final Map<String, Object> env, final AviatorObject arg1) {
    AviatorObject clazzVar = arg1;
    if (clazzVar == null || clazzVar.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Invalid class:" + (clazzVar == null ? "null" : clazzVar.desc(env)));
    }
    try {
        String name = ((AviatorJavaType) clazzVar).getName();
        Class<?> clazz = null;
        if (TypeUtils.PRIMITIVE_TYPES.containsKey(name)) {
            clazz = TypeUtils.PRIMITIVE_TYPES.get(name);
        } else {
            assert (env instanceof Env);
            clazz = ((Env) env).resolveClassSymbol(name);
        }
        return clazz;
    } catch (Throwable t) {
        throw Reflector.sneakyThrow(t);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Env(com.googlecode.aviator.utils.Env)

Example 23 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqAddFunction method call.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
    Object coll = arg1.getValue(env);
    Object key = arg2.getValue(env);
    Object value = arg3.getValue(env);
    if (coll == null) {
        throw new NullPointerException("null seq");
    }
    Class<?> clazz = coll.getClass();
    if (Map.class.isAssignableFrom(clazz)) {
        ((Map) coll).put(key, value);
        return arg1;
    } else if (clazz.isArray()) {
        int index = ((Number) key).intValue();
        ArrayUtils.set(coll, index, value);
        return arg1;
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not a map or array.");
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Example 24 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqContainsKeyFunction method call.

@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    if (first == null) {
        return AviatorBoolean.FALSE;
    }
    Class<?> clazz = first.getClass();
    if (Map.class.isAssignableFrom(clazz)) {
        Map seq = (Map) first;
        try {
            return AviatorBoolean.valueOf(seq.containsKey(arg2.getValue(env)));
        } catch (Exception e) {
            RuntimeUtils.printStackTrace(env, e);
            return AviatorBoolean.FALSE;
        }
    } else if (clazz.isArray()) {
        int index = FunctionUtils.getNumberValue(arg2, env).intValue();
        return AviatorBoolean.valueOf(index >= 0 && index < ArrayUtils.getLength(first));
    } else if (List.class.isAssignableFrom(clazz)) {
        int index = FunctionUtils.getNumberValue(arg2, env).intValue();
        return AviatorBoolean.valueOf(index >= 0 && index < ((List) first).size());
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not a map or array.");
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Example 25 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqEveryFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return AviatorBoolean.TRUE;
    }
    for (Object obj : RuntimeUtils.seq(first, env)) {
        if (!fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            return AviatorBoolean.FALSE;
        }
    }
    return AviatorBoolean.TRUE;
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3