Search in sources :

Example 11 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot 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)

Example 12 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot in project BPjs by bThink-BGU.

the class BProgramSyncSnapshotIO method serialize.

public byte[] serialize(BProgramSyncSnapshot bpss) throws IOException {
    try {
        // need Javascript environment for
        Context ctxt = Context.enter();
        // this, even though we're not
        // executing code per se.
        final ScriptableObject globalScope = ctxt.initStandardObjects();
        try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            ObjectOutputStream outs = new ObjectOutputStream(bytes)) {
            outs.writeObject(new Header(bpss.getBThreadSnapshots().size(), bpss.getExternalEvents().size(), bpss.getFailedAssertion()));
            for (BThreadSyncSnapshot bss : bpss.getBThreadSnapshots()) {
                writeBThreadSnapshot(bss, outs, globalScope);
            }
            for (BEvent ee : bpss.getExternalEvents()) {
                outs.writeObject(ee);
            }
            outs.flush();
            return bytes.toByteArray();
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) ObjectOutputStream(java.io.ObjectOutputStream) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)

Example 13 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot in project BPjs by bThink-BGU.

the class BProgramSyncSnapshotIO method readBThreadSnapshot.

private BThreadSyncSnapshot readBThreadSnapshot(ScriptableInputStream sis, ScriptableObject scope) throws IOException, ClassNotFoundException {
    String name = (String) sis.readObject();
    byte[] contBytes = (byte[]) sis.readObject();
    final BThreadJsProxy btProxy = new BThreadJsProxy();
    final BProgramJsProxy bpProxy = new BProgramJsProxy(bprogram);
    StubProvider stubPrv = (StreamObjectStub stub) -> {
        if (stub == StreamObjectStub.BP_PROXY) {
            return bpProxy;
        }
        if (stub == StreamObjectStub.BT_PROXY) {
            return btProxy;
        }
        throw new IllegalArgumentException("Unknown stub " + stub);
    };
    try (ByteArrayInputStream inBytes = new ByteArrayInputStream(contBytes);
        BThreadSyncSnapshotInputStream bssis = new BThreadSyncSnapshotInputStream(inBytes, scope, stubPrv)) {
        Scriptable btScope = (Scriptable) bssis.readObject();
        Function entryPoint = (Function) bssis.readObject();
        Function interruptHandler = (Function) bssis.readObject();
        BSyncStatement stmt = (BSyncStatement) bssis.readObject();
        Object cont = bssis.readObject();
        final BThreadSyncSnapshot bThreadSyncSnapshot = new BThreadSyncSnapshot(name, entryPoint, interruptHandler, btScope, cont, stmt);
        btProxy.setBThread(bThreadSyncSnapshot);
        return bThreadSyncSnapshot;
    }
}
Also used : BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) BThreadJsProxy(il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BThreadJsProxy) Scriptable(org.mozilla.javascript.Scriptable) BProgramJsProxy(il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy) Function(org.mozilla.javascript.Function) ByteArrayInputStream(java.io.ByteArrayInputStream) ScriptableObject(org.mozilla.javascript.ScriptableObject) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)

Aggregations

BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)13 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)9 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)9 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)9 Test (org.junit.Test)9 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)7 NativeContinuation (org.mozilla.javascript.NativeContinuation)7 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Context (org.mozilla.javascript.Context)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 BProgramJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Scriptable (org.mozilla.javascript.Scriptable)2 ScriptableInputStream (org.mozilla.javascript.serialize.ScriptableInputStream)2 BThreadJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BThreadJsProxy)1 BSyncStatement (il.ac.bgu.cs.bp.bpjs.model.BSyncStatement)1 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1