Skip to content

Commit

Permalink
DottyBytecodeTests#checkBCode: support .java files
Browse files Browse the repository at this point in the history
Will be used in the next commit.
  • Loading branch information
smarter committed Mar 12, 2020
1 parent 022bb18 commit bf283ee
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
16 changes: 11 additions & 5 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,21 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
}
}

def compileFromStrings(sourceCodes: String*): Unit = {
val sourceFiles = sourceCodes.map {sourceCode =>
val virtualFile = new VirtualFile(s"compileFromString-${java.util.UUID.randomUUID().toString}")
def compileFromStrings(scalaSources: List[String], javaSources: List[String] = Nil): Unit = {
def sourceFile(source: String, isJava: Boolean): SourceFile = {
val uuid = java.util.UUID.randomUUID().toString
val ext = if (isJava) ".java" else ".scala"
val virtualFile = new VirtualFile(s"compileFromString-$uuid.$ext")
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
writer.write(sourceCode)
writer.write(source)
writer.close()
new SourceFile(virtualFile, Codec.UTF8)
}
compileSources(sourceFiles.toList)
val sources =
scalaSources.map(sourceFile(_, isJava = false)) ++
javaSources.map(sourceFile(_, isJava = true))

compileSources(sources)
}

/** Print summary; return # of errors encountered */
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/DottyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ trait DottyTest extends ContextEscapeDetection {
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Context = {
val c = compilerWithChecker(checkAfterPhase)(assertion)
val run = c.newRun
run.compileFromStrings(source)
run.compileFromStrings(List(source))
run.runContext
}

Expand Down
7 changes: 5 additions & 2 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ trait DottyBytecodeTest {
ctx0.setSetting(ctx0.settings.outputDir, outputDir)
}

def checkBCode(scalaSource: String)(checkOutput: AbstractFile => Unit): Unit =
checkBCode(List(scalaSource))(checkOutput)

/** Checks source code from raw strings */
def checkBCode(sources: String*)(checkOutput: AbstractFile => Unit): Unit = {
def checkBCode(scalaSources: List[String], javaSources: List[String] = Nil)(checkOutput: AbstractFile => Unit): Unit = {
implicit val ctx: Context = initCtx

val compiler = new Compiler
val run = compiler.newRun
compiler.newRun.compileFromStrings(sources: _*)
compiler.newRun.compileFromStrings(scalaSources, javaSources)

checkOutput(ctx.settings.outputDir.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ class TestBCode extends DottyBytecodeTest {
|}
""".stripMargin

checkBCode(sourceA, sourceB) { dir =>
checkBCode(List(sourceA, sourceB)) { dir =>
val clsNodeA = loadClassNode(dir.lookupName("A.class", directory = false).input, skipDebugInfo = false)
val clsNodeB = loadClassNode(dir.lookupName("B.class", directory = false).input, skipDebugInfo = false)
val a1 = getMethod(clsNodeA, "a1")
Expand Down

0 comments on commit bf283ee

Please sign in to comment.