Search in sources :

Example 1 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method disableFeature.

/**
 * Disable a script engine feature.
 *
 * @see Feature
 * @since 5.0.0
 * @param feature
 */
public void disableFeature(final Feature feature) {
    this.options.get(Options.FEATURE_SET).featureSet.remove(feature);
    for (AviatorFunction fn : feature.getFunctions()) {
        this.removeFunction(fn);
    }
    loadFeatureFunctions();
}
Also used : AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 2 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class InterpretCodeGenerator method emit.

private void emit(IR ir) {
    if (ir instanceof OperatorIR) {
        // check if operator is override.
        final OperatorType op = ((OperatorIR) ir).getOp();
        AviatorFunction fn = this.instance.getOpFunction(op);
        if (fn != null) {
            // replace it with new IR
            ir = new OperatorIR(op, fn);
        }
    }
    this.instruments.add(ir);
}
Also used : AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) OperatorIR(com.googlecode.aviator.code.interpreter.ir.OperatorIR) OperatorType(com.googlecode.aviator.lexer.token.OperatorType)

Example 3 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class AviatorEvaluatorInstanceUnitTest method testInstanceCustomOperator.

@Test
public void testInstanceCustomOperator() {
    AviatorFunction func = (AviatorFunction) this.instance.execute("lambda(x,y) -> x-y end");
    this.instance.addOpFunction(OperatorType.ADD, func);
    assertEquals(-1, this.instance.execute("3+4"));
    assertEquals(7, AviatorEvaluator.execute("3+4"));
    assertEquals(7, AviatorEvaluator.newInstance().execute("3+4"));
}
Also used : AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) Test(org.junit.Test)

Example 4 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class TryCatchFunction method call.

@SuppressWarnings("unchecked")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4) {
    AviatorFunction tryBody = (AviatorFunction) arg1;
    List<CatchHandler> catchHandlers = (List<CatchHandler>) arg2.getValue(env);
    AviatorFunction finallyBody = arg3 != AviatorNil.NIL ? (AviatorFunction) arg3 : null;
    AviatorObject result = null;
    try {
        result = tryBody.call(env);
    } catch (Throwable t) {
        boolean handle = false;
        if (catchHandlers != null) {
            for (CatchHandler handler : catchHandlers) {
                if (handler.isMatch(t.getClass())) {
                    AviatorObject ret = handler.getFunc().call(env, AviatorRuntimeJavaType.valueOf(t));
                    result = chooseResult(result, ret);
                    handle = true;
                    break;
                }
            }
        }
        if (!handle) {
            throw Reflector.sneakyThrow(t);
        }
    } finally {
        if (finallyBody != null) {
            AviatorObject ret = finallyBody.call(env);
            result = chooseResult(result, ret);
        }
    }
    if (isReturnResult(result)) {
        return result;
    }
    Object val = arg4.getValue(env);
    if (val == Constants.REDUCER_EMPTY) {
        return result;
    }
    AviatorFunction continueFn = (AviatorFunction) val;
    AviatorObject contResult = continueFn.call(env);
    if (contResult == Constants.REDUCER_EMPTY) {
        return result;
    } else {
        return contResult;
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) List(java.util.List) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 5 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction 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;
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)16 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 Map (java.util.Map)5 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 IdentityHashMap (java.util.IdentityHashMap)2 FunctionMap (org.casbin.jcasbin.model.FunctionMap)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 Expression (com.googlecode.aviator.Expression)1 Value (com.googlecode.aviator.Options.Value)1 OperatorIR (com.googlecode.aviator.code.interpreter.ir.OperatorIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 OperatorType (com.googlecode.aviator.lexer.token.OperatorType)1