Skip to content

Commit 5c16eed

Browse files
committed
Nice documentation of useful methods.
1 parent 9d8d4a8 commit 5c16eed

20 files changed

+663
-68
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<artifactId>byteskript</artifactId>
99
<version>1.0.9</version>
1010
<name>ByteSkript</name>
11+
<description>A compiled JVM implementation of the Skript language.</description>
1112

1213
<properties>
1314
<maven.compiler.source>17</maven.compiler.source>

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
load script {file} as "skript/myscript"
3131
load script {code} as "skript/myscript"
3232
"""
33-
} // todo
33+
}
3434
)
3535
public class LoadScriptEffect extends Effect {
3636

@@ -40,11 +40,12 @@ public LoadScriptEffect() {
4040
}
4141

4242
@ForceExtract
43+
@SuppressWarnings("deprecation")
4344
public static void loadScript(Object object, String name) throws IOException {
4445
if (object instanceof File file)
45-
Skript.currentInstance().compileLoad(file, name);
46+
Skript.localInstance().compileLoad(file, name);
4647
else if (object instanceof String string)
47-
Skript.currentInstance()
48+
Skript.localInstance()
4849
.compileLoad(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), name);
4950
}
5051

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public UnloadScriptEffect() {
4040
@ForceExtract
4141
public static void unloadScript(Object object) throws IOException {
4242
if (object instanceof Class main)
43-
Skript.currentInstance().unloadScript(main);
43+
Skript.localInstance().unloadScript(main);
4444
else if (object instanceof Script script)
45-
Skript.currentInstance().unloadScript(script);
45+
Skript.localInstance().unloadScript(script);
4646
else if (object instanceof String name)
47-
Skript.currentInstance().unloadScript(Skript.LOADER.findClass(name.replace('/', '.')));
47+
Skript.localInstance().unloadScript(Skript.LOADER.findClass(name.replace('/', '.')));
4848
}
4949

5050
}

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

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,33 @@
2121
import java.lang.reflect.Method;
2222
import java.util.*;
2323

24+
/**
25+
* A handle representation of a script containing its metadata and defined classes.
26+
* <p>
27+
* This can be used to find insights about a script or find a particular function handle.
28+
* <p>
29+
* These should not be stored - they can be arbitrarily graveyarded without any notification
30+
* by the unloader, in which case this will become a dead spot in memory.
31+
*/
2432
public final class Script {
2533
private final File sourceFile;
2634
private final Class<?>[] classes;
2735
private final String name;
2836
private final Map<String, Member> functions;
29-
private final List<Member> events;
3037
private final Collection<SourceData> data;
3138
private final Structure[] members;
39+
private final Skript skript;
3240

33-
public Script(Skript skript, File sourceFile, Class<?>... classes) {
41+
Script(Skript skript, File sourceFile, Class<?>... classes) {
3442
this(true, skript, sourceFile, classes);
3543
}
3644

37-
public Script(boolean init, Skript skript, File sourceFile, Class<?>... classes) {
45+
Script(boolean init, Skript skript, File sourceFile, Class<?>... classes) {
46+
this.skript = skript;
3847
this.sourceFile = sourceFile;
3948
this.classes = classes;
4049
this.name = mainClass().getName();
4150
this.functions = new HashMap<>();
42-
this.events = new ArrayList<>();
4351
this.data = new ArrayList<>();
4452
final List<Structure> structures = new ArrayList<>();
4553
for (final Class<?> type : classes) {
@@ -59,20 +67,19 @@ public Script(boolean init, Skript skript, File sourceFile, Class<?>... classes)
5967
} else if (method.isAnnotationPresent(EventData.class)) {
6068
final EventData event = method.getAnnotation(EventData.class);
6169
final Member member = new Member(this, method, event.async());
62-
this.events.add(member);
6370
skript.registerEventHandler((Class<? extends Event>) skript.getClass(event.event()), new InvokingScriptRunner(mainClass(), member));
6471
}
6572
}
6673
this.members = structures.toArray(new Structure[0]);
6774
if (init) {
68-
verify();
69-
load(skript);
75+
this.verify();
76+
this.load(skript);
7077
}
7178
}
7279

7380
void verify() {
74-
forceLoad(mainClass());
75-
for (Map.Entry<String, Member> entry : functions.entrySet()) {
81+
this.forceLoad(mainClass());
82+
for (final Map.Entry<String, Member> entry : functions.entrySet()) {
7683
final Member value = entry.getValue();
7784
final String name = entry.getKey();
7885
try {
@@ -88,34 +95,81 @@ void load(Skript skript) {
8895
skript.runEvent(new Load(this));
8996
}
9097

98+
/**
99+
* The simple name of the main class for this script.
100+
* This will be something in the format `script`
101+
*
102+
* @return the simple name
103+
*/
91104
public String getSimpleName() {
92105
return mainClass().getSimpleName();
93106
}
94107

108+
/**
109+
* The path of the main class for this script.
110+
* This will be something in the format `skript.path.to.script`
111+
*
112+
* @return the path
113+
*/
95114
public String getPath() {
96115
return mainClass().getName();
97116
}
98117

118+
/**
119+
* Gets the main class of this script, which root-level members occupy.
120+
* Custom types and other members may be moved to other classes, depending on the compiler used.
121+
* This should not be stored - it will prevent the script unloading safely.
122+
*
123+
* @return the main class
124+
*/
99125
public Class<? extends CompiledScript> mainClass() {
100126
return (Class<? extends CompiledScript>) classes[0];
101127
}
102128

129+
/**
130+
* Whether this script has a known source file.
131+
*
132+
* @return the source file
133+
*/
103134
public boolean hasSourceFile() {
104135
return sourceFile != null && sourceFile.exists() && sourceFile.isFile();
105136
}
106137

138+
/**
139+
* Returns a handle for the function with this name.
140+
* Multiple functions may have the same name, this will return an arbitrary one.
141+
*
142+
* @param name the function name
143+
* @return the function
144+
*/
107145
public Member getFunction(String name) {
108146
return functions.get(name);
109147
}
110148

149+
/**
150+
* Finds source data annotations for members in this script.
151+
*
152+
* @return member data
153+
*/
111154
public Collection<SourceData> getMemberData() {
112155
return data;
113156
}
114157

158+
/**
159+
* Returns the known source file for this script.
160+
*
161+
* @return potentially null source file
162+
*/
115163
public File sourceFile() {
116164
return sourceFile;
117165
}
118166

167+
/**
168+
* Returns the known classes for this script.
169+
* This will include the main class and any custom types, etc.
170+
*
171+
* @return the classes
172+
*/
119173
public Class<?>[] classes() {
120174
return classes;
121175
}
@@ -126,6 +180,11 @@ public String toString() {
126180
"name=" + name + ']';
127181
}
128182

183+
/**
184+
* Returns the structures for all members, used for data collection.
185+
*
186+
* @return all found structures
187+
*/
129188
public Structure[] getMembers() {
130189
return members;
131190
}
@@ -137,5 +196,12 @@ private void forceLoad(Class<?> cls) {
137196
}
138197
}
139198

140-
199+
/**
200+
* Returns the Skript runtime that created this script.
201+
*
202+
* @return the runtime
203+
*/
204+
public Skript skriptInstance() {
205+
return skript;
206+
}
141207
}

0 commit comments

Comments
 (0)