Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lihaoyi/Ammonite
Browse files Browse the repository at this point in the history
  • Loading branch information
Li Haoyi committed Aug 11, 2015
2 parents af17899 + cd5de73 commit 5a5f10a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
6 changes: 3 additions & 3 deletions repl/src/main/scala/ammonite/repl/interp/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait Evaluator{
* passing in the callback ensures the printing is still done lazily, but within
* the exception-handling block of the `Evaluator`
*/
def processLine(code: String, printCode: String, printer: Iterator[String] => Unit): Res[Evaluated]
def processLine(code: String, printCode: String, printer: Iterator[String] => Unit, extraImports: Seq[ImportData] = Seq()): Res[Evaluated]
def processScriptBlock(code: String, scriptImports: Seq[ImportData]): Res[Evaluated]

def previousImportBlock: String
Expand Down Expand Up @@ -154,14 +154,14 @@ object Evaluator{
type InvEx = InvocationTargetException
type InitEx = ExceptionInInitializerError

def processLine(code: String, printCode: String, printer: Iterator[String] => Unit) = for {
def processLine(code: String, printCode: String, printer: Iterator[String] => Unit, extraImports: Seq[ImportData] = Seq()) = for {
wrapperName <- Res.Success("cmd" + getCurrentLine)
_ <- Catching{ case e: ThreadDeath => interrupted() }
(classFiles, newImports) <- compileClass(wrapCode(
wrapperName,
code,
printCode,
previousImports.values.toSeq
previousImports.values.toSeq ++ extraImports
))
_ = Timer("eval.processLine compileClass end")
cls <- loadClass(wrapperName, classFiles)
Expand Down
16 changes: 8 additions & 8 deletions repl/src/main/scala/ammonite/repl/interp/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,22 @@ class Interpreter(prompt0: Ref[String],
} yield out
}

def evaluateLine(code: String, printSnippet: Seq[String], printer: Iterator[String] => Unit) = {
def evaluateLine(code: String, printSnippet: Seq[String], printer: Iterator[String] => Unit, extraImports: Seq[ImportData] = Seq() ) = {
val oldClassloader = Thread.currentThread().getContextClassLoader
try{
Thread.currentThread().setContextClassLoader(eval.evalClassloader)
eval.processLine(
code,
s"ammonite.repl.frontend.ReplBridge.repl.Internal.combinePrints(${printSnippet.mkString(", ")})",
printer
printer,
extraImports
)
} finally Thread.currentThread().setContextClassLoader(oldClassloader)
}

def processModule(code: String) = processScript(hardcodedPredef + "\n@\n" + code, eval.processScriptBlock)

def processExec(code: String) = processScript(hardcodedPredef + "\n@\n" + code, { (c, _) => evaluateLine(c, Seq(), _ => ()) })
def processExec(code: String) = processScript(hardcodedPredef + "\n@\n" + code, { (c, i) => evaluateLine(c, Seq(), _ => (), i)})

//common stuff in proccessModule and processExec
def processScript(code: String, evaluate: (String, Seq[ImportData]) => Res[Evaluated]): Unit = {
Expand All @@ -84,22 +85,21 @@ class Interpreter(prompt0: Ref[String],
else
loop(blocks.collect{ case Res.Success(o) => o }, Seq())
Timer("processScript 3")
@tailrec def loop(blocks: Seq[Preprocessor.Output], imports: Seq[ImportData]): Unit = {
@tailrec def loop(blocks: Seq[Preprocessor.Output], imports: Seq[Seq[ImportData]]): Unit = {
if(!blocks.isEmpty){
Timer("processScript loop 0")
val Preprocessor.Output(code, _) = blocks.head //pretty printing results is disabled for scripts
Timer("processScript loop 1")
val ev = evaluate(code, imports)
val ev = evaluate(code, imports.flatten)
Timer("processScript loop 2")
ev match {
case Res.Failure(msg) =>
throw new CompilationError(msg)
case Res.Success(ev) =>
eval.update(ev.imports)
loop(blocks.tail, imports ++ ev.imports)
loop(blocks.tail, imports :+ ev.imports)
case _ => loop(blocks.tail, imports)
}
}
} else imports.lastOption.foreach(eval.update(_))
}
}

Expand Down
3 changes: 3 additions & 0 deletions repl/src/test/resource/scripts/LimitImports.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val res = 1
@
val asd = 1
26 changes: 20 additions & 6 deletions repl/src/test/scala/ammonite/repl/ScriptTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ object ScriptTests extends TestSuite{
r: Int = 24
""")
}
'limitImports{
check.session(s"""
@ import ammonite.ops._

@ load.exec($printedScriptPath/"LimitImports.scala")

@ res
error: not found: value res
""")
}
}
'failures{
'syntaxError{
Expand Down Expand Up @@ -115,9 +125,6 @@ object ScriptTests extends TestSuite{
@ load.exec($printedScriptPath/"MultiBlockError.scala")
error: Compilation Failed

@ val r1 = res1
r1: Int = 1

@ val r2 = res2
error: Compilation Failed
Main.scala:\\d\\+: not found: value res2
Expand Down Expand Up @@ -179,6 +186,16 @@ object ScriptTests extends TestSuite{
r: Int = 24
""")
}
'limitImports{
check.session(s"""
@ import ammonite.ops._

@ load.module($printedScriptPath/"LimitImports.scala")

@ res
error: not found: value res
""")
}
}
'failures{
'syntaxError{
Expand Down Expand Up @@ -226,9 +243,6 @@ object ScriptTests extends TestSuite{
@ load.module($printedScriptPath/"MultiBlockError.scala")
error: Compilation Failed

@ val r1 = res1
r1: Int = 1

@ val r2 = res2
error: Compilation Failed
Main.scala:\\d\\+: not found: value res2
Expand Down
11 changes: 6 additions & 5 deletions terminal/src/main/scala/ammonite/terminal/ReadlineFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ object ReadlineFilters {
// Ctrl-y paste last cut

lazy val navFilter = orElseAll(
Case(Ctrl('b'))((b, c, m) => (b, c-1)), // <- one char
Case(Ctrl('f'))((b, c, m) => (b, c-1)), // -> one char
Case(Alt+"b")((b, c, m) => AdvancedFilters.wordLeft(b, c)), // <- one word
Case(Alt+"f")((b, c, m) => AdvancedFilters.wordRight(b, c)), // -> one word
Case(Ctrl('b'))((b, c, m) => (b, c - 1)), // <- one char
Case(Ctrl('f'))((b, c, m) => (b, c + 1)), // -> one char
Case(Alt + "b")((b, c, m) => AdvancedFilters.wordLeft(b, c)), // <- one word
Case(Alt + "f")((b, c, m) => AdvancedFilters.wordRight(b, c)), // -> one word
Case(Ctrl('a'))((b, c, m) => BasicFilters.moveStart(b, c, m.width)), // <- one line
Case(Ctrl('e'))((b, c, m) => BasicFilters.moveEnd(b, c, m.width)) // -> one line
)
Expand Down Expand Up @@ -73,7 +73,8 @@ object ReadlineFilters {
Case(Ctrl('k'))((b, c, m) => cutAllRight(b, c)),
Case(Alt + "d")((b, c, m) => cutWordRight(b, c)),
Case(Ctrl('w'))((b, c, m) => cutWordLeft(b, c)),
Case(Ctrl('y'))((b, c, m) => paste(b, c))
Case(Ctrl('y'))((b, c, m) => paste(b, c)),
Case(Alt + "\u007f")((b, c, m) => cutWordLeft(b, c))
)
}

Expand Down

0 comments on commit 5a5f10a

Please sign in to comment.