Skip to content

Commit 2d14f14

Browse files
committed
Embryonic but functioning JSR223 support
1 parent fb0e3bc commit 2d14f14

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dotty.tools.repl.ScriptEngine$Factory

compiler/src/dotty/tools/repl/Rendering.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
2626
private[this] var myClassLoader: ClassLoader = _
2727

2828
/** Class loader used to load compiled code */
29-
private[this] def classLoader()(implicit ctx: Context) =
29+
private[repl] def classLoader()(implicit ctx: Context) =
3030
if (myClassLoader != null) myClassLoader
3131
else {
3232
val parent = parentClassLoader.getOrElse {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package dotty.tools
2+
package repl
3+
4+
import java.io.{Reader, StringWriter}
5+
import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine => JScriptEngine, ScriptEngineFactory, ScriptException, SimpleBindings}
6+
import dotc.core.StdNames.str
7+
8+
/** A JSR 223 (Scripting API) compatible wrapper around the REPL for improved
9+
* interoperability with software that supports it.
10+
*
11+
* It works by instantiating a new script engine through the script engine manager.
12+
* The script engine provides a eval method to evaluate scripts in string form.
13+
* Example use:
14+
*
15+
* val m = new javax.script.ScriptEngineManager()
16+
* val e = m.getEngineByName("scala")
17+
* println(e.eval("42"))
18+
*/
19+
class ScriptEngine extends AbstractScriptEngine {
20+
private[this] val driver = new ReplDriver(Array("-usejavacp", "-color:never"), Console.out, None)
21+
private[this] val rendering = new Rendering
22+
private[this] var state: State = driver.initialState
23+
24+
def getFactory: ScriptEngineFactory = new ScriptEngine.Factory
25+
26+
def createBindings: Bindings = new SimpleBindings
27+
28+
/* Evaluate with the given context. */
29+
@throws[ScriptException]
30+
def eval(script: String, context: ScriptContext): Object = {
31+
val vid = state.valIndex
32+
state = driver.run(script)(state)
33+
val oid = state.objectIndex
34+
Class.forName(s"${str.REPL_SESSION_LINE}$oid", true, rendering.classLoader()(state.context))
35+
.getDeclaredMethods.find(_.getName == s"${str.REPL_RES_PREFIX}$vid")
36+
.map(_.invoke(null))
37+
.getOrElse(null)
38+
}
39+
40+
@throws[ScriptException]
41+
def eval(reader: Reader, context: ScriptContext): Object = {
42+
val writer = new StringWriter()
43+
var c = reader.read()
44+
while(c != -1) {
45+
writer.write(c)
46+
c = reader.read()
47+
}
48+
eval(writer.toString(), context)
49+
}
50+
}
51+
52+
object ScriptEngine {
53+
import java.util.Arrays
54+
import scala.util.Properties
55+
56+
class Factory extends ScriptEngineFactory {
57+
def getEngineName = "Scala REPL"
58+
def getEngineVersion = "3.0"
59+
def getExtensions = Arrays.asList("scala")
60+
def getLanguageName = "Scala"
61+
def getLanguageVersion = Properties.versionString
62+
def getMimeTypes = Arrays.asList("application/x-scala")
63+
def getNames = Arrays.asList("scala")
64+
65+
def getMethodCallSyntax(obj: String, m: String, args: String*) = s"$obj.$m(${args.mkString(", ")})"
66+
67+
def getOutputStatement(toDisplay: String) = s"""print("$toDisplay")"""
68+
69+
def getParameter(key: String): Object = key match {
70+
case JScriptEngine.ENGINE => getEngineName
71+
case JScriptEngine.ENGINE_VERSION => getEngineVersion
72+
case JScriptEngine.LANGUAGE => getLanguageName
73+
case JScriptEngine.LANGUAGE_VERSION => getLanguageVersion
74+
case JScriptEngine.NAME => getNames.get(0)
75+
case _ => null
76+
}
77+
78+
def getProgram(statements: String*) = statements.mkString("; ")
79+
80+
def getScriptEngine: JScriptEngine = new ScriptEngine
81+
}
82+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val res0: Int = 42
2+
3+
42
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val m = new javax.script.ScriptEngineManager(getClass().getClassLoader())
4+
val e = m.getEngineByName("scala")
5+
println(e.eval("42"))
6+
}
7+
}
8+

0 commit comments

Comments
 (0)