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