-
Notifications
You must be signed in to change notification settings - Fork 8
/
ContinuationGames.java
192 lines (162 loc) · 7.95 KB
/
ContinuationGames.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* The MIT License
*
* Copyright 2017 michael.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package il.ac.bgu.cs.bp.bpjs.mains;
import il.ac.bgu.cs.bp.bpjs.BPjs;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.serialize.ScriptableInputStream;
import org.mozilla.javascript.serialize.ScriptableOutputStream;
import il.ac.bgu.cs.bp.bpjs.model.BProgram;
import il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot;
import il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot;
import il.ac.bgu.cs.bp.bpjs.model.StringBProgram;
import il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy;
import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
import il.ac.bgu.cs.bp.bpjs.model.BEvent;
import il.ac.bgu.cs.bp.bpjs.model.StorageModificationStrategy;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import org.mozilla.javascript.NativeContinuation;
import org.mozilla.javascript.ScriptRuntime;
/**
* Playing around with Rhino continuations. This file is for technical tests,
* and can be safely ignored by anyone not doing anything Rhino-related.
*
* @author michael
*/
public class ContinuationGames {
static final String SRC =
"gVar=1;\n"
+ "bp.registerBThread( \"bt\", function(){\n"
+ " bp.log.info(\"started\");"
+ " var bVar=1;"
+ " var evt = bp.sync({request: bp.Event(\"e\")});\n"
+ " bp.log.info('gVar:' + gVar + ' bVar:'+bVar + ' event.name:' + evt.name);"
+ " gVar = gVar+1;"
+ " bVar = bVar+1;"
+ "});";
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();
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("TEST");
// Run to first bp.sync
cur = cur.start(execSvc, StorageModificationStrategy.PASSTHROUGH);
// Get a snapshot
final BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
System.out.println(snapshot);
// Serialize snapshot
byte[] serializedContinuationAndScope = null;
try {
Context ctxt = BPjs.enterRhinoContext(); // need Javascript environment for this, even though we're not executing code per se.
// 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) ) {
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 {
execSvc.shutdown();
Context.exit();
}
// Run the BThread a few times:
try {
Context ctxt = BPjs.enterRhinoContext();
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();
}
}
static boolean continuationEq( NativeContinuation a, NativeContinuation b ) {
// same heap
if ( ! ScriptRuntime.shallowEq(a.getParentScope(), b.getParentScope()) ) {
Map<Object,Object> scopeA = scopeValues(a);
Map<Object,Object> scopeB = scopeValues(b);
System.out.println("scopeA = " + scopeA);
System.out.println("scopeB = " + scopeB);
System.out.println("scopeB.equals(scopeA) = " + scopeB.equals(scopeA));
return false;
}
System.out.println("Same parent scope.");
// same stack
System.out.println("implementation = " + a.getImplementation() );
// same pc
return true;
}
static Map<Object, Object> scopeValues(NativeContinuation nc ){
Map<Object, Object> retVal = new HashMap<>();
Scriptable current = nc;
// traverse prototype and parent chains, according to lookup algorithm.
for ( Object o : nc.getIds() ) {
retVal.put(o, nc.get(o));
}
return retVal;
}
}