Skip to content

Commit 48f70d4

Browse files
committed
Embryonic but functioning JSR223 support
1 parent fb0e3bc commit 48f70d4

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
* @param factory the script engine factory
20+
*/
21+
class ScriptEngine(factory: ScriptEngine.Factory) extends AbstractScriptEngine {
22+
private[this] val driver = factory.driver
23+
private[this] val rendering = new Rendering
24+
private[this] var state: State = driver.initialState
25+
26+
def getFactory: ScriptEngineFactory = factory
27+
28+
def createBindings: Bindings = new SimpleBindings
29+
30+
/* Evaluate with the given context. */
31+
@throws[ScriptException]
32+
def eval(script: String, context: ScriptContext): Object = {
33+
val vid = state.valIndex
34+
state = driver.run(script)(state)
35+
val oid = state.objectIndex
36+
Class.forName(s"${str.REPL_SESSION_LINE}$oid", true, rendering.classLoader()(state.context))
37+
.getDeclaredMethods.find(_.getName == s"${str.REPL_RES_PREFIX}$vid")
38+
.map(_.invoke(null))
39+
.getOrElse(null)
40+
}
41+
42+
@throws[ScriptException]
43+
def eval(reader: Reader, context: ScriptContext): Object = {
44+
val writer = new StringWriter()
45+
var c = reader.read()
46+
while(c != -1) {
47+
writer.write(c)
48+
c = reader.read()
49+
}
50+
eval(writer.toString(), context)
51+
}
52+
}
53+
54+
object ScriptEngine {
55+
import java.util.Arrays
56+
import scala.util.Properties
57+
58+
class Factory extends ScriptEngineFactory {
59+
val driver = new ReplDriver(Array("-usejavacp", "-color:never"), Console.out, None)
60+
61+
def getEngineName = "Scala REPL"
62+
def getEngineVersion = "3.0"
63+
def getExtensions = Arrays.asList("scala")
64+
def getLanguageName = "Scala"
65+
def getLanguageVersion = Properties.versionString
66+
def getMimeTypes = Arrays.asList("application/x-scala")
67+
def getNames = Arrays.asList("scala")
68+
69+
def getMethodCallSyntax(obj: String, m: String, args: String*) = s"$obj.$m(${args.mkString(", ")})"
70+
71+
def getOutputStatement(toDisplay: String) = s"""print("$toDisplay")"""
72+
73+
def getParameter(key: String): Object = key match {
74+
case JScriptEngine.ENGINE => getEngineName
75+
case JScriptEngine.ENGINE_VERSION => getEngineVersion
76+
case JScriptEngine.LANGUAGE => getLanguageName
77+
case JScriptEngine.LANGUAGE_VERSION => getLanguageVersion
78+
case JScriptEngine.NAME => getNames.get(0)
79+
case _ => null
80+
}
81+
82+
def getProgram(statements: String*) = statements.mkString("; ")
83+
84+
def getScriptEngine: JScriptEngine = new ScriptEngine(this)
85+
}
86+
}
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)