Skip to content

Commit 6f3e6ec

Browse files
committed
Syntax creation and testing.
1 parent 031aa8e commit 6f3e6ec

File tree

7 files changed

+89
-10
lines changed

7 files changed

+89
-10
lines changed

pom.xml

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

77
<groupId>org.byteskript</groupId>
88
<artifactId>byteskript</artifactId>
9-
<version>1.0.5</version>
9+
<version>1.0.6</version>
1010
<name>ByteSkript</name>
1111

1212
<properties>

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import org.byteskript.skript.runtime.Skript;
1111

1212
import java.io.File;
13+
import java.io.FileInputStream;
1314
import java.io.IOException;
15+
import java.io.InputStream;
1416
import java.net.MalformedURLException;
1517
import java.net.URISyntaxException;
1618
import java.net.URL;
@@ -53,7 +55,11 @@ protected static void registerLibraries(final Skript skript) {
5355
ex.printStackTrace();
5456
}
5557
} else if (file.getName().endsWith(".class")) {
56-
// todo
58+
try (final InputStream stream = new FileInputStream(file)) {
59+
skript.registerLibraryClass(stream.readAllBytes());
60+
} catch (IOException exception) {
61+
throw new ScriptLibraryError("Error while loading library '" + file.getName() + "'", exception);
62+
}
5763
}
5864
}
5965
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class Script {
3030
private final Structure[] members;
3131

3232
public Script(Skript skript, File sourceFile, Class<?>... classes) {
33-
this(false, skript, sourceFile, classes);
33+
this(true, skript, sourceFile, classes);
3434
}
3535

3636
public Script(boolean init, Skript skript, File sourceFile, Class<?>... classes) {

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import mx.kenzie.mirror.Mirror;
1212
import org.byteskript.skript.api.Event;
1313
import org.byteskript.skript.api.Library;
14+
import org.byteskript.skript.api.ModifiableLibrary;
1415
import org.byteskript.skript.compiler.SkriptCompiler;
1516
import org.byteskript.skript.error.ScriptCompileError;
1617
import org.byteskript.skript.error.ScriptLoadError;
@@ -21,6 +22,7 @@
2122
import org.byteskript.skript.runtime.threading.*;
2223

2324
import java.io.*;
25+
import java.nio.charset.StandardCharsets;
2426
import java.nio.file.DirectoryStream;
2527
import java.nio.file.Files;
2628
import java.nio.file.Path;
@@ -170,6 +172,18 @@ public void stop() {
170172
//endregion
171173

172174
//region Libraries
175+
public void registerLibraryClass(byte[] bytes) throws IOException {
176+
final String name = getClassName(new ByteArrayInputStream(bytes));
177+
final Class<?> source = new ClassLoader(Skript.class.getClassLoader()) {
178+
public Class<?> defineClass(String name, byte[] bytes) {
179+
return defineClass(name, bytes, 0, bytes.length);
180+
}
181+
}.defineClass(name, bytes);
182+
final ModifiableLibrary library = new ModifiableLibrary(name);
183+
library.generateSyntaxFrom(source);
184+
compiler.addLibrary(library);
185+
}
186+
173187
public boolean registerLibrary(Library library) {
174188
return compiler.addLibrary(library);
175189
}
@@ -199,10 +213,9 @@ public Collection<File> compileScripts(final File root, final File outputDirecto
199213
if (!root.isDirectory()) throw new ScriptLoadError("Root must be a folder.");
200214
final List<File> files = getFiles(new ArrayList<>(), root.toPath());
201215
final List<File> outputs = new ArrayList<>();
202-
final int length = root.getAbsolutePath().length();
203216
for (final File file : files) {
204217
try (final InputStream input = new FileInputStream(file)) {
205-
final String name = createClassName(file.getName(), file.getAbsolutePath().substring(length));
218+
final String name = getClassName(file, root);
206219
outputs.addAll(compileComplexScript(input, name, outputDirectory));
207220
}
208221
}
@@ -253,6 +266,14 @@ public PostCompileClass[] compileComplexScript(final InputStream stream, final S
253266
return classes;
254267
}
255268

269+
/**
270+
* This method may be unavailable in some distributions.
271+
*/
272+
@Deprecated
273+
public PostCompileClass compileScript(final String code, final String name) {
274+
return compileScript(new ByteArrayInputStream(code.getBytes(StandardCharsets.UTF_8)), name);
275+
}
276+
256277
public PostCompileClass compileScript(final InputStream stream, final String name) {
257278
final PostCompileClass[] classes = compiler.compile(stream, new Type(name));
258279
if (classes.length < 1) throw new ScriptCompileError(-1, "Script does not compile to a class.");

src/main/java/org/byteskript/skript/runtime/threading/ScriptExceptionHandler.java

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

99
import org.byteskript.skript.error.ScriptError;
1010

11+
import java.util.Arrays;
12+
1113
public class ScriptExceptionHandler implements Thread.UncaughtExceptionHandler, ScriptError {
1214

1315
@Override

src/test/java/org/byteskript/skript/test/SyntaxCreationTest.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,37 @@
1818
import org.junit.BeforeClass;
1919
import org.junit.Test;
2020

21+
import java.io.ByteArrayInputStream;
2122
import java.lang.reflect.Method;
23+
import java.nio.charset.StandardCharsets;
2224

2325
public class SyntaxCreationTest extends SkriptTest {
2426

2527
private static final Skript skript = new Skript();
2628
private static Script script;
29+
private static Script use;
2730

2831
@BeforeClass
2932
public static void start() throws Throwable {
3033
final PostCompileClass cls = skript.compileScript(SyntaxCreationTest.class.getClassLoader()
3134
.getResourceAsStream("syntax.bsk"), "skript.syntax");
3235
script = skript.loadScript(cls);
36+
skript.registerLibraryClass(cls.code());
37+
use = skript.loadScript(skript.compileScript(new ByteArrayInputStream("""
38+
function run_syntax:
39+
trigger:
40+
assert true
41+
my cool "hello" and 5
42+
set {var} to a "bean?"
43+
assert {var} exists
44+
assert {var} is "hello"
45+
""".getBytes(StandardCharsets.UTF_8)), "skript.syntax_test"));
46+
}
47+
48+
@Test
49+
public void run_syntax() throws Throwable {
50+
final Member function = use.getFunction("run_syntax");
51+
function.run(skript).get();
3352
}
3453

3554
@Test
@@ -71,7 +90,7 @@ public void test_expression() throws Throwable {
7190
public void test_set_property() throws Throwable {
7291
final Member function = script.getFunction("test_set_property");
7392
assert function != null;
74-
final Method method = script.mainClass().getDeclaredMethod("test_set_property", Object.class);
93+
final Method method = script.mainClass().getDeclaredMethod("test_set_property", Object.class, Object.class);
7594
assert !method.isAnnotationPresent(Expression.class);
7695
assert method.isAnnotationPresent(Property.class);
7796
final Property annotation = method.getAnnotation(Property.class);
@@ -84,7 +103,7 @@ public void test_set_property() throws Throwable {
84103
public void test_get_property() throws Throwable {
85104
final Member function = script.getFunction("test_get_property");
86105
assert function != null;
87-
final Method method = script.mainClass().getDeclaredMethod("test_get_property");
106+
final Method method = script.mainClass().getDeclaredMethod("test_get_property", Object.class);
88107
assert !method.isAnnotationPresent(Expression.class);
89108
assert method.isAnnotationPresent(Property.class);
90109
final Property annotation = method.getAnnotation(Property.class);
@@ -93,4 +112,34 @@ public void test_get_property() throws Throwable {
93112
assert annotation.type() == StandardHandlers.GET;
94113
}
95114

115+
@Test
116+
public void composite() throws Throwable {
117+
final String first = """
118+
function test_eff (name):
119+
syntax:
120+
effect: hello %String%
121+
trigger:
122+
print "hello"
123+
print {name}
124+
""";
125+
final String test = """
126+
function test_eff (name):
127+
syntax:
128+
effect: hello %String%
129+
trigger:
130+
assert true
131+
132+
on load:
133+
trigger:
134+
assert true
135+
hello "hi"
136+
137+
""";
138+
final Skript skript = new Skript();
139+
final PostCompileClass syntax = skript.compileScript(first, "skript.test_blob");
140+
skript.registerLibraryClass(syntax.code());
141+
final PostCompileClass output = skript.compileScript(test, "skript.test_blob");
142+
final Script script = skript.loadScript(output);
143+
}
144+
96145
}

src/test/resources/syntax.bsk

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ function test_effect(string, number):
33
syntax:
44
effect: my [cool] %String% and %Number%
55
trigger:
6-
print "hello"
6+
assert {string} is "hello"
7+
assert {number} is 5
78

89
function test_expression(string):
910
syntax:
@@ -12,14 +13,14 @@ function test_expression(string):
1213
trigger:
1314
return "hello"
1415

15-
function test_set_property(string):
16+
function test_set_property(thing, string):
1617
syntax:
1718
property: prop
1819
mode: set
1920
trigger:
2021
print "hello"
2122

22-
function test_get_property:
23+
function test_get_property(thing):
2324
syntax:
2425
property: prop
2526
mode: get

0 commit comments

Comments
 (0)