-
Notifications
You must be signed in to change notification settings - Fork 130
/
GlobalContext.java
311 lines (255 loc) · 9.6 KB
/
GlobalContext.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package org.perl6.nqp.runtime;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.WeakHashMap;
import org.perl6.nqp.sixmodel.CodePairContainerConfigurer;
import org.perl6.nqp.sixmodel.ContainerConfigurer;
import org.perl6.nqp.sixmodel.KnowHOWBootstrapper;
import org.perl6.nqp.sixmodel.SerializationContext;
import org.perl6.nqp.sixmodel.SixModelObject;
import org.perl6.nqp.sixmodel.reprs.CallCaptureInstance;
public class GlobalContext {
/**
* The KnowHOW.
*/
public SixModelObject KnowHOW;
/**
* The KnowHOWAttribute.
*/
public SixModelObject KnowHOWAttribute;
/**
* BOOTArray type; a basic, method-less type with the VMArray REPR.
*/
public SixModelObject BOOTArray;
/**
* BOOTHash type; a basic, method-less type with the VMHash REPR.
*/
public SixModelObject BOOTHash;
/**
* BOOTIter type; a basic, method-less type with the VMIter REPR.
*/
public SixModelObject BOOTIter;
/**
* BOOTInt type; a basic, method-less type with the P6int REPR.
*/
public SixModelObject BOOTInt;
/**
* BOOTNum type; a basic, method-less type with the P6num REPR.
*/
public SixModelObject BOOTNum;
/**
* BOOTStr type; a basic, method-less type with the P6str REPR.
*/
public SixModelObject BOOTStr;
/**
* BOOTCode type; a basic, method-less type with the CodeRef REPR.
*/
public SixModelObject BOOTCode;
/**
* SCRef type; a basic, method-less type with the SCRef REPR.
*/
public SixModelObject SCRef;
/**
* Continuation type; a basic, method-less type with the Continuation REPR.
*/
public SixModelObject Continuation;
/**
* Lexotic type; a basic, method-less type with the Lexotic REPR.
*/
public SixModelObject Lexotic;
/**
* ContextRef type; a basic, method-less type with the ContextRef REPR.
*/
public SixModelObject ContextRef;
/**
* CallCapture type; a basic, method-less type with the CallContext REPR.
*/
public SixModelObject CallCapture;
/**
* BOOTException type; a basic, method-less type with the VMException REPR.
*/
public SixModelObject BOOTException;
/**
* BOOTIO type; a basic, method-less type with the IOHandle REPR.
*/
public SixModelObject BOOTIO;
/**
* BOOTJava type; a basic, method-less type with the JavaWrap REPR.
*/
public SixModelObject BOOTJava;
/**
* Typed VMArrays.
*/
public SixModelObject BOOTIntArray;
public SixModelObject BOOTNumArray;
public SixModelObject BOOTStrArray;
/**
* Multi-dispatch cache type.
*/
public SixModelObject MultiCache;
/**
* The main, startup thread's ThreadContext.
*/
public ThreadContext mainThread;
/**
* Active HLL configuration (maps HLL name to the configuration).
*/
private HashMap<String, HLLConfig> hllConfiguration;
/**
* HLL configuration of the compiler. We need to distinguish it from
* the HLL configuration of the code being compiled in bootstrap.
*/
private HashMap<String, HLLConfig> compilerHLLConfiguration;
/**
* HLL configuration of the compilee (see above).
*/
private HashMap<String, HLLConfig> compileeHLLConfiguration;
/**
* HLL global symbols.
*/
public HashMap<String, HashMap<String, SixModelObject>> hllSyms;
/**
* Compiler registry.
*/
public HashMap<String, SixModelObject> compilerRegistry;
/**
* Container configurer registry.
*/
public HashMap<String, ContainerConfigurer> contConfigs;
/**
* Serialization context lookup hash.
*/
public HashMap<String, SerializationContext> scs;
/**
* Serialization context wrapper object hash.
*/
public HashMap<String, SixModelObject> scRefs;
/**
* Set of library filenames which have been loaded so far.
*/
public HashSet<String> loaded;
/**
* Whether to dump VM-level stack traces for all exceptions.
*/
public boolean noisyExceptions;
/** Redirected output for eval-server. */
public PrintStream out = System.out;
/** Redirected error for eval-server. */
public PrintStream err = System.err;
/** Redirected input for eval-server. */
public InputStream in = System.in;
/** Whether to disallow exit. */
public boolean interceptExit;
/** If true, we're killing all threads, so disable exception handling. */
public volatile boolean shuttingDown;
/** Exit status or -1. */
public int exitStatus = -1;
// odds and ends nqp wants
int compileeDepth;
/** If true, libraries will be loaded in shared mode by default. */
public boolean sharingHint;
/** Interop object used for jvmbootinterop. */
public BootJavaInterop bootInterop;
HashMap<ContextKey<?,?>,Object> hllGlobalAll;
Object hllGlobalAllLock;
ThreadLocal<WeakReference<ThreadContext>> currentThreadCtxRef;
WeakHashMap<Thread, ThreadContext> allThreads;
public SixModelObject processEnvironment = null;
/**
* Initializes the runtime environment.
*/
public GlobalContext()
{
compileeHLLConfiguration = new HashMap<String, HLLConfig>();
hllConfiguration = compileeHLLConfiguration;
getHLLConfigFor("");
compilerHLLConfiguration = new HashMap<String, HLLConfig>();
hllConfiguration = compilerHLLConfiguration;
getHLLConfigFor("");
scs = new HashMap<String, SerializationContext>();
scRefs = new HashMap<String, SixModelObject>();
compilerRegistry = new HashMap<String, SixModelObject>();
hllSyms = new HashMap<String, HashMap<String, SixModelObject>>();
loaded = new HashSet<String>();
contConfigs = new HashMap<String, ContainerConfigurer>();
contConfigs.put("code_pair", new CodePairContainerConfigurer());
currentThreadCtxRef = new ThreadLocal< >();
allThreads = new WeakHashMap< >();
mainThread = getCurrentThreadContext();
KnowHOWBootstrapper.bootstrap(mainThread);
bootInterop = new BootJavaInterop(this);
// BOOT* not available earlier; fixup some stuff.
setupConfig(compileeHLLConfiguration.get(""));
setupConfig(compilerHLLConfiguration.get(""));
mainThread.savedCC = (CallCaptureInstance)CallCapture.st.REPR.allocate(mainThread, CallCapture.st);
noisyExceptions = System.getenv("NQP_VERBOSE_EXCEPTIONS") != null;
hllGlobalAll = new HashMap<ContextKey<?,?>, Object>();
hllGlobalAllLock = new Object();
}
/**
* Gets HLL configuration object for the specified language.
*/
public HLLConfig getHLLConfigFor(String language) {
synchronized (hllConfiguration) {
HLLConfig config = hllConfiguration.get(language);
if (config == null) {
config = new HLLConfig();
config.name = language;
setupConfig(config);
hllConfiguration.put(language, config);
}
return config;
}
}
private void setupConfig(HLLConfig config) {
config.intBoxType = BOOTInt;
config.numBoxType = BOOTNum;
config.strBoxType = BOOTStr;
config.listType = BOOTArray;
config.hashType = BOOTHash;
config.slurpyArrayType = BOOTArray;
config.slurpyHashType = BOOTHash;
config.arrayIteratorType = BOOTIter;
config.hashIteratorType = BOOTIter;
config.exceptionType = BOOTException;
config.ioType = BOOTIO;
}
public void useCompileeHLLConfig() {
this.hllConfiguration = this.compileeHLLConfiguration;
}
public void useCompilerHLLConfig() {
this.hllConfiguration = this.compilerHLLConfiguration;
}
public void exit(int status) {
if (!interceptExit) System.exit(status);
if (exitStatus < 0) exitStatus = status;
shuttingDown = true;
throw new ThreadDeath();
}
/** Gets the context object for the current thread, creating one if needed. */
public ThreadContext getCurrentThreadContext() {
// The implementation here is complicated by GC concerns. A simple
// ThreadLocal<ThreadContext> would, with the current (1.7)
// implementation of ThreadLocal, indefinitely retain a strong
// reference to the ThreadContext - and anything that can leak
// references that retain a GlobalContext is potentially very bad.
// OTOH, we do want to reuse threadcontexts (they include important
// states like srand seeds), so we can't have them garbage collected at
// random just because no NQP code is running.
// Note that this implementation *does* retain strong references from
// the GlobalContext to the ThreadContext longer than strictly
// necessary. This is judged to be a minor issue, because
// ThreadContexts retain much less than the full GlobalContext.
WeakReference<ThreadContext> tcRef = currentThreadCtxRef.get();
// the ref cannot be cleared while the thread is alive because the object
// is retained by the allThreads map
if (tcRef != null) return tcRef.get();
ThreadContext tc = new ThreadContext(this);
synchronized(this) { allThreads.put(Thread.currentThread(), tc); }
currentThreadCtxRef.set(new WeakReference< >(tc));
return tc;
}
}