| author | wenzelm | 
| Wed, 17 Aug 2011 13:14:20 +0200 | |
| changeset 44236 | b73b7832b384 | 
| parent 43761 | e72ba84ae58f | 
| child 44298 | b8f8488704e2 | 
| permissions | -rw-r--r-- | 
| 43748 | 1 | (* Title: Pure/System/invoke_scala.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | JVM method invocation service via Scala layer. | |
| 5 | ||
| 6 | TODO: proper cancellation! | |
| 7 | *) | |
| 8 | ||
| 9 | signature INVOKE_SCALA = | |
| 10 | sig | |
| 11 | exception Null | |
| 43749 | 12 | val method: string -> string -> string | 
| 13 | val promise_method: string -> string -> string future | |
| 43748 | 14 | val fulfill_method: string -> string -> string -> unit | 
| 15 | end; | |
| 16 | ||
| 17 | structure Invoke_Scala: INVOKE_SCALA = | |
| 18 | struct | |
| 19 | ||
| 43749 | 20 | exception Null; | 
| 21 | ||
| 22 | ||
| 43748 | 23 | (* pending promises *) | 
| 24 | ||
| 25 | val new_id = string_of_int o Synchronized.counter (); | |
| 26 | ||
| 27 | val promises = | |
| 28 | Synchronized.var "Invoke_Scala.promises" (Symtab.empty: string future Symtab.table); | |
| 29 | ||
| 30 | ||
| 31 | (* method invocation *) | |
| 32 | ||
| 43749 | 33 | fun promise_method name arg = | 
| 43748 | 34 | let | 
| 35 | val id = new_id (); | |
| 36 | val promise = Future.promise () : string future; | |
| 37 | val _ = Synchronized.change promises (Symtab.update (id, promise)); | |
| 38 | val _ = Output.raw_message (Markup.invoke_scala name id) arg; | |
| 39 | in promise end; | |
| 40 | ||
| 43749 | 41 | fun method name arg = Future.join (promise_method name arg); | 
| 42 | ||
| 43748 | 43 | |
| 44 | (* fulfill method *) | |
| 45 | ||
| 46 | fun fulfill_method id tag res = | |
| 47 | let | |
| 48 | val result = | |
| 49 | (case tag of | |
| 50 | "0" => Exn.Exn Null | |
| 43761 
e72ba84ae58f
tuned signature -- corresponding to Scala version;
 wenzelm parents: 
43749diff
changeset | 51 | | "1" => Exn.Res res | 
| 43748 | 52 | | "2" => Exn.Exn (ERROR res) | 
| 53 | | "3" => Exn.Exn (Fail res) | |
| 54 | | _ => raise Fail "Bad tag"); | |
| 55 | val promise = | |
| 56 | Synchronized.change_result promises | |
| 57 | (fn tab => (the (Symtab.lookup tab id), Symtab.delete id tab)); | |
| 58 | val _ = Future.fulfill_result promise result; | |
| 59 | in () end; | |
| 60 | ||
| 61 | end; | |
| 62 |