author | wenzelm |
Thu, 27 Aug 2020 12:34:10 +0200 | |
changeset 72212 | 53e8858b839f |
parent 72194 | eef421b724c0 |
child 72213 | 6157757bb133 |
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.io.{File => JFile, StringWriter, PrintWriter} |
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
11 |
|
71868 | 12 |
import scala.tools.nsc.{GenericRunnerSettings, ConsoleWriter, NewLinePrintWriter} |
71864 | 13 |
import scala.tools.nsc.interpreter.IMain |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
14 |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
15 |
|
71849 | 16 |
object Scala |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
17 |
{ |
72193 | 18 |
/** registered functions **/ |
19 |
||
20 |
abstract class Fun(val name: String) extends Function[String, String] |
|
21 |
{ |
|
22 |
override def toString: String = name |
|
23 |
def apply(arg: String): String |
|
24 |
} |
|
25 |
||
26 |
class Functions(val functions: Fun*) extends Isabelle_System.Service |
|
27 |
||
28 |
lazy val functions: List[Fun] = |
|
29 |
Isabelle_System.make_services(classOf[Functions]).flatMap(_.functions) |
|
30 |
||
31 |
||
32 |
||
72152 | 33 |
/** demo functions **/ |
34 |
||
72193 | 35 |
object Echo extends Fun("echo") |
36 |
{ |
|
37 |
def apply(arg: String): String = arg |
|
38 |
} |
|
72152 | 39 |
|
72193 | 40 |
object Sleep extends Fun("sleep") |
72152 | 41 |
{ |
72193 | 42 |
def apply(seconds: String): String = |
43 |
{ |
|
44 |
val t = |
|
45 |
seconds match { |
|
46 |
case Value.Double(s) => Time.seconds(s) |
|
47 |
case _ => error("Malformed argument: " + quote(seconds)) |
|
48 |
} |
|
49 |
val t0 = Time.now() |
|
50 |
t.sleep |
|
51 |
val t1 = Time.now() |
|
52 |
(t1 - t0).toString |
|
53 |
} |
|
72152 | 54 |
} |
55 |
||
56 |
||
57 |
||
71870 | 58 |
/** compiler **/ |
71850 | 59 |
|
71864 | 60 |
object Compiler |
71850 | 61 |
{ |
71868 | 62 |
def context( |
63 |
error: String => Unit = Exn.error, |
|
64 |
jar_dirs: List[JFile] = Nil): Context = |
|
71864 | 65 |
{ |
66 |
def find_jars(dir: JFile): List[String] = |
|
67 |
File.find_files(dir, file => file.getName.endsWith(".jar")). |
|
68 |
map(File.absolute_name) |
|
69 |
||
70 |
val class_path = |
|
71 |
for { |
|
71882 | 72 |
prop <- List("isabelle.scala.classpath", "java.class.path") |
71864 | 73 |
path = System.getProperty(prop, "") if path != "\"\"" |
74 |
elem <- space_explode(JFile.pathSeparatorChar, path) |
|
75 |
} yield elem |
|
76 |
||
71868 | 77 |
val settings = new GenericRunnerSettings(error) |
78 |
settings.classpath.value = |
|
79 |
(class_path ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator) |
|
71850 | 80 |
|
71868 | 81 |
new Context(settings) |
71864 | 82 |
} |
71850 | 83 |
|
71868 | 84 |
def default_print_writer: PrintWriter = |
85 |
new NewLinePrintWriter(new ConsoleWriter, true) |
|
86 |
||
87 |
class Context private [Compiler](val settings: GenericRunnerSettings) |
|
71864 | 88 |
{ |
71869 | 89 |
override def toString: String = settings.toString |
90 |
||
71868 | 91 |
def interpreter( |
92 |
print_writer: PrintWriter = default_print_writer, |
|
93 |
class_loader: ClassLoader = null): IMain = |
|
94 |
{ |
|
95 |
new IMain(settings, print_writer) |
|
96 |
{ |
|
97 |
override def parentClassLoader: ClassLoader = |
|
98 |
if (class_loader == null) super.parentClassLoader |
|
99 |
else class_loader |
|
100 |
} |
|
101 |
} |
|
71850 | 102 |
|
71868 | 103 |
def toplevel(source: String): List[String] = |
104 |
{ |
|
105 |
val out = new StringWriter |
|
106 |
val interp = interpreter(new PrintWriter(out)) |
|
107 |
val rep = new interp.ReadEvalPrint |
|
108 |
val ok = interp.withLabel("\u0001") { rep.compile(source) } |
|
109 |
out.close |
|
71864 | 110 |
|
71868 | 111 |
val Error = """(?s)^\S* error: (.*)$""".r |
112 |
val errors = |
|
113 |
space_explode('\u0001', Library.strip_ansi_color(out.toString)). |
|
114 |
collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) }) |
|
115 |
||
116 |
if (!ok && errors.isEmpty) List("Error") else errors |
|
117 |
} |
|
71864 | 118 |
} |
72194 | 119 |
} |
71850 | 120 |
|
72194 | 121 |
object Toplevel extends Fun("scala_toplevel") |
122 |
{ |
|
123 |
def apply(source: String): String = |
|
72193 | 124 |
{ |
72194 | 125 |
val errors = |
126 |
try { Compiler.context().toplevel(source) } |
|
127 |
catch { case ERROR(msg) => List(msg) } |
|
128 |
locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) } |
|
72193 | 129 |
} |
71872 | 130 |
} |
131 |
||
71850 | 132 |
|
133 |
||
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
134 |
/** invoke Scala functions from ML **/ |
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
135 |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
136 |
/* invoke function */ |
43748 | 137 |
|
138 |
object Tag extends Enumeration |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
139 |
{ |
65638 | 140 |
val NULL, OK, ERROR, FAIL, INTERRUPT = Value |
43748 | 141 |
} |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
142 |
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
143 |
def function(name: String, arg: String): (Tag.Value, String) = |
71873 | 144 |
functions.find(fun => fun.name == name) match { |
145 |
case Some(fun) => |
|
72193 | 146 |
Exn.capture { fun(arg) } match { |
43748 | 147 |
case Exn.Res(null) => (Tag.NULL, "") |
148 |
case Exn.Res(res) => (Tag.OK, res) |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56387
diff
changeset
|
149 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "") |
44158 | 150 |
case Exn.Exn(e) => (Tag.ERROR, Exn.message(e)) |
43748 | 151 |
} |
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
152 |
case None => (Tag.FAIL, "Unknown Isabelle/Scala function: " + quote(name)) |
43748 | 153 |
} |
52111
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 |
|
72157 | 156 |
/* protocol handler */ |
157 |
||
158 |
class Handler extends Session.Protocol_Handler |
|
159 |
{ |
|
160 |
private var session: Session = null |
|
161 |
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
|
162 |
|
72157 | 163 |
override def init(init_session: Session): Unit = |
164 |
synchronized { session = init_session } |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
165 |
|
72157 | 166 |
override def exit(): Unit = synchronized |
167 |
{ |
|
168 |
for ((id, future) <- futures) cancel(id, future) |
|
169 |
futures = Map.empty |
|
170 |
} |
|
65219 | 171 |
|
72157 | 172 |
private def result(id: String, tag: Scala.Tag.Value, res: String): Unit = |
173 |
synchronized |
|
174 |
{ |
|
175 |
if (futures.isDefinedAt(id)) { |
|
176 |
session.protocol_command("Scala.result", id, tag.id.toString, res) |
|
177 |
futures -= id |
|
178 |
} |
|
179 |
} |
|
65220 | 180 |
|
72157 | 181 |
private def cancel(id: String, future: Future[Unit]) |
182 |
{ |
|
183 |
future.cancel |
|
184 |
result(id, Scala.Tag.INTERRUPT, "") |
|
185 |
} |
|
186 |
||
187 |
private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized |
|
56387 | 188 |
{ |
72157 | 189 |
msg.properties match { |
190 |
case Markup.Invoke_Scala(name, id) => |
|
191 |
futures += (id -> |
|
192 |
Future.fork { |
|
193 |
val (tag, res) = Scala.function(name, msg.text) |
|
194 |
result(id, tag, res) |
|
195 |
}) |
|
196 |
true |
|
197 |
case _ => false |
|
56387 | 198 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
199 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
200 |
|
72157 | 201 |
private def cancel_scala(msg: Prover.Protocol_Output): Boolean = synchronized |
202 |
{ |
|
203 |
msg.properties match { |
|
204 |
case Markup.Cancel_Scala(id) => |
|
205 |
futures.get(id) match { |
|
206 |
case Some(future) => cancel(id, future) |
|
207 |
case None => |
|
208 |
} |
|
209 |
true |
|
210 |
case _ => false |
|
211 |
} |
|
212 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
213 |
|
72212 | 214 |
override val functions = |
72157 | 215 |
List( |
216 |
Markup.Invoke_Scala.name -> invoke_scala, |
|
217 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
218 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
219 |
} |
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
220 |
|
72193 | 221 |
class Scala_Functions extends Scala.Functions( |
222 |
Scala.Echo, |
|
223 |
Scala.Sleep, |
|
72194 | 224 |
Scala.Toplevel, |
225 |
Bibtex.Check_Database) |