Search in sources :

Example 16 with Function

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 17 with Function

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);
        }
    }
}
Also used : Function(org.mozilla.javascript.Function) ExtendedJavaClass(com.scratchdisk.script.rhino.ExtendedJavaClass)

Example 18 with Function

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();
        }
    }
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) TimingFunction(org.apache.sling.scripting.sightly.js.impl.async.TimingFunction) SightlyException(org.apache.sling.scripting.sightly.SightlyException) HybridObject(org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with Function

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;
}
Also used : Function(org.mozilla.javascript.Function) TimingFunction(org.apache.sling.scripting.sightly.js.impl.async.TimingFunction) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer) Resource(org.apache.sling.api.resource.Resource) HybridObject(org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 20 with Function

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);
}
Also used : BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function)

Aggregations

Function (org.mozilla.javascript.Function)21 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 Context (org.mozilla.javascript.Context)10 Scriptable (org.mozilla.javascript.Scriptable)6 ContextAction (org.mozilla.javascript.ContextAction)4 Map (java.util.Map)3 SightlyException (org.apache.sling.scripting.sightly.SightlyException)3 BaseFunction (org.mozilla.javascript.BaseFunction)3 HashMap (java.util.HashMap)2 TimingFunction (org.apache.sling.scripting.sightly.js.impl.async.TimingFunction)2 HybridObject (org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject)2 ContinuationPending (org.mozilla.javascript.ContinuationPending)2 NativeArray (org.mozilla.javascript.NativeArray)2 NativeObject (org.mozilla.javascript.NativeObject)2 RhinoException (org.mozilla.javascript.RhinoException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ResultBuilder (com.googlecode.jslint4java.JSLintResult.ResultBuilder)1 ArgumentReader (com.scratchdisk.script.ArgumentReader)1 StringArgumentReader (com.scratchdisk.script.StringArgumentReader)1 ExtendedJavaClass (com.scratchdisk.script.rhino.ExtendedJavaClass)1