kardenal.mendoza kardenal.mendoza - 2 months ago 17
Scala Question

interrupting REPL ILoop programmatically

i have a wrapper for the scala REPL api (ILoop etc...) using the refactored 2.9.x classes.

this works well.

as this is a tool shipped to users, it is possible the user will type something like :

while(-1 < 1) {}


how can the user break from the currentlly executing code?

other posts talk about

<ctrl> + <c>


kind of working on some platforms under appropriate key bindings.

inspecting the scala REPL api and any available docs, it isnt obious how to actually implement this.

Answer Source

Check out how the Scala REPL does it in the file ILoopInit.scala. There the following signal interrupt handler is installed,

  protected def installSigIntHandler() {
    // ....
    SignalManager("INT") = {
      if (intp == null || intp.lineManager == null)
        onExit()
      else if (intp.lineManager.running)
        intp.lineManager.cancel()
      // ...
    }
  }

Note that the field intp: IMain is defined in class ILoop, which is the self-type of ILoopInit.

To summarize: in your SIGINT handler you can interrupt the REPL with intp.lineManager.cancel.