6
6
import java .io .OutputStream ;
7
7
import java .io .PrintWriter ;
8
8
9
+ import scala .Console ;
10
+ import scala .Function0 ;
9
11
import scala .Option ;
12
+ import scala .runtime .AbstractFunction0 ;
10
13
import scala .tools .nsc .interpreter .ILoop ;
11
14
import scala .tools .nsc .Settings ;
12
15
13
16
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 ) {
15
18
// Without the below settings, there will be error:
16
19
// Failed to initialize compiler: object scala.runtime in compiler mirror not found.
17
20
// ** 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
23
26
// loader but the "java.class.path" system property does not contain Scala JARs
24
27
//
25
28
// 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 ();
27
30
28
31
// http://www.scala-lang.org/old/node/8002
29
32
settings .classpath ().value_$eq (classpath );
@@ -33,12 +36,32 @@ public static void run(ClassLoader cl, String classpath, InputStream in, OutputS
33
36
// http://stackoverflow.com/questions/5950025/multiple-instances-of-static-variables
34
37
settings .explicitParentLoader_$eq (Option .apply (cl ));
35
38
36
- ILoop repl = new ILoop (
39
+ final ILoop repl = new ILoop (
37
40
new BufferedReader (new InputStreamReader (in )),
38
41
new PrintWriter (out )
39
42
);
40
43
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
+ });
43
66
}
44
67
}
0 commit comments