Skip to content

Commit

Permalink
Cleanup and add a test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
knirski committed Mar 11, 2017
1 parent 0328ba4 commit 87e53ec
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbti;

public interface ConsoleFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbti;

public interface ConsoleInterface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbti;

/** Public interface for repl responses. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbti;

public enum ConsoleResult {
Expand Down
7 changes: 0 additions & 7 deletions internal/compiler-bridge/src/main/java/xsbti/F0.java

This file was deleted.

11 changes: 0 additions & 11 deletions internal/compiler-bridge/src/main/java/xsbti/Logger.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package xsbtpamflet
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbt

import xsbti.Logger

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package xsbtpamflet
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbt

import scala.tools.nsc.interpreter.IR
import xsbti.ConsoleResult
Expand Down
79 changes: 18 additions & 61 deletions internal/compiler-bridge/src/main/scala/xsbt/ConsoleInterface.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package xsbtpamflet
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbt

import java.io.{ PrintWriter, StringWriter }

import xsbt.Message
import xsbti.Logger
import xsbtpamflet.ConsoleHelper._
import ConsoleHelper._

import scala.tools.nsc.interpreter.IMain
import scala.tools.nsc.{ GenericRunnerCommand, Settings }
Expand All @@ -14,61 +20,20 @@ class ConsoleInterface(args: Array[String], bootClasspathString: String,
loader: ClassLoader, bindNames: Array[String], bindValues: Array[AnyRef],
log: Logger) extends xsbti.ConsoleInterface {
lazy val interpreterSettings = MakeSettings.sync(args.toList, { message => log.error(Message(message)) })
val compilerSettings = MakeSettings.sync(args, bootClasspathString, classpathString, { message => log.error(Message(message)) })
// we need rt.jar from JDK, so java classpath is required
val useJavaCp = "-usejavacp"
val compilerSettings = MakeSettings.sync(args :+ useJavaCp, bootClasspathString, classpathString, { message => log.error(Message(message)) })
if (!bootClasspathString.isEmpty)
compilerSettings.bootclasspath.value = bootClasspathString
compilerSettings.classpath.value = classpathString
val outWriter: StringWriter = new StringWriter
val poutWriter: PrintWriter = new PrintWriter(outWriter)

// log.info(Message("Starting scala interpreter..."))
// log.info(Message(""))

// val loop = new InterpreterLoop(None, poutWriter) {
// override def createInterpreter() = {
// if (loader ne null) {
// in = InteractiveReader.createDefault()
// interpreter = new Interpreter(settings) {
// override protected def parentClassLoader = if (loader eq null) super.parentClassLoader else loader
// override protected def newCompiler(settings: Settings, reporter: Reporter) = super.newCompiler(compilerSettings, reporter)
// }
// interpreter.setContextClassLoader()
// } else
// super.createInterpreter()
//
// def bind(values: Seq[(String, Any)]) {
// // for 2.8 compatibility
// final class Compat {
// def bindValue(id: String, value: Any) =
// interpreter.bind(id, value.asInstanceOf[AnyRef].getClass.getName, value)
// }
// implicit def compat(a: AnyRef): Compat = new Compat
// for ((id, value) <- values)
// interpreter.beQuietDuring(interpreter.bindValue(id, value))
// }
//
// bind(bindNames zip bindValues)
//
// if (!initialCommands.isEmpty)
// interpreter.interpret(initialCommands)
// }
// override def closeInterpreter() {
// if (!cleanupCommands.isEmpty)
// interpreter.interpret(cleanupCommands)
// super.closeInterpreter()
// }
// }

val interpreter: IMain = new IMain(compilerSettings, new PrintWriter(outWriter)) {
def lastReq = prevRequestList.last
}

// val interpreter = new Interpreter(compilerSettings) {
// TODO: Fix this
// override protected def parentClassLoader = if (loader eq null) super.parentClassLoader else loader
// override protected def newCompiler(settings: Settings, reporter: Reporter) = super.newCompiler(compilerSettings, reporter)
//}
def interpret(line: String, synthetic: Boolean): ConsoleResponse =
override def interpret(line: String, synthetic: Boolean): ConsoleResponse =
{
clearBuffer()
val r = interpreter.interpret(line, synthetic)
Expand Down Expand Up @@ -103,17 +68,9 @@ object MakeSettings {
compilerSettings
}

def sync(options: List[String], onError: String => Unit) =
{
val settings = apply(options, onError)

// -Yrepl-sync is only in 2.9.1+
final class Compat {
def Yreplsync = settings.BooleanSetting("-Yrepl-sync", "For compatibility only.")
}
implicit def compat(s: Settings): Compat = new Compat

settings.Yreplsync.value = true
settings
}
def sync(options: List[String], onError: String => Unit) = {
val settings = apply(options, onError)
settings.Yreplsync.value = true
settings
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package xsbtpamflet
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbt

import xsbti.ConsoleResult

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package xsbt

import sbt.internal.util.UnitSpec
import sbt.util.Logger
import xsbti.ConsoleResult

// This is a specification to check the REPL block parsing.
class ConsoleInterfaceSpecification extends UnitSpec {

private val consoleFactory = new ConsoleFactory

def consoleWithArgs(args: String*) = consoleFactory.createConsole(
args = args.toArray,
bootClasspathString = "",
classpathString = "",
initialCommands = "",
cleanupCommands = "",
loader = this.getClass.getClassLoader,
bindNames = Array.empty,
bindValues = Array.empty,
log = Logger.Null
)

private val consoleWithoutArgs = consoleWithArgs()

"Scala interpreter" should "evaluate arithmetic expression" in {
val response = consoleWithoutArgs.interpret("1+1", false)
response.output.trim shouldBe "res0: Int = 2"
response.result shouldBe ConsoleResult.Success
}

it should "evaluate list constructor" in {
val response = consoleWithoutArgs.interpret("List(1,2)", false)
response.output.trim shouldBe "res1: List[Int] = List(1, 2)"
response.result shouldBe ConsoleResult.Success
}

it should "evaluate import" in {
val response = consoleWithoutArgs.interpret("import xsbt._", false)
response.output.trim shouldBe "import xsbt._"
response.result shouldBe ConsoleResult.Success
}

it should "mark partial expression as incomplete" in {
val response = consoleWithoutArgs.interpret("val a =", false)
response.result shouldBe ConsoleResult.Incomplete
}

it should "not evaluate incorrect expression" in {
val response = consoleWithoutArgs.interpret("1 ++ 1", false)
response.result shouldBe ConsoleResult.Error
}

val postfixOpExpression = "import scala.concurrent.duration._\nval t = 1 second"

it should "evaluate postfix op with a warning" in {
val response = consoleWithoutArgs.interpret(postfixOpExpression, false)
response.output.trim should startWith("warning")
response.result shouldBe ConsoleResult.Success
}

private val consoleWithPostfixOps = consoleWithArgs("-language:postfixOps")

it should "evaluate postfix op without warning when -language:postfixOps arg passed" in {
val response = consoleWithPostfixOps.interpret(postfixOpExpression, false)
response.output.trim should not startWith "warning"
response.result shouldBe ConsoleResult.Success
}

}

0 comments on commit 87e53ec

Please sign in to comment.