Search in sources :

Example 1 with DispatchFunction

use of com.googlecode.aviator.runtime.function.DispatchFunction in project aviatorscript by killme2008.

the class AviatorJavaType method defineValue.

@Override
public AviatorObject defineValue(final AviatorObject value, final Map<String, Object> env) {
    if (this.containsDot) {
        return setProperty(value, env);
    }
    Object v = getAssignedValue(value, env);
    // special processing for define functions.
    if (v instanceof LambdaFunction) {
        // try to define a function
        Object existsFn = getValue(env);
        if (existsFn instanceof DispatchFunction) {
            // It's already an overload function, install the new branch.
            ((DispatchFunction) existsFn).install((LambdaFunction) v);
            return AviatorRuntimeJavaType.valueOf(existsFn);
        } else if (existsFn instanceof LambdaFunction) {
            // cast it to an overload function
            DispatchFunction newFn = new DispatchFunction(this.name);
            // install the exists branch
            newFn.install((LambdaFunction) existsFn);
            // and the new branch.
            newFn.install(((LambdaFunction) v));
            v = newFn;
        } else if (existsFn == null && ((LambdaFunction) v).isVariadic()) {
            // cast variadic function to overload function
            DispatchFunction newFn = new DispatchFunction(this.name);
            newFn.install(((LambdaFunction) v));
            v = newFn;
        }
    }
    ((Env) env).override(this.name, v);
    return AviatorRuntimeJavaType.valueOf(v);
}
Also used : DispatchFunction(com.googlecode.aviator.runtime.function.DispatchFunction) LambdaFunction(com.googlecode.aviator.runtime.function.LambdaFunction) Env(com.googlecode.aviator.utils.Env)

Aggregations

DispatchFunction (com.googlecode.aviator.runtime.function.DispatchFunction)1 LambdaFunction (com.googlecode.aviator.runtime.function.LambdaFunction)1 Env (com.googlecode.aviator.utils.Env)1