author | wenzelm |
Wed, 12 Mar 2025 11:39:00 +0100 | |
changeset 82265 | 4b875a4c83b0 |
parent 82142 | 508a673c87ac |
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 |
|
75654 | 10 |
import java.io.{File => JFile, PrintStream, ByteArrayOutputStream, OutputStream} |
11 |
||
12 |
import scala.collection.mutable |
|
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
13 |
|
75654 | 14 |
import dotty.tools.dotc.CompilationUnit |
15 |
import dotty.tools.repl |
|
82142 | 16 |
import dotty.tools.repl.ReplDriver |
75654 | 17 |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
18 |
|
75393 | 19 |
object Scala { |
72193 | 20 |
/** registered functions **/ |
21 |
||
75393 | 22 |
abstract class Fun(val name: String, val thread: Boolean = false) { |
72193 | 23 |
override def toString: String = name |
75586 | 24 |
def single: Boolean = false |
25 |
def bytes: Boolean = false |
|
72756 | 26 |
def position: Properties.T = here.position |
27 |
def here: Scala_Project.Here |
|
77027 | 28 |
def invoke(session: Session, args: List[Bytes]): List[Bytes] |
73565 | 29 |
} |
30 |
||
75588 | 31 |
trait Single_Fun extends Fun { override def single: Boolean = true } |
32 |
trait Bytes_Fun extends Fun { override def bytes: Boolean = true } |
|
75586 | 33 |
|
73565 | 34 |
abstract class Fun_Strings(name: String, thread: Boolean = false) |
75393 | 35 |
extends Fun(name, thread = thread) { |
77027 | 36 |
override def invoke(session: Session, args: List[Bytes]): List[Bytes] = |
73565 | 37 |
apply(args.map(_.text)).map(Bytes.apply) |
38 |
def apply(args: List[String]): List[String] |
|
39 |
} |
|
40 |
||
41 |
abstract class Fun_String(name: String, thread: Boolean = false) |
|
75588 | 42 |
extends Fun_Strings(name, thread = thread) with Single_Fun { |
73565 | 43 |
override def apply(args: List[String]): List[String] = |
73571 | 44 |
List(apply(Library.the_single(args))) |
72193 | 45 |
def apply(arg: String): String |
46 |
} |
|
47 |
||
75588 | 48 |
abstract class Fun_Bytes(name: String, thread: Boolean = false) |
49 |
extends Fun(name, thread = thread) with Single_Fun with Bytes_Fun { |
|
77027 | 50 |
override def invoke(session: Session, args: List[Bytes]): List[Bytes] = |
75588 | 51 |
List(apply(Library.the_single(args))) |
52 |
def apply(arg: Bytes): Bytes |
|
53 |
} |
|
54 |
||
75586 | 55 |
val encode_fun: XML.Encode.T[Fun] = { fun => |
56 |
import XML.Encode._ |
|
57 |
pair(string, pair(pair(bool, bool), properties))( |
|
58 |
fun.name, ((fun.single, fun.bytes), fun.position)) |
|
59 |
} |
|
60 |
||
72193 | 61 |
class Functions(val functions: Fun*) extends Isabelle_System.Service |
62 |
||
63 |
lazy val functions: List[Fun] = |
|
64 |
Isabelle_System.make_services(classOf[Functions]).flatMap(_.functions) |
|
65 |
||
66 |
||
67 |
||
72152 | 68 |
/** demo functions **/ |
69 |
||
75393 | 70 |
object Echo extends Fun_String("echo") { |
72756 | 71 |
val here = Scala_Project.here |
72193 | 72 |
def apply(arg: String): String = arg |
73 |
} |
|
72152 | 74 |
|
75393 | 75 |
object Sleep extends Fun_String("sleep") { |
72756 | 76 |
val here = Scala_Project.here |
75393 | 77 |
def apply(seconds: String): String = { |
72193 | 78 |
val t = |
79 |
seconds match { |
|
80 |
case Value.Double(s) => Time.seconds(s) |
|
81 |
case _ => error("Malformed argument: " + quote(seconds)) |
|
82 |
} |
|
83 |
val t0 = Time.now() |
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73576
diff
changeset
|
84 |
t.sleep() |
72193 | 85 |
val t1 = Time.now() |
86 |
(t1 - t0).toString |
|
87 |
} |
|
72152 | 88 |
} |
89 |
||
90 |
||
91 |
||
71870 | 92 |
/** compiler **/ |
71850 | 93 |
|
75393 | 94 |
object Compiler { |
75654 | 95 |
object Message { |
78606 | 96 |
enum Kind { case error, warning, info, other } |
97 |
||
75654 | 98 |
private val Header = """^--.* (Error|Warning|Info): .*$""".r |
78606 | 99 |
val header_kind: String => Kind = |
75654 | 100 |
{ |
101 |
case "Error" => Kind.error |
|
102 |
case "Warning" => Kind.warning |
|
103 |
case "Info" => Kind.info |
|
104 |
case _ => Kind.other |
|
105 |
} |
|
106 |
||
107 |
// see compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala |
|
108 |
def split(str: String): List[Message] = { |
|
109 |
var kind = Kind.other |
|
110 |
val text = new mutable.StringBuilder |
|
111 |
val result = new mutable.ListBuffer[Message] |
|
112 |
||
113 |
def flush(): Unit = { |
|
114 |
if (text.nonEmpty) { result += Message(kind, text.toString) } |
|
115 |
kind = Kind.other |
|
116 |
text.clear() |
|
117 |
} |
|
118 |
||
119 |
for (line <- Library.trim_split_lines(str)) { |
|
120 |
line match { |
|
121 |
case Header(k) => flush(); kind = header_kind(k) |
|
122 |
case _ => if (line.startsWith("-- ")) flush() |
|
123 |
} |
|
124 |
if (text.nonEmpty) { text += '\n' } |
|
125 |
text ++= line |
|
126 |
} |
|
127 |
flush() |
|
128 |
result.toList |
|
129 |
} |
|
130 |
} |
|
131 |
||
78606 | 132 |
sealed case class Message(kind: Message.Kind, text: String) |
75654 | 133 |
{ |
134 |
def is_error: Boolean = kind == Message.Kind.error |
|
135 |
override def toString: String = text |
|
136 |
} |
|
137 |
||
138 |
sealed case class Result( |
|
139 |
state: repl.State, |
|
140 |
messages: List[Message], |
|
141 |
unit: Option[CompilationUnit] = None |
|
142 |
) { |
|
143 |
val errors: List[String] = messages.flatMap(msg => if (msg.is_error) Some(msg.text) else None) |
|
144 |
def ok: Boolean = errors.isEmpty |
|
145 |
def check_state: repl.State = if (ok) state else error(cat_lines(errors)) |
|
146 |
override def toString: String = if (ok) "Result(ok)" else "Result(error)" |
|
147 |
} |
|
75443 | 148 |
|
71868 | 149 |
def context( |
75654 | 150 |
settings: List[String] = Nil, |
75702 | 151 |
jar_files: List[JFile] = Nil, |
75442 | 152 |
class_loader: Option[ClassLoader] = None |
75393 | 153 |
): Context = { |
75654 | 154 |
val isabelle_settings = |
155 |
Word.explode(Isabelle_System.getenv_strict("ISABELLE_SCALAC_OPTIONS")) |
|
75705 | 156 |
val classpath = Classpath(jar_files = jar_files) |
157 |
new Context(isabelle_settings ::: settings, classpath, class_loader) |
|
71864 | 158 |
} |
71850 | 159 |
|
75442 | 160 |
class Context private [Compiler]( |
75705 | 161 |
_settings: List[String], |
162 |
val classpath: Classpath, |
|
75654 | 163 |
val class_loader: Option[ClassLoader] = None |
75442 | 164 |
) { |
75705 | 165 |
def settings: List[String] = |
166 |
_settings ::: List("-classpath", classpath.platform_path) |
|
167 |
||
75654 | 168 |
private val out_stream = new ByteArrayOutputStream(1024) |
169 |
private val out = new PrintStream(out_stream) |
|
170 |
private val driver: ReplDriver = new ReplDriver(settings.toArray, out, class_loader) |
|
71869 | 171 |
|
75654 | 172 |
def init_state: repl.State = driver.initialState |
71850 | 173 |
|
75654 | 174 |
def compile(source: String, state: repl.State = init_state): Result = { |
175 |
out.flush() |
|
176 |
out_stream.reset() |
|
76537 | 177 |
given repl.State = state |
178 |
val state1 = driver.run(source) |
|
75654 | 179 |
out.flush() |
180 |
val messages = Message.split(out_stream.toString(UTF8.charset)) |
|
181 |
out_stream.reset() |
|
182 |
Result(state1, messages) |
|
183 |
} |
|
71864 | 184 |
} |
72194 | 185 |
} |
71850 | 186 |
|
75393 | 187 |
object Toplevel extends Fun_String("scala_toplevel") { |
72756 | 188 |
val here = Scala_Project.here |
75654 | 189 |
def apply(source: String): String = { |
72194 | 190 |
val errors = |
75654 | 191 |
try { Compiler.context().compile(source).errors.map("Scala error: " + _) } |
72194 | 192 |
catch { case ERROR(msg) => List(msg) } |
193 |
locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) } |
|
72193 | 194 |
} |
71872 | 195 |
} |
196 |
||
71850 | 197 |
|
198 |
||
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
199 |
/** interpreter thread **/ |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
200 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
201 |
object Interpreter { |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
202 |
/* requests */ |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
203 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
204 |
sealed abstract class Request |
75654 | 205 |
case class Execute(command: (Compiler.Context, repl.State) => repl.State) extends Request |
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
206 |
case object Shutdown extends Request |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
207 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
208 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
209 |
/* known interpreters */ |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
210 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
211 |
private val known = Synchronized(Set.empty[Interpreter]) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
212 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
213 |
def add(interpreter: Interpreter): Unit = known.change(_ + interpreter) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
214 |
def del(interpreter: Interpreter): Unit = known.change(_ - interpreter) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
215 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
216 |
def get[A](which: PartialFunction[Interpreter, A]): Option[A] = |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
217 |
known.value.collectFirst(which) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
218 |
} |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
219 |
|
75654 | 220 |
class Interpreter(context: Compiler.Context, out: OutputStream = Console.out) { |
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
221 |
interpreter => |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
222 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
223 |
private val running = Synchronized[Option[Thread]](None) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
224 |
def running_thread(thread: Thread): Boolean = running.value.contains(thread) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
225 |
def interrupt_thread(): Unit = running.change({ opt => opt.foreach(_.interrupt()); opt }) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
226 |
|
75654 | 227 |
private var state = context.init_state |
228 |
||
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
229 |
private lazy val thread: Consumer_Thread[Interpreter.Request] = |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
230 |
Consumer_Thread.fork("Scala.Interpreter") { |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
231 |
case Interpreter.Execute(command) => |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
232 |
try { |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
233 |
running.change(_ => Some(Thread.currentThread())) |
75654 | 234 |
state = command(context, state) |
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
235 |
} |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
236 |
finally { |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
237 |
running.change(_ => None) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
238 |
Exn.Interrupt.dispose() |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
239 |
} |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
240 |
true |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
241 |
case Interpreter.Shutdown => |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
242 |
Interpreter.del(interpreter) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
243 |
false |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
244 |
} |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
245 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
246 |
def shutdown(): Unit = { |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
247 |
thread.send(Interpreter.Shutdown) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
248 |
interrupt_thread() |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
249 |
thread.shutdown() |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
250 |
} |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
251 |
|
75654 | 252 |
def execute(command: (Compiler.Context, repl.State) => repl.State): Unit = |
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
253 |
thread.send(Interpreter.Execute(command)) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
254 |
|
75654 | 255 |
def reset(): Unit = |
256 |
thread.send(Interpreter.Execute((context, _) => context.init_state)) |
|
257 |
||
75444
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
258 |
Interpreter.add(interpreter) |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
259 |
thread |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
260 |
} |
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
261 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
262 |
|
331f96a67924
clarified management of interpreter threads: more generic;
wenzelm
parents:
75443
diff
changeset
|
263 |
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
264 |
/** invoke Scala functions from ML **/ |
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
265 |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
266 |
/* invoke function */ |
43748 | 267 |
|
78605 | 268 |
enum Tag { case NULL, OK, ERROR, FAIL, INTERRUPT } |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
269 |
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
270 |
def function_thread(name: String): Boolean = |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
271 |
functions.find(fun => fun.name == name) match { |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
272 |
case Some(fun) => fun.thread |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
273 |
case None => false |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
274 |
} |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
275 |
|
78605 | 276 |
def function_body(session: Session, name: String, args: List[Bytes]): (Tag, List[Bytes]) = |
71873 | 277 |
functions.find(fun => fun.name == name) match { |
278 |
case Some(fun) => |
|
77027 | 279 |
Exn.capture { fun.invoke(session, args) } match { |
73565 | 280 |
case Exn.Res(null) => (Tag.NULL, Nil) |
43748 | 281 |
case Exn.Res(res) => (Tag.OK, res) |
73565 | 282 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, Nil) |
78767
aa67309a7960
clarified user errors vs. failures, e.g. java.lang.StackOverflowError;
wenzelm
parents:
78606
diff
changeset
|
283 |
case Exn.Exn(ERROR(msg)) => (Tag.ERROR, List(Bytes(msg))) |
aa67309a7960
clarified user errors vs. failures, e.g. java.lang.StackOverflowError;
wenzelm
parents:
78606
diff
changeset
|
284 |
case Exn.Exn(e) => (Tag.FAIL, List(Bytes(Exn.message(e)))) |
43748 | 285 |
} |
73565 | 286 |
case None => (Tag.FAIL, List(Bytes("Unknown Isabelle/Scala function: " + quote(name)))) |
43748 | 287 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
288 |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
289 |
|
72157 | 290 |
/* protocol handler */ |
291 |
||
75393 | 292 |
class Handler extends Session.Protocol_Handler { |
72157 | 293 |
private var session: Session = null |
294 |
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
|
295 |
|
72213 | 296 |
override def init(session: Session): Unit = |
297 |
synchronized { this.session = session } |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
298 |
|
75380 | 299 |
override def exit(): Unit = synchronized { |
72157 | 300 |
for ((id, future) <- futures) cancel(id, future) |
301 |
futures = Map.empty |
|
302 |
} |
|
65219 | 303 |
|
78605 | 304 |
private def result(id: String, tag: Scala.Tag, res: List[Bytes]): Unit = |
75380 | 305 |
synchronized { |
72157 | 306 |
if (futures.isDefinedAt(id)) { |
78605 | 307 |
session.protocol_command_raw( |
308 |
"Scala.result", Bytes(id) :: Bytes(tag.ordinal.toString) :: res) |
|
72157 | 309 |
futures -= id |
310 |
} |
|
311 |
} |
|
65220 | 312 |
|
75393 | 313 |
private def cancel(id: String, future: Future[Unit]): Unit = { |
73367 | 314 |
future.cancel() |
73565 | 315 |
result(id, Scala.Tag.INTERRUPT, Nil) |
72157 | 316 |
} |
317 |
||
75380 | 318 |
private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized { |
72157 | 319 |
msg.properties match { |
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
320 |
case Markup.Invoke_Scala(name, id) => |
75441
400e325a5416
clarified signature, based on hints by IntelliJ IDEA;
wenzelm
parents:
75440
diff
changeset
|
321 |
def body(): Unit = { |
77027 | 322 |
val (tag, res) = Scala.function_body(session, name, msg.chunks) |
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
323 |
result(id, tag, res) |
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
324 |
} |
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
325 |
val future = |
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
326 |
if (Scala.function_thread(name)) { |
75441
400e325a5416
clarified signature, based on hints by IntelliJ IDEA;
wenzelm
parents:
75440
diff
changeset
|
327 |
Future.thread(name = Isabelle_Thread.make_name(base = "invoke_scala"))(body()) |
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
328 |
} |
75441
400e325a5416
clarified signature, based on hints by IntelliJ IDEA;
wenzelm
parents:
75440
diff
changeset
|
329 |
else Future.fork(body()) |
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
330 |
futures += (id -> future) |
72157 | 331 |
true |
332 |
case _ => false |
|
56387 | 333 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
334 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
335 |
|
75380 | 336 |
private def cancel_scala(msg: Prover.Protocol_Output): Boolean = synchronized { |
72157 | 337 |
msg.properties match { |
338 |
case Markup.Cancel_Scala(id) => |
|
339 |
futures.get(id) match { |
|
340 |
case Some(future) => cancel(id, future) |
|
341 |
case None => |
|
342 |
} |
|
343 |
true |
|
344 |
case _ => false |
|
345 |
} |
|
346 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
347 |
|
75440 | 348 |
override val functions: Session.Protocol_Functions = |
72157 | 349 |
List( |
350 |
Markup.Invoke_Scala.name -> invoke_scala, |
|
351 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
352 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
353 |
} |
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
354 |
|
72193 | 355 |
class Scala_Functions extends Scala.Functions( |
356 |
Scala.Echo, |
|
357 |
Scala.Sleep, |
|
72194 | 358 |
Scala.Toplevel, |
75660
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
wenzelm
parents:
75654
diff
changeset
|
359 |
Scala_Build.Scala_Fun, |
75620 | 360 |
Base64.Decode, |
361 |
Base64.Encode, |
|
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
75705
diff
changeset
|
362 |
Compress.XZ_Compress, |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
75705
diff
changeset
|
363 |
Compress.XZ_Uncompress, |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
75705
diff
changeset
|
364 |
Compress.Zstd_Compress, |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
75705
diff
changeset
|
365 |
Compress.Zstd_Uncompress, |
72760
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
72756
diff
changeset
|
366 |
Doc.Doc_Names, |
72763 | 367 |
Bibtex.Check_Database, |
77028
f5896dea6fce
more direct check of bibtex entries via Isabelle/Scala;
wenzelm
parents:
77027
diff
changeset
|
368 |
Bibtex.Session_Entries, |
73314
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73228
diff
changeset
|
369 |
Isabelle_System.Make_Directory, |
73322 | 370 |
Isabelle_System.Copy_Dir, |
371 |
Isabelle_System.Copy_File, |
|
372 |
Isabelle_System.Copy_File_Base, |
|
73324 | 373 |
Isabelle_System.Rm_Tree, |
73323 | 374 |
Isabelle_System.Download, |
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73431
diff
changeset
|
375 |
Isabelle_System.Isabelle_Id, |
73418
7d7d959547a1
support for SystemOnTPTP in Isabelle/ML and Isabelle/Scala (without perl);
wenzelm
parents:
73367
diff
changeset
|
376 |
Isabelle_Tool.Isabelle_Tools, |
73431
f27d7b12e8a4
support for SystemOnTPTP.run_system, with strict error following scripts/remote_atp;
wenzelm
parents:
73419
diff
changeset
|
377 |
isabelle.atp.SystemOnTPTP.List_Systems, |
76514
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
76351
diff
changeset
|
378 |
isabelle.atp.SystemOnTPTP.Run_System, |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
76351
diff
changeset
|
379 |
Prismjs.Languages, |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
76351
diff
changeset
|
380 |
Prismjs.Tokenize) |