use of org.mozilla.javascript.Scriptable in project gradle by gradle.
the class JsHintWorker method process.
@Override
public JsHintResult process(JsHintSpec spec) {
Scriptable jsHintScope = RhinoWorkerUtils.parse(spec.getJsHint(), "UTF-8");
String encoding = spec.getEncoding();
Map<File, Map<String, Object>> results = new LinkedHashMap<File, Map<String, Object>>();
for (File target : spec.getSource()) {
LOGGER.info("Reading file: {}", target.getAbsolutePath());
String source = readFile(target, encoding);
Map<String, Object> result = jsHint(jsHintScope, source, target.getName());
results.put(target, result);
}
return new JsHintResult(results);
}
use of org.mozilla.javascript.Scriptable in project gradle by gradle.
the class CoffeeScriptCompilerWorker method process.
public void process(SerializableCoffeeScriptCompileSpec spec) {
Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {
public void execute(Context context) {
context.setOptimizationLevel(-1);
}
});
String encoding = spec.getOptions().getEncoding();
CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());
for (RelativeFile target : spec.getSource()) {
String source = readFile(target.getFile(), encoding);
String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
}
}
use of org.mozilla.javascript.Scriptable in project gradle by gradle.
the class RhinoWorkerUtils method childScope.
public static <R> R childScope(Scriptable parentScope, ScopeOperation<R> operation) {
Context context = Context.enter();
try {
operation.initContext(context);
Scriptable childScope = context.newObject(parentScope);
childScope.setParentScope(parentScope);
return operation.action(childScope, context);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Scriptable in project SmartZPN by andforce.
the class PacScriptParser method runScript.
private String runScript(String js, String functionName, Object[] functionParams) {
Context rhino = Context.enter();
rhino.setOptimizationLevel(-1);
try {
Scriptable scope = rhino.initStandardObjects();
rhino.evaluateString(scope, js, "JavaScript", js.split("\n").length, null);
Function function = (Function) scope.get(functionName, scope);
Object result = function.call(rhino, scope, scope, functionParams);
if (result instanceof String) {
return (String) result;
} else if (result instanceof NativeJavaObject) {
return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
} else if (result instanceof NativeObject) {
return (String) ((NativeObject) result).getDefaultValue(String.class);
}
return result.toString();
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Scriptable in project scriptographer by scriptographer.
the class ExtendedJavaClass method get.
public Object get(String name, Scriptable start) {
// We are completely redefining get here without relying on
// NativeJavaClass' implementation, in order to add more JS
// like behavior.
// When used as a constructor, ScriptRuntime.newObject() asks
// for our prototype to create an object of the correct type.
// We don't really care what the object is, since we're returning
// one constructed out of whole cloth, so we return null.
boolean isProto = name.equals("prototype");
if (!isProto) {
if (members.has(name, true)) {
return members.get(this, name, javaObject, true);
}
// to be more logical / java-like. TODO: Is this a Rhino bug?
if (staticFieldAndMethods != null) {
Object result = staticFieldAndMethods.get(name);
if (result != null)
return result;
}
}
if (properties != null && properties.containsKey(name)) {
// see whether this object defines the property.
return properties.get(name);
} else if (isProto) {
// getPrototype creates prototype Objects on the fly:
return getInstancePrototype();
} else {
Scriptable proto = getPrototype();
if (proto != null) {
Object result = proto.get(name, start);
if (result != Scriptable.NOT_FOUND)
return result;
}
}
// Experimental: look for nested classes by appending $name to
// current class' name.
Class<?> nestedClass = findNestedClass(getClassObject(), name);
if (nestedClass != null) {
ExtendedJavaClass nestedValue = new ExtendedJavaClass(ScriptableObject.getTopLevelScope(this), nestedClass, properties != null);
nestedValue.setParentScope(this);
return nestedValue;
}
return Scriptable.NOT_FOUND;
}
Aggregations