use of org.mozilla.javascript.Function 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.Function in project scriptographer by scriptographer.
the class TopLevel method mapJavaClass.
/**
* Maps a Java class to a JavaScript prototype, so this can be used
* instead for wrapping of returned java types. So far this is only
* used for java.io.File in Scriptographer.
*/
public static void mapJavaClass(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
if (args.length == 2) {
for (int i = 0; i < args.length; i++) args[i] = Context.jsToJava(args[i], Object.class);
if (args[0] instanceof Class && args[1] instanceof Function) {
Class cls = (Class) args[0];
Function proto = (Function) args[1];
RhinoWrapFactory factory = (RhinoWrapFactory) cx.getWrapFactory();
factory.mapJavaClass(cls, proto);
}
}
}
use of org.mozilla.javascript.Function in project sling by apache.
the class SlyBindingsValuesProvider method processBindings.
public void processBindings(Bindings bindings) {
if (needsInit()) {
throw new SightlyException("Attempted to call processBindings without calling initialise first.");
}
Context context = null;
try {
context = Context.enter();
Object qInstance = obtainQInstance(context, bindings);
if (qInstance == null) {
return;
}
for (Map.Entry<String, Function> entry : factories.entrySet()) {
addBinding(context, entry.getValue(), bindings, entry.getKey(), qInstance);
}
} finally {
if (context != null) {
Context.exit();
}
}
}
use of org.mozilla.javascript.Function in project sling by apache.
the class SlyBindingsValuesProvider method loadFactory.
private Function loadFactory(ResourceResolver resolver, JsEnvironment jsEnvironment, String path, Bindings bindings) {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new SightlyException("Sly namespace loader could not find the following script: " + path);
}
AsyncContainer container = jsEnvironment.runResource(resource, createBindings(bindings), new SimpleBindings());
Object obj = container.getResult();
if (!(obj instanceof Function)) {
throw new SightlyException("Script " + path + " was expected to return a function.");
}
return (Function) obj;
}
use of org.mozilla.javascript.Function in project sling by apache.
the class UseFunction method call.
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Function function;
List<String> depNames;
if (args.length == 0) {
throw new IllegalArgumentException("Not enough arguments for use");
} else if (args.length == 1) {
function = decodeCallback(args[0]);
depNames = Collections.emptyList();
} else {
function = decodeCallback(args[1]);
depNames = decodeDepNames(args[0]);
}
return use(depNames, function, cx, scope);
}
Aggregations