Search in sources :

Example 41 with ScriptableObject

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

the class SlingContext method initStandardObjects.

@Override
public ScriptableObject initStandardObjects(ScriptableObject scope, boolean sealed) {
    ScriptableObject rootScope = super.initStandardObjects(scope, sealed);
    // prepare the ImporterToplevel host object because it will be
    // used as top level scope for the RhinoJavaScriptEngine but is
    // not initialized with the rest of the standard objects
    ImporterTopLevel.init(this, rootScope, sealed);
    // add Sling global objects
    SlingGlobal.init(rootScope, sealed);
    return rootScope;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 42 with ScriptableObject

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

the class ScriptEngineHelper method eval.

public Object eval(String javascriptCode, Map<String, Object> data, final StringWriter sw) throws ScriptException {
    final PrintWriter pw = new PrintWriter(sw, true);
    ScriptContext ctx = new SimpleScriptContext();
    final Bindings b = new SimpleBindings();
    b.put("out", pw);
    if (data != null) {
        for (Map.Entry<String, Object> e : data.entrySet()) {
            b.put(e.getKey(), e.getValue());
        }
    }
    ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
    ctx.setWriter(sw);
    ctx.setErrorWriter(new OutputStreamWriter(System.err));
    Object result = getEngine().eval(javascriptCode, ctx);
    if (result instanceof Wrapper) {
        result = ((Wrapper) result).unwrap();
    }
    if (result instanceof ScriptableObject) {
        Context.enter();
        try {
            result = ((ScriptableObject) result).getDefaultValue(null);
        } finally {
            Context.exit();
        }
    }
    return result;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) OutputStreamWriter(java.io.OutputStreamWriter) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 43 with ScriptableObject

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

the class AsyncExtractor method extract.

public void extract(Object jsObj, UnaryCallback unaryCallback) {
    if (!isPromise(jsObj)) {
        unaryCallback.invoke(jsObj);
    }
    if (jsObj instanceof AsyncContainer) {
        ((AsyncContainer) jsObj).addListener(unaryCallback);
    }
    if (jsObj instanceof ScriptableObject) {
        ScriptableObject scriptableObject = (ScriptableObject) jsObj;
        decodeJSPromise(scriptableObject, unaryCallback);
    }
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 44 with ScriptableObject

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

the class SlyBindingsValuesProvider method createQScope.

private ScriptableObject createQScope() {
    Context context = Context.enter();
    try {
        ScriptableObject scope = context.initStandardObjects();
        ScriptableObject.putProperty(scope, Variables.SET_IMMEDIATE, TimingFunction.INSTANCE);
        ScriptableObject.putProperty(scope, Variables.SET_TIMEOUT, TimingFunction.INSTANCE);
        return scope;
    } finally {
        Context.exit();
    }
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

ScriptableObject (org.mozilla.javascript.ScriptableObject)44 Context (org.mozilla.javascript.Context)20 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)8 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)8 ContextAction (org.mozilla.javascript.ContextAction)6 ContextFactory (org.mozilla.javascript.ContextFactory)6 Scriptable (org.mozilla.javascript.Scriptable)6 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 IOException (java.io.IOException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 ServletContext (javax.servlet.ServletContext)3 Function (org.mozilla.javascript.Function)3 Function (com.google.common.base.Function)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2