Search in sources :

Example 6 with ContinuationPending

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ContinuationPending(org.mozilla.javascript.ContinuationPending)

Example 7 with ContinuationPending

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.");
                }
            });
        });
    }
}
Also used : ResumeBThread(il.ac.bgu.cs.bp.bpjs.execution.tasks.ResumeBThread) Context(org.mozilla.javascript.Context) BProgramException(il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException) ContinuationPending(org.mozilla.javascript.ContinuationPending) Set(java.util.Set) Logger(java.util.logging.Logger) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Future(java.util.concurrent.Future) Collectors.toList(java.util.stream.Collectors.toList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) StartBThread(il.ac.bgu.cs.bp.bpjs.execution.tasks.StartBThread) Scriptable(org.mozilla.javascript.Scriptable) BProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener) Collections(java.util.Collections) Collectors.toSet(java.util.stream.Collectors.toSet) BPEngineTask(il.ac.bgu.cs.bp.bpjs.execution.tasks.BPEngineTask) ExecutorService(java.util.concurrent.ExecutorService) ContinuationPending(org.mozilla.javascript.ContinuationPending) Scriptable(org.mozilla.javascript.Scriptable) BProgramException(il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException)

Example 8 with ContinuationPending

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;
}
Also used : ContinuationPending(org.mozilla.javascript.ContinuationPending)

Aggregations

ContinuationPending (org.mozilla.javascript.ContinuationPending)8 Context (org.mozilla.javascript.Context)6 Script (org.mozilla.javascript.Script)3 Function (org.mozilla.javascript.Function)2 Scriptable (org.mozilla.javascript.Scriptable)2 BProgramException (il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException)1 BProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener)1 BPEngineTask (il.ac.bgu.cs.bp.bpjs.execution.tasks.BPEngineTask)1 ResumeBThread (il.ac.bgu.cs.bp.bpjs.execution.tasks.ResumeBThread)1 StartBThread (il.ac.bgu.cs.bp.bpjs.execution.tasks.StartBThread)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1