+
+class status =
+ object
+ val timestamp = (time0 : timestamp)
+ method timestamp = timestamp
+ method set_timestamp v = {< timestamp = v >}
+ method set_library_status
+ : 'status. < timestamp : timestamp; .. > as 'status -> 'self
+ = fun o -> {< timestamp = o#timestamp >}
+ end
+
+let time_travel status =
+ let sto,ali,cac,inc = status#timestamp in
+ let diff_len = List.length !storage - List.length sto in
+ let to_be_deleted,_ = HExtlib.split_nth diff_len !storage in
+ if List.length to_be_deleted > 0 then
+ NCicEnvironment.invalidate_item (HExtlib.list_last to_be_deleted);
+ storage := sto; local_aliases := ali; cache := cac; includes := inc
+;;
+
+let serialize ~baseuri dump =
+ let ch = open_out (path_of_baseuri baseuri) in
+ Marshal.to_channel ch (magic,dump) [];
+ close_out ch;
+ List.iter
+ (function
+ | `Obj (uri,obj) ->
+ let ch = open_out (path_of_baseuri uri) in
+ Marshal.to_channel ch (magic,obj) [];
+ close_out ch
+ | `Constr _ -> ()
+ ) !storage;
+ set_global_aliases (!local_aliases @ get_global_aliases ());
+ List.iter (fun u -> add_deps u [baseuri]) !includes;
+ time_travel (new status)
+;;
+
+module type Serializer =
+ sig
+ type status
+ type obj
+ val register:
+ string ->
+ ('a -> refresh_uri_in_universe:(NCic.universe -> NCic.universe) -> refresh_uri_in_term:(NCic.term -> NCic.term) -> 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 = string * 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;
+ let old_require1 = !require1 in
+ require1 :=
+ (fun (tag',data) as x ->
+ if tag=tag' then
+ require (Obj.magic data) ~refresh_uri_in_universe ~refresh_uri_in_term
+ else
+ old_require1 x);
+ (fun x -> tag,Obj.repr x)
+
+ let serialize = serialize
+
+ let require ~baseuri status =
+ includes := baseuri::!includes;
+ let dump = require0 ~baseuri in
+ List.fold_right !require1 dump status
+end
+
+let decompile ~baseuri =
+ let baseuris = get_deps baseuri in
+ List.iter (fun baseuri ->
+ remove_deps baseuri;
+ HExtlib.safe_remove (path_of_baseuri baseuri);
+ let basepath = path_of_baseuri ~no_suffix:true baseuri in
+ try
+ let od = Unix.opendir basepath in
+ let rec aux names =
+ try
+ let name = Unix.readdir od in
+ if name <> "." && name <> ".." then aux (name::names) else aux names
+ with
+ End_of_file -> names in
+ let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
+ Unix.closedir od;
+ List.iter Unix.unlink names;
+ HExtlib.rmdir_descend basepath;
+ set_global_aliases
+ (List.filter
+ (fun (_,_,NReference.Ref (nuri,_)) ->
+ Filename.dirname (NUri.string_of_uri nuri) <> NUri.string_of_uri baseuri
+ ) (get_global_aliases ()))
+ with
+ Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
+ ) baseuris
+;;
+
+LibraryClean.set_decompile_cb
+ (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
+
+let fetch_obj uri =
+ let obj = require0 ~baseuri:uri in
+ refresh_uri_in_obj obj
+;;