Skip to content

Commit 0810a7e

Browse files
committed
feat(compiler): allow compiler to emit non-class resources
(cherry picked from commit ea315dc)
1 parent 0b332e2 commit 0810a7e

16 files changed

Lines changed: 157 additions & 85 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.byteskript.skript.api.resource;
2+
3+
import mx.kenzie.foundation.language.PostCompileClass;
4+
5+
import java.io.*;
6+
7+
/**
8+
* A resource which represents a compiled class file.
9+
* @param source The source class file.
10+
* */
11+
public record ClassResource(PostCompileClass source) implements Resource {
12+
@Override
13+
public InputStream open() {
14+
return new ByteArrayInputStream(source.code());
15+
}
16+
17+
@Override
18+
public String getEntryName() {
19+
return source.internalName() + ".class";
20+
}
21+
}

src/main/java/org/byteskript/skript/api/resource/Resource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static Resource ofString(final String name, final String text, final Charset cha
8989
* @param compiledClass The compiled class to use as the source of the resource.
9090
* @return The newly created resource.
9191
* */
92-
static Resource ofCompiledClass(final PostCompileClass compiledClass) {
93-
return ofBytes(compiledClass.internalName() + ".class", compiledClass.code());
92+
static ClassResource ofCompiledClass(final PostCompileClass compiledClass) {
93+
return new ClassResource(compiledClass);
9494
}
9595

9696
/**

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
package org.byteskript.skript.app;
88

9-
import mx.kenzie.foundation.language.PostCompileClass;
9+
import org.byteskript.skript.api.resource.Resource;
1010
import org.byteskript.skript.runtime.Skript;
1111
import org.byteskript.skript.runtime.internal.ExtractedSyntaxCalls;
1212

@@ -55,10 +55,8 @@ public static void main(String... args) throws Throwable {
5555
final File file = new File(name);
5656
registerLibraries(SKRIPT);
5757
try (final InputStream stream = new FileInputStream(file)) {
58-
final PostCompileClass[] classes = SKRIPT.compileComplexScript(stream, "skript." + file.getName());
59-
for (final PostCompileClass type : classes) {
60-
SKRIPT.loadScript(type);
61-
}
58+
final Resource[] classes = SKRIPT.compileComplexScript(stream, "skript." + file.getName());
59+
SKRIPT.loadScript(classes);
6260
}
6361
new SimpleThrottleController(SKRIPT).run();
6462
} else if (args[0].equalsIgnoreCase("jar")) {
@@ -79,10 +77,8 @@ public static void main(String... args) throws Throwable {
7977
final File file = new File(name);
8078
registerLibraries(SKRIPT);
8179
try (final InputStream stream = new FileInputStream(file)) {
82-
final PostCompileClass[] classes = SKRIPT.compileComplexScript(stream, "skript." + file.getName());
83-
for (final PostCompileClass type : classes) {
84-
SKRIPT.loadScript(type);
85-
}
80+
final Resource[] classes = SKRIPT.compileComplexScript(stream, "skript." + file.getName());
81+
SKRIPT.loadScript(classes);
8682
}
8783
new SimpleThrottleController(SKRIPT).run();
8884
} else if (args[0].equalsIgnoreCase("debug")) {

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

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

99
import mx.kenzie.foundation.assembler.JarBuilder;
1010
import mx.kenzie.foundation.assembler.Manifest;
11-
import mx.kenzie.foundation.language.PostCompileClass;
1211
import org.byteskript.skript.api.Library;
1312
import org.byteskript.skript.api.resource.Resource;
1413
import org.byteskript.skript.runtime.Skript;
1514

1615
import java.io.File;
1716
import java.io.IOException;
18-
import java.util.ArrayList;
19-
import java.util.List;
17+
import java.util.*;
2018

2119
import static org.byteskript.skript.runtime.internal.ConsoleColour.*;
2220

@@ -26,15 +24,15 @@ public final class ScriptJarBuilder extends SkriptApp {
2624
public static void main(String... args) throws IOException {
2725
registerLibraries(SKRIPT);
2826
final String name = args.length > 0 ? args[0] : "CompiledScripts";
29-
final PostCompileClass[] scripts = SKRIPT.compileScripts(SOURCE);
27+
final Resource[] scripts = SKRIPT.compileScripts(SOURCE);
3028
final File jar = new File(OUTPUT, name + ".jar");
3129
compileResource(jar, scripts);
3230
System.out.println(RESET + "Available scripts have been compiled to " + CYAN + CYAN_UNDERLINED + "skripts/" + jar.getName() + RESET);
3331
}
3432

35-
static void compileResource(File jar, PostCompileClass... classes) throws IOException {
33+
static void compileResource(File jar, Resource... compiledResources) throws IOException {
3634
if (!jar.exists()) jar.createNewFile();
37-
final List<Resource> runtime = new ArrayList<>();
35+
final List<Resource> runtime = new ArrayList<>(Arrays.asList(compiledResources));
3836
scrapeRuntimeResources(runtime);
3937
for (final File file : getFiles(new ArrayList<>(), RESOURCES.toPath())) {
4038
runtime.add(Resource.ofFile(file.getName(), file));
@@ -43,7 +41,6 @@ static void compileResource(File jar, PostCompileClass... classes) throws IOExce
4341
for (final Resource resource : runtime) {
4442
builder.write(resource.getEntryName(), resource.open());
4543
}
46-
builder.write(classes);
4744
final String version = ScriptJarBuilder.class.getPackage().getImplementationVersion();
4845
builder.manifest(new Manifest(ScriptRunner.class.getName(), "Skript Compiler " + version, "Skript Jar Builder"));
4946
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mx.kenzie.foundation.*;
1010
import mx.kenzie.foundation.compiler.State;
1111
import org.byteskript.skript.api.*;
12+
import org.byteskript.skript.api.resource.Resource;
1213
import org.byteskript.skript.api.syntax.Section;
1314
import org.byteskript.skript.compiler.structure.*;
1415

@@ -62,6 +63,8 @@ public void setStoredVariableName(String storedVariableName) {
6263
public abstract ClassBuilder getSuppressedBuilder(Type type);
6364

6465
public abstract ClassBuilder getSuppressedBuilder();
66+
67+
public abstract void addResource(final Resource resource);
6568

6669
public abstract MethodBuilder getMethod();
6770

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package org.byteskript.skript.compiler;
88

99
import mx.kenzie.foundation.Type;
10-
import mx.kenzie.foundation.language.PostCompileClass;
1110
import org.byteskript.skript.api.Library;
11+
import org.byteskript.skript.api.resource.Resource;
1212

1313
import java.io.InputStream;
1414
import java.io.OutputStream;
@@ -38,15 +38,15 @@ protected void compileLine(String line, FileContext context) {
3838
}
3939

4040
@Override
41-
public PostCompileClass[] compile(InputStream stream, Type path) {
41+
public Resource[] compile(InputStream stream, Type path) {
4242
this.stream.print("\n");
4343
this.stream.print("--" + path.internalName());
4444
this.stream.print("\n\n");
4545
return super.compile(stream, path);
4646
}
4747

4848
@Override
49-
public PostCompileClass[] compile(String source, Type path) {
49+
public Resource[] compile(String source, Type path) {
5050
this.stream.print("\n");
5151
this.stream.print("--" + path.internalName());
5252
this.stream.print("\n\n");

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mx.kenzie.foundation.*;
1010
import mx.kenzie.foundation.language.PostCompileClass;
1111
import org.byteskript.skript.api.*;
12+
import org.byteskript.skript.api.resource.Resource;
1213
import org.byteskript.skript.compiler.structure.*;
1314
import org.byteskript.skript.error.ScriptCompileError;
1415
import org.byteskript.skript.lang.handler.StandardHandlers;
@@ -25,14 +26,15 @@
2526
* This should usually be modified as a {@link Context}.
2627
*/
2728
public class FileContext extends Context {
28-
29+
2930
protected final Map<String, Type> types = new HashMap<>();
3031
protected final List<Function> functions = new ArrayList<>();
3132
protected final List<ProgrammaticSplitTree> trees = new ArrayList<>();
3233
final Type type;
3334
final List<Consumer<Context>> endOfLine = new ArrayList<>();
3435
final Map<HandlerType, List<PropertyAccessGenerator>> usedProperties = new HashMap<>();
3536
final List<ClassBuilder> suppressedClasses = new ArrayList<>();
37+
final List<Resource> resources = new ArrayList<>();
3638
final List<Flag> flags = new ArrayList<>();
3739
public int indent, lineIndent, lineNumber, lambdaIndex, indexShift;
3840
public boolean sectionHeader;
@@ -48,7 +50,7 @@ public class FileContext extends Context {
4850
LanguageElement expected;
4951
SyntaxElement currentEffect;
5052
private HandlerType mode = StandardHandlers.GET;
51-
53+
5254
public FileContext(Type type) {
5355
this(type, -1);
5456
}
@@ -84,7 +86,7 @@ public List<ProgrammaticSplitTree> getTrees() {
8486
return trees;
8587
}
8688

87-
public PostCompileClass[] compile() {
89+
public Resource[] compile() {
8890
this.writer.addAnnotation(ScriptData.class).addValue("sourceFile", sourceFile);
8991
for (List<PropertyAccessGenerator> value : usedProperties.values()) {
9092
for (PropertyAccessGenerator generator : value) {
@@ -116,7 +118,12 @@ This error cannot be directly triaged, but likely comes from a malformed syntax
116118
}
117119
}
118120
}
119-
return classes.toArray(new PostCompileClass[0]);
121+
final List<Resource> compiledResources = new ArrayList<>(classes.size() + resources.size());
122+
compiledResources.addAll(resources);
123+
for (final PostCompileClass compiledClass : classes) {
124+
compiledResources.add(Resource.ofCompiledClass(compiledClass));
125+
}
126+
return compiledResources.toArray(new Resource[0]);
120127
}
121128

122129
@Override
@@ -189,7 +196,12 @@ public ClassBuilder getSuppressedBuilder(final Type type) {
189196
public ClassBuilder getSuppressedBuilder() {
190197
return suppressedClasses.get(0);
191198
}
192-
199+
200+
@Override
201+
public void addResource(final Resource resource) {
202+
this.resources.add(resource);
203+
}
204+
193205
@Override
194206
public MethodBuilder getMethod() {
195207
return method;

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import mx.kenzie.jupiter.stream.Stream;
1313
import org.byteskript.skript.api.Library;
1414
import org.byteskript.skript.api.SyntaxElement;
15+
import org.byteskript.skript.api.resource.ClassResource;
16+
import org.byteskript.skript.api.resource.Resource;
1517
import org.byteskript.skript.api.syntax.InnerModifyExpression;
1618
import org.byteskript.skript.api.syntax.Section;
1719
import org.byteskript.skript.compiler.structure.ErrorDetails;
@@ -44,18 +46,38 @@ protected static synchronized int getAnonymous() {
4446
return ++anonymous;
4547
}
4648

47-
@Override
48-
public PostCompileClass compileClass(InputStream source) {
49-
return this.compile(source)[0];
49+
public Resource compileClassWithResources(InputStream source) {
50+
return this.compileWithResources(source)[0];
5051
}
5152

52-
@Override
53-
public PostCompileClass[] compile(InputStream source) {
53+
public Resource[] compileWithResources(InputStream source) {
5454
final int index = getAnonymous();
5555
final String path = "skript/unknown_" + index;
5656
return this.compile(source, path);
5757
}
58-
58+
59+
@Override
60+
public PostCompileClass compileClass(InputStream inputStream) {
61+
final Resource compiled = compileClassWithResources(inputStream);
62+
if (!(compiled instanceof final ClassResource compiledClass))
63+
throw new ScriptCompileError(-1, "The script included resources when only one class was expected.");
64+
65+
return compiledClass.source();
66+
}
67+
68+
@Override
69+
public PostCompileClass[] compile(InputStream inputStream) {
70+
final Resource[] compiled = compileWithResources(inputStream);
71+
final List<PostCompileClass> classes = new ArrayList<>();
72+
for (final Resource resource : compiled) {
73+
if (!(resource instanceof final ClassResource compiledClass))
74+
throw new ScriptCompileError(-1, "The script included resources when only classes were expected.");
75+
classes.add(compiledClass.source());
76+
}
77+
78+
return classes.toArray(new PostCompileClass[0]);
79+
}
80+
5981
@Override
6082
public void compileAndLoad(InputStream inputStream) {
6183
throw new ScriptCompileError(-1, "This compiler does not support this feature.");
@@ -304,7 +326,7 @@ public boolean removeLibrary(Library library) {
304326
}
305327

306328
@Override
307-
public PostCompileClass[] compile(InputStream stream, Type path) {
329+
public Resource[] compile(InputStream stream, Type path) {
308330
final FileContext context = this.createContext(path);
309331
context.libraries.addAll(libraries);
310332
for (final Library library : libraries) {
@@ -323,13 +345,13 @@ public PostCompileClass[] compile(InputStream stream, Type path) {
323345
}
324346

325347
@Override
326-
public PostCompileClass[] compile(InputStream source, String path) {
327-
if (path == null) return this.compile(source);
348+
public Resource[] compile(InputStream source, String path) {
349+
if (path == null) return this.compileWithResources(source);
328350
return compile(source, new Type(path));
329351
}
330352

331353
@Override
332-
public PostCompileClass[] compile(String source, Type path) {
354+
public Resource[] compile(String source, Type path) {
333355
final FileContext context = this.createContext(path);
334356
context.libraries.addAll(libraries);
335357
for (final Library library : libraries) {

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

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

99
import mx.kenzie.foundation.Type;
1010
import mx.kenzie.foundation.language.Compiler;
11-
import mx.kenzie.foundation.language.PostCompileClass;
1211
import org.byteskript.skript.api.Library;
12+
import org.byteskript.skript.api.resource.Resource;
1313
import org.byteskript.skript.runtime.internal.ModifiableCompiler;
1414
import org.byteskript.skript.runtime.type.Converter;
1515
import org.byteskript.skript.runtime.type.OperatorFunction;
@@ -53,11 +53,11 @@ public SkriptLangSpec getLanguage() {
5353
return map;
5454
}
5555

56-
public abstract PostCompileClass[] compile(InputStream stream, Type name);
56+
public abstract Resource[] compile(InputStream stream, Type name);
5757

58-
public abstract PostCompileClass[] compile(InputStream file, String path);
58+
public abstract Resource[] compile(InputStream file, String path);
5959

60-
public abstract PostCompileClass[] compile(String file, Type path);
60+
public abstract Resource[] compile(String file, Type path);
6161

6262
@Override
6363
public SkriptCompiler clone() {

0 commit comments

Comments
 (0)