use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqIntoFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object fromSeq = arg2.getValue(env);
AviatorObject result = arg1;
for (Object e : RuntimeUtils.seq(fromSeq, env)) {
result = SEQ_ADD.call(env, result, AviatorRuntimeJavaType.valueOf(e));
}
return result;
}
use of com.googlecode.aviator.runtime.type.AviatorObject 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.AviatorObject in project aviatorscript by killme2008.
the class SeqAddFunction method call.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
Object coll = arg1.getValue(env);
Object key = arg2.getValue(env);
Object value = arg3.getValue(env);
if (coll == null) {
throw new NullPointerException("null seq");
}
Class<?> clazz = coll.getClass();
if (Map.class.isAssignableFrom(clazz)) {
((Map) coll).put(key, value);
return arg1;
} else if (clazz.isArray()) {
int index = ((Number) key).intValue();
ArrayUtils.set(coll, index, value);
return arg1;
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not a map or array.");
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqContainsKeyFunction 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();
if (Map.class.isAssignableFrom(clazz)) {
Map seq = (Map) first;
try {
return AviatorBoolean.valueOf(seq.containsKey(arg2.getValue(env)));
} catch (Exception e) {
RuntimeUtils.printStackTrace(env, e);
return AviatorBoolean.FALSE;
}
} else if (clazz.isArray()) {
int index = FunctionUtils.getNumberValue(arg2, env).intValue();
return AviatorBoolean.valueOf(index >= 0 && index < ArrayUtils.getLength(first));
} else if (List.class.isAssignableFrom(clazz)) {
int index = FunctionUtils.getNumberValue(arg2, env).intValue();
return AviatorBoolean.valueOf(index >= 0 && index < ((List) first).size());
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not a map or array.");
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject 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;
}
Aggregations