Search in sources :

Example 1 with Env

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

the class FunctionTest method testParseBigNumbers.

@Test
public void testParseBigNumbers() {
    assertEquals(new BigInteger("99999999999999999999999999999999"), this.instance.exec("99999999999999999999999999999999"));
    assertEquals(new BigInteger("99999999999999999999999999999999"), this.instance.exec("99999999999999999999999999999999N"));
    assertEquals(new BigInteger("199999999999999999999999999999998"), this.instance.exec("99999999999999999999999999999999+99999999999999999999999999999999"));
    Env env = new Env(null);
    env.setInstance(this.instance);
    assertEquals(new BigDecimal("99999999999999999999999999999999.99999999", RuntimeUtils.getMathContext(env)), this.instance.exec("99999999999999999999999999999999.99999999M"));
}
Also used : BigInteger(java.math.BigInteger) Env(com.googlecode.aviator.utils.Env) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with Env

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

the class AviatorEvaluatorInstance method requireScript.

/**
 * Loads a script from path and return it's exports with module caching.
 *
 * @param path
 * @param args arguments to execute the script.
 * @throws IOException
 * @return the exports map
 * @since 5.0.0
 */
public Map<String, Object> requireScript(final String path) throws IOException {
    ensureFeatureEnabled(Feature.Module);
    if (!path.endsWith(".av")) {
        // internal modules
        Map<String, Object> exports = (Env) this.moduleCache.get(path);
        if (exports != null) {
            return exports;
        }
    }
    File file = tryFindScriptFile(path);
    final String abPath = file.getAbsolutePath();
    Map<String, Object> exports = (Env) this.moduleCache.get(abPath);
    if (exports != null) {
        return exports;
    } else {
        // TODO better lock
        synchronized (abPath.intern()) {
            exports = (Env) this.moduleCache.get(abPath);
            if (exports != null) {
                return exports;
            }
            exports = loadScript0(abPath);
            this.moduleCache.put(abPath, exports);
            return exports;
        }
    }
}
Also used : Env(com.googlecode.aviator.utils.Env) File(java.io.File)

Example 3 with Env

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

the class AviatorEvaluatorInstance method addModule.

/**
 * Adds a module class and import it's public static methods as module's exports into module
 * cache, return the exports map.
 *
 * @param moduleClazz
 * @return the exports map
 * @since 5.0.0
 */
public Env addModule(final Class<?> moduleClazz) throws NoSuchMethodException, IllegalAccessException {
    String namespace = moduleClazz.getSimpleName();
    Import importAnt = moduleClazz.getAnnotation(Import.class);
    if (importAnt != null) {
        namespace = importAnt.ns();
        if (namespace == null || namespace.isEmpty() || !ExpressionParser.isJavaIdentifier(namespace)) {
            throw new IllegalArgumentException("Invalid namespace in Import annotation: " + namespace);
        }
    }
    Env exports = null;
    synchronized (namespace.intern()) {
        exports = (Env) this.moduleCache.get(namespace);
        if (exports != null) {
            return exports;
        }
        exports = loadModule(moduleClazz);
        this.moduleCache.put(namespace, exports);
        return exports;
    }
}
Also used : Import(com.googlecode.aviator.annotation.Import) Env(com.googlecode.aviator.utils.Env)

Example 4 with Env

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

the class BaseExpression method newEnv.

protected Env newEnv(final Map<String, Object> map, final boolean direct) {
    Env env;
    if (direct) {
        env = new Env(map, map == Collections.EMPTY_MAP ? new HashMap<String, Object>() : map);
    } else {
        env = new Env(map);
    }
    env.configure(this.instance, this);
    return env;
}
Also used : Env(com.googlecode.aviator.utils.Env)

Example 5 with Env

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

the class OptimizeCodeGenerator method getCompileEnv.

private Env getCompileEnv() {
    if (this.compileEnv == null) {
        this.compileEnv = new Env();
        this.compileEnv.setInstance(this.instance);
    }
    return this.compileEnv;
}
Also used : Env(com.googlecode.aviator.utils.Env)

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