Skip to content

Commit 373f0e8

Browse files
committed
Embryonic but functioning JSR223 support
1 parent fb0e3bc commit 373f0e8

File tree

5 files changed

+103
-1
lines changed

5 files changed

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