Search in sources :

Example 81 with Context

use of org.mozilla.javascript.Context 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 82 with Context

use of org.mozilla.javascript.Context 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 83 with Context

use of org.mozilla.javascript.Context 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 84 with Context

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

the class GeneratedMethodNameTest method doTest.

public void doTest(final String scriptCode) throws Exception {
    final Context cx = ContextFactory.getGlobal().enterContext();
    try {
        Scriptable topScope = cx.initStandardObjects();
        topScope.put("javaNameGetter", topScope, new JavaNameGetter());
        Script script = cx.compileString(scriptCode, "myScript", 1, null);
        script.exec(cx, topScope);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) Scriptable(org.mozilla.javascript.Scriptable)

Example 85 with Context

use of org.mozilla.javascript.Context 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)152 Scriptable (org.mozilla.javascript.Scriptable)68 ScriptableObject (org.mozilla.javascript.ScriptableObject)62 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 Date (java.util.Date)5 HashSet (java.util.HashSet)5 List (java.util.List)5