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();
}
}
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();
}
}
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();
}
}
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);
}
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();
}
}
Aggregations