Search in sources :

Example 46 with Scriptable

use of org.mozilla.javascript.Scriptable in project sling by apache.

the class AsyncExtractor method convertCallback.

private static Function convertCallback(final UnaryCallback unaryCallback) {
    return new BaseFunction() {

        @Override
        public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            Object arg = (args.length == 0) ? Context.getUndefinedValue() : args[0];
            unaryCallback.invoke(arg);
            return Context.getUndefinedValue();
        }
    };
}
Also used : Context(org.mozilla.javascript.Context) BaseFunction(org.mozilla.javascript.BaseFunction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 47 with Scriptable

use of org.mozilla.javascript.Scriptable in project sling by apache.

the class SlyBindingsValuesProvider method createQInstance.

private Object createQInstance(Context context, Script qScript) {
    CommonJsModule module = new CommonJsModule();
    Scriptable tempScope = context.newObject(qScope);
    ScriptableObject.putProperty(tempScope, Variables.MODULE, module);
    ScriptableObject.putProperty(tempScope, Variables.EXPORTS, module.getExports());
    qScript.exec(context, tempScope);
    return module.getExports();
}
Also used : CommonJsModule(org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule) Scriptable(org.mozilla.javascript.Scriptable)

Example 48 with Scriptable

use of org.mozilla.javascript.Scriptable in project gradle by gradle.

the class RhinoWorkerUtils method parse.

public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }
    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }
    return scope;
}
Also used : Context(org.mozilla.javascript.Context) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) Scriptable(org.mozilla.javascript.Scriptable) FileInputStream(java.io.FileInputStream)

Example 49 with Scriptable

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

Example 50 with Scriptable

use of org.mozilla.javascript.Scriptable in project jslint4java by happygiraffe.

the class JSLint method buildResults.

/**
 * Assemble the {@link JSLintResult} object.
 */
@NeedsContext
private JSLintResult buildResults(final String systemId, final long startNanos, final long endNanos) {
    return (JSLintResult) contextFactory.call(new ContextAction() {

        public Object run(Context cx) {
            ResultBuilder b = new JSLintResult.ResultBuilder(systemId);
            b.duration(TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos));
            for (Issue issue : readErrors(systemId)) {
                b.addIssue(issue);
            }
            // Collect a report on what we've just linted.
            b.report(callReport(false));
            // Extract JSLINT.data() output and set it on the result.
            Object o = lintFunc.get("data", lintFunc);
            // Real JSLINT will always have this, but some of my test stubs don't.
            if (o != UniqueTag.NOT_FOUND) {
                Function reportFunc = (Function) o;
                Scriptable data = (Scriptable) reportFunc.call(cx, lintFunc, null, Context.emptyArgs);
                for (String global : Util.listValueOfType("global", String.class, data)) {
                    b.addGlobal(global);
                }
                b.json(Util.booleanValue("json", data));
                for (JSFunction f : Util.listValue("functions", data, new JSFunctionConverter())) {
                    b.addFunction(f);
                }
            }
            // Extract the list of properties. Note that we don't expose the counts, as it
            // doesn't seem that useful.
            Object properties = lintFunc.get("property", lintFunc);
            if (properties != UniqueTag.NOT_FOUND) {
                for (Object id : ScriptableObject.getPropertyIds((Scriptable) properties)) {
                    b.addProperty(id.toString());
                }
            }
            return b.build();
        }
    });
}
Also used : Context(org.mozilla.javascript.Context) ResultBuilder(com.googlecode.jslint4java.JSLintResult.ResultBuilder) Function(org.mozilla.javascript.Function) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)106 Context (org.mozilla.javascript.Context)49 ScriptableObject (org.mozilla.javascript.ScriptableObject)30 ContextAction (org.mozilla.javascript.ContextAction)12 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 NativeObject (org.mozilla.javascript.NativeObject)7 Function (org.mozilla.javascript.Function)6 Map (java.util.Map)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 RhinoException (org.mozilla.javascript.RhinoException)4 Wrapper (org.mozilla.javascript.Wrapper)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 ScriptContext (javax.script.ScriptContext)3 Callable (org.mozilla.javascript.Callable)3 ContextFactory (org.mozilla.javascript.ContextFactory)3 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)3