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