use of com.googlecode.aviator.runtime.type.AviatorObject 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.AviatorObject in project aviatorscript by killme2008.
the class TraceFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, final AviatorObject arg11) {
traceArgs(env, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
AviatorObject ret = this.rawFunc.call(env, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
traceResult(env, ret);
return ret;
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class TraceFunction method traceArgs.
private void traceArgs(final Map<String, Object> env, final Object... args) {
StringBuilder sb = new StringBuilder();
boolean wasFirst = true;
for (Object arg : args) {
if (wasFirst) {
wasFirst = false;
} else {
sb.append(",");
}
if (arg instanceof String) {
sb.append(arg.toString());
} else {
sb.append(((AviatorObject) arg).desc(env));
}
}
RuntimeUtils.printlnTrace(env, "Func : " + getName() + "(" + sb.toString() + ")");
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqGetFunction method call.
@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object coll = arg1.getValue(env);
Object key = arg2.getValue(env);
if (coll == null) {
throw new NullPointerException("the collection of seq.get is null, which the value of key/index is `" + key + "`");
}
Class<?> clazz = coll.getClass();
if (List.class.isAssignableFrom(clazz)) {
if (!(key instanceof Number)) {
throw new IllegalArgumentException("Invalid index `" + key + "` for list,it's not a number.");
}
Object value = ((List) coll).get(((Number) key).intValue());
return AviatorRuntimeJavaType.valueOf(value);
} else if (Set.class.isAssignableFrom(clazz)) {
if (((Set) coll).contains(key)) {
return AviatorRuntimeJavaType.valueOf(key);
} else {
return AviatorNil.NIL;
}
} else if (Map.class.isAssignableFrom(clazz)) {
Object value = ((Map) coll).get(key);
return AviatorRuntimeJavaType.valueOf(value);
} else if (clazz.isArray()) {
if (!(key instanceof Number)) {
throw new IllegalArgumentException("Invalid index `" + key + "` for list,it's not a number.");
}
return AviatorRuntimeJavaType.valueOf(ArrayUtils.get(coll, ((Number) key).intValue()));
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not a collection.");
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqIncludeFunction 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();
boolean contains = false;
if (Set.class.isAssignableFrom(clazz)) {
contains = ((Set) first).contains(arg2.getValue(env));
} else {
try {
for (Object obj : RuntimeUtils.seq(first, env)) {
if (AviatorRuntimeJavaType.valueOf(obj).compareEq(arg2, env) == 0) {
contains = true;
break;
}
}
} catch (Exception e) {
RuntimeUtils.printStackTrace(env, e);
return AviatorBoolean.FALSE;
}
}
return AviatorBoolean.valueOf(contains);
}
Aggregations