Skip to content

Commit 5aedbf3

Browse files
committed
Embryonic but functioning JSR223 support
1 parent fb0e3bc commit 5aedbf3

File tree

5 files changed

+101
-1
lines changed

5 files changed

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