use of org.mozilla.javascript.ContinuationPending 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();
}
}
use of org.mozilla.javascript.ContinuationPending in project BPjs by bThink-BGU.
the class BProgramSyncSnapshot method handleInterrupts.
private void handleInterrupts(BEvent anEvent, Iterable<BProgramRunnerListener> listeners, BProgram bprog, Context ctxt) {
Set<BThreadSyncSnapshot> interrupted = threadSnapshots.stream().filter(bt -> bt.getBSyncStatement().getInterrupt().contains(anEvent)).collect(toSet());
if (!interrupted.isEmpty()) {
threadSnapshots.removeAll(interrupted);
interrupted.forEach(bt -> {
listeners.forEach(l -> l.bthreadRemoved(bprog, bt));
bt.getInterrupt().ifPresent(func -> {
final Scriptable scope = bt.getScope();
// can't call bsync from a break handler.
scope.delete("bsync");
try {
ctxt.callFunctionWithContinuations(func, scope, new Object[] { anEvent });
} catch (ContinuationPending ise) {
throw new BProgramException("Cannot call bsync from a break-upon handler. Please consider pushing an external event.");
}
});
});
}
}
use of org.mozilla.javascript.ContinuationPending in project BPjs by bThink-BGU.
the class BProgramJsProxy method captureBThreadState.
private void captureBThreadState(BSyncStatement stmt) throws ContinuationPending {
ContinuationPending capturedContinuation = Context.getCurrentContext().captureContinuation();
capturedContinuation.setApplicationState(stmt);
throw capturedContinuation;
}
Aggregations