Search in sources :

Example 6 with ContextAction

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

the class TypeOfTest method testCustomizeTypeOf.

private void testCustomizeTypeOf(final String expected, final Scriptable obj) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context context) {
            final Scriptable scope = context.initStandardObjects();
            scope.put("myObj", scope, obj);
            return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
        }
    };
    doTest(expected, action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 7 with ContextAction

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

the class TypeOfTest method doTest.

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

        public Object run(final Context context) {
            final Scriptable scope = context.initStandardObjects();
            return context.evaluateString(scope, script, "test script", 1, null);
        }
    };
    doTest(expected, action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 8 with ContextAction

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

the class TypeOfTest method doTest.

private void doTest(final int optimizationLevel, final String expected, final ContextAction action) {
    Object o = new ContextFactory().call(new ContextAction() {

        public Object run(final Context context) {
            context.setOptimizationLevel(optimizationLevel);
            return Context.toString(action.run(context));
        }
    });
    assertEquals(expected, o);
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 9 with ContextAction

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

the class Utils method executeScript.

/**
	 * Execute the provided script in a fresh context as "myScript.js".
	 * @param script the script code
	 */
static void executeScript(final String script, final int optimizationLevel) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            return cx.evaluateString(scope, script, "myScript.js", 1, null);
        }
    };
    Utils.runWithOptimizationLevel(action, optimizationLevel);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 10 with ContextAction

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

the class WriteReadOnlyPropertyTest method testWriteReadOnly.

void testWriteReadOnly(final boolean acceptWriteReadOnly) throws Exception {
    final Method readMethod = Foo.class.getMethod("getMyProp", (Class[]) null);
    final Foo foo = new Foo("hello");
    foo.defineProperty("myProp", null, readMethod, null, ScriptableObject.EMPTY);
    final String script = "foo.myProp = 123; foo.myProp";
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final ScriptableObject top = cx.initStandardObjects();
            ScriptableObject.putProperty(top, "foo", foo);
            cx.evaluateString(top, script, "script", 0, null);
            return null;
        }
    };
    final ContextFactory contextFactory = new ContextFactory() {

        @Override
        protected boolean hasFeature(final Context cx, final int featureIndex) {
            if (Context.FEATURE_STRICT_MODE == featureIndex) {
                return !acceptWriteReadOnly;
            }
            return super.hasFeature(cx, featureIndex);
        }
    };
    contextFactory.call(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextFactory(org.mozilla.javascript.ContextFactory) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Method(java.lang.reflect.Method)

Aggregations

Context (org.mozilla.javascript.Context)23 ContextAction (org.mozilla.javascript.ContextAction)23 Scriptable (org.mozilla.javascript.Scriptable)12 ScriptableObject (org.mozilla.javascript.ScriptableObject)11 Function (org.mozilla.javascript.Function)5 ContextFactory (org.mozilla.javascript.ContextFactory)4 Test (org.junit.Test)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Script (org.mozilla.javascript.Script)2 ImmutableList (com.google.common.collect.ImmutableList)1 ResultBuilder (com.googlecode.jslint4java.JSLintResult.ResultBuilder)1 ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 BaseObjectColumnValueSelector (org.apache.druid.segment.BaseObjectColumnValueSelector)1 BaseFunction (org.mozilla.javascript.BaseFunction)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1 RhinoException (org.mozilla.javascript.RhinoException)1