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