]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicLibrary.ml
FIX OF THE PREVIOUS EXPERIMENTAL COMMIT:
[helm.git] / helm / software / components / ng_kernel / nCicLibrary.ml
index cff822316e45c311e4fdbd05e8430f84e3d62128..414e5c5de1fad5296fefc5f9fdd8549fbb87ffe6 100644 (file)
@@ -26,8 +26,6 @@ let cache = ref NUri.UriMap.empty;;
 
 let time_travel (sto,ali,cac) = storage := sto; aliases := ali; cache := cac;;
 
-let magic = 0;;
-
 let path_of_baseuri baseuri =
  let uri = NUri.string_of_uri baseuri in
  let path = String.sub uri 4 (String.length uri - 4) in
@@ -37,6 +35,18 @@ let path_of_baseuri baseuri =
   path ^ ".ng"
 ;;
 
+let magic = 0;;
+
+let require0 ~baseuri =
+ let ch = open_in (path_of_baseuri baseuri) in
+ let mmagic,dump = Marshal.from_channel ch in
+  close_in ch;
+  if mmagic <> magic then
+   raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
+  else
+   dump
+;;
+
 let serialize ~baseuri dump =
  let ch = open_out (path_of_baseuri baseuri) in
  Marshal.to_channel ch (magic,dump) [Marshal.Closures];
@@ -47,18 +57,40 @@ let serialize ~baseuri dump =
     Marshal.to_channel ch (magic,obj) [];
     close_out ch
   ) !storage;
- time_travel time0;
+ time_travel time0
 ;;
 
-let require ~baseuri =
- let ch = open_in (path_of_baseuri baseuri) in
- let mmagic,dump = Marshal.from_channel ch in
-  close_in ch;
-  if mmagic <> magic then
-   raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
-  else
-   dump
-;;
+module type Serializer =
+ sig
+  type status
+  type obj
+  val register: string -> ('a -> status -> status) -> ('a -> obj)
+  val serialize: baseuri:NUri.uri -> obj list -> unit
+  val require: baseuri:NUri.uri -> status -> status
+ end
+
+module Serializer(S: sig type status end) =
+ struct
+  type status = S.status
+  type obj = Obj.t
+
+  let require1 = ref (fun _ -> assert false (* unknown data*))
+  let already_registered = ref []
+
+  let register tag require =
+   assert (not (List.mem tag !already_registered));
+   already_registered := tag :: !already_registered;
+   require1 :=
+    (fun (tag',data) as x ->
+     if tag=tag' then Obj.magic require data else Obj.magic !require1 x);
+   Obj.magic (fun x -> tag,x)
+
+  let serialize = serialize
+
+  let require ~baseuri status =
+   let dump = require0 ~baseuri in
+    List.fold_right (Obj.magic !require1) dump status
+end
 
 let decompile ~baseuri =
  Unix.unlink (path_of_baseuri baseuri)
@@ -67,7 +99,7 @@ let decompile ~baseuri =
 
 (* the miracles of Marshal... *)
 let fetch_obj uri =
- let obj = require ~baseuri:uri in
+ let obj = require0 ~baseuri:uri in
   (* here we need to refresh the URIs! *)
   obj
 ;;
@@ -141,4 +173,3 @@ let get_obj u =
 ;;
 
 let clear_cache () = cache := NUri.UriMap.empty;;
-