| author | wenzelm |
| Sat, 23 May 2020 22:09:55 +0200 | |
| changeset 71873 | a7b81dd9954e |
| parent 71872 | b5191ededb6c |
| child 71874 | 9d31fe4ecaea |
| permissions | -rw-r--r-- |
| 71849 | 1 |
/* Title: Pure/System/scala.scala |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
3 |
|
| 71849 | 4 |
Support for Scala at runtime. |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
6 |
|
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
8 |
|
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
9 |
|
| 71864 | 10 |
import java.lang.reflect.{Modifier, InvocationTargetException}
|
11 |
import java.io.{File => JFile, StringWriter, PrintWriter}
|
|
|
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
12 |
|
| 43748 | 13 |
import scala.util.matching.Regex |
| 71868 | 14 |
import scala.tools.nsc.{GenericRunnerSettings, ConsoleWriter, NewLinePrintWriter}
|
| 71864 | 15 |
import scala.tools.nsc.interpreter.IMain |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
16 |
|
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
17 |
|
| 71849 | 18 |
object Scala |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
19 |
{
|
| 71870 | 20 |
/** compiler **/ |
| 71850 | 21 |
|
| 71864 | 22 |
object Compiler |
| 71850 | 23 |
{
|
| 71870 | 24 |
lazy val default_context: Context = context() |
25 |
||
| 71868 | 26 |
def context( |
27 |
error: String => Unit = Exn.error, |
|
28 |
jar_dirs: List[JFile] = Nil): Context = |
|
| 71864 | 29 |
{
|
30 |
def find_jars(dir: JFile): List[String] = |
|
31 |
File.find_files(dir, file => file.getName.endsWith(".jar")).
|
|
32 |
map(File.absolute_name) |
|
33 |
||
34 |
val class_path = |
|
35 |
for {
|
|
36 |
prop <- List("scala.boot.class.path", "java.class.path")
|
|
37 |
path = System.getProperty(prop, "") if path != "\"\"" |
|
38 |
elem <- space_explode(JFile.pathSeparatorChar, path) |
|
39 |
} yield elem |
|
40 |
||
| 71868 | 41 |
val settings = new GenericRunnerSettings(error) |
42 |
settings.classpath.value = |
|
43 |
(class_path ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator) |
|
| 71850 | 44 |
|
| 71868 | 45 |
new Context(settings) |
| 71864 | 46 |
} |
| 71850 | 47 |
|
| 71868 | 48 |
def default_print_writer: PrintWriter = |
49 |
new NewLinePrintWriter(new ConsoleWriter, true) |
|
50 |
||
51 |
class Context private [Compiler](val settings: GenericRunnerSettings) |
|
| 71864 | 52 |
{
|
| 71869 | 53 |
override def toString: String = settings.toString |
54 |
||
| 71868 | 55 |
def interpreter( |
56 |
print_writer: PrintWriter = default_print_writer, |
|
57 |
class_loader: ClassLoader = null): IMain = |
|
58 |
{
|
|
59 |
new IMain(settings, print_writer) |
|
60 |
{
|
|
61 |
override def parentClassLoader: ClassLoader = |
|
62 |
if (class_loader == null) super.parentClassLoader |
|
63 |
else class_loader |
|
64 |
} |
|
65 |
} |
|
| 71850 | 66 |
|
| 71868 | 67 |
def toplevel(source: String): List[String] = |
68 |
{
|
|
69 |
val out = new StringWriter |
|
70 |
val interp = interpreter(new PrintWriter(out)) |
|
71 |
val rep = new interp.ReadEvalPrint |
|
72 |
val ok = interp.withLabel("\u0001") { rep.compile(source) }
|
|
73 |
out.close |
|
| 71864 | 74 |
|
| 71868 | 75 |
val Error = """(?s)^\S* error: (.*)$""".r |
76 |
val errors = |
|
77 |
space_explode('\u0001', Library.strip_ansi_color(out.toString)).
|
|
78 |
collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) })
|
|
79 |
||
80 |
if (!ok && errors.isEmpty) List("Error") else errors
|
|
81 |
} |
|
| 71872 | 82 |
|
83 |
def check(body: String): List[String] = |
|
84 |
{
|
|
85 |
try { toplevel("package test\nobject Test { " + body + " }") }
|
|
86 |
catch { case ERROR(msg) => List(msg) }
|
|
87 |
} |
|
| 71864 | 88 |
} |
| 71850 | 89 |
} |
90 |
||
| 71872 | 91 |
def check_yxml(body: String): String = |
92 |
{
|
|
93 |
val errors = Compiler.default_context.check(body) |
|
94 |
locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) }
|
|
95 |
} |
|
96 |
||
| 71850 | 97 |
|
98 |
||
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
99 |
/** invoke Scala functions from ML **/ |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
100 |
|
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
101 |
/* registered functions */ |
| 71849 | 102 |
|
| 71873 | 103 |
sealed case class Fun(name: String, apply: String => String) |
| 43748 | 104 |
|
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
105 |
lazy val functions: List[Fun] = |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
106 |
Isabelle_System.services.collect { case c: Isabelle_Scala_Functions => c.functions.toList }.flatten
|
| 43748 | 107 |
|
108 |
||
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
109 |
/* invoke function */ |
| 43748 | 110 |
|
111 |
object Tag extends Enumeration |
|
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
112 |
{
|
| 65638 | 113 |
val NULL, OK, ERROR, FAIL, INTERRUPT = Value |
| 43748 | 114 |
} |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
115 |
|
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
116 |
def function(name: String, arg: String): (Tag.Value, String) = |
| 71873 | 117 |
functions.find(fun => fun.name == name) match {
|
118 |
case Some(fun) => |
|
119 |
Exn.capture { fun.apply(arg) } match {
|
|
| 43748 | 120 |
case Exn.Res(null) => (Tag.NULL, "") |
121 |
case Exn.Res(res) => (Tag.OK, res) |
|
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56387
diff
changeset
|
122 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "") |
| 44158 | 123 |
case Exn.Exn(e) => (Tag.ERROR, Exn.message(e)) |
| 43748 | 124 |
} |
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
125 |
case None => (Tag.FAIL, "Unknown Isabelle/Scala function: " + quote(name)) |
| 43748 | 126 |
} |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
127 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
128 |
|
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
129 |
|
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
130 |
/* protocol handler */ |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
131 |
|
| 71849 | 132 |
class Scala extends Session.Protocol_Handler |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
133 |
{
|
| 65219 | 134 |
private var session: Session = null |
| 61561 | 135 |
private var futures = Map.empty[String, Future[Unit]] |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
136 |
|
| 65219 | 137 |
override def init(init_session: Session): Unit = |
138 |
synchronized { session = init_session }
|
|
139 |
||
| 65220 | 140 |
override def exit(): Unit = synchronized |
141 |
{
|
|
142 |
for ((id, future) <- futures) cancel(id, future) |
|
143 |
futures = Map.empty |
|
144 |
} |
|
145 |
||
| 71849 | 146 |
private def fulfill(id: String, tag: Scala.Tag.Value, res: String): Unit = |
| 56387 | 147 |
synchronized |
148 |
{
|
|
149 |
if (futures.isDefinedAt(id)) {
|
|
| 71849 | 150 |
session.protocol_command("Scala.fulfill", id, tag.id.toString, res)
|
| 56387 | 151 |
futures -= id |
152 |
} |
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
153 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
154 |
|
| 65219 | 155 |
private def cancel(id: String, future: Future[Unit]) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
156 |
{
|
| 61561 | 157 |
future.cancel |
| 71849 | 158 |
fulfill(id, Scala.Tag.INTERRUPT, "") |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
159 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
160 |
|
| 65219 | 161 |
private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
162 |
{
|
|
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
52582
diff
changeset
|
163 |
msg.properties match {
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
164 |
case Markup.Invoke_Scala(name, id) => |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
165 |
futures += (id -> |
| 61561 | 166 |
Future.fork {
|
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
167 |
val (tag, result) = Scala.function(name, msg.text) |
| 65219 | 168 |
fulfill(id, tag, result) |
|
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
169 |
}) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
170 |
true |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
171 |
case _ => false |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
172 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
173 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
174 |
|
| 65219 | 175 |
private def cancel_scala(msg: Prover.Protocol_Output): Boolean = synchronized |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
176 |
{
|
|
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
52582
diff
changeset
|
177 |
msg.properties match {
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
178 |
case Markup.Cancel_Scala(id) => |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
179 |
futures.get(id) match {
|
| 65219 | 180 |
case Some(future) => cancel(id, future) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
181 |
case None => |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
182 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
183 |
true |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
184 |
case _ => false |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
185 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
186 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
187 |
|
| 65219 | 188 |
val functions = |
189 |
List( |
|
| 71673 | 190 |
Markup.Invoke_Scala.name -> invoke_scala, |
191 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
192 |
} |
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
193 |
|
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
194 |
|
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
195 |
/* registered functions */ |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
196 |
|
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
197 |
class Isabelle_Scala_Functions(val functions: Scala.Fun*) extends Isabelle_System.Service |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
198 |
|
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
199 |
class Functions extends Isabelle_Scala_Functions( |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
200 |
Scala.Fun("echo", identity),
|
| 71872 | 201 |
Scala.Fun("scala_check", Scala.check_yxml),
|
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
202 |
Scala.Fun("check_bibtex_database", Bibtex.check_database_yxml))
|