author | wenzelm |
Sat, 23 May 2020 10:54:04 +0200 | |
changeset 71865 | 3882df6a4a93 |
parent 71864 | bfc120aa737a |
child 71868 | 06ec50d9fc0a |
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 |
71850 | 14 |
import scala.tools.nsc.GenericRunnerSettings |
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 |
{ |
71864 | 20 |
/* compiler */ |
71850 | 21 |
|
71864 | 22 |
object Compiler |
71850 | 23 |
{ |
71864 | 24 |
def classpath(jar_dirs: List[JFile]): String = |
25 |
{ |
|
26 |
def find_jars(dir: JFile): List[String] = |
|
27 |
File.find_files(dir, file => file.getName.endsWith(".jar")). |
|
28 |
map(File.absolute_name) |
|
29 |
||
30 |
val class_path = |
|
31 |
for { |
|
32 |
prop <- List("scala.boot.class.path", "java.class.path") |
|
33 |
path = System.getProperty(prop, "") if path != "\"\"" |
|
34 |
elem <- space_explode(JFile.pathSeparatorChar, path) |
|
35 |
} yield elem |
|
36 |
||
37 |
(class_path ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator) |
|
38 |
} |
|
39 |
||
40 |
type Settings = scala.tools.nsc.Settings |
|
71850 | 41 |
|
71864 | 42 |
def settings( |
43 |
error: String => Unit = Exn.error, |
|
44 |
jar_dirs: List[JFile] = Nil): Settings = |
|
45 |
{ |
|
46 |
val settings = new GenericRunnerSettings(error) |
|
47 |
settings.classpath.value = classpath(jar_dirs) |
|
48 |
settings |
|
49 |
} |
|
71850 | 50 |
|
71864 | 51 |
def toplevel(settings: Settings, source: String): List[String] = |
52 |
{ |
|
53 |
val out = new StringWriter |
|
54 |
val interp = new IMain(settings, new PrintWriter(out)) |
|
55 |
val rep = new interp.ReadEvalPrint |
|
56 |
val ok = interp.withLabel("\u0001") { rep.compile(source) } |
|
57 |
out.close |
|
71850 | 58 |
|
71864 | 59 |
val Error = """(?s)^\S* error: (.*)$""".r |
60 |
val errors = |
|
61 |
space_explode('\u0001', Library.strip_ansi_color(out.toString)). |
|
71865 | 62 |
collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) }) |
71864 | 63 |
|
64 |
if (!ok && errors.isEmpty) List("Error") else errors |
|
65 |
} |
|
71850 | 66 |
} |
67 |
||
68 |
||
69 |
||
71849 | 70 |
/** invoke JVM method via Isabelle/Scala **/ |
71 |
||
43748 | 72 |
/* method reflection */ |
73 |
||
74 |
private val Ext = new Regex("(.*)\\.([^.]*)") |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
75 |
private val STRING = Class.forName("java.lang.String") |
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
76 |
|
43748 | 77 |
private def get_method(name: String): String => String = |
78 |
name match { |
|
79 |
case Ext(class_name, method_name) => |
|
80 |
val m = |
|
81 |
try { Class.forName(class_name).getMethod(method_name, STRING) } |
|
82 |
catch { |
|
43751
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
83 |
case _: ClassNotFoundException | _: NoSuchMethodException => |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
84 |
error("No such method: " + quote(name)) |
43748 | 85 |
} |
86 |
if (!Modifier.isStatic(m.getModifiers)) error("Not at static method: " + m.toString) |
|
43751
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
87 |
if (m.getReturnType != STRING) error("Bad method return type: " + m.toString) |
43748 | 88 |
|
43751
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
89 |
(arg: String) => { |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
90 |
try { m.invoke(null, arg).asInstanceOf[String] } |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
91 |
catch { |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
92 |
case e: InvocationTargetException if e.getCause != null => |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
93 |
throw e.getCause |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
94 |
} |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
95 |
} |
43748 | 96 |
case _ => error("Malformed method name: " + quote(name)) |
97 |
} |
|
98 |
||
99 |
||
100 |
/* method invocation */ |
|
101 |
||
102 |
object Tag extends Enumeration |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
103 |
{ |
65638 | 104 |
val NULL, OK, ERROR, FAIL, INTERRUPT = Value |
43748 | 105 |
} |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
106 |
|
43748 | 107 |
def method(name: String, arg: String): (Tag.Value, String) = |
108 |
Exn.capture { get_method(name) } match { |
|
109 |
case Exn.Res(f) => |
|
110 |
Exn.capture { f(arg) } match { |
|
111 |
case Exn.Res(null) => (Tag.NULL, "") |
|
112 |
case Exn.Res(res) => (Tag.OK, res) |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56387
diff
changeset
|
113 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "") |
44158 | 114 |
case Exn.Exn(e) => (Tag.ERROR, Exn.message(e)) |
43748 | 115 |
} |
44158 | 116 |
case Exn.Exn(e) => (Tag.FAIL, Exn.message(e)) |
43748 | 117 |
} |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
118 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
119 |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
120 |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
121 |
/* protocol handler */ |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
122 |
|
71849 | 123 |
class Scala extends Session.Protocol_Handler |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
124 |
{ |
65219 | 125 |
private var session: Session = null |
61561 | 126 |
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
|
127 |
|
65219 | 128 |
override def init(init_session: Session): Unit = |
129 |
synchronized { session = init_session } |
|
130 |
||
65220 | 131 |
override def exit(): Unit = synchronized |
132 |
{ |
|
133 |
for ((id, future) <- futures) cancel(id, future) |
|
134 |
futures = Map.empty |
|
135 |
} |
|
136 |
||
71849 | 137 |
private def fulfill(id: String, tag: Scala.Tag.Value, res: String): Unit = |
56387 | 138 |
synchronized |
139 |
{ |
|
140 |
if (futures.isDefinedAt(id)) { |
|
71849 | 141 |
session.protocol_command("Scala.fulfill", id, tag.id.toString, res) |
56387 | 142 |
futures -= id |
143 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
144 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
145 |
|
65219 | 146 |
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
|
147 |
{ |
61561 | 148 |
future.cancel |
71849 | 149 |
fulfill(id, Scala.Tag.INTERRUPT, "") |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
150 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
151 |
|
65219 | 152 |
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
|
153 |
{ |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
52582
diff
changeset
|
154 |
msg.properties match { |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
155 |
case Markup.Invoke_Scala(name, id) => |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
156 |
futures += (id -> |
61561 | 157 |
Future.fork { |
71849 | 158 |
val (tag, result) = Scala.method(name, msg.text) |
65219 | 159 |
fulfill(id, tag, result) |
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
160 |
}) |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
161 |
true |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
162 |
case _ => false |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
163 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
164 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
165 |
|
65219 | 166 |
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
|
167 |
{ |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
52582
diff
changeset
|
168 |
msg.properties match { |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
169 |
case Markup.Cancel_Scala(id) => |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
170 |
futures.get(id) match { |
65219 | 171 |
case Some(future) => cancel(id, future) |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
172 |
case None => |
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 |
true |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
175 |
case _ => false |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
176 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
177 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
178 |
|
65219 | 179 |
val functions = |
180 |
List( |
|
71673 | 181 |
Markup.Invoke_Scala.name -> invoke_scala, |
182 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
183 |
} |