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;
}
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));
}
}
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;
}
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;
}
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);
}
}
Aggregations