use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class OperationRuntime method eval.
/**
* Eval with unary operator
*
* @param arg
* @param env
* @param opType
* @return
*/
public static AviatorObject eval(final AviatorObject arg, final Map<String, Object> env, final OperatorType opType) {
AviatorFunction func = RuntimeUtils.getInstance(env).getOpFunction(opType);
AviatorObject ret = eval0(arg, env, opType, func);
if (RuntimeUtils.isTracedEval(env)) {
trace(env, opType, ret, arg);
}
return ret;
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class AviatorEvaluatorInstance method setOption.
/**
* Adds a evaluator option
*
* @since 2.3.4
* @see Options
* @param opt
* @param val
*/
public void setOption(final Options opt, final Object val) {
if (opt == null || val == null) {
throw new IllegalArgumentException("Option and value should not be null.");
}
if (!opt.isValidValue(val)) {
throw new IllegalArgumentException("Invalid value for option:" + opt.name());
}
Map<Options, Value> newOpts = new IdentityHashMap<>(this.options);
newOpts.put(opt, opt.intoValue(val));
if (opt == Options.FEATURE_SET) {
Set<Feature> oldSet = new HashSet<>(getFeatures());
@SuppressWarnings("unchecked") Set<Feature> newSet = (Set<Feature>) val;
if (oldSet.removeAll(newSet)) {
// removed functions that feature is disabled.
for (Feature feat : oldSet) {
for (AviatorFunction fn : feat.getFunctions()) {
this.removeFunction(fn);
}
}
}
}
this.options = newOpts;
if (opt == Options.FEATURE_SET) {
loadFeatureFunctions();
}
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class AviatorEvaluatorInstance method defineFunction.
/**
* Define a function by name and expression with the execution env.
*
* @param name the function name
* @param expression the expression to be executed and it's result must be a function.
* @param env the expression execution env
* @since 4.0.0
*/
public void defineFunction(final String name, final String expression, final Map<String, Object> env) {
AviatorFunction function = (AviatorFunction) this.execute(expression, env);
this.addFunction(name, function);
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class AviatorEvaluatorInstance method loadInternalFunctions.
private Map<String, AviatorFunction> loadInternalFunctions() {
Map<String, AviatorFunction> funcs = new HashMap<>();
for (String lib : libs) {
try (final InputStream in = this.getClass().getResourceAsStream("/" + lib);
final BufferedInputStream bis = new BufferedInputStream(in);
final Reader reader = new InputStreamReader(bis)) {
Expression exp = this.compile(lib, Utils.readFully(reader), false);
Map<String, Object> exports = executeModule(exp, lib);
for (Map.Entry<String, Object> entry : exports.entrySet()) {
if (entry.getValue() instanceof AviatorFunction) {
final AviatorFunction fn = (AviatorFunction) entry.getValue();
addFunction(entry.getKey(), fn);
funcs.put(entry.getKey(), fn);
}
}
} catch (IOException e) {
throw new IllegalStateException("Fail to load internal lib: " + lib, e);
}
}
return funcs;
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class ClassPathConfigFunctionLoader method loadClass.
private AviatorFunction loadClass(String className) {
info("Loading custom aviator function class: '" + className + "'.");
try {
@SuppressWarnings("unchecked") Class<AviatorFunction> clazz = (Class<AviatorFunction>) Class.forName(className);
AviatorFunction func = clazz.newInstance();
if (func != null) {
totalCustomFunctions++;
}
return func;
} catch (Throwable e) {
error("Load custom aviator function class: " + className + "' failed with error:" + e.getMessage() + ".");
}
return null;
}
Aggregations