Search in sources :

Example 41 with Context

use of org.mozilla.javascript.Context in project gocd by gocd.

the class JsonTester method javascriptParse.

public static Object javascriptParse(String other) {
    try {
        Context ctx = Context.enter();
        ScriptableObject scope = ctx.initStandardObjects();
        return parse(ctx.evaluateString(scope, "json = " + other, "JsonTester", 1, null));
    } catch (Exception evaluator) {
        evaluator.printStackTrace();
        System.err.println("Invalid json:\n" + other);
        throw bomb("Invalid javascript", evaluator);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) JSONException(org.json.JSONException)

Example 42 with Context

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

the class AsyncExtractor method decodeJSPromise.

private void decodeJSPromise(final Scriptable promise, final UnaryCallback callback) {
    try {
        Context context = Context.enter();
        final AsyncContainer errorContainer = new AsyncContainer();
        final Function errorHandler = createErrorHandler(errorContainer);
        final Function successHandler = convertCallback(callback);
        EventLoopInterop.schedule(context, new Runnable() {

            @Override
            public void run() {
                ScriptableObject.callMethod(promise, THEN_METHOD, new Object[] { successHandler, errorHandler });
            }
        });
        if (errorContainer.isCompleted()) {
            throw new SightlyException("Promise has completed with failure: " + Context.toString(errorContainer.getResult()));
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) SightlyException(org.apache.sling.scripting.sightly.SightlyException) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 43 with Context

use of org.mozilla.javascript.Context 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 44 with Context

use of org.mozilla.javascript.Context 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 45 with Context

use of org.mozilla.javascript.Context 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)

Aggregations

Context (org.mozilla.javascript.Context)153 Scriptable (org.mozilla.javascript.Scriptable)69 ScriptableObject (org.mozilla.javascript.ScriptableObject)63 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 Date (java.util.Date)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 HashSet (java.util.HashSet)5 List (java.util.List)5