blob: 8a918a829c5815492210634ba6e81e5e5b7a96e0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package scala.tools.nsc.scratchpad
import java.io.{PrintStream, OutputStreamWriter, Writer}
import scala.runtime.ScalaRunTime.stringOf
import java.lang.reflect.InvocationTargetException
import scala.reflect.ReflectionUtils._
object Executor {
println("exec started")
private var currentWriter: CommentWriter = null
/** Execute module with given name, redirecting all output to given
* source inserter. Catch all exceptions and print stacktrace of underlying causes.
*/
def execute(name: String, si: SourceInserter, classLoader: ClassLoader = getClass.getClassLoader) {
val oldSysOut = System.out
val oldSysErr = System.err
val oldConsOut = Console.out
val oldConsErr = Console.err
val oldCwr = currentWriter
currentWriter = new CommentWriter(si)
val newOut = new PrintStream(new CommentOutputStream(currentWriter))
System.setOut(newOut)
System.setErr(newOut)
Console.setOut(newOut)
Console.setErr(newOut)
try {
singletonInstance(classLoader, name)
} catch {
case ex: Throwable =>
unwrapThrowable(ex) match {
case _: StopException => ;
case cause => cause.printStackTrace()
}
} finally {
currentWriter.close()
System.setOut(oldSysOut)
System.setErr(oldSysErr)
Console.setOut(oldConsOut)
Console.setErr(oldConsErr)
currentWriter = oldCwr
}
}
def $skip(n: Int) = currentWriter.skip(n)
def $stop() = throw new StopException
def $show(x: Any): String = stringOf(x, scala.Int.MaxValue)
}
class StopException extends Exception
|