Skip to content

Commit 873abbc

Browse files
committed
Allow redirection of print output.
1 parent e233ae5 commit 873abbc

File tree

7 files changed

+70
-16
lines changed

7 files changed

+70
-16
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.31</version>
9+
<version>1.0.32</version>
1010
<name>ByteSkript</name>
1111
<description>A compiled JVM implementation of the Skript language.</description>
1212

src/main/java/org/byteskript/skript/lang/syntax/flow/execute/RunAsyncEffect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
9393
final MethodErasure bootstrap = new MethodErasure(LambdaMetafactory.class.getMethod("metafactory", MethodHandles.Lookup.class, String.class, MethodType.class, MethodType.class, MethodHandle.class, MethodType.class));
9494
method.writeCode((writer, visitor) -> visitor.visitInvokeDynamicInsn("run", creator.getDescriptor(), new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", bootstrap.name(), bootstrap.getDescriptor(), false), org.objectweb.asm.Type.getType("()V"), new Handle(6, internal, runnable.name(), runnable.getDescriptor(), false), org.objectweb.asm.Type.getType("()V")));
9595
// todo hold synchronicity to following sleep
96+
// todo error protection?
9697
}
9798
final Method target = ExtractedSyntaxCalls.class.getMethod("runOnAsyncThread", Runnable.class);
9899
this.writeCall(method, target, context);

src/main/java/org/byteskript/skript/lang/syntax/generic/PrintEffect.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.byteskript.skript.compiler.Pattern;
1616
import org.byteskript.skript.compiler.SkriptLangSpec;
1717
import org.byteskript.skript.lang.element.StandardElements;
18+
import org.byteskript.skript.runtime.internal.ExtractedSyntaxCalls;
1819

1920
import java.io.PrintStream;
2021

@@ -37,17 +38,10 @@ public PrintEffect() {
3738
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "print %Object%");
3839
}
3940

40-
@Override
41-
public void preCompile(Context context, Pattern.Match match) throws Throwable {
42-
final MethodBuilder method = context.getMethod();
43-
method.writeCode(WriteInstruction.getField(System.class.getField("out")));
44-
super.preCompile(context, match);
45-
}
46-
4741
@Override
4842
public void compile(Context context, Pattern.Match match) throws Throwable {
4943
final MethodBuilder method = context.getMethod();
50-
method.writeCode(WriteInstruction.invokeVirtual(PrintStream.class.getMethod("println", Object.class)));
44+
method.writeCode(WriteInstruction.invokeStatic(ExtractedSyntaxCalls.class.getMethod("print", Object.class)));
5145
context.setState(CompileState.CODE_BODY);
5246
}
5347

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.byteskript.skript.error.ScriptCompileError;
2121
import org.byteskript.skript.error.ScriptLoadError;
2222
import org.byteskript.skript.error.ScriptRuntimeError;
23+
import org.byteskript.skript.runtime.event.Unload;
2324
import org.byteskript.skript.runtime.internal.*;
2425
import org.byteskript.skript.runtime.threading.*;
2526
import org.byteskript.skript.runtime.type.Converter;
@@ -83,6 +84,8 @@ public final class Skript {
8384
final List<Script> scripts = new ArrayList<>(); // the only strong reference, be careful!
8485
@Ignore
8586
final Map<Converter.Data, Converter<?, ?>> converters;
87+
@Ignore
88+
protected PrintStream out = System.out;
8689

8790
@Description("""
8891
Create a Skript runtime with a custom (non-default) Skript compiler.
@@ -745,6 +748,7 @@ public void unloadScript(Class<?> main) {
745748
""")
746749
@GenerateExample
747750
public void unloadScript(Script script) {
751+
final Unload unload = new Unload(script);
748752
script.stop();
749753
synchronized (events) {
750754
for (final EventHandler value : events.values()) {
@@ -755,8 +759,9 @@ public void unloadScript(Script script) {
755759
}
756760
}
757761
}
758-
scripts.remove(script);
762+
this.scripts.remove(script);
759763
UnsafeAccessor.graveyard(script);
764+
this.runEvent(unload);
760765
}
761766

762767
@Description("""
@@ -983,6 +988,26 @@ public Script loadScript(final File source, final String name)
983988
}
984989
}
985990

991+
//region Output
992+
993+
/**
994+
* Set the current print stream used by the `print` effect.
995+
* This can be used to redirect output in a particular state.
996+
*/
997+
public void setOutput(final PrintStream out) {
998+
if (out == null) this.out = System.out;
999+
else this.out = out;
1000+
}
1001+
1002+
public void println(Object object) {
1003+
this.out.println(object);
1004+
}
1005+
1006+
public void print(Object object) {
1007+
this.out.print(object);
1008+
}
1009+
//endregion
1010+
9861011
@Description("""
9871012
Generates a script thread from the given process.
9881013
""")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.runtime.event;
8+
9+
import mx.kenzie.autodoc.api.note.Ignore;
10+
import org.byteskript.skript.api.Event;
11+
import org.byteskript.skript.api.note.EventValue;
12+
import org.byteskript.skript.runtime.Script;
13+
14+
@Ignore
15+
public class Unload extends Event {
16+
17+
protected final String name, path;
18+
19+
public Unload(Script script) {
20+
this.name = script.getSimpleName();
21+
this.path = script.getPath();
22+
}
23+
24+
@EventValue("name")
25+
public String getName() {
26+
return name;
27+
}
28+
29+
@EventValue("script")
30+
public String getPath() {
31+
return path;
32+
}
33+
34+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public static void runOnMainThread(final Instruction<?> runnable) throws Throwab
9898
}
9999
}
100100

101+
public static void print(Object object) throws Throwable {
102+
final Skript skript = findInstance();
103+
skript.println(object);
104+
}
105+
101106
public static String readSystemInput() throws Throwable {
102107
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
103108
return reader.readLine();

src/test/resources/tests/monitor.bsk

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ function test:
44
set {lock} to a new object
55
monitor {lock}
66
set {thread} to the current process
7-
set {lock} to a new object
87
set {@thing} to false
98
set {var} to a new runnable:
109
monitor {lock}
11-
assert {@thing} is null
1210
set {@thing} to true
1311
wake {thread}
1412
monitor {lock}:
1513
assert {@thing} is false
16-
run {var} in the background
17-
delete {@thing}
18-
assert {@thing} is null
19-
sleep
14+
wait for {var}
2015
assert {@thing} is true
2116
delete {@thing}
2217
return true

0 commit comments

Comments
 (0)