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;
}
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);
}
}
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;
}
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));
}
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);
}
Aggregations