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