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