Search in sources :

Example 86 with Context

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

the class StackTraceTest method runWithExpectedStackTrace.

private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            try {
                cx.evaluateString(scope, _source, "test.js", 0, null);
            } catch (final JavaScriptException e) {
                assertEquals(_expectedStackTrace, e.getScriptStackTrace());
                return null;
            }
            throw new RuntimeException("Exception expected!");
        }
    };
    Utils.runWithOptimizationLevel(action, -1);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 87 with Context

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

the class StrictModeApiTest method testStrictModeError.

public void testStrictModeError() {
    contextFactory = new MyContextFactory();
    Context cx = contextFactory.enterContext();
    try {
        global = cx.initStandardObjects();
        try {
            runScript("({}.nonexistent);");
            fail();
        } catch (EvaluatorException e) {
            assertTrue(e.getMessage().startsWith("Reference to undefined property"));
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) EvaluatorException(org.mozilla.javascript.EvaluatorException)

Example 88 with Context

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

the class Bug448816Test method setUp.

@SuppressWarnings("unchecked")
@Override
protected void setUp() {
    // set up a reference map
    reference = new LinkedHashMap<Object, Object>();
    reference.put("a", "a");
    reference.put("b", Boolean.TRUE);
    reference.put("c", new HashMap<Object, Object>());
    reference.put(new Integer(1), new Integer(42));
    // get a js object as map
    Context context = Context.enter();
    ScriptableObject scope = context.initStandardObjects();
    map = (Map<Object, Object>) context.evaluateString(scope, "({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});", "testsrc", 1, null);
    Context.exit();
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 89 with Context

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

the class Bug482203 method testJavaApi.

public void testJavaApi() throws Exception {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileReader(new InputStreamReader(Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        cx.executeScriptWithContinuations(script, scope);
        for (; ; ) {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if (cont == null) {
                break;
            }
            cx.resumeContinuation(cont, scope, null);
        }
    } finally {
        Context.exit();
    }
}
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)

Example 90 with Context

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

the class ClassShutterExceptionTest method helper.

public void helper(String source) {
    Context cx = Context.enter();
    Context.ClassShutterSetter setter = cx.getClassShutterSetter();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        if (setter == null) {
            setter = classShutterSetter;
        } else {
            classShutterSetter = setter;
        }
        setter.setClassShutter(new OpaqueShutter());
        cx.evaluateString(globalScope, source, "test source", 1, null);
    } finally {
        setter.setClassShutter(null);
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) 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