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;
}
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();
}
}
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;
}
}
Aggregations