author | wenzelm |
Sat, 23 May 2020 12:04:24 +0200 | |
changeset 71870 | 82abfda58667 |
parent 71869 | 2b7840fb2f90 |
child 71871 | 28def00726ca |
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 |
} |
|
71864 | 82 |
} |
71850 | 83 |
} |
84 |
||
85 |
||
86 |
||
71849 | 87 |
/** invoke JVM method via Isabelle/Scala **/ |
88 |
||
43748 | 89 |
/* method reflection */ |
90 |
||
91 |
private val Ext = new Regex("(.*)\\.([^.]*)") |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
92 |
private val STRING = Class.forName("java.lang.String") |
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
93 |
|
43748 | 94 |
private def get_method(name: String): String => String = |
95 |
name match { |
|
96 |
case Ext(class_name, method_name) => |
|
97 |
val m = |
|
98 |
try { Class.forName(class_name).getMethod(method_name, STRING) } |
|
99 |
catch { |
|
43751
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
100 |
case _: ClassNotFoundException | _: NoSuchMethodException => |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
101 |
error("No such method: " + quote(name)) |
43748 | 102 |
} |
103 |
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
|
104 |
if (m.getReturnType != STRING) error("Bad method return type: " + m.toString) |
43748 | 105 |
|
43751
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
106 |
(arg: String) => { |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
107 |
try { m.invoke(null, arg).asInstanceOf[String] } |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
108 |
catch { |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
109 |
case e: InvocationTargetException if e.getCause != null => |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
110 |
throw e.getCause |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
111 |
} |
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
wenzelm
parents:
43748
diff
changeset
|
112 |
} |
43748 | 113 |
case _ => error("Malformed method name: " + quote(name)) |
114 |
} |
|
115 |
||
116 |
||
117 |
/* method invocation */ |
|
118 |
||
119 |
object Tag extends Enumeration |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
120 |
{ |
65638 | 121 |
val NULL, OK, ERROR, FAIL, INTERRUPT = Value |
43748 | 122 |
} |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
123 |
|
43748 | 124 |
def method(name: String, arg: String): (Tag.Value, String) = |
125 |
Exn.capture { get_method(name) } match { |
|
126 |
case Exn.Res(f) => |
|
127 |
Exn.capture { f(arg) } match { |
|
128 |
case Exn.Res(null) => (Tag.NULL, "") |
|
129 |
case Exn.Res(res) => (Tag.OK, res) |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56387
diff
changeset
|
130 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "") |
44158 | 131 |
case Exn.Exn(e) => (Tag.ERROR, Exn.message(e)) |
43748 | 132 |
} |
44158 | 133 |
case Exn.Exn(e) => (Tag.FAIL, Exn.message(e)) |
43748 | 134 |
} |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
135 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
136 |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
137 |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
138 |
/* protocol handler */ |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
139 |
|
71849 | 140 |
class Scala extends Session.Protocol_Handler |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
141 |
{ |
65219 | 142 |
private var session: Session = null |
61561 | 143 |
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
|
144 |
|
65219 | 145 |
override def init(init_session: Session): Unit = |
146 |
synchronized { session = init_session } |
|
147 |
||
65220 | 148 |
override def exit(): Unit = synchronized |
149 |
{ |
|
150 |
for ((id, future) <- futures) cancel(id, future) |
|
151 |
futures = Map.empty |
|
152 |
} |
|
153 |
||
71849 | 154 |
private def fulfill(id: String, tag: Scala.Tag.Value, res: String): Unit = |
56387 | 155 |
synchronized |
156 |
{ |
|
157 |
if (futures.isDefinedAt(id)) { |
|
71849 | 158 |
session.protocol_command("Scala.fulfill", id, tag.id.toString, res) |
56387 | 159 |
futures -= id |
160 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
161 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
162 |
|
65219 | 163 |
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
|
164 |
{ |
61561 | 165 |
future.cancel |
71849 | 166 |
fulfill(id, Scala.Tag.INTERRUPT, "") |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
167 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
168 |
|
65219 | 169 |
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
|
170 |
{ |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
52582
diff
changeset
|
171 |
msg.properties match { |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
172 |
case Markup.Invoke_Scala(name, id) => |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
173 |
futures += (id -> |
61561 | 174 |
Future.fork { |
71849 | 175 |
val (tag, result) = Scala.method(name, msg.text) |
65219 | 176 |
fulfill(id, tag, result) |
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
177 |
}) |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
178 |
true |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
179 |
case _ => false |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
180 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
181 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
182 |
|
65219 | 183 |
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
|
184 |
{ |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
52582
diff
changeset
|
185 |
msg.properties match { |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
186 |
case Markup.Cancel_Scala(id) => |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
187 |
futures.get(id) match { |
65219 | 188 |
case Some(future) => cancel(id, future) |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
189 |
case None => |
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 |
true |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
192 |
case _ => false |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
193 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
194 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
195 |
|
65219 | 196 |
val functions = |
197 |
List( |
|
71673 | 198 |
Markup.Invoke_Scala.name -> invoke_scala, |
199 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
200 |
} |