Skip to content

Commit 9d8d4a8

Browse files
committed
Script-managing syntax.
1 parent 0ec2972 commit 9d8d4a8

File tree

11 files changed

+159
-15
lines changed

11 files changed

+159
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
import org.byteskript.skript.lang.syntax.map.KeyInMap;
5353
import org.byteskript.skript.lang.syntax.map.MapCreator;
5454
import org.byteskript.skript.lang.syntax.maths.*;
55-
import org.byteskript.skript.lang.syntax.script.CurrentScriptExpression;
56-
import org.byteskript.skript.lang.syntax.script.LoadScriptEffect;
57-
import org.byteskript.skript.lang.syntax.script.UnloadScriptEffect;
55+
import org.byteskript.skript.lang.syntax.script.*;
5856
import org.byteskript.skript.lang.syntax.timing.*;
5957
import org.byteskript.skript.lang.syntax.type.*;
6058
import org.byteskript.skript.lang.syntax.type.property.FinalEntry;
@@ -212,6 +210,8 @@ private SkriptLangSpec() {
212210
new ExternalFunctionExpression(),
213211
new PropertyFunctionExpression(),
214212
new FunctionExpression(),
213+
new CompilerExpression(),
214+
new LoadedScriptsExpression(),
215215
new CurrentScriptExpression(),
216216
new CurrentEventExpression(),
217217
new NoArgsFunctionExpression(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.lang.syntax.script;
8+
9+
import mx.kenzie.foundation.Type;
10+
import org.byteskript.skript.api.note.Documentation;
11+
import org.byteskript.skript.api.syntax.SimpleExpression;
12+
import org.byteskript.skript.compiler.CommonTypes;
13+
import org.byteskript.skript.compiler.SkriptLangSpec;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
import org.byteskript.skript.lang.handler.StandardHandlers;
16+
import org.byteskript.skript.runtime.internal.ExtractedSyntaxCalls;
17+
18+
@Documentation(
19+
name = "Compiler",
20+
description = """
21+
Obtains the current compiler. This may not exist.
22+
This can be used to check whether this installation is able to load scripts.
23+
If the compiler is `null`, the load effect will be unavailable.
24+
""",
25+
examples = {
26+
"""
27+
set {var} to the compiler
28+
if the compiler exists:
29+
print "can load scripts :)"
30+
"""
31+
}
32+
)
33+
public class CompilerExpression extends SimpleExpression {
34+
35+
public CompilerExpression() {
36+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "[the] compiler");
37+
handlers.put(StandardHandlers.GET, findMethod(ExtractedSyntaxCalls.class, "getCompiler"));
38+
}
39+
40+
@Override
41+
public Type getReturnType() {
42+
return CommonTypes.OBJECT;
43+
}
44+
45+
@Override
46+
public boolean allowAsInputFor(Type type) {
47+
return CommonTypes.OBJECT.equals(type) || super.allowAsInputFor(type);
48+
}
49+
50+
}

src/main/java/org/byteskript/skript/lang/syntax/script/LoadScriptEffect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
name = "Load Script",
2424
description = """
2525
Loads a script from the given file or string source code.
26+
Not all installations can load scripts after start-up: check whether the compiler exists first.
2627
""",
2728
examples = {
2829
"""
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.lang.syntax.script;
8+
9+
import mx.kenzie.foundation.Type;
10+
import org.byteskript.skript.api.note.Documentation;
11+
import org.byteskript.skript.api.syntax.SimpleExpression;
12+
import org.byteskript.skript.compiler.CommonTypes;
13+
import org.byteskript.skript.compiler.SkriptLangSpec;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
import org.byteskript.skript.lang.handler.StandardHandlers;
16+
import org.byteskript.skript.runtime.internal.ExtractedSyntaxCalls;
17+
18+
@Documentation(
19+
name = "Loaded Scripts",
20+
description = """
21+
A list of the main classes for all loaded scripts.
22+
Storing these will prevent them being unloaded safely.
23+
""",
24+
examples = {
25+
"""
26+
set {list} to the loaded scripts
27+
loop {script} in {list}:
28+
print name of {script}
29+
"""
30+
}
31+
)
32+
public class LoadedScriptsExpression extends SimpleExpression {
33+
34+
public LoadedScriptsExpression() {
35+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "[(the|all)] loaded scripts");
36+
handlers.put(StandardHandlers.GET, findMethod(ExtractedSyntaxCalls.class, "getLoadedScripts"));
37+
}
38+
39+
@Override
40+
public Type getReturnType() {
41+
return CommonTypes.LIST;
42+
}
43+
44+
@Override
45+
public boolean allowAsInputFor(Type type) {
46+
return CommonTypes.LIST.equals(type) || super.allowAsInputFor(type);
47+
}
48+
49+
}

src/main/java/org/byteskript/skript/lang/syntax/script/UnloadScriptEffect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"""
2929
unload script skript/myscript
3030
"""
31-
} // todo
31+
}
3232
)
3333
public class UnloadScriptEffect extends Effect {
3434

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ public static void runOnAsyncThread(final Runnable runnable) {
160160
//endregion
161161

162162
//region Script Control
163+
164+
/**
165+
* Gets a copy of the handles for all loaded scripts.
166+
* Storing a strong reference to these will prevent them being unloaded safely.
167+
* These can be graveyarded (annulled in memory) without warning.
168+
* <p>
169+
* This is designed for looping and discarding.
170+
*
171+
* @return the scripts owned by this instance
172+
*/
173+
public Script[] getScripts() {
174+
return scripts.toArray(new Script[0]);
175+
}
176+
163177
public Collection<OperationController> getProcesses() {
164178
return processes;
165179
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.security.PrivilegedActionException;
1313
import java.security.PrivilegedExceptionAction;
1414

15-
@SuppressWarnings("removal")
15+
@SuppressWarnings({"removal"})
1616
class UnsafeAccessor {
1717

1818
private static final Unsafe UNSAFE;

src/main/java/org/byteskript/skript/runtime/internal/ExtractedSyntaxCalls.java

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

99
import mx.kenzie.mirror.MethodAccessor;
1010
import org.byteskript.skript.error.ScriptRuntimeError;
11+
import org.byteskript.skript.runtime.Script;
1112
import org.byteskript.skript.runtime.Skript;
1213
import org.byteskript.skript.runtime.threading.ScriptThread;
14+
import org.byteskript.skript.runtime.type.DataList;
1315

1416
import java.io.BufferedReader;
1517
import java.io.InputStreamReader;
@@ -21,6 +23,18 @@
2123

2224
public class ExtractedSyntaxCalls {
2325

26+
public static ModifiableCompiler getCompiler() {
27+
return Skript.currentInstance().getCompiler();
28+
}
29+
30+
public static DataList getLoadedScripts() {
31+
final DataList list = new DataList();
32+
for (final Script script : Skript.currentInstance().getScripts()) {
33+
list.add(script.mainClass());
34+
}
35+
return list;
36+
}
37+
2438
public static String getSystemInput() throws Throwable {
2539
final Instruction<String> instruction = new Instruction<>() {
2640
private String value;

src/test/java/org/byteskript/skript/test/InlineTest.java renamed to src/test/java/org/byteskript/skript/test/ScriptsTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
import org.junit.BeforeClass;
1414
import org.junit.Test;
1515

16-
public class InlineTest extends SkriptTest {
16+
public class ScriptsTest extends SkriptTest {
1717

1818
private static final Skript skript = new Skript();
1919
private static Script script;
2020

2121
@BeforeClass
2222
public static void start() throws Throwable {
23-
final PostCompileClass cls = skript.compileScript(InlineTest.class.getClassLoader()
24-
.getResourceAsStream("inline.bsk"), "skript.inline");
23+
final PostCompileClass cls = skript.compileScript(ScriptsTest.class.getClassLoader()
24+
.getResourceAsStream("scripts_test.bsk"), "skript.scripts_test");
2525
script = skript.loadScript(cls);
2626
}
2727

@@ -32,4 +32,11 @@ public void simple() throws Throwable {
3232
function.invoke();
3333
}
3434

35+
@Test
36+
public void test_load() throws Throwable {
37+
final Member function = script.getFunction("test_load");
38+
assert function != null;
39+
function.invoke();
40+
}
41+
3542
}

src/test/resources/inline.bsk

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)