Skip to content

Commit cb6545b

Browse files
committed
Restructure the script and library loaders.
1 parent 554dd28 commit cb6545b

File tree

13 files changed

+109
-35
lines changed

13 files changed

+109
-35
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<dependency>
9090
<groupId>mx.kenzie</groupId>
9191
<artifactId>mirror</artifactId>
92-
<version>5.0.2</version>
92+
<version>5.0.3</version>
9393
<scope>compile</scope>
9494
</dependency>
9595
<dependency>

src/main/java/org/byteskript/skript/api/syntax/EventHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void compile(Context context, Pattern.Match match) {
5151
method
5252
.addAnnotation(EventData.class).setVisible(true)
5353
.addValue("name", name())
54-
.addValue("event", org.objectweb.asm.Type.getObjectType(new Type(eventClass).internalName()))
54+
.addValue("event", eventClass.getName())
5555
.addValue("async", AsyncEvent.class.isAssignableFrom(eventClass));
5656
context.addFlag(AreaFlag.IN_EVENT);
5757
}

src/main/java/org/byteskript/skript/app/SkriptApp.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88

99
import org.byteskript.skript.error.ScriptLibraryError;
1010
import org.byteskript.skript.runtime.Skript;
11+
import org.byteskript.skript.runtime.internal.LibraryClassLoader;
1112

1213
import java.io.File;
1314
import java.io.FileInputStream;
1415
import java.io.IOException;
1516
import java.io.InputStream;
16-
import java.net.MalformedURLException;
1717
import java.net.URISyntaxException;
18-
import java.net.URL;
19-
import java.net.URLClassLoader;
2018
import java.nio.file.DirectoryStream;
2119
import java.nio.file.Files;
2220
import java.nio.file.Path;
@@ -64,11 +62,8 @@ protected static void registerLibraries(final Skript skript) {
6462
}
6563
}
6664

