1+ package mods .Hileb .shotaasm .impl .groovy ;
2+
3+ import org .codehaus .groovy .control .customizers .ImportCustomizer ;
4+ import org .codehaus .groovy .control .CompilerConfiguration ;
5+ import it .unimi .dsi .fastutil .objects .Object2ObjectOpenHashMap ;
6+ import groovy .lang .Binding ;
7+ import groovy .lang .GroovyClassLoader ;
8+ import groovy .lang .Script ;
9+ import org .codehaus .groovy .runtime .InvokerHelper ;
10+ import mods .Hileb .shotaasm .api .ScriptFile ;
11+ import net .minecraft .launchwrapper .Launch ;
12+
13+ import java .util .*;
14+
15+ public class ShotaGroovySandbox {
16+
17+ ImportCustomizer importCustomizer = new ImportCustomizer ();
18+ CompilerConfiguration config = new CompilerConfiguration ();
19+ Map <String , Object > bindings = new Object2ObjectOpenHashMap <>();
20+ Binding binding = new Binding (this .bindings );
21+ GroovyClassLoader classLoader ;
22+
23+ public ShotaGroovySandbox () {
24+ importCustomizer = new ImportCustomizer ();
25+ config = new CompilerConfiguration ();
26+
27+ importCustomizer .addImports (
28+ "mods.Hileb.shotaasm.api.TransformerRegistry" ,
29+ "mods.Hileb.shotaasm.api.ShotaContext"
30+ );
31+ importCustomizer .addStarImports (
32+ "org.objectweb.asm" ,
33+ "org.objectweb.asm.tree" ,
34+ "org.objectweb.asm.util" ,
35+ "org.objectweb.asm.commons" ,
36+ "org.objectweb.asm.signature"
37+ );
38+ importCustomizer .addStaticStars ("org.objectweb.asm.Opcodes" );
39+ config .addCompilationCustomizers (importCustomizer );
40+ classLoader = new GroovyClassLoader (Launch .classLoader , config , false );
41+ }
42+
43+ public Class <?> compile (String name , String text ) {
44+ return classLoader .parseClass (text , name );
45+ }
46+
47+ public Runnable makeScript (Class <?> cls ){
48+ final Script script = InvokerHelper .createScript (cls , binding );
49+ return script ::run ;
50+ }
51+
52+ static ShotaGroovySandbox box = null ;
53+ public static Runnable compileScript (String name , String file ){
54+ if (box == null ) box = new ShotaGroovySandbox ();
55+ return box .makeScript (box .compile (name , file ));
56+ }
57+ }
0 commit comments