Search in sources :

Example 1 with BProgramJsProxy

use of il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy in project BPjs by bThink-BGU.

the class BProgram method initProgramScope.

private void initProgramScope(Context cx) {
    // load and execute globalScopeInit.js
    ImporterTopLevel importer = new ImporterTopLevel(cx);
    programScope = cx.initStandardObjects(importer);
    BProgramJsProxy proxy = new BProgramJsProxy(this);
    programScope.put("bp", programScope, Context.javaToJS(proxy, programScope));
    // evaluateResource("globalScopeInit.js");// <-- Currently not needed. Leaving in as we might need it soon.
    initialScopeValues.entrySet().forEach(e -> putInGlobalScope(e.getKey(), e.getValue()));
    initialScopeValues = null;
}
Also used : ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) BProgramJsProxy(il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy)

Example 2 with BProgramJsProxy

use of il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy 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 BProgramJsProxy

use of il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy 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

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