67-
protected static void callLibrary(final File file, final String main, final Skript skript) throws MalformedURLException, ClassNotFoundException {
68-
final URLClassLoader child = new URLClassLoader(
69-
new URL[]{file.toURI().toURL()},
70-
SkriptApp.class.getClassLoader()
71-
);
65+
protected static void callLibrary(final File file, final String main, final Skript skript) throws IOException, ClassNotFoundException {
66+
final LibraryClassLoader child = new LibraryClassLoader(file, SkriptApp.class.getClassLoader());
7267
final Class<?> target = Class.forName(main, true, child);
7368
try {
7469
target.getMethod("load", Skript.class)

src/main/java/org/byteskript/skript/compiler/BridgeCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public BridgeCompiler(MethodHandles.Lookup lookup, String owner, MethodType sour
3737
this.owner = owner;
3838
this.source = source;
3939
this.target = target;
40-
this.location = owner + "$" + "Bridge" + new Random().nextInt(100000, 999999);
40+
this.location = owner + "$" + "Bridge" + new Random().nextInt(100000, 999999) + Math.abs(owner.hashCode());
4141
}
4242

4343
public Class<?> createClass()

src/main/java/org/byteskript/skript/compiler/FileContext.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public class FileContext extends Context {
2424
final Type type;
2525
protected final Map<String, Type> types = new HashMap<>();
2626
protected String indentUnit;
27-
int indent;
28-
int lineNumber;
29-
int lambdaIndex;
30-
int indexShift;
31-
boolean sectionHeader;
27+
public int indent;
28+
public int lineNumber;
29+
public int lambdaIndex;
30+
public int indexShift;
31+
public boolean sectionHeader;
3232
LanguageElement expected;
3333
SyntaxElement currentEffect;
3434
private HandlerType mode = StandardHandlers.GET;
@@ -37,8 +37,8 @@ public class FileContext extends Context {
3737
final List<ClassBuilder> suppressedClasses = new ArrayList<>();
3838
final List<Flag> flags = new ArrayList<>();
3939

40-
ElementTree line;
41-
ElementTree current;
40+
public ElementTree line;
41+
public ElementTree current;
4242
protected final List<Function> functions = new ArrayList<>();
4343
protected List<PreVariable> variables = new ArrayList<>();
4444
protected final List<ProgrammaticSplitTree> trees = new ArrayList<>();

src/main/java/org/byteskript/skript/compiler/SimpleSkriptCompiler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.byteskript.skript.compiler.structure.SectionMeta;
1919
import org.byteskript.skript.error.ScriptCompileError;
2020
import org.byteskript.skript.error.ScriptParseError;
21+
import org.byteskript.skript.runtime.Skript;
2122

2223
import java.io.*;
2324
import java.nio.charset.StandardCharsets;
@@ -117,7 +118,7 @@ public PostCompileClass[] compile(String file, Type path) {
117118
return context.compile();
118119
}
119120

120-
List<String> removeComments(final String string) {
121+
protected List<String> removeComments(final String string) {
121122
final List<String> original = string.lines().toList(); // stream of sadness :(
122123
final List<String> lines = new ArrayList<>();
123124
final String regex = "\\s+$";
@@ -163,7 +164,6 @@ public Library[] getLibraries() {
163164

164165
private final java.util.regex.Pattern unitMatch = java.util.regex.Pattern.compile("(?<=^)[\\t ]+(?=\\S)");
165166

166-
167167
protected void compileLine(final String line, final FileContext context) {
168168
final ElementTree tree = parseLine(line, context);
169169
if (tree == null) return;
@@ -316,8 +316,7 @@ public ElementTree assembleExpression(String expression, final Type expected, fi
316316

317317
@Override
318318
public Class<?> load(byte[] bytecode, String name) {
319-
return new PostCompileClass(bytecode, name, name.replace(".", "/"))
320-
.compileAndLoad();
319+
return Skript.LOADER.loadClass(name, bytecode);
321320
}
322321

323322
@Override

src/main/java/org/byteskript/skript/runtime/Script.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.byteskript.skript.runtime;
88

9+
import org.byteskript.skript.api.Event;
910
import org.byteskript.skript.error.ScriptLoadError;
1011
import org.byteskript.skript.runtime.data.EventData;
1112
import org.byteskript.skript.runtime.data.Function;
@@ -59,7 +60,7 @@ public Script(boolean init, Skript skript, File sourceFile, Class<?>... classes)
5960
final EventData event = method.getAnnotation(EventData.class);
6061
final Member member = new Member(this, method, event.async());
6162
this.events.add(member);
62-
skript.registerEventHandler(event.event(), new InvokingScriptRunner(mainClass(), member));
63+
skript.registerEventHandler((Class<? extends Event>) skript.getClass(event.event()), new InvokingScriptRunner(mainClass(), member));
6364
}
6465
}
6566
this.members = structures.toArray(new Structure[0]);

src/main/java/org/byteskript/skript/runtime/Skript.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import mx.kenzie.foundation.Type;
1010
import mx.kenzie.foundation.language.PostCompileClass;
11+
import mx.kenzie.mirror.ClassProvider;
1112
import mx.kenzie.mirror.Mirror;
1213
import org.byteskript.skript.api.Event;
1314
import org.byteskript.skript.api.Library;
@@ -33,6 +34,7 @@ public final class Skript {
3334

3435
public static final ThreadGroup THREAD_GROUP = new ThreadGroup("skript");
3536
public static final int JAVA_VERSION = 61;
37+
public static final RuntimeClassLoader LOADER = new RuntimeClassLoader(Skript.class.getClassLoader());
3638
private static Skript skript;
3739
private static ExecutorService executor;
3840
static SkriptThreadProvider factory;
@@ -44,9 +46,43 @@ public final class Skript {
4446
final SkriptMirror mirror = new SkriptMirror(Skript.class);
4547
static final GlobalVariableMap VARIABLES = new GlobalVariableMap();
4648

49+
public static class RuntimeClassLoader extends ClassLoader implements ClassProvider {
50+
protected RuntimeClassLoader(ClassLoader parent) {
51+
super(parent);
52+
}
53+
54+
@Override
55+
public Class<?> loadClass(String name) throws ClassNotFoundException {
56+
return super.loadClass(name);
57+
}
58+
59+
@Override
60+
public Class<?> findClass(String name) {
61+
try {
62+
return super.findClass(name);
63+
} catch (ClassNotFoundException e) {
64+
return Skript.currentInstance().getClass(name);
65+
}
66+
}
67+
68+
public Class<?> loadClass(String name, byte[] bytecode) {
69+
return super.defineClass(name, bytecode, 0, bytecode.length);
70+
}
71+
72+
@Override
73+
public Class<?> loadClass(Class<?> aClass, String name, byte[] bytecode) {
74+
try {
75+
return Class.forName(name, false, this);
76+
} catch (ClassNotFoundException ex) {
77+
return super.defineClass(name, bytecode, 0, bytecode.length);
78+
}
79+
}
80+
}
81+
4782
static class SkriptMirror extends Mirror<Object> {
4883
protected SkriptMirror(Object target) {
4984
super(target);
85+
useProvider(LOADER);
5086
}
5187

5288
@Override
@@ -172,6 +208,24 @@ public void stop() {
172208
//endregion
173209

174210
//region Libraries
211+
public Class<?> getClass(String name) {
212+
final Class<?> found = getClass(name, Skript.class);
213+
if (found != null) return found;
214+
for (Library library : compiler.getLibraries()) {
215+
final Class<?> test = getClass(name, library.getClass());
216+
if (test != null) return test;
217+
}
218+
return null;
219+
}
220+
221+
private static Class<?> getClass(String name, Class<?> owner) {
222+
try {
223+
return Class.forName(name, false, owner.getClassLoader());
224+
} catch (ClassNotFoundException ex) {
225+
return null;
226+
}
227+
}
228+
175229
public void registerLibraryClass(byte[] bytes) throws IOException {
176230
final String name = getClassName(new ByteArrayInputStream(bytes));
177231
final Class<?> source = new ClassLoader(Skript.class.getClassLoader()) {

src/main/java/org/byteskript/skript/runtime/data/EventData.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.byteskript.skript.runtime.data;
88

9-
import org.byteskript.skript.api.Event;
10-
119
import java.lang.annotation.ElementType;
1210
import java.lang.annotation.Retention;
1311
import java.lang.annotation.RetentionPolicy;
@@ -19,7 +17,7 @@
1917

2018
String name();
2119

22-
Class<? extends Event> event();
20+
String event();
2321

2422
boolean async();
2523

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.runtime.internal;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.net.URL;
12+
import java.net.URLClassLoader;
13+
14+
public class LibraryClassLoader extends URLClassLoader {
15+
16+
public LibraryClassLoader(File file, ClassLoader parent) throws IOException {
17+
super(new URL[]{file.toURI().toURL()}, parent);
18+
}
19+
20+
@Override
21+
protected Class<?> findClass(String name) throws ClassNotFoundException {
22+
return super.findClass(name);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)