Search in sources :

Example 76 with Context

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

the class ContinuationsApiTest method testScriptWithNestedContinuations.

public void testScriptWithNestedContinuations() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString("myObject.g( myObject.f(1) ) + 2;", "test source", 1, null);
        cx.executeScriptWithContinuations(script, globalScope);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        try {
            Object applicationState = pending.getApplicationState();
            assertEquals(new Integer(1), applicationState);
            int saved = (Integer) applicationState;
            cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
            fail("Should throw another ContinuationPending");
        } catch (ContinuationPending pending2) {
            Object applicationState2 = pending2.getApplicationState();
            assertEquals(new Integer(4), applicationState2);
            int saved2 = (Integer) applicationState2;
            Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 2);
            assertEquals(8, ((Number) result2).intValue());
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContinuationPending(org.mozilla.javascript.ContinuationPending)

Example 77 with Context

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

the class ContinuationsApiTest method testErrorOnEvalCall.

/**
   * Since a continuation can only capture JavaScript frames and not Java
   * frames, ensure that Rhino throws an exception when the JavaScript frames
   * don't reach all the way to the code called by
   * executeScriptWithContinuations or callFunctionWithContinuations.
   */
public void testErrorOnEvalCall() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString("eval('myObject.f(3);');", "test source", 1, null);
        cx.executeScriptWithContinuations(script, globalScope);
        fail("Should throw IllegalStateException");
    } catch (WrappedException we) {
        Throwable t = we.getWrappedException();
        assertTrue(t instanceof IllegalStateException);
        assertTrue(t.getMessage().startsWith("Cannot capture continuation"));
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) WrappedException(org.mozilla.javascript.WrappedException)

Example 78 with Context

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

the class CustomSetterAcceptNullScriptableTest method testSetNullForScriptableSetter.

public void testSetNullForScriptableSetter() throws Exception {
    final String scriptCode = "foo.myProp = new Foo2();\n" + "foo.myProp = null;";
    final ContextFactory factory = new ContextFactory();
    final Context cx = factory.enterContext();
    try {
        final ScriptableObject topScope = cx.initStandardObjects();
        final Foo foo = new Foo();
        // define custom setter method
        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);
        topScope.put("foo", topScope, foo);
        ScriptableObject.defineClass(topScope, Foo2.class);
        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) Method(java.lang.reflect.Method)

Example 79 with Context

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

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

the class DoctestsTest method runDoctest.

@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " + testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
        System.out.println(name + "(" + optimizationLevel + "): FAILED due to " + ex);
        throw ex;
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) Global(org.mozilla.javascript.tools.shell.Global) IOException(java.io.IOException)

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