use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class IfCallccFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
if (arg1 instanceof ReducerResult) {
return arg1;
} else {
final Object nextClauseVal = arg2.getValue(env);
if (nextClauseVal == Constants.REDUCER_EMPTY) {
return arg1;
}
AviatorFunction otherClausesFn = (AviatorFunction) nextClauseVal;
AviatorObject result = otherClausesFn.call(env);
// No remaining statements, return the if statement result.
if (result == Constants.REDUCER_EMPTY) {
return arg1;
}
return result;
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class NewInstanceFunction method variadicCall.
@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
if (args == null || args.length == 0) {
throw new IllegalArgumentException("Missing className for new");
}
AviatorObject firstArg = args[0];
if (firstArg.getAviatorType() != AviatorType.JavaType) {
throw new IllegalArgumentException("Invalid class name: " + firstArg.desc(env));
}
String className = ((AviatorJavaType) firstArg).getName();
try {
assert (env instanceof Env);
Class<?> clazz = ((Env) env).resolveClassSymbol(className);
Constructor<?>[] constructors = clazz.getConstructors();
final Object[] constructArgs = new Object[args.length - 1];
for (int i = 1; i < args.length; i++) {
constructArgs[i - 1] = args[i].getValue(env);
}
Constructor<?> bestMatch = null;
for (Constructor<?> constructor : constructors) {
final Class<?>[] pTypes = constructor.getParameterTypes();
if (pTypes.length == constructArgs.length) {
if (Reflector.isCongruent(pTypes, constructArgs)) {
bestMatch = constructor;
for (int i = 0; i < constructArgs.length; i++) {
constructArgs[i] = Reflector.boxArg(pTypes[i], constructArgs[i]);
}
break;
}
}
}
if (bestMatch == null) {
throw new IllegalStateException("Could not find constructor for class " + className + " with arguments: " + Arrays.toString(constructArgs));
}
return AviatorRuntimeJavaType.valueOf(bestMatch.newInstance(constructArgs));
} catch (Throwable t) {
throw Reflector.sneakyThrow(t);
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class ReducerFunction method call.
@Override
public final AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
Object coll = arg1.getValue(env);
AviatorFunction iteratorFn = (AviatorFunction) arg2;
int maxLoopCount = RuntimeUtils.getInstance(env).getOptionValue(Options.MAX_LOOP_COUNT).number;
AviatorObject result = AviatorNil.NIL;
long c = 0;
if (coll != Range.LOOP) {
long arities = (long) arg2.meta(Constants.ARITIES_META);
long index = 0;
boolean unboxEntry = arities == 2 && coll != null && Map.class.isAssignableFrom(coll.getClass());
// for..in loop
for (Object obj : RuntimeUtils.seq(coll, env)) {
if (arities == 1) {
result = iteratorFn.call(env, AviatorRuntimeJavaType.valueOf(obj));
} else {
if (unboxEntry) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
result = iteratorFn.call(env, AviatorRuntimeJavaType.valueOf(entry.getKey()), AviatorRuntimeJavaType.valueOf(entry.getValue()));
} else {
result = iteratorFn.call(env, AviatorLong.valueOf(index++), AviatorRuntimeJavaType.valueOf(obj));
}
}
if (!(result instanceof ReducerResult)) {
continue;
}
boolean breakOut = false;
ReducerResult midResult = (ReducerResult) result;
result = midResult.obj;
if (midResult.state == ReducerState.Empty) {
continue;
}
switch(midResult.state) {
case Break:
breakOut = true;
break;
case Return:
return midResult;
default:
break;
}
if (breakOut) {
break;
}
}
} else {
// while statement
while (true) {
if (maxLoopCount > 0 && ++c > maxLoopCount) {
throw new ExpressionRuntimeException("Overflow max loop count: " + maxLoopCount);
}
result = iteratorFn.call(env);
if (!(result instanceof ReducerResult)) {
continue;
}
boolean breakOut = false;
ReducerResult midResult = (ReducerResult) result;
result = midResult.obj;
if (midResult.state == ReducerState.Empty) {
continue;
}
switch(midResult.state) {
case Break:
breakOut = true;
break;
case Return:
return midResult;
default:
break;
}
if (breakOut) {
break;
}
}
}
Object contObj = arg3.getValue(env);
if (contObj == Constants.REDUCER_EMPTY) {
return result;
}
AviatorObject contResult = ((AviatorFunction) contObj).call(env);
if (contResult == Constants.REDUCER_EMPTY) {
// empty continuation, return current result.
return result;
} else {
return contResult;
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class AbstractSeqMinMaxFunction method call.
@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
Object first = arg1.getValue(env);
if (first == null) {
return AviatorNil.NIL;
}
Sequence seq = RuntimeUtils.seq(first, env);
boolean wasFirst = true;
Object result = null;
for (Object obj : seq) {
result = compareObjects(result, obj, wasFirst);
if (wasFirst) {
wasFirst = false;
}
if (getOp() == Op.Min && result == null) {
break;
}
}
return AviatorRuntimeJavaType.valueOf(result);
}
use of com.googlecode.aviator.runtime.type.AviatorObject 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;
}
Aggregations