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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/* NEST (New Scala Test)
* Copyright 2007-2008 LAMP/EPFL
* @author Philipp Haller
*/
// $Id$
package scala.tools.partest.nest
import scala.tools.nsc.{Global, Settings, CompilerCommand}
import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}
import java.io.{File, BufferedReader, PrintWriter, FileWriter, StringWriter}
class ExtConsoleReporter(override val settings: Settings, reader: BufferedReader, var writer: PrintWriter) extends ConsoleReporter(settings, reader, writer) {
def this(settings: Settings) = {
this(settings, Console.in, new PrintWriter(new FileWriter("/dev/null")))
}
def hasWarnings: Boolean = WARNING.count != 0
}
abstract class SimpleCompiler {
def compile(file: File, kind: String): Boolean
def compile(file: File, kind: String, log: File): Boolean
}
class TestSettings extends {
override val bootclasspathDefault =
System.getProperty("sun.boot.class.path", "")
override val extdirsDefault =
System.getProperty("java.ext.dirs", "")
} with Settings(x => ())
class DirectCompiler(val fileManager: FileManager) extends SimpleCompiler {
def newGlobal(settings: Settings, reporter: Reporter): Global =
new Global(settings, reporter)
def newGlobal(settings: Settings, logWriter: FileWriter): Global = {
val rep = new ExtConsoleReporter(new TestSettings,
Console.in,
new PrintWriter(logWriter))
rep.shortname = true
newGlobal(settings, rep)
}
def newSettings = {
val settings = new TestSettings
settings.deprecation.value = true
settings.nowarnings.value = false
settings.encoding.value = "iso-8859-1"
settings
}
def newReporter(sett: Settings) = new ExtConsoleReporter(sett,
Console.in,
new PrintWriter(new StringWriter))
def compile(file: File, kind: String, log: File): Boolean = {
val testSettings = newSettings
val logWriter = new FileWriter(log)
val args = List.fromArray(fileManager.SCALAC_OPTS.split("\\s"))
val command = new CompilerCommand(args, testSettings, x => {}, false)
val global = newGlobal(command.settings, logWriter)
val testRep: ExtConsoleReporter = global.reporter.asInstanceOf[ExtConsoleReporter]
val test: TestFile = kind match {
case "pos" => PosTestFile(file, fileManager)
case "neg" => NegTestFile(file, fileManager)
case "run" => RunTestFile(file, fileManager)
case "jvm" => JvmTestFile(file, fileManager)
case "jvm5" => Jvm5TestFile(file, fileManager)
case "shootout" => ShootoutTestFile(file, fileManager)
}
test.defineSettings(testSettings)
val toCompile = List(file.getPath)
try {
(new global.Run) compile toCompile
testRep.printSummary
testRep.writer.flush
testRep.writer.close
} catch {
case e: Exception =>
e.printStackTrace()
return false
} finally {
logWriter.close()
}
!testRep.hasErrors
}
def compile(file: File, kind: String): Boolean = {
val testSettings = newSettings
val testRep = newReporter(testSettings)
val args = List.fromArray(fileManager.SCALAC_OPTS.split("\\s"))
val command = new CompilerCommand(args, testSettings, x => {}, false)
val global = newGlobal(command.settings, testRep)
val test: TestFile = kind match {
case "pos" => PosTestFile(file, fileManager)
case "neg" => NegTestFile(file, fileManager)
case "run" => RunTestFile(file, fileManager)
case "jvm" => JvmTestFile(file, fileManager)
case "jvm5" => Jvm5TestFile(file, fileManager)
case "shootout" => ShootoutTestFile(file, fileManager)
}
test.defineSettings(testSettings)
val toCompile = List(file.getPath)
try {
(new global.Run) compile toCompile
testRep.printSummary
testRep.writer.flush
testRep.writer.close
} catch {
case e: Exception =>
e.printStackTrace()
return false
}
!testRep.hasErrors
}
}
class ReflectiveCompiler(val fileManager: ConsoleFileManager) extends SimpleCompiler {
import fileManager.{latestCompFile, latestPartestFile}
val sepUrls = Array(latestCompFile.toURL, latestPartestFile.toURL)
//NestUI.verbose("constructing URLClassLoader from URLs "+latestCompFile+" and "+latestPartestFile)
val sepLoader = new java.net.URLClassLoader(sepUrls, null)
val sepCompilerClass =
sepLoader.loadClass("scala.tools.partest.nest.DirectCompiler")
val sepCompiler = sepCompilerClass.newInstance()
// needed for reflective invocation
val fileClass = Class.forName("java.io.File")
val stringClass = Class.forName("java.lang.String")
val sepCompileMethod =
sepCompilerClass.getMethod("compile", Array(fileClass, stringClass): _*)
val sepCompileMethod2 =
sepCompilerClass.getMethod("compile", Array(fileClass, stringClass, fileClass): _*)
/* This method throws java.lang.reflect.InvocationTargetException
* if the compiler crashes.
* This exception is handled in the shouldCompile and shouldFailCompile
* methods of class CompileManager.
*/
def compile(file: File, kind: String): Boolean = {
val fileArgs: Array[AnyRef] = Array(file, kind)
val res = sepCompileMethod.invoke(sepCompiler, fileArgs: _*).asInstanceOf[java.lang.Boolean]
res.booleanValue()
}
/* This method throws java.lang.reflect.InvocationTargetException
* if the compiler crashes.
* This exception is handled in the shouldCompile and shouldFailCompile
* methods of class CompileManager.
*/
def compile(file: File, kind: String, log: File): Boolean = {
val fileArgs: Array[AnyRef] = Array(file, kind, log)
val res = sepCompileMethod2.invoke(sepCompiler, fileArgs: _*).asInstanceOf[java.lang.Boolean]
res.booleanValue()
}
}
class CompileManager(val fileManager: FileManager) {
import scala.actors.Actor._
import scala.actors.{Actor, Exit, TIMEOUT}
var compiler: SimpleCompiler = new /*ReflectiveCompiler*/ DirectCompiler(fileManager)
var numSeparateCompilers = 1
def createSeparateCompiler() = {
numSeparateCompilers += 1
compiler = new /*ReflectiveCompiler*/ DirectCompiler(fileManager)
}
val delay = fileManager.timeout.toLong
def withTimeout(file: File)(thunk: => Boolean): Boolean = {
createSeparateCompiler()
val parent = self
self.trapExit = true
val child = link {
parent ! (self, thunk)
}
receiveWithin(delay) {
case TIMEOUT =>
println("compilation timed out")
false
case Exit(from, reason) if from == child =>
val From = from
reason match {
case 'normal =>
receive {
case (From, result: Boolean) => result
}
case t: Throwable =>
NestUI.verbose("while invoking compiler ("+file+"):")
NestUI.verbose("caught "+t)
t.printStackTrace
if (t.getCause != null)
t.getCause.printStackTrace
false
}
}
}
/* This method returns true iff compilation succeeds.
*/
def shouldCompile(file: File, kind: String, log: File): Boolean =
withTimeout(file) {
compiler.compile(file, kind, log)
}
/* This method returns true iff compilation fails
* _and_ the compiler does _not_ crash or loop.
*
* If the compiler crashes, this method returns false.
*/
def shouldFailCompile(file: File, kind: String, log: File): Boolean =
withTimeout(file) {
!compiler.compile(file, kind, log)
}
}
|