English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Dynamic loading of bytecode
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-08-18 (01:15)
From: Pietro Abate <Pietro.Abate@a...>
Subject: Re: [Caml-list] Dynamic loading of bytecode
On Mon, Aug 14, 2006 at 05:21:25PM +0200, Tom wrote:
> How can you load the whole module hiearchy (many modules, dependent on one
> another) with a single command in toploop?

I use these two functions to resolve linear dependencies. 
I'm sure there is a better solution... 

let modules = Hashtbl.create 17;;
let are_loading = Hashtbl.create 17;;

let find_in_path path name =
 let filename = ((String.uncapitalize name) ^ ".cmo") in
 if not (Filename.is_implicit filename) then
 if Sys.file_exists filename then filename else raise Not_found
 let rec try_dir = function
 | [] -> raise Not_found
 | dir::rem ->
 let fullname = Filename.concat dir filename in
 if Sys.file_exists fullname then fullname
 else try_dir rem
 in try_dir path

let rec load_module modname path =
 Hashtbl.find modules modname
 Not_found ->
 Hashtbl.add modules modname ();
 Hashtbl.add are_loading modname ();
 (* Printf.printf "Loading: %s ..." modname; *)
 Dynlink.loadfile (modname);
 (* print_endline "done."; *)
 Hashtbl.remove are_loading modname
 | Dynlink.Error(Dynlink.Unavailable_unit(depend))
 | Dynlink.Error(
 ) ->
 if Hashtbl.mem are_loading depend
 then failwith ("Crossing with "^depend);
 load_module (find_in_path path depend) path;
 Hashtbl.remove modules modname;
 load_module modname path
 with Not_found ->
 failwith ("Cannot find "
 ^String.lowercase(depend)^" in "^
 (List.fold_left (fun s x -> s^x) " " path))
 | Dynlink.Error(e) -> failwith (Dynlink.error_message e)

++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html