Search in sources :

Example 26 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project hackpad by dropbox.

the class ErrorPropertiesTest method testIt.

private void testIt(final String script, final Object expected) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            try {
                final ScriptableObject scope = cx.initStandardObjects();
                final Object o = cx.evaluateString(scope, script, "myScript.js", 1, null);
                Assert.assertEquals(expected, o);
                return o;
            } catch (final RuntimeException e) {
                throw e;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException)

Example 27 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project hackpad by dropbox.

the class Bug448816Test method setUp.

@SuppressWarnings("unchecked")
@Override
protected void setUp() {
    // set up a reference map
    reference = new LinkedHashMap<Object, Object>();
    reference.put("a", "a");
    reference.put("b", Boolean.TRUE);
    reference.put("c", new HashMap<Object, Object>());
    reference.put(new Integer(1), new Integer(42));
    // get a js object as map
    Context context = Context.enter();
    ScriptableObject scope = context.initStandardObjects();
    map = (Map<Object, Object>) context.evaluateString(scope, "({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});", "testsrc", 1, null);
    Context.exit();
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 28 with ScriptableObject

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

the class JsRuntimeReplFactoryBuilder method initJsScope.

@NonNull
private ScriptableObject initJsScope(@NonNull Context jsContext) {
    // Set the main Rhino goodies
    ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext);
    ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false);
    ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope));
    try {
        importClasses(jsContext, scope);
        importPackages(jsContext, scope);
        importConsole(scope);
        importVariables(scope);
        importFunctions(scope);
    } catch (StethoJsException e) {
        String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e));
        LogUtil.e(e, message);
        CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message);
    }
    return scope;
}
Also used : ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) ScriptableObject(org.mozilla.javascript.ScriptableObject) NonNull(android.support.annotation.NonNull)

Example 29 with ScriptableObject

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

the class JsonTester method parseMap.

private static Map<String, Object> parseMap(ScriptableObject o) {
    Map<String, Object> jsonMap = new LinkedHashMap<>();
    for (Object basicId : o.getIds()) {
        String id = valueOf(basicId);
        jsonMap.put(id, parse(o.get(id, o)));
    }
    return jsonMap;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with ScriptableObject

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

the class JSLintBuilder method fromReader.

/**
     * Initialize the scope with an arbitrary jslint.
     *
     * @param reader
     *            an input source providing jslint.js.
     * @param name
     *            the name of the resource backed by the reader
     * @return a configured {@link JSLint}
     * @throws IOException
     *             if there are any problems reading from {@code reader} .
     */
@NeedsContext
public JSLint fromReader(Reader reader, String name) throws IOException {
    try {
        Context cx = contextFactory.enterContext();
        ScriptableObject scope = cx.initStandardObjects();
        cx.evaluateReader(scope, reader, name, 1, null);
        Function lintFunc = (Function) scope.get("JSLINT", scope);
        return new JSLint(contextFactory, lintFunc);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) 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