Search in sources :

Example 26 with Context

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

the class Bug482203 method testJsApi.

public void testJsApi() throws Exception {
    Context cx = Context.enter();
    cx.setOptimizationLevel(-1);
    Script script = cx.compileReader(new InputStreamReader(Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null);
    Scriptable scope = cx.initStandardObjects();
    script.exec(cx, scope);
    for (; ; ) {
        Object cont = ScriptableObject.getProperty(scope, "c");
        if (cont == null) {
            break;
        }
        ((Callable) cont).call(cx, scope, scope, new Object[] { null });
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) Callable(org.mozilla.javascript.Callable)

Example 27 with Context

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

the class ContextFactoryTest method testCustomContextFactory.

public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
    // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
    /* TODO(stevey): fix this functionality in parser
            Object result = cx.evaluateString(globalScope,
                    "var obj = {};" +
                    "function obj.foo() { return 'bar'; }" +
                    "obj.foo();",
                    "test source", 1, null);
            assertEquals("bar", result);
            */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 28 with Context

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

the class ContinuationsApiTest method testScriptWithMultipleContinuations.

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

Example 29 with Context

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

the class ContinuationsApiTest method setUp.

@Override
public void setUp() {
    Context cx = Context.enter();
    try {
        globalScope = cx.initStandardObjects();
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        globalScope.put("myObject", globalScope, Context.javaToJS(new MyClass(), globalScope));
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context)

Example 30 with Context

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

the class ContinuationsApiTest method testScriptWithContinuations.

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

Aggregations

Context (org.mozilla.javascript.Context)153 Scriptable (org.mozilla.javascript.Scriptable)69 ScriptableObject (org.mozilla.javascript.ScriptableObject)63 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 Date (java.util.Date)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 HashSet (java.util.HashSet)5 List (java.util.List)5