Search in sources :

Example 6 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class Main method newEnv.

private static Map<String, Object> newEnv(final Expression exp, final String abPath, final String[] args) throws IOException {
    final Env exports = new Env();
    final Map<String, Object> module = exp.newEnv("exports", exports, "path", abPath, "dir", getFileDir(abPath));
    Map<String, Object> env = exp.newEnv("__MODULE__", module, "exports", exports, "ARGV", args);
    return env;
}
Also used : Env(com.googlecode.aviator.utils.Env)

Example 7 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class AviatorPattern method match.

@Override
public AviatorObject match(final AviatorObject other, final Map<String, Object> env) {
    switch(other.getAviatorType()) {
        case Nil:
            return AviatorBoolean.FALSE;
        case String:
            AviatorString aviatorString = (AviatorString) other;
            Matcher m = this.pattern.matcher(aviatorString.getLexeme(env));
            if (m.matches()) {
                boolean captureGroups = RuntimeUtils.getInstance(env).getOptionValue(Options.PUT_CAPTURING_GROUPS_INTO_ENV).bool;
                if (captureGroups && env != Collections.EMPTY_MAP) {
                    int groupCount = m.groupCount();
                    for (int i = 0; i <= groupCount; i++) {
                        ((Env) env).override("$" + i, m.group(i));
                    }
                }
                return AviatorBoolean.TRUE;
            } else {
                return AviatorBoolean.FALSE;
            }
        case JavaType:
            AviatorJavaType javaType = (AviatorJavaType) other;
            final Object javaValue = javaType.getValue(env);
            if (javaValue == null) {
                return AviatorBoolean.FALSE;
            }
            if (TypeUtils.isString(javaValue)) {
                return match(new AviatorString(String.valueOf(javaValue)), env);
            } else {
                throw new ExpressionRuntimeException(desc(env) + " could not match " + other.desc(env));
            }
        default:
            throw new ExpressionRuntimeException(desc(env) + " could not match " + other.desc(env));
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) Matcher(java.util.regex.Matcher) Env(com.googlecode.aviator.utils.Env)

Example 8 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class UseFunction method variadicCall.

/**
 * use package.{class1, class2};
 */
@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    if (args.length < 3) {
        throw new IllegalArgumentException("Wrong arguments(" + args.length + ") passed to __use variadicCall");
    }
    if (args[0].getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Can't use other aviator type except varaible");
    }
    final String packageSym = ((AviatorJavaType) args[0]).getName();
    assert (env instanceof Env);
    final Env theEnv = (Env) env;
    for (int i = 1; i < args.length; i++) {
        if (args[i].getAviatorType() != AviatorType.JavaType) {
            throw new IllegalArgumentException("Can't use other aviator type except varaible");
        }
        final String name = ((AviatorJavaType) args[i]).getName();
        addSym(theEnv, packageSym, name);
    }
    return AviatorNil.NIL;
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Env(com.googlecode.aviator.utils.Env)

Example 9 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class UseFunction method call.

/**
 * use package.* or use.package.{class};
 */
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    if (arg1.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Can't use other aviator type except varaible");
    }
    if (arg2.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Can't use other aviator type except varaible");
    }
    final String packageSym = ((AviatorJavaType) arg1).getName();
    final String name = ((AviatorJavaType) arg2).getName();
    assert (env instanceof Env);
    final Env theEnv = (Env) env;
    addSym(theEnv, packageSym, name);
    return AviatorNil.NIL;
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Env(com.googlecode.aviator.utils.Env)

Example 10 with Env

use of com.googlecode.aviator.utils.Env 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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Env(com.googlecode.aviator.utils.Env) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

Env (com.googlecode.aviator.utils.Env)28 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 Test (org.junit.Test)8 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 LRUMap (com.googlecode.aviator.utils.LRUMap)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Import (com.googlecode.aviator.annotation.Import)1 InterpretContext (com.googlecode.aviator.code.interpreter.InterpretContext)1 LoadIR (com.googlecode.aviator.code.interpreter.ir.LoadIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)1 ClassMethodFunction (com.googlecode.aviator.runtime.function.ClassMethodFunction)1