Skip to content

Embryonic but functioning JSR223 support #5686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotty.tools.repl.ScriptEngine$Factory
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
private[this] var myClassLoader: ClassLoader = _

/** Class loader used to load compiled code */
private[this] def classLoader()(implicit ctx: Context) =
private[repl] def classLoader()(implicit ctx: Context) =
if (myClassLoader != null) myClassLoader
else {
val parent = parentClassLoader.getOrElse {
Expand Down
74 changes: 74 additions & 0 deletions compiler/src/dotty/tools/repl/ScriptEngine.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dotty.tools
package repl

import java.io.Reader
import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine => JScriptEngine, ScriptEngineFactory, ScriptException, SimpleBindings}
import dotc.core.StdNames.str

/** A JSR 223 (Scripting API) compatible wrapper around the REPL for improved
* interoperability with software that supports it.
*
* It works by instantiating a new script engine through the script engine manager.
* The script engine provides a eval method to evaluate scripts in string form.
* Example use:
*
* val m = new javax.script.ScriptEngineManager()
* val e = m.getEngineByName("scala")
* println(e.eval("42"))
*/
class ScriptEngine extends AbstractScriptEngine {
private[this] val driver = new ReplDriver(Array("-usejavacp", "-color:never"), Console.out, None)
private[this] val rendering = new Rendering
private[this] var state: State = driver.initialState

def getFactory: ScriptEngineFactory = new ScriptEngine.Factory

def createBindings: Bindings = new SimpleBindings

/* Evaluate with the given context. */
@throws[ScriptException]
def eval(script: String, context: ScriptContext): Object = {
val vid = state.valIndex
state = driver.run(script)(state)
val oid = state.objectIndex
Class.forName(s"${str.REPL_SESSION_LINE}$oid", true, rendering.classLoader()(state.context))
.getDeclaredMethods.find(_.getName == s"${str.REPL_RES_PREFIX}$vid")
.map(_.invoke(null))
.getOrElse(null)
}

@throws[ScriptException]
def eval(reader: Reader, context: ScriptContext): Object = throw new UnsupportedOperationException
}

object ScriptEngine {
import java.util.Arrays
import scala.util.Properties

class Factory extends ScriptEngineFactory {
def getEngineName = "Scala REPL"
def getEngineVersion = "3.0"
def getExtensions = Arrays.asList("scala")
def getLanguageName = "Scala"
def getLanguageVersion = Properties.versionString
def getMimeTypes = Arrays.asList("application/x-scala")
def getNames = Arrays.asList("scala")

def getMethodCallSyntax(obj: String, m: String, args: String*) = s"$obj.$m(${args.mkString(", ")})"

def getOutputStatement(toDisplay: String) = s"""print("$toDisplay")"""

def getParameter(key: String): Object = key match {
case JScriptEngine.ENGINE => getEngineName
case JScriptEngine.ENGINE_VERSION => getEngineVersion
case JScriptEngine.LANGUAGE => getLanguageName
case JScriptEngine.LANGUAGE_VERSION => getLanguageVersion
case JScriptEngine.NAME => getNames.get(0)
case _ => null
}

def getProgram(statements: String*) = statements.mkString("; ")

def getScriptEngine: JScriptEngine = new ScriptEngine
}
}
3 changes: 3 additions & 0 deletions tests/run-with-compiler/scripting.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val res0: Int = 42

42
8 changes: 8 additions & 0 deletions tests/run-with-compiler/scripting.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test {
def main(args: Array[String]): Unit = {
val m = new javax.script.ScriptEngineManager(getClass().getClassLoader())
val e = m.getEngineByName("scala")
println(e.eval("42"))
}
}