Search in sources :

Example 16 with ContextAction

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

the class DeletePropertyTest method testDeletePropInPrototype.

/**
	 * delete should not delete anything in the prototype chain.
	 */
@Test
public void testDeletePropInPrototype() throws Exception {
    final String script = "Array.prototype.foo = function() {};\n" + "Array.prototype[1] = function() {};\n" + "var t = [];\n" + "[].foo();\n" + "for (var i in t) delete t[i];\n" + "[].foo();\n" + "[][1]();\n";
    final ContextAction action = new ContextAction() {

        public Object run(final Context _cx) {
            final ScriptableObject scope = _cx.initStandardObjects();
            final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
            return null;
        }
    };
    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) Test(org.junit.Test)

Example 17 with ContextAction

use of org.mozilla.javascript.ContextAction 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 18 with ContextAction

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

the class FunctionTest method assertEvaluates.

private void assertEvaluates(final Object expected, final String source) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
            assertEquals(expected, rep);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 19 with ContextAction

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

the class GeneratedClassNameTest method doTest.

private void doTest(final String expectedName, final String scriptName) throws Exception {
    final Script script = (Script) ContextFactory.getGlobal().call(new ContextAction() {

        public Object run(final Context context) {
            return context.compileString("var f = 1", scriptName, 1, null);
        }
    });
    // remove serial number
    String name = script.getClass().getSimpleName();
    assertEquals(expectedName, name.substring(0, name.lastIndexOf('_')));
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContextAction(org.mozilla.javascript.ContextAction)

Example 20 with ContextAction

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

the class PrimitiveTypeScopeResolutionTest method testWithTwoScopes.

private void testWithTwoScopes(final String scriptScope1, final String scriptScope2) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final Scriptable scope1 = cx.initStandardObjects(new MySimpleScriptableObject("scope1"));
            final Scriptable scope2 = cx.initStandardObjects(new MySimpleScriptableObject("scope2"));
            cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
            scope1.put("scope2", scope1, scope2);
            return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

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