Search in sources :

Example 1 with ScriptableInputStream

use of org.mozilla.javascript.serialize.ScriptableInputStream in project hackpad by dropbox.

the class ContinuationsApiTest method testSerializationWithContinuations.

public void testSerializationWithContinuations() throws IOException, ClassNotFoundException {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        cx.evaluateString(globalScope, "function f(a) { var k = myObject.f(a); var t = []; return k; }", "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) {
        // serialize
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ScriptableOutputStream sos = new ScriptableOutputStream(baos, globalScope);
        sos.writeObject(globalScope);
        sos.writeObject(pending.getContinuation());
        sos.close();
        baos.close();
        byte[] serializedData = baos.toByteArray();
        // deserialize
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
        ScriptableInputStream sis = new ScriptableInputStream(bais, globalScope);
        globalScope = (Scriptable) sis.readObject();
        Object continuation = sis.readObject();
        sis.close();
        bais.close();
        Object result = cx.resumeContinuation(continuation, globalScope, 8);
        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) ScriptableInputStream(org.mozilla.javascript.serialize.ScriptableInputStream) ScriptableOutputStream(org.mozilla.javascript.serialize.ScriptableOutputStream) Scriptable(org.mozilla.javascript.Scriptable)

Example 2 with ScriptableInputStream

use of org.mozilla.javascript.serialize.ScriptableInputStream in project BPjs by bThink-BGU.

the class ContinuationGames method main.

public static void main(String[] args) throws Exception {
    // Create a program
    BProgram bprog = new StringBProgram(SRC);
    // Run the top-level code (b-threads are registered but not yet run)
    BProgramSyncSnapshot cur = bprog.setup();
    // Run to first bsync
    cur = cur.start(ExecutorServiceMaker.makeWithName("TEST"));
    // Get a snapshot
    final BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
    System.out.println(snapshot);
    // Serialize snapshot
    byte[] serializedContinuationAndScope = null;
    Object bp = null;
    try {
        // need Javascript environment for this, even though we're not executing code per se.
        Context ctxt = Context.enter();
        // first, get bp out of the scope
        Object cnt = snapshot.getContinuation();
        final Scriptable scope = snapshot.getScope();
        for (Scriptable sc = scope; sc != null; sc = sc.getParentScope()) {
            System.out.println("SCOPE START");
            if (sc.has("bp", sc)) {
                bp = sc.get("bp", sc);
                sc.delete("bp");
                System.out.println("bp deleted.");
            }
            if (sc.has("j", sc)) {
                System.out.println("Found j:" + sc.get("j", sc));
            }
            System.out.println("SCOPE END\n");
        }
        // second, serialize
        final Scriptable topLevelScope = ctxt.initSafeStandardObjects();
        try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            ScriptableOutputStream outs = new ScriptableOutputStream(bytes, topLevelScope)) {
            outs.writeObject(cnt);
            outs.flush();
            serializedContinuationAndScope = bytes.toByteArray();
        }
        System.out.println("Seriazlied to " + serializedContinuationAndScope.length + " bytes.");
    } finally {
        Context.exit();
    }
    // Run the BThread a few times:
    try {
        Context ctxt = ContextFactory.getGlobal().enterContext();
        // must use interpreter mode
        ctxt.setOptimizationLevel(-1);
        final Scriptable topLevelScope = ctxt.initSafeStandardObjects();
        for (int i = 0; i < 10; i++) {
            try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(serializedContinuationAndScope), topLevelScope)) {
                // read cnt and scope
                Scriptable cnt2 = (Scriptable) sis.readObject();
                // re-add bp to the scope
                cnt2.getParentScope().put("bp", cnt2.getParentScope(), new BProgramJsProxy(bprog));
                // go - we can push whichever event we want.
                ctxt.resumeContinuation(cnt2, cnt2, new BEvent("e-" + i));
                // this extra run will use the same control flow, but the variable
                // values will be from the previous run. So don't do this :-)
                ctxt.resumeContinuation(cnt2, cnt2, new BEvent("arbitrary/" + i));
            }
        }
        // create three continuation objects that should be the same.
        Scriptable[] cnts = new Scriptable[3];
        for (int i = 0; i < 3; i++) {
            try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(serializedContinuationAndScope), topLevelScope)) {
                // read cnt and scope
                cnts[i] = (Scriptable) sis.readObject();
            }
        }
        System.out.println(cnts[0].equals(cnts[0]));
        System.out.println(cnts[0].equals(cnts[1]));
        System.out.println("continuationsEq = " + continuationEq((NativeContinuation) cnts[0], (NativeContinuation) cnts[1]));
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableInputStream(org.mozilla.javascript.serialize.ScriptableInputStream) ScriptableOutputStream(org.mozilla.javascript.serialize.ScriptableOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Scriptable(org.mozilla.javascript.Scriptable) BProgramJsProxy(il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) ByteArrayInputStream(java.io.ByteArrayInputStream) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)

Example 3 with ScriptableInputStream

use of org.mozilla.javascript.serialize.ScriptableInputStream in project BPjs by bThink-BGU.

the class BProgramSyncSnapshotIO method deserialize.

public BProgramSyncSnapshot deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    try {
        Context ctxt = ContextFactory.getGlobal().enterContext();
        // must use interpreter mode
        ctxt.setOptimizationLevel(-1);
        final ScriptableObject globalScope = ctxt.initStandardObjects();
        try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(bytes), globalScope)) {
            Header header = (Header) sis.readObject();
            Set<BThreadSyncSnapshot> bthreads = new HashSet<>(header.bthreadCount);
            for (int i = 0; i < header.bthreadCount; i++) {
                bthreads.add(readBThreadSnapshot(sis, globalScope));
            }
            List<BEvent> events = new ArrayList<>(header.externalEventCount);
            for (int i = 0; i < header.externalEventCount; i++) {
                events.add((BEvent) sis.readObject());
            }
            return new BProgramSyncSnapshot(bprogram, bthreads, events, header.fa);
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableInputStream(org.mozilla.javascript.serialize.ScriptableInputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) HashSet(java.util.HashSet)

Aggregations

Context (org.mozilla.javascript.Context)3 ScriptableInputStream (org.mozilla.javascript.serialize.ScriptableInputStream)3 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)2 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)2 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Scriptable (org.mozilla.javascript.Scriptable)2 ScriptableOutputStream (org.mozilla.javascript.serialize.ScriptableOutputStream)2 BProgramJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy)1 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)1 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ContinuationPending (org.mozilla.javascript.ContinuationPending)1 Function (org.mozilla.javascript.Function)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1