forked from CleanroomMC/GroovyScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomGroovyScriptEngine.java
More file actions
576 lines (515 loc) · 23.6 KB
/
CustomGroovyScriptEngine.java
File metadata and controls
576 lines (515 loc) · 23.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package com.cleanroommc.groovyscript.sandbox;
import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.helper.JsonHelper;
import com.google.common.collect.AbstractIterator;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import groovy.lang.GroovyCodeSource;
import groovy.util.ResourceConnector;
import groovy.util.ResourceException;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.apache.commons.io.FileUtils;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.classgen.GeneratorContext;
import org.codehaus.groovy.control.*;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.tools.gse.DependencyTracker;
import org.codehaus.groovy.tools.gse.StringSetMap;
import org.codehaus.groovy.vmplugin.VMPlugin;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.util.*;
public class CustomGroovyScriptEngine implements ResourceConnector {
/**
* Changing this number will force the cache to be deleted and every script has to be recompiled.
* Useful when changes to the compilation process were made.
*/
public static final int CACHE_VERSION = 4;
/**
* Setting this to false will cause compiled classes to never be cached.
* As a side effect some compilation behaviour might change. Can be useful for debugging.
*/
public static final boolean ENABLE_CACHE = true;
/**
* Setting this to true will cause the cache to be deleted before each script run.
* Useful for debugging.
*/
public static final boolean DELETE_CACHE_ON_RUN = Boolean.parseBoolean(System.getProperty("groovyscript.disable_cache"));
private static WeakReference<ThreadLocal<LocalData>> localData = new WeakReference<>(null);
private static synchronized ThreadLocal<LocalData> getLocalData() {
ThreadLocal<LocalData> local = localData.get();
if (local != null) return local;
local = new ThreadLocal<>();
localData = new WeakReference<>(local);
return local;
}
private final URL[] scriptEnvironment;
private final File cacheRoot;
private final File scriptRoot;
private final CompilerConfiguration config;
private final ScriptClassLoader classLoader;
private final Map<String, CompiledScript> index = new Object2ObjectOpenHashMap<>();
private final Map<String, CompiledClass> loadedClasses = new Object2ObjectOpenHashMap<>();
public CustomGroovyScriptEngine(URL[] scriptEnvironment, File cacheRoot, File scriptRoot, CompilerConfiguration config) {
this.scriptEnvironment = scriptEnvironment;
this.cacheRoot = cacheRoot;
this.scriptRoot = scriptRoot;
this.config = config;
this.classLoader = new ScriptClassLoader(CustomGroovyScriptEngine.class.getClassLoader(), config, Collections.unmodifiableMap(this.loadedClasses));
readIndex();
}
public File getScriptRoot() {
return scriptRoot;
}
public File getCacheRoot() {
return cacheRoot;
}
public CompilerConfiguration getConfig() {
return config;
}
public GroovyScriptClassLoader getClassLoader() {
return classLoader;
}
public Iterable<Class<?>> getAllLoadedScriptClasses() {
return () -> new AbstractIterator<>() {
private final Iterator<CompiledClass> it = loadedClasses.values().iterator();
private Iterator<CompiledClass> innerClassesIt;
@Override
protected Class<?> computeNext() {
if (innerClassesIt != null && innerClassesIt.hasNext()) {
return innerClassesIt.next().clazz;
}
innerClassesIt = null;
CompiledClass cc;
while (it.hasNext()) {
cc = it.next();
if (cc instanceof CompiledScript cs && !cs.preprocessorCheckFailed() && cs.clazz != null) {
if (!cs.innerClasses.isEmpty()) {
innerClassesIt = cs.innerClasses.iterator();
}
return cs.clazz;
}
}
return endOfData();
}
};
}
void readIndex() {
this.index.clear();
JsonElement jsonElement = JsonHelper.loadJson(new File(this.cacheRoot, "_index.json"));
if (jsonElement == null || !jsonElement.isJsonObject()) return;
JsonObject json = jsonElement.getAsJsonObject();
int cacheVersion = json.get("version").getAsInt();
String java = json.has("java") ? json.get("java").getAsString() : "";
if (cacheVersion != CACHE_VERSION || !java.equals(VMPlugin.getJavaVersion())) {
// cache version changed -> force delete cache
deleteScriptCache();
return;
}
for (JsonElement element : json.getAsJsonArray("index")) {
if (element.isJsonObject()) {
CompiledScript cs = CompiledScript.fromJson(element.getAsJsonObject(), this.scriptRoot.getPath(), this.cacheRoot.getPath());
if (cs != null) {
this.index.put(cs.path, cs);
this.loadedClasses.put(cs.name, cs);
for (CompiledClass cc : cs.innerClasses) {
this.loadedClasses.put(cc.name, cc);
}
}
}
}
}
void writeIndex() {
if (!ENABLE_CACHE) return;
JsonObject json = new JsonObject();
json.addProperty("!DANGER!", "DO NOT EDIT THIS FILE!!!");
json.addProperty("version", CACHE_VERSION);
json.addProperty("java", VMPlugin.getJavaVersion());
JsonArray index = new JsonArray();
json.add("index", index);
for (Map.Entry<String, CompiledScript> entry : this.index.entrySet()) {
index.add(entry.getValue().toJson());
}
JsonHelper.saveJson(new File(this.cacheRoot, "_index.json"), json);
}
@ApiStatus.Internal
public boolean deleteScriptCache() {
this.index.values().forEach(script -> script.deleteCache(this.cacheRoot.getPath()));
this.index.clear();
this.loadedClasses.clear();
getClassLoader().clearCache();
try {
if (this.cacheRoot.exists()) FileUtils.cleanDirectory(this.cacheRoot);
return true;
} catch (IOException e) {
GroovyScript.LOGGER.throwing(e);
return false;
}
}
List<CompiledScript> findScripts(Collection<File> files) {
List<CompiledScript> scripts = new ArrayList<>(files.size());
for (File file : files) {
CompiledScript cs = checkScriptLoadability(file);
if (cs != null && !cs.preprocessorCheckFailed()) scripts.add(cs);
}
return scripts;
}
void loadScript(CompiledScript script) {
if (script != null && script.requiresReload() && !script.preprocessorCheckFailed()) {
Class<?> clazz = loadScriptClassInternal(new File(script.path), true);
script.setRequiresReload(false);
if (script.clazz == null) {
// should not happen
GroovyLog.get().errorMC("Class for {} was loaded, but didn't receive class created callback!", script.path);
if (ENABLE_CACHE) script.clazz = clazz;
}
}
}
CompiledScript loadScriptClass(File file) {
CompiledScript compiledScript = checkScriptLoadability(file);
loadScript(compiledScript);
return compiledScript;
}
CompiledScript checkScriptLoadability(File file) {
String relativeFileName = FileUtil.relativize(this.scriptRoot.getPath(), file.getPath());
if (!FileUtil.validateScriptPath(relativeFileName)) {
// skip
return null;
}
// File relativeFile = new File(relativeFileName);
long lastModified = file.lastModified();
CompiledScript comp = this.index.get(relativeFileName);
if (ENABLE_CACHE && comp != null && lastModified <= comp.lastEdited && comp.clazz == null && comp.readData(this.cacheRoot.getPath())) {
// class is not loaded, but the cached class bytes are still valid
comp.setRequiresReload(false);
if (comp.checkPreprocessorsFailed(this.scriptRoot)) {
return comp;
}
comp.ensureLoaded(getClassLoader(), this.loadedClasses, this.cacheRoot.getPath());
} else if (!ENABLE_CACHE || (comp == null || comp.clazz == null || lastModified > comp.lastEdited)) {
// class is not loaded and class bytes don't exist yet or script has been edited
if (comp == null) {
comp = new CompiledScript(relativeFileName, 0);
this.index.put(relativeFileName, comp);
}
if (comp.clazz != null) {
InvokerHelper.removeClass(comp.clazz);
comp.clazz = null;
}
comp.setRequiresReload(true);
if (lastModified > comp.lastEdited || comp.preprocessors == null) {
// recompile preprocessors if there is no data or script was edited
comp.preprocessors = Preprocessor.parsePreprocessors(file);
}
comp.lastEdited = lastModified;
if (comp.checkPreprocessorsFailed(this.scriptRoot)) {
// delete class bytes to make sure it's recompiled once the preprocessors returns true
comp.deleteCache(this.cacheRoot.getPath());
return comp;
}
} else {
// class is loaded and script wasn't edited
comp.setRequiresReload(false);
if (comp.checkPreprocessorsFailed(this.scriptRoot)) {
return comp;
}
comp.ensureLoaded(getClassLoader(), this.loadedClasses, this.cacheRoot.getPath());
}
comp.setPreprocessorCheckFailed(false);
return comp;
}
protected Class<?> loadScriptClassInternal(File file, boolean isFileRelative) {
Class<?> scriptClass = null;
try {
scriptClass = parseDynamicScript(file, isFileRelative);
} catch (Exception e) {
GroovyLog.get().exception("An error occurred while trying to load script class " + file.toString(), e);
}
return scriptClass;
}
@Nullable
private File findScriptFileOfClass(String className) {
for (String ending : this.config.getScriptExtensions()) {
File file = findScriptFile(className + "." + ending);
if (file != null) return file;
}
return null;
}
@Nullable
private File findScriptFile(String scriptName) {
File file;
for (URL root : this.scriptEnvironment) {
try {
File rootFile = new File(root.toURI());
// try to combine the root with the file ending
file = new File(rootFile, scriptName);
if (file.exists()) {
// found a valid file
return file;
}
} catch (URISyntaxException e) {
GroovyScript.LOGGER.throwing(e);
}
}
return null;
}
private @Nullable Class<?> parseDynamicScript(File file, boolean isFileRelative) {
if (isFileRelative) {
File absoutefile = findScriptFile(file.getPath());
if (absoutefile == null) {
throw new IllegalArgumentException("Now file was found for '" + file.getPath() + "'");
}
file = absoutefile;
}
Class<?> clazz = null;
try {
String encoding = config.getSourceEncoding();
String content = IOGroovyMethods.getText(new FileInputStream(file), encoding);
clazz = this.classLoader.parseClassRaw(content, file.toPath().toUri().toURL().toExternalForm());
// manually load the file as a groovy script
//clazz = this.classLoader.parseClassRaw(path.toFile());
} catch (IOException e) {
GroovyScript.LOGGER.throwing(e);
}
return clazz;
}
/**
* Called via mixin when groovy compiled a class from scripts.
*/
@ApiStatus.Internal
public void onCompileClass(SourceUnit su, String path, Class<?> clazz, byte[] code, boolean inner) {
String shortPath = FileUtil.relativize(this.scriptRoot.getPath(), path);
// if the script was compiled because another script depends on it, the source unit is wrong
// we need to find the source unit of the compiled class
SourceUnit trueSource = su.getAST().getUnit().getScriptSourceLocation(mainClassName(clazz.getName()));
String truePath = trueSource == null ? shortPath : FileUtil.relativize(this.scriptRoot.getPath(), trueSource.getName());
if (shortPath.equals(truePath) && su.getAST().getMainClassName() != null && !su.getAST().getMainClassName().equals(clazz.getName())) {
inner = true;
}
boolean finalInner = inner;
CompiledScript comp = this.index.computeIfAbsent(truePath, k -> new CompiledScript(k, finalInner ? -1 : 0));
CompiledClass innerClass = comp;
if (inner) innerClass = comp.findInnerClass(clazz.getName());
innerClass.onCompile(code, clazz, this.cacheRoot.getPath());
this.loadedClasses.put(innerClass.name, innerClass);
}
/**
* Called via mixin when a script class needs to be recompiled. This happens when a script was loaded because another script depends on
* it. Groovy will then try to compile the script again. If we already compiled the class we just stop the compilation process.
*/
@ApiStatus.Internal
public Class<?> onRecompileClass(URL source, String className) {
String path = source.toExternalForm();
String rel = FileUtil.relativize(this.scriptRoot.getPath(), path);
CompiledScript cs = this.index.get(rel);
Class<?> c = null;
if (cs != null) {
if (cs.clazz == null && cs.readData(this.cacheRoot.getPath())) {
cs.ensureLoaded(getClassLoader(), this.loadedClasses, this.cacheRoot.getPath());
}
c = cs.clazz;
}
return c;
}
private static String mainClassName(String name) {
return name.contains("$") ? name.split("\\$", 2)[0] : name;
}
@Override
public URLConnection getResourceConnection(String resourceName) throws ResourceException {
// Get the URLConnection
URLConnection groovyScriptConn = null;
ResourceException se = null;
for (URL root : this.scriptEnvironment) {
URL scriptURL = null;
try {
scriptURL = new URL(root, resourceName);
groovyScriptConn = openConnection(scriptURL);
break; // Now this is a bit unusual
} catch (MalformedURLException e) {
String message = "Malformed URL: with context=" + root + " and spec=" + resourceName + " because " + e.getMessage();
if (se == null) {
se = new ResourceException(message);
} else {
se = new ResourceException(message, se);
}
} catch (IOException e1) {
String message = "Cannot open URL: " + scriptURL;
groovyScriptConn = null;
if (se == null) {
se = new ResourceException(message);
} else {
se = new ResourceException(message, se);
}
}
}
if (se == null) se = new ResourceException("No resource for " + resourceName + " was found");
// If we didn't find anything, report on all the exceptions that occurred.
if (groovyScriptConn == null) throw se;
return groovyScriptConn;
}
private static URLConnection openConnection(URL scriptURL) throws IOException {
URLConnection urlConnection = scriptURL.openConnection();
verifyInputStream(urlConnection);
return scriptURL.openConnection();
}
private static void forceClose(URLConnection urlConnection) {
if (urlConnection != null) {
// We need to get the input stream and close it to force the open
// file descriptor to be released. Otherwise, we will reach the limit
// for number of files open at one time.
try {
verifyInputStream(urlConnection);
} catch (Exception e) {
// Do nothing: We were not going to use it anyway.
}
}
}
private static void verifyInputStream(URLConnection urlConnection) throws IOException {
try (InputStream in = urlConnection.getInputStream()) {
}
}
private static class LocalData {
CompilationUnit cu;
final StringSetMap dependencyCache = new StringSetMap();
final Map<String, String> precompiledEntries = new HashMap<>();
}
private class ScriptClassLoader extends GroovyScriptClassLoader {
public ScriptClassLoader(ClassLoader loader, CompilerConfiguration config, Map<String, CompiledClass> cache) {
super(loader, config, cache);
init();
}
@Override
public URL loadResource(String name) throws MalformedURLException {
File file = CustomGroovyScriptEngine.this.findScriptFileOfClass(name);
if (file != null) {
return file.toURI().toURL();
}
return null;
}
@Override
protected ClassCollector createCustomCollector(CompilationUnit unit, SourceUnit su) {
return super.createCustomCollector(unit, su).creatClassCallback((code, clz) -> {
onCompileClass(su, su.getName(), clz, code, clz.getName().contains("$"));
});
}
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration configuration, CodeSource source) {
CompilationUnit cu = super.createCompilationUnit(configuration, source);
LocalData local = getLocalData().get();
local.cu = cu;
final StringSetMap cache = local.dependencyCache;
final Map<String, String> precompiledEntries = local.precompiledEntries;
// "." is used to transfer compilation dependencies, which will be
// recollected later during compilation
for (String depSourcePath : cache.get(".")) {
try {
cache.get(depSourcePath);
cu.addSource(getResourceConnection(depSourcePath).getURL()); // todo remove usage of resource connection
} catch (ResourceException e) {
/* ignore */
}
}
// remove all old entries including the "." entry
cache.clear();
cu.addPhaseOperation((final SourceUnit sourceUnit, final GeneratorContext context, final ClassNode classNode) -> {
// GROOVY-4013: If it is an inner class, tracking its dependencies doesn't really
// serve any purpose and also interferes with the caching done to track dependencies
if (classNode.getOuterClass() != null) return;
DependencyTracker dt = new DependencyTracker(sourceUnit, cache, precompiledEntries);
dt.visitClass(classNode);
}, Phases.CLASS_GENERATION);
cu.setClassNodeResolver(new ClassNodeResolver() {
@Override
public LookupResult findClassNode(String origName, CompilationUnit compilationUnit) {
String name = origName.replace('.', '/');
File scriptFile = CustomGroovyScriptEngine.this.findScriptFileOfClass(name);
if (scriptFile != null) {
CompiledScript result = checkScriptLoadability(scriptFile);
if (result != null) {
if (result.requiresReload() || result.clazz == null) {
try {
return new LookupResult(compilationUnit.addSource(scriptFile.toURI().toURL()), null);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} else {
return new LookupResult(null, ClassHelper.make(result.clazz));
}
}
}
return super.findClassNode(origName, compilationUnit);
}
});
return cu;
}
@Override
protected Class<?> recompile(URL source, String className) throws CompilationFailedException, IOException {
if (source != null) {
Class<?> c = CustomGroovyScriptEngine.this.onRecompileClass(source, className);
if (c != null) {
return c;
}
}
return super.recompile(source, className);
}
@Override
public Class<?> parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException {
synchronized (sourceCache) {
File file = codeSource.getFile();
if (file == null) {
file = new File(codeSource.getName());
}
CompiledScript compiledScript = loadScriptClass(file);
if (compiledScript.preprocessorCheckFailed()) {
throw new IllegalStateException("Figure this out");
}
if (compiledScript.requiresReload() || compiledScript.clazz == null) {
compiledScript.clazz = CustomGroovyScriptEngine.this.parseDynamicScript(file, false);
}
return compiledScript.clazz;
}
}
public Class<?> parseClassRaw(GroovyCodeSource source) {
synchronized (sourceCache) {
return doParseClass(source);
}
}
public Class<?> parseClassRaw(File file) throws IOException {
return parseClassRaw(new GroovyCodeSource(file, CustomGroovyScriptEngine.this.config.getSourceEncoding()));
}
public Class<?> parseClassRaw(final String text, final String fileName) throws CompilationFailedException {
GroovyCodeSource gcs = new GroovyCodeSource(text, fileName, "/groovy/script");
gcs.setCachable(false);
return parseClassRaw(gcs);
}
private Class<?> doParseClass(GroovyCodeSource codeSource) {
// local is kept as hard reference to avoid garbage collection
ThreadLocal<LocalData> localTh = getLocalData();
LocalData localData = new LocalData();
localTh.set(localData);
StringSetMap cache = localData.dependencyCache;
Class<?> answer = null;
try {
answer = super.parseClass(codeSource, false);
} finally {
cache.clear();
localTh.remove();
}
return answer;
}
}
}