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