Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reloading #15

Merged
merged 4 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/org/byteskript/skript/runtime/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,18 +758,26 @@ public void unloadScript(Class<?> main) {
public void unloadScript(Script script) {
final Unload unload = new Unload(script);
script.stop();

loaders.removeIf(ref -> ref.refersTo((ScriptClassLoader) script.mainClass().getClassLoader()));
this.runEvent(unload);

synchronized (events) {
for (final EventHandler value : events.values()) {
final List<Class<? extends Event>> toRemove = new ArrayList<>();
for (final Map.Entry<Class<? extends Event>, EventHandler> entry : events.entrySet()) {
final EventHandler value = entry.getValue();
for (final ScriptRunner trigger : value.getTriggers().toArray(new ScriptRunner[0])) {
if (trigger.owner() != script.mainClass()) continue;
value.getTriggers().remove(trigger);
UnsafeAccessor.graveyard(trigger);
toRemove.add(entry.getKey());
}
}

for (final Class<? extends Event> clazz : toRemove) events.remove(clazz);
}
this.scripts.remove(script);
UnsafeAccessor.graveyard(script);
this.runEvent(unload);
}

@Description("""
Expand Down Expand Up @@ -843,7 +851,8 @@ private SkriptMirror createLoader() {
@GenerateExample
public Script loadScript(final PostCompileClass[] data) {
final Class<?>[] classes = new Class[data.length];
for (int i = 0; i < data.length; i++) classes[i] = this.loadClass(data[i].name(), data[i].code());
final SkriptMirror loader = createLoader();
for (int i = 0; i < data.length; i++) classes[i] = loader.loadClass(data[i].name(), data[i].code());
return this.loadScript(classes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public Member(Script script, Method method, boolean async) {
this.script = script;
this.async = async;
this.parameters = method.getParameterCount();
final Mirror<?> mirror = Mirror.of(script.mainClass()).useProvider(script.skriptInstance().getLoader());
final Mirror<?> mirror = Mirror.of(script.mainClass()).useProvider(
(ScriptClassLoader) script.mainClass().getClassLoader());
this.invoker = mirror.method(method);
this.verifier = mirror.method(method.getName() + "_verify", method.getParameterTypes());
}
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/byteskript/skript/test/RecompileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.byteskript.skript.test;

import org.byteskript.skript.runtime.*;
import org.byteskript.skript.runtime.event.Load;
import org.junit.Test;

import java.io.*;

public class RecompileTest extends SkriptTest {

private static final Skript skript = new Skript();

private static Script loadSync(final String source) {
final Script script = skript.assembleScript(
skript.compileScript(source, "skript.recompilation.bsk"));
skript.runEvent(new Load.LoadThis(script), script).all().join();
return script;
}

@Test
public void testRecompile() {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
skript.setOutput(new PrintStream(output));

Script script = loadSync("""
on script load:
trigger:
print "Foo"
""");
skript.unloadScript(script);

script = loadSync("""
on script load:
trigger:
print "Bar"
""");
skript.unloadScript(script);

assert output.toString().equals("Foo\nBar\n");
}

}