2
import java.io.{OutputStream, PrintStream}
3
import scala.runtime.ScalaRunTime.stringOf
5
/** A utility object that's needed by the code that executes a worksheet.
7
@deprecated("SI-6458: Instrumentation logic will be moved out of the compiler.","2.10.0")
8
object WorksheetSupport {
10
/** The offset in the source which should be printed */
11
private var currentOffset = 0
13
/** A stream that flushes in regular intervals so that output can be captured
14
* in real time. The flush interval is determined by the field "flushInterval".
15
* By default it is 30ms.
17
private class FlushedOutputStream(out: OutputStream) extends OutputStream {
18
protected def flushInterval = 30000000L // interval between flushes, by default 30ms
19
protected def width = 80 // output width, by default 80 characters
20
protected def tabInc = 8 // tab increment, by default 8 characters
21
private var lastFlush: Long = 0L
23
override def write(b: Array[Byte], off: Int, len: Int) = {
24
for (idx <- off until (off + len min b.length)) writeOne(b(idx))
27
override def write(c: Int) {
31
override def flush() {
32
val current = System.nanoTime
33
if (current - lastFlush >= flushInterval) {
38
def writeOne(c: Int) {
41
write((currentOffset+" ").getBytes)
46
else if (c == '\t') (col / tabInc) * tabInc + tabInc
48
if (col >= width) writeOne('\n')
50
def ensureNewLine() = if (col > 0) writeOne('\n')
53
private val flushedOut = new FlushedOutputStream(System.out)
54
private val printOut = new PrintStream(flushedOut)
56
private def redirected(op: => Unit) = {
57
val oldSysOut = System.out
58
val oldSysErr = System.err
59
val oldConsOut = Console.out
60
val oldConsErr = Console.err
61
System.setOut(printOut)
62
System.setErr(printOut)
63
Console.setOut(printOut)
64
Console.setErr(printOut)
68
System.setOut(oldSysOut)
69
System.setErr(oldSysErr)
70
Console.setOut(oldConsOut)
71
Console.setErr(oldConsErr)
75
def $execute(op: => Unit) = redirected {
78
case ex: StopException => ;
79
case ex: Throwable => ex.printStackTrace()
84
flushedOut.ensureNewLine()
88
def $stop() = throw new StopException
90
def $show(x: Any): String = stringOf(x)
93
class StopException extends Exception