Search in sources :

Example 91 with Context

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

the class ContinuationsApiTest method testFunctionWithContinuations.

public void testFunctionWithContinuations() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        cx.evaluateString(globalScope, "function f(a) { return myObject.f(a); }", "function test source", 1, null);
        Function f = (Function) globalScope.get("f", globalScope);
        Object[] args = { 7 };
        cx.callFunctionWithContinuations(f, globalScope, args);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        Object applicationState = pending.getApplicationState();
        assertEquals(7, ((Number) applicationState).intValue());
        int saved = (Integer) applicationState;
        Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
        assertEquals(8, ((Number) result).intValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ContinuationPending(org.mozilla.javascript.ContinuationPending)

Example 92 with Context

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

the class Bug421071Test method compileScript.

private Script compileScript() {
    String scriptSource = "importPackage(java.util);\n" + "var searchmon = 3;\n" + "var searchday = 10;\n" + "var searchyear = 2008;\n" + "var searchwkday = 0;\n" + "\n" + "var myDate = Calendar.getInstance();\n // this is a java.util.Calendar" + "myDate.set(Calendar.MONTH, searchmon);\n" + "myDate.set(Calendar.DATE, searchday);\n" + "myDate.set(Calendar.YEAR, searchyear);\n" + "searchwkday.value = myDate.get(Calendar.DAY_OF_WEEK);";
    Script script;
    Context context = factory.enterContext();
    try {
        script = context.compileString(scriptSource, "testScript", 1, null);
        return script;
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script)

Example 93 with Context

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

the class Utils method runWithOptimizationLevel.

/**
	 * Runs the provided action at the given optimization level
	 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel) {
    final Context cx = contextFactory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        action.run(cx);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context)

Example 94 with Context

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

the class ComplianceTest method createTest.

private static Test createTest(final File testDir, final String name) {
    return new TestCase(name) {

        @Override
        public int countTestCases() {
            return 1;
        }

        @Override
        public void runBare() throws Throwable {
            final Context cx = Context.enter();
            try {
                cx.setOptimizationLevel(-1);
                final Scriptable scope = cx.initStandardObjects();
                ScriptableObject.putProperty(scope, "print", new Print(scope));
                createRequire(testDir, cx, scope).requireMain(cx, "program");
            } finally {
                Context.exit();
            }
        }
    };
}
Also used : Context(org.mozilla.javascript.Context) TestCase(junit.framework.TestCase) Scriptable(org.mozilla.javascript.Scriptable)

Example 95 with Context

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

the class RequireTest method testWithSandboxedRequire.

private void testWithSandboxedRequire(String moduleId) throws Exception {
    final Context cx = createContext();
    getSandboxedRequire(cx).requireMain(cx, moduleId);
}
Also used : Context(org.mozilla.javascript.Context)

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