Search in sources :

Example 11 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project stetho by facebook.

the class JsRuntimeReplFactoryBuilder method importVariables.

private void importVariables(@NonNull ScriptableObject scope) throws StethoJsException {
    // Define the variables
    for (Map.Entry<String, Object> entrySet : mVariables.entrySet()) {
        String varName = entrySet.getKey();
        Object varValue = entrySet.getValue();
        try {
            Object jsValue;
            if (varValue instanceof Scriptable || varValue instanceof Undefined) {
                jsValue = varValue;
            } else {
                jsValue = Context.javaToJS(varValue, scope);
            }
            ScriptableObject.putProperty(scope, varName, jsValue);
        } catch (Exception e) {
            throw new StethoJsException(e, "Failed to setup variable: %s", varName);
        }
    }
}
Also used : Undefined(org.mozilla.javascript.Undefined) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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 13 with ScriptableObject

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

the class JsonTester method parseList.

private static List parseList(ScriptableObject o) {
    List jsonList = new ArrayList();
    for (Object basicId : o.getIds()) {
        Integer id = (Integer) basicId;
        jsonList.add(parse(o.get(id, o)));
    }
    return jsonList;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 14 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.

the class WebAppSessionListener method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();
    List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS);
    if (jsListeners == null) {
        return;
    }
    JaggeryContext shared = WebAppManager.sharedJaggeryContext(ctx);
    Context cx = shared.getEngine().enterContext();
    JaggeryContext context = CommonManager.getJaggeryContext();
    if (CommonManager.getJaggeryContext() == null) {
        context = WebAppManager.clonedJaggeryContext(ctx);
        CommonManager.setJaggeryContext(context);
    }
    RhinoEngine engine = context.getEngine();
    ScriptableObject clonedScope = context.getScope();
    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(cx.newObject(clonedScope, "Session", new Object[] { httpSessionEvent.getSession() }));
    session.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(clonedScope, session);
    for (Object jsListener : jsListeners) {
        CommonManager.getCallstack(context).push((String) jsListener);
        try {
            ScriptReader sr = new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {

                @Override
                protected void build() throws IOException {
                    try {
                        sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                    } catch (ScriptException e) {
                        throw new IOException(e);
                    }
                }
            };
            engine.exec(sr, clonedScope, null);
        } catch (ScriptException e) {
            log.error(e.getMessage(), e);
        } finally {
            CommonManager.getCallstack(context).pop();
        }
    }
    Context.exit();
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) Context(org.mozilla.javascript.Context) ServletContext(javax.servlet.ServletContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) IOException(java.io.IOException) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) List(java.util.List) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 15 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.

the class CommandLineExecutor method parseJaggeryScript.

/**
     * Parse Jaggery scripts resides in the file path
     *
     * @param fileURL url of the file
     */
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
static void parseJaggeryScript(final String fileURL) {
    FileInputStream fstream = null;
    try {
        //Initialize the Rhino context
        RhinoEngine.enterGlobalContext();
        fstream = new FileInputStream(fileURL);
        final RhinoEngine engine = CommandLineManager.getCommandLineEngine();
        final ScriptableObject scope = engine.getRuntimeScope();
        //initialize JaggeryContext
        final JaggeryContext jaggeryContext = new JaggeryContext();
        jaggeryContext.setTenantDomain(DEFAULT_TENANTDOMAIN);
        jaggeryContext.setEngine(engine);
        jaggeryContext.setScope(scope);
        jaggeryContext.addProperty(CommonManager.JAGGERY_OUTPUT_STREAM, System.out);
        RhinoEngine.putContextProperty("jaggeryContext", jaggeryContext);
        //Parsing the script
        final Reader source = new ScriptReader(new BufferedInputStream(fstream));
        out.println("\n");
        ShellUtilityService.initializeUtilityServices();
        engine.exec(source, scope, null);
        ShellUtilityService.destroyUtilityServices();
        out.flush();
        out.println("\n");
    } catch (Exception e) {
        out.println("\n");
        out.println("Error: " + e.getMessage());
        out.println("\n");
    } finally {
        if (fstream != null) {
            try {
                fstream.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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