Search in sources :

Example 36 with Context

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

the class RhinoJavaScriptEngineFactory method getRootScope.

@SuppressWarnings("unchecked")
private Scriptable getRootScope() {
    if (rootScope == null) {
        final Context rhinoContext = Context.enter();
        try {
            rhinoContext.setOptimizationLevel(optimizationLevel);
            Scriptable tmpScope = rhinoContext.initStandardObjects(new ImporterTopLevel(rhinoContext), false);
            // default classes
            addHostObjects(tmpScope, (Class<? extends ScriptableObject>[]) HOSTOBJECT_CLASSES);
            // provided classes
            for (RhinoHostObjectProvider provider : hostObjectProvider) {
                addHostObjects(tmpScope, provider.getHostObjectClasses());
                addImportedClasses(rhinoContext, tmpScope, provider.getImportedClasses());
                addImportedPackages(rhinoContext, tmpScope, provider.getImportedPackages());
            }
            // only assign the root scope when complete set up
            rootScope = tmpScope;
        } finally {
            // ensure the context is exited after setting up the
            // the new root scope
            Context.exit();
        }
    }
    return rootScope;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) BundleContext(org.osgi.framework.BundleContext) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoHostObjectProvider(org.apache.sling.scripting.javascript.RhinoHostObjectProvider) NativeJavaClass(org.mozilla.javascript.NativeJavaClass) Scriptable(org.mozilla.javascript.Scriptable)

Example 37 with Context

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

the class JsUtils method callFn.

public static Object callFn(Function function, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    boolean exitContext = false;
    if (Context.getCurrentContext() == null) {
        Context.enter();
        exitContext = true;
    }
    Context context = (cx == null) ? Context.getCurrentContext() : cx;
    Object result = function.call(context, scope, thisObj, args);
    if (exitContext) {
        Context.exit();
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context)

Example 38 with Context

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

the class JavascriptEngine method execute.

/** Execute supplied code against supplied data, 
	 * 	see JavascriptEngineTest for examples */
public String execute(String code, String jsonData) throws IOException {
    final String jsCode = "data=" + jsonData + ";\n" + code;
    final Context rhinoContext = Context.enter();
    final ScriptableObject scope = rhinoContext.initStandardObjects();
    // execute the script, out script variable maps to sw
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw, true);
    ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
    final int lineNumber = 1;
    final Object securityDomain = null;
    try {
        rhinoContext.evaluateString(scope, jsCode, getClass().getSimpleName(), lineNumber, securityDomain);
    } catch (Exception e) {
        final IOException ioe = new IOException("While executing [" + code + "]:" + e);
        ioe.initCause(e);
        throw ioe;
    }
    // check script output
    pw.flush();
    return sw.toString().trim();
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) StringWriter(java.io.StringWriter) ScriptableObject(org.mozilla.javascript.ScriptableObject) IOException(java.io.IOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 39 with Context

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

the class RhinoWorkerUtils method parseRhino.

public static <T> T parseRhino(File rhinoScript, ScopeOperation<T> operation) {
    Context context = Context.enter();
    try {
        operation.initContext(context);
        Scriptable scope = context.initStandardObjects();
        String printFunction = "function print(message) {}";
        context.evaluateString(scope, printFunction, "print", 1, null);
        context.evaluateString(scope, readFile(rhinoScript, "UTF-8"), rhinoScript.getName(), 1, null);
        return operation.action(scope, context);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 40 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) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) 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