Search in sources :

Example 11 with Env

use of com.googlecode.aviator.utils.Env 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 12 with Env

use of com.googlecode.aviator.utils.Env 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 13 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class AviatorPatternUnitTest method createEnvWith.

private Map<String, Object> createEnvWith(String name, Object obj) {
    Env env = TestUtils.getTestEnv();
    if (name != null) {
        env.put(name, obj);
    }
    env.put("true", Boolean.TRUE);
    env.put("false", Boolean.FALSE);
    return env;
}
Also used : Env(com.googlecode.aviator.utils.Env)

Example 14 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class MathAbsFunctionUnitTest method testCall.

@Test
public void testCall() {
    Env env = TestUtils.getTestEnv();
    assertEquals(3, this.function.call(env, AviatorNumber.valueOf(-3)).getValue(null));
    assertEquals(3.9, this.function.call(env, AviatorNumber.valueOf(-3.9)).getValue(null));
    assertEquals(400, this.function.call(env, AviatorNumber.valueOf(400)).getValue(null));
    assertEquals(new BigInteger("300000000000000000000000000000000"), this.function.call(env, AviatorNumber.valueOf(new BigInteger("-300000000000000000000000000000000"))).getValue(null));
    assertEquals(new BigDecimal("300000000000000000000000000000000"), this.function.call(env, AviatorNumber.valueOf(new BigDecimal("-300000000000000000000000000000000.0000002223333"))).getValue(null));
    assertEquals(400, this.function.call(env, AviatorNumber.valueOf(400)).getValue(null));
    env.put("a", 300);
    env.put("b", -3.14);
    env.put("c", new BigInteger("-300000000000000000000000000000000"));
    env.put("d", new BigDecimal("-300000000000000000000000000000000.0000002223333"));
    assertEquals(300, this.function.call(env, new AviatorJavaType("a")).getValue(null));
    assertEquals(3.14, this.function.call(env, new AviatorJavaType("b")).getValue(null));
    assertEquals(new BigInteger("300000000000000000000000000000000"), this.function.call(env, new AviatorJavaType("c")).getValue(null));
    assertEquals(new BigDecimal("300000000000000000000000000000000"), this.function.call(env, new AviatorJavaType("d")).getValue(null));
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) BigInteger(java.math.BigInteger) Env(com.googlecode.aviator.utils.Env) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 15 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class AviatorJavaType method defineValue.

@Override
public AviatorObject defineValue(final AviatorObject value, final Map<String, Object> env) {
    if (this.containsDot) {
        return setProperty(value, env);
    }
    Object v = getAssignedValue(value, env);
    // special processing for define functions.
    if (v instanceof LambdaFunction) {
        // try to define a function
        Object existsFn = getValue(env);
        if (existsFn instanceof DispatchFunction) {
            // It's already an overload function, install the new branch.
            ((DispatchFunction) existsFn).install((LambdaFunction) v);
            return AviatorRuntimeJavaType.valueOf(existsFn);
        } else if (existsFn instanceof LambdaFunction) {
            // cast it to an overload function
            DispatchFunction newFn = new DispatchFunction(this.name);
            // install the exists branch
            newFn.install((LambdaFunction) existsFn);
            // and the new branch.
            newFn.install(((LambdaFunction) v));
            v = newFn;
        } else if (existsFn == null && ((LambdaFunction) v).isVariadic()) {
            // cast variadic function to overload function
            DispatchFunction newFn = new DispatchFunction(this.name);
            newFn.install(((LambdaFunction) v));
            v = newFn;
        }
    }
    ((Env) env).override(this.name, v);
    return AviatorRuntimeJavaType.valueOf(v);
}
Also used : DispatchFunction(com.googlecode.aviator.runtime.function.DispatchFunction) LambdaFunction(com.googlecode.aviator.runtime.function.LambdaFunction) Env(com.googlecode.aviator.utils.Env)

Aggregations

Env (com.googlecode.aviator.utils.Env)28 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 Test (org.junit.Test)8 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 LRUMap (com.googlecode.aviator.utils.LRUMap)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Import (com.googlecode.aviator.annotation.Import)1 InterpretContext (com.googlecode.aviator.code.interpreter.InterpretContext)1 LoadIR (com.googlecode.aviator.code.interpreter.ir.LoadIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)1 ClassMethodFunction (com.googlecode.aviator.runtime.function.ClassMethodFunction)1