Search in sources :

Example 6 with AviatorJavaType

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

the class FunctionUtils method getFunction.

/**
 * Get a function from env in follow orders:
 * <ul>
 * <li>arg value</li>
 * <li>env</li>
 * <li>current evaluator instance.</li>
 * </ul>
 *
 * @param arg
 * @param env
 * @param arity
 * @return
 */
public static AviatorFunction getFunction(final AviatorObject arg, final Map<String, Object> env, final int arity) {
    if (arg.getAviatorType() != AviatorType.JavaType && arg.getAviatorType() != AviatorType.Lambda) {
        throw new ClassCastException(arg.desc(env) + " is not a function");
    }
    // Runtime type.
    Object val = null;
    if (arg instanceof AviatorFunction) {
        return (AviatorFunction) arg;
    }
    if (arg instanceof AviatorRuntimeJavaType && (val = arg.getValue(env)) instanceof AviatorFunction) {
        return (AviatorFunction) val;
    }
    // resolve by name.
    // special processing for "-" operator
    String name = ((AviatorJavaType) arg).getName();
    if (name.equals("-")) {
        if (arity == 2) {
            name = "-sub";
        } else {
            name = "-neg";
        }
    }
    AviatorFunction rt = null;
    if (env != null) {
        rt = (AviatorFunction) env.get(name);
    }
    if (rt == null) {
        AviatorEvaluatorInstance instance = RuntimeUtils.getInstance(env);
        rt = instance.getFunction(name);
    }
    return rt;
}
Also used : AviatorEvaluatorInstance(com.googlecode.aviator.AviatorEvaluatorInstance) AviatorRuntimeJavaType(com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 7 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType 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 8 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType 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 9 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType 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)

Example 10 with AviatorJavaType

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

the class PrintlnFunctionUnitTest method testCall_WithTwoArgument.

@Test
public void testCall_WithTwoArgument() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("out", out);
    this.fun.call(env, new AviatorJavaType("out"), new AviatorString("hello"));
    out.flush();
    out.close();
    String lineSeparator = System.getProperty("line.separator");
    byte[] data = out.toByteArray();
    assertEquals("hello" + lineSeparator, new String(data));
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Aggregations

AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)45 Test (org.junit.Test)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)25 HashMap (java.util.HashMap)16 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)11 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)10 Env (com.googlecode.aviator.utils.Env)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 LinkedList (java.util.LinkedList)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)2 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)1