Skip to content

Commit fe0fb9e

Browse files
committed
Fix #11 println("hello") outputs empty line
1 parent 4dd0912 commit fe0fb9e

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/main/java/scalive/Repl.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import java.io.OutputStream;
77
import java.io.PrintWriter;
88

9+
import scala.Console;
10+
import scala.Function0;
911
import scala.Option;
12+
import scala.runtime.AbstractFunction0;
1013
import scala.tools.nsc.interpreter.ILoop;
1114
import scala.tools.nsc.Settings;
1215

1316
public class Repl {
14-
public static void run(ClassLoader cl, String classpath, InputStream in, OutputStream out) {
17+
public static void run(ClassLoader cl, String classpath, final InputStream in, final OutputStream out) {
1518
// Without the below settings, there will be error:
1619
// Failed to initialize compiler: object scala.runtime in compiler mirror not found.
1720
// ** Note that as of 2.8 scala does not assume use of the java classpath.
@@ -23,7 +26,7 @@ public static void run(ClassLoader cl, String classpath, InputStream in, OutputS
2326
// loader but the "java.class.path" system property does not contain Scala JARs
2427
//
2528
// http://stackoverflow.com/questions/18150961/scala-runtime-in-compiler-mirror-not-found-but-working-when-started-with-xboo
26-
Settings settings = new Settings();
29+
final Settings settings = new Settings();
2730

2831
// http://www.scala-lang.org/old/node/8002
2932
settings.classpath().value_$eq(classpath);
@@ -33,12 +36,32 @@ public static void run(ClassLoader cl, String classpath, InputStream in, OutputS
3336
// http://stackoverflow.com/questions/5950025/multiple-instances-of-static-variables
3437
settings.explicitParentLoader_$eq(Option.apply(cl));
3538

36-
ILoop repl = new ILoop(
39+
final ILoop repl = new ILoop(
3740
new BufferedReader(new InputStreamReader(in)),
3841
new PrintWriter(out)
3942
);
4043

41-
// This call does not return until the stream (connection) is closed
42-
repl.process(settings);
44+
// https://github.com/xitrum-framework/scalive/issues/11
45+
// http://stackoverflow.com/questions/25623779/implementing-a-scala-function-in-java
46+
Console.withIn(in, new AbstractFunction0<Object>() {
47+
@Override
48+
public Object apply() {
49+
Console.withOut(out, new AbstractFunction0<Object>() {
50+
@Override
51+
public Object apply() {
52+
Console.withErr(out, new AbstractFunction0<Object>() {
53+
@Override
54+
public Object apply() {
55+
// This call does not return until the stream (connection) is closed
56+
repl.process(settings);
57+
return null;
58+
}
59+
});
60+
return null;
61+
}
62+
});
63+
return null;
64+
}
65+
});
4366
}
4467
}

src/main/java/scalive/Server.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import java.io.InputStream;
44
import java.io.OutputStream;
55
import java.io.PrintStream;
6-
import java.lang.reflect.Method;
76
import java.net.Socket;
87
import java.net.URLClassLoader;
98

109
public class Server {
1110
public static void serve(Socket client, String[] jarSearchDirs) throws Exception {
11+
// Create a REPL console and wire IO streams of the socket to it
12+
1213
InputStream in = client.getInputStream();
1314
OutputStream out = client.getOutputStream();
1415

@@ -24,10 +25,8 @@ public static void serve(Socket client, String[] jarSearchDirs) throws Exception
2425
URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
2526
loadDependencyJars(cl, jarSearchDirs);
2627

27-
String classpath = Classpath.getClasspath(cl);
28-
Class<?> repl = Class.forName("scalive.Repl");
29-
Method method = repl.getMethod("run", ClassLoader.class, String.class, InputStream.class, OutputStream.class);
30-
method.invoke(null, cl, classpath, in, out);
28+
String classpath = Classpath.getClasspath(cl);
29+
Repl.run(cl, classpath, in, out);
3130
} finally {
3231
System.setIn(oldIn);
3332
System.setOut(oldOut);

0 commit comments

Comments
 (0)