1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.zel.vm;
20
21
22 import java.io.InputStream;
23 import java.io.PrintStream;
24 import java.util.Map;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.ThreadPoolExecutor;
27
28
29
30
31
32
33
34 public class ExecutionContext
35 implements Cloneable
36 {
37
38 public ThreadPoolExecutor execService;
39
40 public ResultCache resultCache;
41
42
43
44
45 public Map heap = null;
46
47
48
49
50 public Map memContext = null;
51
52
53
54
55 public Program code = null;
56
57
58
59
60 public int ip = 0;
61
62
63
64
65 public boolean terminated = false;
66
67
68
69
70 public SimpleStack stack;
71
72
73
74
75 public SimpleStack memContextStack = new SimpleStack(32);
76
77
78
79
80 public InputStream in = null;
81
82
83
84
85 public PrintStream out = null;
86
87
88
89
90 public PrintStream err = null;
91
92
93
94
95
96 public ExecutionContext parent = null;
97
98
99
100
101
102 private ExecutionContext()
103 {
104 this.in = System.in;
105 this.out = System.out;
106 this.err = System.err;
107 }
108
109
110
111
112
113
114
115 public ExecutionContext(Program program, java.util.Map heap, ThreadPoolExecutor execService)
116 {
117 this();
118 code = program;
119 this.heap = heap;
120 this.memContext = heap;
121 this.execService= execService;
122 init();
123 }
124
125
126 private void init()
127 {
128 this.stack = new SimpleStack(32);
129 if (execService == null)
130 {
131 this.stack = new SimpleStack(32);
132 this.resultCache = new SimpleResultCache();
133 }
134 else
135 {
136 this.resultCache = new SynchronizedResultCache();
137 }
138 }
139
140
141
142
143
144
145
146
147
148
149 public ExecutionContext(Program program, java.util.Map heap, InputStream in, PrintStream out, PrintStream err, ThreadPoolExecutor execService)
150 {
151 code = program;
152 this.heap = heap;
153 this.memContext = heap;
154 this.in = in;
155 this.out = out;
156 this.err = err;
157 this.execService= execService;
158 init();
159 }
160
161
162
163
164
165
166 public Object popSyncStackVal()
167 {
168 Object result = this.stack.pop();
169 if (result instanceof Future<?>)
170 try
171 {
172 return ((Future<Object>) result).get();
173 } catch (Exception ex)
174 {
175 throw new RuntimeException(ex);
176 }
177 else return result;
178 }
179
180
181
182 public ExecutionContext getSubProgramContext(Program program)
183 {
184 ExecutionContext ec;
185 try
186 {
187 ec = (ExecutionContext) this.clone();
188 } catch (CloneNotSupportedException ex)
189 {
190 throw new RuntimeException("Execution Context must be clonable", ex);
191 }
192 ec.code = program;
193 ec.ip =0;
194 ec.stack = new SimpleStack(32);
195 ec.parent = this;
196 return ec;
197 }
198
199
200 public Map newMem()
201 {
202 try
203 {
204 return this.memContext.getClass().newInstance();
205 } catch (InstantiationException ex)
206 {
207 throw new RuntimeException(ex);
208 } catch (IllegalAccessException ex)
209 {
210 throw new RuntimeException(ex);
211 }
212 }
213
214
215
216
217
218 @Override
219 public String toString()
220 {
221 StringBuilder result = new StringBuilder();
222 result.append("\nStack:");
223 result.append(stack.toString());
224 result.append("\nMemCtx:");
225 result.append(memContext);
226 result.append("\nHeap:");
227 result.append(heap);
228 result.append("\nProgram:");
229 result.append(code);
230 result.append("\nInstruction pointer: " + ip);
231 if (parent != null)
232 result.append("\n Called from: ").append(parent);
233 return result.toString();
234 }
235 }