use of com.googlecode.aviator.runtime.type.AviatorFunction 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.AviatorFunction 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.AviatorFunction 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.AviatorFunction in project aviatorscript by killme2008.
the class OperationRuntime method eval.
/**
* Eval with arguments array.
*
* @param args
* @param opType
* @return
*/
public static AviatorObject eval(final Map<String, Object> env, final AviatorObject[] args, final OperatorType opType) {
AviatorFunction func = RuntimeUtils.getInstance(env).getOpFunction(opType);
AviatorObject ret = eval0(env, args, opType, func);
if (RuntimeUtils.isTracedEval(env)) {
trace(env, opType, ret, args);
}
return ret;
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class SeqMakePredicateFunctionUnitTest method testMakePredicate.
@Test
public void testMakePredicate() {
SeqMakePredicateFunFunction fun = new SeqMakePredicateFunFunction("eq", OperatorType.EQ);
Map<String, Object> env = new HashMap<String, Object>();
AviatorObject predicateName = fun.call(env, AviatorRuntimeJavaType.valueOf("hello"));
assertNotNull(predicateName);
AviatorFunction predicate = (AviatorFunction) predicateName.getValue(env);
assertNotNull(predicate);
AviatorObject result = predicate.call(null, AviatorRuntimeJavaType.valueOf("hello"));
// equals self
assertTrue(result.booleanValue(null));
}
Aggregations