Search in sources :

Example 16 with AviatorObject

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

the class LambdaFunction method newEnv.

protected Map<String, Object> newEnv(final Map<String, Object> parentEnv, final AviatorObject... args) {
    Env env = null;
    if (!this.inheritEnv) {
        final Env contextEnv = new Env(parentEnv, this.context);
        contextEnv.configure(this.context.getInstance(), this.expression);
        env = new Env(contextEnv);
        env.configure(this.context.getInstance(), this.expression);
    } else {
        assert (parentEnv == this.context);
        env = (Env) parentEnv;
    }
    if (args.length != this.params.size()) {
        throw new IllegalArgumentException("Wrong number of args(" + args.length + ") passed to " + getName() + "(" + this.params.size() + ")");
    }
    for (int i = 0; i < this.params.size(); i++) {
        FunctionParam param = this.params.get(i);
        final AviatorObject arg = args[i];
        Object value = arg.getValue(parentEnv);
        if (value == null && arg.getAviatorType() == AviatorType.JavaType && !parentEnv.containsKey(((AviatorJavaType) arg).getName())) {
            value = RuntimeUtils.getInstance(parentEnv).getFunction(((AviatorJavaType) arg).getName());
        }
        env.override(param.getName(), value);
    }
    return env;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Env(com.googlecode.aviator.utils.Env) FunctionParam(com.googlecode.aviator.runtime.FunctionParam)

Example 17 with AviatorObject

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

the class TraceFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, final AviatorObject arg11) {
    traceArgs(env, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
    AviatorObject ret = this.rawFunc.call(env, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
    traceResult(env, ret);
    return ret;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 18 with AviatorObject

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

the class TraceFunction method traceArgs.

private void traceArgs(final Map<String, Object> env, final Object... args) {
    StringBuilder sb = new StringBuilder();
    boolean wasFirst = true;
    for (Object arg : args) {
        if (wasFirst) {
            wasFirst = false;
        } else {
            sb.append(",");
        }
        if (arg instanceof String) {
            sb.append(arg.toString());
        } else {
            sb.append(((AviatorObject) arg).desc(env));
        }
    }
    RuntimeUtils.printlnTrace(env, "Func   : " + getName() + "(" + sb.toString() + ")");
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 19 with AviatorObject

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

the class SeqGetFunction method call.

@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object coll = arg1.getValue(env);
    Object key = arg2.getValue(env);
    if (coll == null) {
        throw new NullPointerException("the collection of seq.get is null, which the value of key/index is `" + key + "`");
    }
    Class<?> clazz = coll.getClass();
    if (List.class.isAssignableFrom(clazz)) {
        if (!(key instanceof Number)) {
            throw new IllegalArgumentException("Invalid index `" + key + "` for list,it's not a number.");
        }
        Object value = ((List) coll).get(((Number) key).intValue());
        return AviatorRuntimeJavaType.valueOf(value);
    } else if (Set.class.isAssignableFrom(clazz)) {
        if (((Set) coll).contains(key)) {
            return AviatorRuntimeJavaType.valueOf(key);
        } else {
            return AviatorNil.NIL;
        }
    } else if (Map.class.isAssignableFrom(clazz)) {
        Object value = ((Map) coll).get(key);
        return AviatorRuntimeJavaType.valueOf(value);
    } else if (clazz.isArray()) {
        if (!(key instanceof Number)) {
            throw new IllegalArgumentException("Invalid index `" + key + "` for list,it's not a number.");
        }
        return AviatorRuntimeJavaType.valueOf(ArrayUtils.get(coll, ((Number) key).intValue()));
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not a collection.");
    }
}
Also used : Set(java.util.Set) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) List(java.util.List) Map(java.util.Map)

Example 20 with AviatorObject

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

the class SeqIncludeFunction 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();
    boolean contains = false;
    if (Set.class.isAssignableFrom(clazz)) {
        contains = ((Set) first).contains(arg2.getValue(env));
    } else {
        try {
            for (Object obj : RuntimeUtils.seq(first, env)) {
                if (AviatorRuntimeJavaType.valueOf(obj).compareEq(arg2, env) == 0) {
                    contains = true;
                    break;
                }
            }
        } catch (Exception e) {
            RuntimeUtils.printStackTrace(env, e);
            return AviatorBoolean.FALSE;
        }
    }
    return AviatorBoolean.valueOf(contains);
}
Also used : 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